comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/OutputStateBase.java @ 657:af3f56758f59

merged gnv-artifacts/0.5
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:13:53 +0200
parents a94ed2755480
children dde7f51dbe1e
comparison
equal deleted inserted replaced
590:5f5f273c8566 657:af3f56758f59
1 /**
2 *
3 */
4 package de.intevation.gnv.state;
5
6 import java.io.OutputStream;
7 import java.util.ArrayList;
8 import java.util.Collection;
9 import java.util.HashMap;
10 import java.util.Iterator;
11 import java.util.List;
12 import java.util.Locale;
13
14 import javax.xml.xpath.XPathConstants;
15
16 import net.sf.ehcache.Cache;
17
18 import org.apache.log4j.Logger;
19 import org.w3c.dom.Document;
20 import org.w3c.dom.Element;
21 import org.w3c.dom.Node;
22 import org.w3c.dom.NodeList;
23
24 import de.intevation.artifactdatabase.Config;
25 import de.intevation.artifactdatabase.XMLUtils;
26 import de.intevation.artifacts.CallContext;
27 import de.intevation.artifacts.CallMeta;
28 import de.intevation.gnv.artifacts.cache.CacheFactory;
29 import de.intevation.gnv.artifacts.ressource.RessourceFactory;
30 import de.intevation.gnv.geobackend.base.Result;
31 import de.intevation.gnv.geobackend.base.query.QueryExecutor;
32 import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory;
33 import de.intevation.gnv.geobackend.base.query.exception.QueryException;
34 import de.intevation.gnv.state.describedata.MinMaxDescribeData;
35 import de.intevation.gnv.state.exception.StateException;
36 import de.intevation.gnv.utils.InputValidator;
37
38 /**
39 * @author Tim Englich <tim.englich@intevation.de>
40 *
41 */
42 public abstract class OutputStateBase
43 extends StateBase
44 implements OutputState
45 {
46 public static final String XPATH_OUTPUT_MODE =
47 "/art:action/art:out/@name";
48
49 public static final String XPATH_EXPORT_MODE =
50 "/art:action/art:out/art:export/@name";
51
52 public static final String XPATH_MIME_TYPE =
53 "/art:action/art:out/art:mime-type/@value";
54
55 public static final String XPATH_EXPORTS =
56 "exportModes/export";
57
58 /**
59 * The UID of this Class
60 */
61 private static final long serialVersionUID = -1718732895737303823L;
62
63 /**
64 * the logger, used to log exceptions and additonaly information
65 */
66 private static Logger log = Logger.getLogger(OutputStateBase.class);
67
68 /**
69 * The different Outputmodes which are provided by an OutputState
70 */
71 protected Collection<OutputMode> outputModes = null;
72
73 protected String queryODVID = null;
74
75 /**
76 * Constructor
77 */
78 public OutputStateBase() {
79 super();
80 }
81
82 /**
83 * @see de.intevation.gnv.state.OutputState#getOutputModes()
84 */
85 public Collection<OutputMode> getOutputModes() {
86 log.debug("OutputStateBase.getOutputModes");
87 return this.outputModes;
88 }
89
90 /**
91 * @see de.intevation.gnv.state.StateBase#setup(org.w3c.dom.Node)
92 */
93 @Override
94 public void setup(Node configuration) {
95 log.debug("OutputStateBase.setup");
96 super.setup(configuration);
97
98 this.queryODVID = Config.getStringXPath(configuration,"queryID-odv");
99
100 NodeList outputModeList = Config.getNodeSetXPath(configuration,
101 "outputsModes/outputsMode");
102 if (outputModeList != null) {
103 log.debug(outputModeList.getLength() + " were found.");
104 this.outputModes = new ArrayList<OutputMode>(outputModeList
105 .getLength());
106 for (int i = 0; i < outputModeList.getLength(); i++) {
107 Element currentNode = (Element)outputModeList.item(i);
108 String name = currentNode.getAttribute("name");
109 String description =currentNode.getAttribute("description");
110 String mimeType = currentNode.getAttribute("mime-type");
111 NodeList inputValuesList = Config.getNodeSetXPath(currentNode,
112 "parameters/inputvalue");
113 Collection<InputValue> inputParameters = null;
114 if (inputValuesList != null) {
115 inputParameters = new ArrayList<InputValue>(inputValuesList
116 .getLength());
117 for (int j = 0; j < inputValuesList.getLength(); j++) {
118 Element currentInputValuesNode = (Element)inputValuesList.item(j);
119 String inputValueName = currentInputValuesNode.getAttribute("name");
120 String inputValueType = currentInputValuesNode.getAttribute("type");
121 String defaultValue =currentInputValuesNode.getAttribute("value");
122 boolean isMultiselect = false;
123 InputValue inputValue = new DefaultInputValue(
124 inputValueName, inputValueType, defaultValue,
125 isMultiselect);
126 inputParameters.add(inputValue);
127 }
128 }
129
130 // parse export modes
131 List<ExportMode> exportList = null;
132 NodeList exports = (NodeList) XMLUtils.xpath(
133 currentNode, XPATH_EXPORTS, XPathConstants.NODESET);
134
135 if (exports != null) {
136 int exportSize = exports.getLength();
137
138 exportList = new ArrayList<ExportMode>(exportSize);
139 for (int k = 0; k < exportSize; k++) {
140 Element exp = (Element) exports.item(k);
141 String expName = exp.getAttribute("name");
142 String expDesc = exp.getAttribute("description");
143 String expMime = exp.getAttribute("mime-type");
144
145 exportList.add(
146 new DefaultExportMode(expName, expDesc, expMime));
147 }
148 }
149
150 OutputMode outputMode = new DefaultOutputMode(name,
151 description, mimeType, inputParameters, exportList);
152 log.debug(outputMode.toString());
153 this.outputModes.add(outputMode);
154
155 }
156 }
157 }
158
159 /**
160 * @see de.intevation.gnv.state.StateBase#advance()
161 */
162 @Override
163 public void advance(String uuid, CallContext context)
164 throws StateException
165 {
166 }
167
168 @Override
169 public void initialize(String uuid, CallContext context)
170 throws StateException
171 {
172 }
173
174 public void out(
175 Document format,
176 Collection<InputData> inputData,
177 OutputStream outputStream,
178 String uuid,
179 CallMeta callMeta
180 )
181 throws StateException
182 {
183 }
184
185 /**
186 * @see de.intevation.gnv.state.OutputState#out(java.lang.String,
187 * java.util.Collection, java.io.OutputStream)
188 */
189 public void out(String outputMode, Collection<InputData> inputData,
190 OutputStream outputStream) throws StateException {
191 }
192
193 /**
194 * @return
195 */
196 protected Object getChartResult(String uuid, CallContext callContext) {
197 log.debug("OutputStateBase.getChartResult");
198 CacheFactory factory = CacheFactory.getInstance();
199
200 if (factory.isInitialized()) {
201 // we use a cache
202 log.info("Using cache.");
203 Cache cache = factory.getCache();
204 String key = "chart_" + getHash();
205
206 net.sf.ehcache.Element value = cache.get(key);
207 if (value != null) {
208 log.debug("Found element in cache.");
209 return value.getObjectValue();
210 }
211 else {
212 log.debug("Element not in cache, we need to ask the database");
213 Object result = getData(queryID);
214 cache.put(new net.sf.ehcache.Element(key, result));
215
216 return result;
217 }
218 }
219 else {
220 // we don't use a cache, so we have to query the database every
221 // single time
222 log.info("Not using a cache.");
223 return getData(queryID);
224 }
225 }
226
227 protected Object getChartFromCache(String uuid, CallContext callContext) {
228 log.debug("Fetch chart [" + uuid + "] from cache");
229 CacheFactory cacheFactory = CacheFactory.getInstance();
230 if (cacheFactory.isInitialized()) {
231 String key = "chart_" + getHash();
232 net.sf.ehcache.Element object = cacheFactory.getCache().get(key);
233
234 if (object != null) {
235 return object.getObjectValue();
236 }
237 }
238 return null;
239 }
240
241 protected Collection<Result> getODVResult(String uuid) {
242 log.debug("OutputStateBase.getODVResult");
243 // TODO add Caching? I think it's not nessessary
244 Collection<Result> returnValue = null;
245 if (this.queryODVID != null){
246 returnValue = this.getData(this.queryODVID);
247 }else{
248 log.warn("No Query for ODV Data is defined.");
249 }
250 return returnValue;
251 }
252
253 /**
254 * @param returnValue
255 * @return
256 */
257 protected Collection<Result> getData(String queryID) {
258 log.debug("OutputStateBase.getData");
259 Collection<Result> returnValue = null;
260 try {
261 String[] filterValues = this.generateFilterValuesFromInputData();
262 try {
263 QueryExecutor queryExecutor = QueryExecutorFactory
264 .getInstance()
265 .getQueryExecutor();
266 returnValue = queryExecutor.executeQuery(queryID,filterValues);
267 } catch (RuntimeException e) {
268 log.error(e, e);
269 }
270 } catch (QueryException e) {
271 log.error(e, e);
272 }
273 return returnValue;
274 }
275
276 protected void removeChartResult(String uuid) {
277 log.debug("OutputStateBase.getChartResult");
278 if (CacheFactory.getInstance().isInitialized()) {
279 String key = "chart_" + getHash();
280 log.debug("Hash for Queryelements: " + key);
281 net.sf.ehcache.Element value = CacheFactory.getInstance().getCache().get(key);
282 if (value != null) {
283 CacheFactory.getInstance().getCache().remove(key);
284 }
285 }
286 }
287
288 protected void removeChart(String uuid) {
289 log.debug("OutputStateBase.removeChart from cache");
290
291 CacheFactory cacheFactory = CacheFactory.getInstance();
292 if (cacheFactory.isInitialized()) {
293 String key = "chart_" + getHash();
294 net.sf.ehcache.Element object = cacheFactory.getCache().get(key);
295 if (object != null)
296 cacheFactory.getCache().remove(key);
297 }
298 }
299
300 protected void purifyChart(Object chart, String uuid) {
301 log.debug("Prufify chart [" + uuid + "]");
302 CacheFactory cacheFactory = CacheFactory.getInstance();
303 if (cacheFactory.isInitialized()) {
304 String key = "chart_" + getHash();
305 cacheFactory.getCache().put(new net.sf.ehcache.Element(key, chart));
306 }
307 }
308
309
310 @Override
311 public void feed(Collection<InputData> inputData, String uuid)
312 throws StateException
313 {
314 putInputData(inputData, uuid);
315 }
316
317 /**
318 * @see de.intevation.gnv.state.StateBase#putInputData(java.util.Collection, java.lang.String)
319 */
320 @Override
321 public void putInputData(Collection<InputData> inputData,
322 String uuid)
323 throws StateException {
324 log.debug("OutputStateBase.putInputData");
325 this.removeChartResult(uuid);
326 this.removeChart(uuid);
327
328 if (inputData != null) {
329 Iterator<InputData> it = inputData.iterator();
330 InputValidator iv = new InputValidator();
331 while (it.hasNext()) {
332 InputData tmpItem = it.next();
333 Object tmpObj = tmpItem.getObject();
334 InputValue inputValue = this.inputValues.get(tmpItem.getName());
335 if (inputValue != null) {
336 if (this.inputData == null) {
337 this.inputData = new HashMap<String, InputData>(
338 inputData.size());
339 }
340
341 boolean valid = iv.isInputValid(tmpItem.getValue(),
342 inputValue.getType());
343 if (valid) {
344 if (tmpItem.getName().equals(MINVALUEFIELDNAME)){
345 String minValue = tmpItem.getValue();
346 String maxValue = getInputValue4ID(inputData, MAXVALUEFIELDNAME);
347 valid = iv.isInputValid(maxValue,inputValue.getType());
348 if (!valid){
349 String errMsg = "Wrong input for " + tmpItem.getValue()
350 + " is not an " + inputValue.getType()
351 + " Value.";
352 log.warn(errMsg);
353 throw new StateException(errMsg);
354 }
355
356 valid = iv.isInputValid(minValue,
357 maxValue,
358 inputValue.getType());
359 if (!valid){
360 String errMsg = "MaxValue-Input is less than MinValue-Input ";
361 log.warn(errMsg);
362 throw new StateException(errMsg);
363 }
364 }else if (tmpItem.getName().equals(MAXVALUEFIELDNAME)){
365 String minValue = getInputValue4ID(inputData, MINVALUEFIELDNAME);
366 String maxValue = tmpItem.getValue();
367 valid = iv.isInputValid(minValue,inputValue.getType());
368 if (!valid){
369 String errMsg = "Wrong input for " + tmpItem.getValue()
370 + " is not an " + inputValue.getType()
371 + " Value.";
372 log.warn(errMsg);
373 throw new StateException(errMsg);
374 }
375
376 valid = iv.isInputValid(minValue,
377 maxValue,
378 inputValue.getType());
379 if (!valid){
380 String errMsg = "MaxValue-Input is less than MinValue-Input ";
381 log.warn(errMsg);
382 throw new StateException(errMsg);
383 }
384 }
385 this.inputData.put(tmpItem.getName(), tmpItem);
386 } else {
387 String errMsg = "Wrong input for " + tmpItem.getValue()
388 + " is not an " + inputValue.getType()
389 + " Value.";
390 log.warn(errMsg);
391 throw new StateException(errMsg);
392 }
393
394 }
395 else if (tmpObj != null && tmpObj instanceof MinMaxDescribeData) {
396 MinMaxDescribeData data = (MinMaxDescribeData) tmpObj;
397 if (this.inputData == null) {
398 this.inputData = new HashMap<String, InputData>(inputData.size());
399 }
400 this.inputData.put(tmpItem.getName(), tmpItem);
401 this.inputData.put("minvalue", new DefaultInputData("minvalue", (String) data.getMinValue()));
402 this.inputData.put("maxvalue", new DefaultInputData("maxvalue", (String) data.getMaxValue()));
403 }
404 else {
405
406 String errMsg = "No Inputvalue given for Inputdata "
407 + tmpItem.getName();
408 log.warn(errMsg + "Value will be ignored");
409
410 }
411 }
412 } else {
413 log.warn("No Inputdata given");
414 }
415 }
416
417 public void out(String outputMode, Collection<InputData> inputData,
418 OutputStream outputStream, String uuid, CallMeta callMeta)
419 throws StateException {
420 }
421
422
423 protected String getMessage(Locale locale, String key, String value) {
424 return RessourceFactory.getInstance().getRessource(
425 locale,
426 key,
427 value
428 );
429 }
430 }
431 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8:

http://dive4elements.wald.intevation.org