comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/OutputStateBase.java @ 540:80630520e25a

merged gnv-artifacts/0.4
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:49 +0200
parents cef17cc90fd0
children 6484464d2059
comparison
equal deleted inserted replaced
415:9f4a0b990d27 540:80630520e25a
1 /**
2 *
3 */
4 package de.intevation.gnv.state;
5
6 import de.intevation.artifactdatabase.Config;
7
8 import de.intevation.artifacts.CallContext;
9 import de.intevation.artifacts.CallMeta;
10
11 import de.intevation.gnv.artifacts.cache.CacheFactory;
12
13 import de.intevation.gnv.artifacts.ressource.RessourceFactory;
14
15 import de.intevation.gnv.geobackend.base.Result;
16
17 import de.intevation.gnv.geobackend.base.query.QueryExecutor;
18 import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory;
19
20 import de.intevation.gnv.geobackend.base.query.exception.QueryException;
21
22 import de.intevation.gnv.state.exception.StateException;
23
24 import java.io.OutputStream;
25
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Locale;
29
30 import org.apache.log4j.Logger;
31
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Element;
34 import org.w3c.dom.Node;
35 import org.w3c.dom.NodeList;
36
37 /**
38 * @author Tim Englich <tim.englich@intevation.de>
39 *
40 */
41 public abstract class OutputStateBase
42 extends StateBase
43 implements OutputState
44 {
45 public static final String XPATH_OUTPUT_MODE =
46 "/art:action/art:out/@name";
47
48 public static final String XPATH_MIME_TYPE =
49 "/art:action/art:out/art:mime-type/@value";
50
51 /**
52 * The UID of this Class
53 */
54 private static final long serialVersionUID = -1718732895737303823L;
55
56 /**
57 * the logger, used to log exceptions and additonaly information
58 */
59 private static Logger log = Logger.getLogger(OutputStateBase.class);
60
61 /**
62 * The different Outputmodes which are provided by an OutputState
63 */
64 protected Collection<OutputMode> outputModes = null;
65
66 protected String queryODVID = null;
67
68 /**
69 * Constructor
70 */
71 public OutputStateBase() {
72 super();
73 }
74
75 /**
76 * @see de.intevation.gnv.state.OutputState#getOutputModes()
77 */
78 public Collection<OutputMode> getOutputModes() {
79 log.debug("OutputStateBase.getOutputModes");
80 return this.outputModes;
81 }
82
83 /**
84 * @see de.intevation.gnv.state.StateBase#setup(org.w3c.dom.Node)
85 */
86 @Override
87 public void setup(Node configuration) {
88 log.debug("OutputStateBase.setup");
89 super.setup(configuration);
90
91 this.queryODVID = Config.getStringXPath(configuration,"queryID-odv");
92
93 NodeList outputModeList = Config.getNodeSetXPath(configuration,
94 "outputsModes/outputsMode");
95 if (outputModeList != null) {
96 log.debug(outputModeList.getLength() + " were found.");
97 this.outputModes = new ArrayList<OutputMode>(outputModeList
98 .getLength());
99 for (int i = 0; i < outputModeList.getLength(); i++) {
100 Element currentNode = (Element)outputModeList.item(i);
101 String name = currentNode.getAttribute("name");
102 String description =currentNode.getAttribute("description");
103 String mimeType = currentNode.getAttribute("mime-type");
104 NodeList inputValuesList = Config.getNodeSetXPath(currentNode,
105 "parameters/inputvalue");
106 Collection<InputValue> inputParameters = null;
107 if (inputValuesList != null) {
108 inputParameters = new ArrayList<InputValue>(inputValuesList
109 .getLength());
110 for (int j = 0; j < inputValuesList.getLength(); j++) {
111 Element currentInputValuesNode = (Element)inputValuesList.item(j);
112 String inputValueName = currentInputValuesNode.getAttribute("name");
113 String inputValueType = currentInputValuesNode.getAttribute("type");
114 String defaultValue =currentInputValuesNode.getAttribute("value");
115 boolean isMultiselect = false;
116 InputValue inputValue = new DefaultInputValue(
117 inputValueName, inputValueType, defaultValue,
118 isMultiselect);
119 inputParameters.add(inputValue);
120 }
121 }
122
123 OutputMode outputMode = new DefaultOutputMode(name,
124 description, mimeType, inputParameters);
125 log.debug(outputMode.toString());
126 this.outputModes.add(outputMode);
127
128 }
129 }
130 }
131
132 /**
133 * @see de.intevation.gnv.state.StateBase#advance()
134 */
135 @Override
136 public void advance(String uuid, CallContext context)
137 throws StateException
138 {
139 }
140
141 @Override
142 public void initialize(String uuid, CallContext context)
143 throws StateException
144 {
145 }
146
147 public void out(
148 Document format,
149 Collection<InputData> inputData,
150 OutputStream outputStream,
151 String uuid,
152 CallMeta callMeta
153 )
154 throws StateException
155 {
156 }
157
158 /**
159 * @see de.intevation.gnv.state.OutputState#out(java.lang.String,
160 * java.util.Collection, java.io.OutputStream)
161 */
162 public void out(String outputMode, Collection<InputData> inputData,
163 OutputStream outputStream) throws StateException {
164 }
165
166 /**
167 * @return
168 */
169 protected Object getChartResult(String uuid, CallContext callContext) {
170 log.debug("OutputStateBase.getChartResult");
171 Object result = null;
172 if (CacheFactory.getInstance().isInitialized()) {
173 String key = uuid + super.getID();
174 log.debug("Hash for Queryelements: " + key);
175 net.sf.ehcache.Element value = CacheFactory.getInstance().getCache().get(key);
176 if (value != null) {
177 result = value.getObjectValue();
178 }else{
179 result = this.getData(this.queryID);
180 if (CacheFactory.getInstance().isInitialized()) {
181 CacheFactory.getInstance().getCache().put(new net.sf.ehcache.Element(key, result));
182 }
183
184 }
185 }
186 return result;
187 }
188
189 protected Object getChartFromCache(String uuid, CallContext callContext) {
190 log.debug("Fetch chart [" + uuid + "] from cache");
191 CacheFactory cacheFactory = CacheFactory.getInstance();
192 if (cacheFactory.isInitialized()) {
193 String key = "chart_" + uuid + super.getID();
194 net.sf.ehcache.Element object = cacheFactory.getCache().get(key);
195
196 if (object != null) {
197 return object.getObjectValue();
198 }
199 }
200 return null;
201 }
202
203 protected Collection<Result> getODVResult(String uuid) {
204 log.debug("OutputStateBase.getODVResult");
205 // TODO add Caching? I think it's not nessessary
206 Collection<Result> returnValue = null;
207 if (this.queryODVID != null){
208 returnValue = this.getData(this.queryODVID);
209 }else{
210 log.warn("No Query for ODV Data is defined.");
211 }
212 return returnValue;
213 }
214
215 /**
216 * @param returnValue
217 * @return
218 */
219 private Collection<Result> getData(String queryID) {
220 log.debug("OutputStateBase.getData");
221 Collection<Result> returnValue = null;
222 try {
223 String[] filterValues = this.generateFilterValuesFromInputData();
224 try {
225 QueryExecutor queryExecutor = QueryExecutorFactory
226 .getInstance()
227 .getQueryExecutor();
228 returnValue = queryExecutor.executeQuery(queryID,filterValues);
229 } catch (RuntimeException e) {
230 log.error(e, e);
231 }
232 } catch (QueryException e) {
233 log.error(e, e);
234 }
235 return returnValue;
236 }
237
238 protected void removeChartResult(String uuid) {
239 log.debug("OutputStateBase.getChartResult");
240 if (CacheFactory.getInstance().isInitialized()) {
241 String key = uuid + super.getID();
242 log.debug("Hash for Queryelements: " + key);
243 net.sf.ehcache.Element value = CacheFactory.getInstance().getCache().get(key);
244 if (value != null) {
245 CacheFactory.getInstance().getCache().remove(key);
246 }
247 }
248 }
249
250 protected void removeChart(String uuid) {
251 log.debug("OutputStateBase.removeChart from cache");
252
253 CacheFactory cacheFactory = CacheFactory.getInstance();
254 if (cacheFactory.isInitialized()) {
255 String key = "chart_" + uuid + super.getID();
256 net.sf.ehcache.Element object = cacheFactory.getCache().get(key);
257 if (object != null)
258 cacheFactory.getCache().remove(key);
259 }
260 }
261
262 protected void purifyChart(Object chart, String uuid) {
263 log.debug("Prufify chart [" + uuid + "]");
264 CacheFactory cacheFactory = CacheFactory.getInstance();
265 if (cacheFactory.isInitialized()) {
266 String key = "chart_" + uuid + getID();
267 cacheFactory.getCache().put(new net.sf.ehcache.Element(key, chart));
268 }
269 }
270
271 /**
272 * @see de.intevation.gnv.state.StateBase#putInputData(java.util.Collection, java.lang.String)
273 */
274 @Override
275 public void putInputData(Collection<InputData> inputData,
276 String uuid)
277 throws StateException {
278 log.debug("OutputStateBase.putInputData");
279 this.removeChartResult(uuid);
280 this.removeChart(uuid);
281 super.putInputData(inputData, uuid);
282 }
283
284 public void out(String outputMode, Collection<InputData> inputData,
285 OutputStream outputStream, String uuid, CallMeta callMeta)
286 throws StateException {
287 }
288
289
290 protected String getMessage(Locale locale, String key, String value) {
291 return RessourceFactory.getInstance().getRessource(
292 locale,
293 key,
294 value
295 );
296 }
297 }

http://dive4elements.wald.intevation.org