Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/transition/TransitionBase.java @ 127:f6f0e4ce4a35
merged gnv-artifacts/0.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:41 +0200 |
parents | 5ebc059064a6 |
children | 7fb9441dd8af |
comparison
equal
deleted
inserted
replaced
49:94a07d1d9316 | 127:f6f0e4ce4a35 |
---|---|
1 /** | |
2 * | |
3 */ | |
4 package de.intevation.gnv.transition; | |
5 | |
6 import java.util.ArrayList; | |
7 import java.util.Collection; | |
8 import java.util.Date; | |
9 import java.util.GregorianCalendar; | |
10 import java.util.HashMap; | |
11 import java.util.HashSet; | |
12 import java.util.Iterator; | |
13 import java.util.List; | |
14 import java.util.Map; | |
15 import java.util.Set; | |
16 | |
17 import org.apache.log4j.Logger; | |
18 import org.w3c.dom.Document; | |
19 import org.w3c.dom.Element; | |
20 import org.w3c.dom.Node; | |
21 import org.w3c.dom.NodeList; | |
22 | |
23 import de.intevation.artifactdatabase.Config; | |
24 import de.intevation.artifacts.CallMeta; | |
25 import de.intevation.gnv.artifacts.GNVArtifactBase; | |
26 import de.intevation.gnv.artifacts.ressource.RessourceFactory; | |
27 import de.intevation.gnv.geobackend.base.Result; | |
28 import de.intevation.gnv.geobackend.base.query.QueryExecutor; | |
29 import de.intevation.gnv.geobackend.base.query.QueryExecutorFactory; | |
30 import de.intevation.gnv.geobackend.base.query.exception.QueryException; | |
31 import de.intevation.gnv.geobackend.util.DateUtils; | |
32 import de.intevation.gnv.transition.describedata.DefaultKeyValueDescribeData; | |
33 import de.intevation.gnv.transition.describedata.KeyValueDescibeData; | |
34 import de.intevation.gnv.transition.describedata.MinMaxDescribeData; | |
35 import de.intevation.gnv.transition.describedata.NamedArrayList; | |
36 import de.intevation.gnv.transition.describedata.NamedCollection; | |
37 import de.intevation.gnv.transition.describedata.SingleValueDescribeData; | |
38 import de.intevation.gnv.transition.exception.TransitionException; | |
39 import de.intevation.gnv.utils.ArtifactXMLUtilities; | |
40 import de.intevation.gnv.utils.InputValidator; | |
41 | |
42 /** | |
43 * @author Tim Englich <tim.englich@intevation.de> | |
44 * | |
45 */ | |
46 public abstract class TransitionBase implements Transition { | |
47 | |
48 /** | |
49 * The UID of this Class | |
50 */ | |
51 private static final long serialVersionUID = 2411169179001645426L; | |
52 | |
53 /** | |
54 * the logger, used to log exceptions and additonaly information | |
55 */ | |
56 private static Logger log = Logger.getLogger(GNVArtifactBase.class); | |
57 | |
58 private String id = null; | |
59 | |
60 private String description = null; | |
61 | |
62 protected String dataName = null; | |
63 | |
64 protected boolean dataMultiSelect = false; | |
65 | |
66 protected String queryID = null; | |
67 | |
68 private Collection<String> reachableTransitions = null; | |
69 | |
70 protected Collection<String> inputValueNames = null; | |
71 | |
72 private Map<String,InputValue> inputValues = null; | |
73 | |
74 private Transition parent = null; | |
75 | |
76 protected Map<String,InputData> inputData = null; | |
77 | |
78 protected Collection<Object> descibeData = null; | |
79 | |
80 /** | |
81 * Constructor | |
82 */ | |
83 public TransitionBase() { | |
84 super(); | |
85 } | |
86 | |
87 /** | |
88 * @see de.intevation.gnv.transition.Transition#getID() | |
89 */ | |
90 public String getID() { | |
91 return this.id; | |
92 } | |
93 | |
94 /** | |
95 * @see de.intevation.gnv.transition.Transition#getDescription() | |
96 */ | |
97 public String getDescription() { | |
98 return this.description; | |
99 } | |
100 | |
101 /** | |
102 * @see de.intevation.gnv.transition.Transition#reachableTransitions() | |
103 */ | |
104 public Collection<String> reachableTransitions() { | |
105 return this.reachableTransitions; | |
106 } | |
107 | |
108 /** | |
109 * @see de.intevation.gnv.transition.Transition#getRequiredInputValues() | |
110 */ | |
111 public Collection<InputValue> getRequiredInputValues() { | |
112 return this.inputValues.values(); | |
113 } | |
114 | |
115 /** | |
116 * @see de.intevation.gnv.transition.Transition#setup(org.w3c.dom.Node) | |
117 */ | |
118 public void setup(Node configuration) { | |
119 log.debug("TransitionBase.setup"); | |
120 this.id = Config.getStringXPath(configuration,"@id"); | |
121 this.description = Config.getStringXPath(configuration,"@description"); | |
122 | |
123 log.info("Transition-ID = "+ this.id); | |
124 NodeList nodes = Config.getNodeSetXPath(configuration,"reachableTransitions/transition"); | |
125 this.reachableTransitions = new ArrayList<String>(nodes.getLength()); | |
126 for (int i = 0 ; i < nodes.getLength(); i++){ | |
127 String reachableTransition = nodes.item(i).getTextContent(); | |
128 log.info("ReachableTransition ==> "+ reachableTransition); | |
129 this.reachableTransitions.add(reachableTransition); | |
130 | |
131 } | |
132 | |
133 NodeList inputValuesNodes = Config.getNodeSetXPath(configuration,"inputvalues/inputvalue"); | |
134 this.inputValues = new HashMap<String,InputValue>(inputValuesNodes.getLength()); | |
135 this.inputValueNames = new ArrayList<String>(inputValuesNodes.getLength()); | |
136 for (int i = 0 ; i < inputValuesNodes.getLength(); i++){ | |
137 Node inputValueNode = inputValuesNodes.item(i); | |
138 String usedinQueryValue = Config.getStringXPath(inputValueNode,"@usedinquery"); | |
139 int usedinQuery = 1; | |
140 if (usedinQueryValue != null){ | |
141 try { | |
142 usedinQuery = Integer.parseInt(usedinQueryValue); | |
143 } catch (NumberFormatException e) { | |
144 log.warn("Used in Query Value cannot be transformed into a Number"); | |
145 } | |
146 } | |
147 InputValue inputValue = new DefaultInputValue( | |
148 Config.getStringXPath(inputValueNode,"@name"), | |
149 Config.getStringXPath(inputValueNode,"@type"), | |
150 Boolean.parseBoolean(Config.getStringXPath(inputValueNode,"@multiselect")), | |
151 usedinQuery); | |
152 log.debug(inputValue.toString()); | |
153 this.inputValues.put(inputValue.getName(),inputValue); | |
154 this.inputValueNames.add(inputValue.getName()); | |
155 } | |
156 | |
157 this.queryID = Config.getStringXPath(configuration,"queryID"); | |
158 log.info("QueryID ==> "+ this.queryID); | |
159 | |
160 this.dataName = Config.getStringXPath(configuration,"dataname"); | |
161 | |
162 String dataMultiSelectValue = Config.getStringXPath(configuration,"data-multiselect"); | |
163 if (dataMultiSelectValue != null){ | |
164 this.dataMultiSelect = Boolean.parseBoolean(dataMultiSelectValue); | |
165 } | |
166 | |
167 } | |
168 | |
169 /** | |
170 * @see de.intevation.gnv.transition.Transition#getParent() | |
171 */ | |
172 public Transition getParent() { | |
173 return this.parent; | |
174 } | |
175 | |
176 /** | |
177 * @see de.intevation.gnv.transition.Transition#setParent(de.intevation.gnv.transition.Transition) | |
178 */ | |
179 public void setParent(Transition transition) { | |
180 this.parent = transition; | |
181 } | |
182 | |
183 /** | |
184 * @see de.intevation.gnv.transition.Transition#putInputData(java.util.Collection) | |
185 */ | |
186 public void putInputData(Collection<InputData> inputData, String uuid) throws TransitionException { | |
187 log.debug("TransitionBase.putInputData"); | |
188 if (inputData != null){ | |
189 Iterator<InputData> it = inputData.iterator(); | |
190 InputValidator iv = new InputValidator(); | |
191 while(it.hasNext()){ | |
192 InputData tmpItem = it.next(); | |
193 InputValue inputValue = this.inputValues.get(tmpItem.getName()); | |
194 if (inputValue != null){ | |
195 if (this.inputData == null){ | |
196 this.inputData = new HashMap<String,InputData>(inputData.size()); | |
197 } | |
198 boolean valid = iv.isInputValid(tmpItem.getValue(), inputValue.getType()); | |
199 if (valid){ | |
200 this.setSelection(tmpItem); | |
201 this.inputData.put(tmpItem.getName(),tmpItem); | |
202 }else{ | |
203 String errMsg = "Wrong input for "+tmpItem.getValue()+"is not an "+inputValue.getType()+" Value."; | |
204 log.warn(errMsg); | |
205 throw new TransitionException(errMsg); | |
206 } | |
207 | |
208 }else{ | |
209 String errMsg = "No Inputvalue given for Inputdata "+ tmpItem.getName(); | |
210 log.warn(errMsg+ "Value will be ignored"); | |
211 | |
212 } | |
213 } | |
214 }else{ | |
215 log.warn("No Inputdata given"); | |
216 } | |
217 } | |
218 | |
219 private void setSelection(InputData inputData){ | |
220 log.debug("TransitionBase.setSelection"); | |
221 | |
222 Object o = this.getDescribeData(inputData.getName()); | |
223 if(o != null){ | |
224 if (o instanceof Collection<?>){ | |
225 Collection<KeyValueDescibeData> values = (Collection<KeyValueDescibeData>)o; | |
226 | |
227 String value = inputData.getValue(); | |
228 String[] selectedValues = value.split(","); | |
229 Set<String> selectedItems = new HashSet<String>(selectedValues.length); | |
230 for (int i = 0; i < selectedValues.length; i++){ | |
231 selectedItems.add(selectedValues[i].trim()); | |
232 } | |
233 // Selektion umsetzen | |
234 Iterator<KeyValueDescibeData> it = values.iterator(); | |
235 while (it.hasNext()){ | |
236 KeyValueDescibeData data = it.next(); | |
237 String key = ""+data.getKey(); | |
238 boolean selected = selectedItems.contains(key); | |
239 data.setSelected(selected); | |
240 } | |
241 }else if (o instanceof MinMaxDescribeData){ | |
242 MinMaxDescribeData data = (MinMaxDescribeData)o; | |
243 if (inputData.getName().equals(data.getMinName())){ | |
244 // TODO: müssen die werte geparst werden? | |
245 data.setMinValue(inputData.getValue()); | |
246 } | |
247 if (inputData.getName().equals(data.getMaxName())){ | |
248 // TODO: müssen die werte geparst werden? | |
249 data.setMaxValue(inputData.getValue()); | |
250 } | |
251 } | |
252 } | |
253 } | |
254 | |
255 private Object getDescribeData(String name){ | |
256 log.debug("TransitionBase.getDescribeData"); | |
257 if (this.descibeData != null){ | |
258 Iterator<Object> it = this.descibeData.iterator(); | |
259 while (it.hasNext()){ | |
260 Object o = it.next(); | |
261 if (o instanceof NamedCollection<?>){ | |
262 if (name.equals(((NamedCollection<?>)o).getName())){ | |
263 return o; | |
264 } | |
265 }else if (o instanceof MinMaxDescribeData){ | |
266 if (name.equals(((MinMaxDescribeData)o).getMinName())){ | |
267 return o; | |
268 } | |
269 if (name.equals(((MinMaxDescribeData)o).getMaxName())){ | |
270 return o; | |
271 } | |
272 } | |
273 } | |
274 } | |
275 return null; | |
276 | |
277 } | |
278 | |
279 /** | |
280 * @see de.intevation.gnv.transition.Transition#isTransitionReachable(java.lang.String) | |
281 */ | |
282 public boolean isTransitionReachable(String transitionID) { | |
283 log.debug("TransitionBase.isTransitionReachable"); | |
284 boolean returnValue = false; | |
285 Iterator<String> transitions = reachableTransitions.iterator(); | |
286 while (transitions.hasNext()){ | |
287 if(transitions.next().equals(transitionID)){ | |
288 log.debug("Transition "+transitionID+" wird unterstützt."); | |
289 returnValue = true; | |
290 break; | |
291 } | |
292 } | |
293 return returnValue; | |
294 } | |
295 | |
296 /** | |
297 * @see de.intevation.gnv.transition.Transition#advance(java.lang.String, de.intevation.artifacts.CallMeta) | |
298 */ | |
299 public void advance(String uuid, CallMeta callMeta) throws TransitionException { | |
300 log.debug("TransitionBase.advance"); | |
301 try { | |
302 List<String> list = new ArrayList<String>(); | |
303 | |
304 | |
305 Iterator<String> it = this.inputValueNames.iterator(); | |
306 int i = 0; | |
307 while (it.hasNext()){ | |
308 String value = it.next(); | |
309 InputData data = this.inputData.get(value); | |
310 if (data != null && this.inputValues.containsKey(data.getName())){ | |
311 int size = this.inputValues.get(data.getName()).usedInQueries(); | |
312 String type = this.inputValues.get(data.getName()).getType(); | |
313 String requestValue = data.getValue(); | |
314 if (type.equalsIgnoreCase("string")){ | |
315 requestValue = this.prepareInputData4DBQuery(requestValue); | |
316 }else if (type.equalsIgnoreCase("date")){ | |
317 requestValue = this.prepareInputData4DateDBQuery(requestValue); | |
318 } | |
319 for (int j = 0; j < size; j++){ | |
320 list.add(requestValue); | |
321 } | |
322 } | |
323 } | |
324 String[] filterValues = list.toArray(new String[0]); | |
325 Collection<Result> result = null; | |
326 try { | |
327 if (this.queryID != null){ | |
328 QueryExecutor queryExecutor = QueryExecutorFactory.getInstance().getQueryExecutor(); | |
329 result = queryExecutor.executeQuery(this.queryID, filterValues); | |
330 } | |
331 this.purifyResult(result, uuid); | |
332 } catch (RuntimeException e) { | |
333 log.error(e,e); | |
334 } | |
335 } catch (QueryException e) { | |
336 log.error(e,e); | |
337 throw new TransitionException(e); | |
338 } | |
339 } | |
340 | |
341 private String prepareInputData4DateDBQuery(String value){ | |
342 log.debug("TransitionBase.prepareInputData4DateDBQuery"); | |
343 if (value != null){ | |
344 String[] values = value.split(","); | |
345 String newValue = ""; | |
346 for (int i = 0; i < values.length; i++){ | |
347 if (newValue.length() > 0){ | |
348 newValue= newValue + " , "; | |
349 } | |
350 // TODO JUST HACK FIND A BETTER RESOLUTION | |
351 newValue = newValue + "to_date('"+values[i].trim()+"', 'YYYY.MM.DD HH24:MI:SS')"; | |
352 } | |
353 return newValue; | |
354 } | |
355 | |
356 return value; | |
357 } | |
358 private String prepareInputData4DBQuery(String value){ | |
359 log.debug("TransitionBase.prepareInputData4DBQuery"); | |
360 if (value != null){ | |
361 String[] values = value.split(","); | |
362 String newValue = ""; | |
363 for (int i = 0; i < values.length; i++){ | |
364 if (newValue.length() > 0){ | |
365 newValue= newValue + " , "; | |
366 } | |
367 newValue = newValue + "'"+values[i].trim()+"'"; | |
368 } | |
369 return newValue; | |
370 } | |
371 | |
372 return value; | |
373 | |
374 } | |
375 | |
376 /** | |
377 * @param result | |
378 */ | |
379 protected void purifyResult(Collection<Result> result, String uuid) { | |
380 log.debug("TransitionBase.purifyResult"); | |
381 if (this.descibeData == null){ | |
382 this.descibeData = new ArrayList<Object>(); | |
383 } | |
384 Iterator<Result> rit = result.iterator(); | |
385 NamedCollection<KeyValueDescibeData> keyValueDescibeData = new NamedArrayList<KeyValueDescibeData>(this.dataName,result.size()); | |
386 keyValueDescibeData.setMultiSelect(this.dataMultiSelect); | |
387 while(rit.hasNext()){ | |
388 Result resultValue = rit.next(); | |
389 keyValueDescibeData.add(new DefaultKeyValueDescribeData(resultValue.getString("KEY"), resultValue.getString("VALUE"))); | |
390 } | |
391 this.descibeData.add(keyValueDescibeData); | |
392 } | |
393 | |
394 /** | |
395 * @see de.intevation.gnv.transition.Transition#describe(org.w3c.dom.Document, org.w3c.dom.Node, de.intevation.artifacts.CallMeta) | |
396 */ | |
397 public void describe(Document document, Node rootNode, CallMeta callMeta) { | |
398 log.debug("TransitionBase.describe"); | |
399 if(this.descibeData != null){ | |
400 ArtifactXMLUtilities xmlutilities = new ArtifactXMLUtilities(); | |
401 Iterator<Object> it = this.descibeData.iterator(); | |
402 Node staticNode = xmlutilities.createArtifactElement(document, "static"); | |
403 Node dynamic = xmlutilities.createArtifactElement(document, "dynamic"); | |
404 rootNode.appendChild(staticNode); | |
405 rootNode.appendChild(dynamic); | |
406 while (it.hasNext()){ | |
407 | |
408 Object o = it.next(); | |
409 if (o instanceof Collection<?>){ | |
410 String name = null; | |
411 boolean multiselect = false; | |
412 if (o instanceof NamedCollection<?>){ | |
413 NamedCollection<?> nc = ((NamedCollection<?>)o); | |
414 name = nc.getName(); | |
415 multiselect = nc.isMultiSelect(); | |
416 }else{ | |
417 Object[] names = this.inputValueNames.toArray(); | |
418 name = names[names.length-1].toString(); | |
419 } | |
420 | |
421 | |
422 Element selectNode = xmlutilities.createXFormElement(document,multiselect ? "select" : "select1"); | |
423 selectNode.setAttribute("ref", name); | |
424 | |
425 Element lableNode = xmlutilities.createXFormElement(document, "label"); | |
426 lableNode.setTextContent(RessourceFactory.getInstance().getRessource(callMeta.getLanguages(), name, name)); | |
427 Element choiceNode = xmlutilities.createXFormElement(document, "choices"); | |
428 | |
429 Collection<KeyValueDescibeData> values = (Collection<KeyValueDescibeData>)o; | |
430 Iterator<KeyValueDescibeData> resultIt = values.iterator(); | |
431 while (resultIt.hasNext()){ | |
432 KeyValueDescibeData result = resultIt.next(); | |
433 Element itemNode = xmlutilities.createXFormElement(document, "item"); | |
434 | |
435 if (result.isSelected()){ | |
436 itemNode.setAttribute("selected", "true"); | |
437 } | |
438 | |
439 | |
440 Element choiceLableNode = xmlutilities.createXFormElement(document, "label"); | |
441 choiceLableNode.setTextContent(result.getValue()); | |
442 itemNode.appendChild(choiceLableNode); | |
443 | |
444 Element choicValueNode = xmlutilities.createXFormElement(document, "value"); | |
445 choicValueNode.setTextContent(""+result.getKey()); | |
446 itemNode.appendChild(choicValueNode); | |
447 choiceNode.appendChild(itemNode); | |
448 } | |
449 selectNode.appendChild(lableNode); | |
450 selectNode.appendChild(choiceNode); | |
451 | |
452 if (!it.hasNext()){ | |
453 dynamic.appendChild(selectNode); | |
454 }else{ | |
455 staticNode.appendChild(selectNode); | |
456 } | |
457 | |
458 }else if (o instanceof MinMaxDescribeData){ | |
459 MinMaxDescribeData descibeData = (MinMaxDescribeData)o; | |
460 Object min = descibeData.getMinValue(); | |
461 Object max = descibeData.getMaxValue(); | |
462 if (min instanceof GregorianCalendar){ | |
463 Date d = ((GregorianCalendar)min).getTime(); | |
464 min = DateUtils.getPatternedDateAmer(d); | |
465 } | |
466 | |
467 if (max instanceof GregorianCalendar){ | |
468 Date d = ((GregorianCalendar)max).getTime(); | |
469 max = DateUtils.getPatternedDateAmer(d); | |
470 } | |
471 | |
472 Element inputMinNode = xmlutilities.createXFormElement(document, "input"); | |
473 inputMinNode.setAttribute("ref", "minvalue"); | |
474 Element inputMinLableNode = xmlutilities.createXFormElement(document, "label"); | |
475 inputMinLableNode.setTextContent(RessourceFactory.getInstance().getRessource(callMeta.getLanguages(), "minvalue", "minvalue")); | |
476 inputMinNode.appendChild(inputMinLableNode); | |
477 | |
478 Element inputMinValueNode = xmlutilities.createXFormElement(document, "value"); | |
479 inputMinValueNode.setTextContent(min.toString()); | |
480 inputMinNode.appendChild(inputMinValueNode); | |
481 | |
482 Element inputMaxNode = xmlutilities.createXFormElement(document, "input"); | |
483 inputMaxNode.setAttribute("ref", "maxvalue"); | |
484 Element inputMaxLableNode = xmlutilities.createXFormElement(document, "label"); | |
485 inputMaxLableNode.setTextContent(RessourceFactory.getInstance().getRessource(callMeta.getLanguages(), "maxvalue", "maxvalue")); | |
486 inputMaxNode.appendChild(inputMaxLableNode); | |
487 | |
488 Element inputMaxValueNode = xmlutilities.createXFormElement(document, "value"); | |
489 inputMaxValueNode.setTextContent(max.toString()); | |
490 inputMaxNode.appendChild(inputMaxValueNode); | |
491 | |
492 | |
493 if (!it.hasNext()){ | |
494 dynamic.appendChild(inputMinNode); | |
495 dynamic.appendChild(inputMaxNode); | |
496 }else{ | |
497 staticNode.appendChild(inputMinNode); | |
498 staticNode.appendChild(inputMaxNode); | |
499 } | |
500 }else if (o instanceof SingleValueDescribeData){ | |
501 | |
502 SingleValueDescribeData svdb = (SingleValueDescribeData)o; | |
503 | |
504 Element inputNode = xmlutilities.createXFormElement(document, "input"); | |
505 inputNode.setAttribute("ref", svdb.getName()); | |
506 | |
507 Element inputLableNode = xmlutilities.createXFormElement(document, "label"); | |
508 inputLableNode.setTextContent(RessourceFactory.getInstance().getRessource(callMeta.getLanguages(), svdb.getName(), svdb.getName())); | |
509 inputNode.appendChild(inputLableNode); | |
510 | |
511 Element inputValueNode = xmlutilities.createXFormElement(document, "value"); | |
512 inputValueNode.setTextContent(svdb.getValue()); | |
513 inputNode.appendChild(inputValueNode); | |
514 | |
515 | |
516 if (!it.hasNext()){ | |
517 dynamic.appendChild(inputNode); | |
518 }else{ | |
519 staticNode.appendChild(inputNode); | |
520 } | |
521 } | |
522 | |
523 } | |
524 } | |
525 } | |
526 | |
527 /** | |
528 * @see de.intevation.gnv.transition.Transition#getDescibeData() | |
529 */ | |
530 public Collection<Object> getDescibeData() { | |
531 return this.descibeData; | |
532 } | |
533 | |
534 /** | |
535 * @see de.intevation.gnv.transition.Transition#setDescibeData(java.util.Collection) | |
536 */ | |
537 public void setDescibeData(Collection<Object> descibeData) { | |
538 this.descibeData = descibeData; | |
539 | |
540 } | |
541 | |
542 /** | |
543 * @see de.intevation.gnv.transition.Transition#getInputData() | |
544 */ | |
545 public Collection<InputData> getInputData() throws TransitionException { | |
546 return this.inputData != null ? this.inputData.values() : null; | |
547 } | |
548 } |