comparison gnv-artifacts/src/main/java/de/intevation/gnv/state/StateBase.java @ 607:292fbcd5e9ac

Parted the cache blob into pieces and changed the way how user input is stored on each state. gnv-artifacts/trunk@673 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 10 Feb 2010 11:43:35 +0000
parents e8ebdbc7f1e3
children 74dfb9346574
comparison
equal deleted inserted replaced
606:9efc1c256dbb 607:292fbcd5e9ac
11 import java.util.List; 11 import java.util.List;
12 import java.util.Map; 12 import java.util.Map;
13 import java.util.Set; 13 import java.util.Set;
14 14
15 import javax.xml.xpath.XPathConstants; 15 import javax.xml.xpath.XPathConstants;
16
17 import net.sf.ehcache.Cache;
16 18
17 import org.apache.log4j.Logger; 19 import org.apache.log4j.Logger;
18 import org.w3c.dom.Document; 20 import org.w3c.dom.Document;
19 import org.w3c.dom.Element; 21 import org.w3c.dom.Element;
20 import org.w3c.dom.Node; 22 import org.w3c.dom.Node;
57 /** 59 /**
58 * the logger, used to log exceptions and additonaly information 60 * the logger, used to log exceptions and additonaly information
59 */ 61 */
60 private static Logger log = Logger.getLogger(GNVArtifactBase.class); 62 private static Logger log = Logger.getLogger(GNVArtifactBase.class);
61 63
62 private final static String MINVALUEFIELDNAME = "minvalue"; 64 protected final static String MINVALUEFIELDNAME = "minvalue";
63 private final static String MAXVALUEFIELDNAME = "maxvalue"; 65 protected final static String MAXVALUEFIELDNAME = "maxvalue";
64 66
65 private final static String NODATASELECTIONKEY = "n/n"; 67 private final static String NODATASELECTIONKEY = "n/n";
66 68
67 public final static String DESCRIBEDATAKEY = "_DESCRIBEDATA"; 69 public final static String DESCRIBEDATAKEY = "_DESCRIBEDATA";
68 70
86 88
87 protected String queryID = null; 89 protected String queryID = null;
88 90
89 protected Collection<String> inputValueNames = null; 91 protected Collection<String> inputValueNames = null;
90 92
91 private Map<String, InputValue> inputValues = null; 93 protected Map<String, InputValue> inputValues = null;
92 94
93 private State parent = null; 95 protected State parent = null;
94 96
95 protected Map<String, InputData> inputData = null; 97 protected Map<String, InputData> inputData = null;
96 98
97 private Map<String, InputData> preSettings = null; 99 protected Map<String, InputData> preSettings = null;
98 100
99 /** 101 /**
100 * Constructor 102 * Constructor
101 */ 103 */
102 public StateBase() { 104 public StateBase() {
126 return this.inputValues.values(); 128 return this.inputValues.values();
127 } 129 }
128 130
129 131
130 public void reset(String uuid) { 132 public void reset(String uuid) {
131 133
132 if (parent != null) { 134 if (parent != null) {
133 inputData = parent.inputData(); 135 inputData = parent.inputData();
134 } 136 }
135 } 137 }
136 138
137 139
138 /** 140 /**
139 * @see de.intevation.gnv.state.State#setup(org.w3c.dom.Node) 141 * @see de.intevation.gnv.state.State#setup(org.w3c.dom.Node)
140 */ 142 */
141 public void setup(Node configuration) { 143 public void setup(Node configuration) {
142 log.debug("StateBase.setup");
143 this.id = ((Element)configuration).getAttribute("id"); 144 this.id = ((Element)configuration).getAttribute("id");
144 this.description = ((Element)configuration).getAttribute("description"); 145 this.description = ((Element)configuration).getAttribute("description");
145 146
146 log.info("State-ID = " + this.id); 147 log.info("State-ID = " + this.id);
147 148
165 } 166 }
166 InputValue inputValue = new DefaultInputValue(inputValueNode.getAttribute("name"), 167 InputValue inputValue = new DefaultInputValue(inputValueNode.getAttribute("name"),
167 inputValueNode.getAttribute("type"), 168 inputValueNode.getAttribute("type"),
168 Boolean.parseBoolean(inputValueNode. 169 Boolean.parseBoolean(inputValueNode.
169 getAttribute("multiselect")), usedinQuery); 170 getAttribute("multiselect")), usedinQuery);
170 log.debug(inputValue.toString());
171 this.inputValues.put(inputValue.getName(), inputValue); 171 this.inputValues.put(inputValue.getName(), inputValue);
172 this.inputValueNames.add(inputValue.getName()); 172 this.inputValueNames.add(inputValue.getName());
173 } 173 }
174 174
175 this.queryID = Config.getStringXPath(configuration, "queryID"); 175 this.queryID = Config.getStringXPath(configuration, "queryID");
205 */ 205 */
206 public void setParent(State state) { 206 public void setParent(State state) {
207 this.parent = state; 207 this.parent = state;
208 } 208 }
209 209
210 /** 210
211 * @see de.intevation.gnv.state.State#putInputData(java.util.Collection) 211 public void feed(Collection<InputData> inputData, String uuid)
212 */ 212 throws StateException
213 public void putInputData(Collection<InputData> inputData, String uuid) 213 {
214 throws StateException {
215 log.debug("StateBase.putInputData");
216 if (inputData != null) { 214 if (inputData != null) {
217 Iterator<InputData> it = inputData.iterator(); 215 Iterator<InputData> it = inputData.iterator();
218 InputValidator iv = new InputValidator(); 216 InputValidator iv = new InputValidator();
219 while (it.hasNext()) { 217 while (it.hasNext()) {
220 InputData tmpItem = it.next(); 218 InputData tmpItem = it.next();
222 if (inputValue != null) { 220 if (inputValue != null) {
223 if (this.inputData == null) { 221 if (this.inputData == null) {
224 this.inputData = new HashMap<String, InputData>( 222 this.inputData = new HashMap<String, InputData>(
225 inputData.size()); 223 inputData.size());
226 } 224 }
227 225
228 boolean valid = iv.isInputValid(tmpItem.getValue(), 226 boolean valid = iv.isInputValid(tmpItem.getValue(),
229 inputValue.getType()); 227 inputValue.getType());
230 if (valid) { 228 if (valid) {
231 if (tmpItem.getName().equals(MINVALUEFIELDNAME)){ 229 if (tmpItem.getName().equals(MINVALUEFIELDNAME)){
232 String minValue = tmpItem.getValue(); 230 String minValue = tmpItem.getValue();
237 + " is not an " + inputValue.getType() 235 + " is not an " + inputValue.getType()
238 + " Value."; 236 + " Value.";
239 log.warn(errMsg); 237 log.warn(errMsg);
240 throw new StateException(errMsg); 238 throw new StateException(errMsg);
241 } 239 }
242 240
243 valid = iv.isInputValid(minValue, 241 valid = iv.isInputValid(minValue,
244 maxValue, 242 maxValue,
245 inputValue.getType()); 243 inputValue.getType());
246 if (!valid){ 244 if (!valid){
247 String errMsg = "MaxValue-Input is less than MinValue-Input "; 245 String errMsg = "MaxValue-Input is less than MinValue-Input ";
248 log.warn(errMsg); 246 log.warn(errMsg);
257 + " is not an " + inputValue.getType() 255 + " is not an " + inputValue.getType()
258 + " Value."; 256 + " Value.";
259 log.warn(errMsg); 257 log.warn(errMsg);
260 throw new StateException(errMsg); 258 throw new StateException(errMsg);
261 } 259 }
262 260
263 valid = iv.isInputValid(minValue, 261 valid = iv.isInputValid(minValue,
264 maxValue, 262 maxValue,
265 inputValue.getType()); 263 inputValue.getType());
266 if (!valid){ 264 if (!valid){
267 String errMsg = "MaxValue-Input is less than MinValue-Input "; 265 String errMsg = "MaxValue-Input is less than MinValue-Input ";
268 log.warn(errMsg); 266 log.warn(errMsg);
269 throw new StateException(errMsg); 267 throw new StateException(errMsg);
270 } 268 }
271 } 269 }
272 this.setSelection(tmpItem, uuid);
273 this.inputData.put(tmpItem.getName(), tmpItem); 270 this.inputData.put(tmpItem.getName(), tmpItem);
274 } else { 271 } else {
275 String errMsg = "Wrong input for " + tmpItem.getValue() 272 String errMsg = "Wrong input for " + tmpItem.getValue()
276 + " is not an " + inputValue.getType() 273 + " is not an " + inputValue.getType()
277 + " Value."; 274 + " Value.";
290 log.warn("No Inputdata given"); 287 log.warn("No Inputdata given");
291 } 288 }
292 } 289 }
293 290
294 /** 291 /**
292 * @see de.intevation.gnv.state.State#putInputData(java.util.Collection)
293 */
294 public void putInputData(Collection<InputData> inputData, String uuid)
295 throws StateException {
296 if (inputData != null) {
297 Iterator<InputData> it = inputData.iterator();
298 InputValidator iv = new InputValidator();
299 while (it.hasNext()) {
300 InputData tmpItem = it.next();
301 InputValue inputValue = this.inputValues.get(tmpItem.getName());
302 if (inputValue != null) {
303 if (this.inputData == null) {
304 this.inputData = new HashMap<String, InputData>(
305 inputData.size());
306 }
307
308 boolean valid = iv.isInputValid(tmpItem.getValue(),
309 inputValue.getType());
310 if (valid) {
311 if (tmpItem.getName().equals(MINVALUEFIELDNAME)){
312 String minValue = tmpItem.getValue();
313 String maxValue = this.getInputValue4ID(inputData, MAXVALUEFIELDNAME);
314 valid = iv.isInputValid(maxValue,inputValue.getType());
315 if (!valid){
316 String errMsg = "Wrong input for " + tmpItem.getValue()
317 + " is not an " + inputValue.getType()
318 + " Value.";
319 log.warn(errMsg);
320 throw new StateException(errMsg);
321 }
322
323 valid = iv.isInputValid(minValue,
324 maxValue,
325 inputValue.getType());
326 if (!valid){
327 String errMsg = "MaxValue-Input is less than MinValue-Input ";
328 log.warn(errMsg);
329 throw new StateException(errMsg);
330 }
331 }else if (tmpItem.getName().equals(MAXVALUEFIELDNAME)){
332 String minValue = this.getInputValue4ID(inputData, MINVALUEFIELDNAME);
333 String maxValue = tmpItem.getValue();
334 valid = iv.isInputValid(minValue,inputValue.getType());
335 if (!valid){
336 String errMsg = "Wrong input for " + tmpItem.getValue()
337 + " is not an " + inputValue.getType()
338 + " Value.";
339 log.warn(errMsg);
340 throw new StateException(errMsg);
341 }
342
343 valid = iv.isInputValid(minValue,
344 maxValue,
345 inputValue.getType());
346 if (!valid){
347 String errMsg = "MaxValue-Input is less than MinValue-Input ";
348 log.warn(errMsg);
349 throw new StateException(errMsg);
350 }
351 }
352 this.inputData.put(tmpItem.getName(), tmpItem);
353 } else {
354 String errMsg = "Wrong input for " + tmpItem.getValue()
355 + " is not an " + inputValue.getType()
356 + " Value.";
357 log.warn(errMsg);
358 throw new StateException(errMsg);
359 }
360
361 } else {
362 String errMsg = "No Inputvalue given for Inputdata "
363 + tmpItem.getName();
364 log.warn(errMsg + "Value will be ignored");
365
366 }
367 }
368 } else {
369 log.warn("No Inputdata given");
370 }
371
372 debugInputData();
373 }
374
375 private void debugInputData() {
376 if (log.isDebugEnabled()) {
377 Iterator iter = inputData.keySet().iterator();
378 while (iter.hasNext()) {
379 String key = (String) iter.next();
380 }
381 }
382 }
383
384 /**
295 * 385 *
296 * @see de.intevation.gnv.state.State#setPreSettings(java.util.Map) 386 * @see de.intevation.gnv.state.State#setPreSettings(java.util.Map)
297 */ 387 */
298 public void setPreSettings(Map<String, InputData> preSettings) { 388 public void setPreSettings(Map<String, InputData> preSettings) {
299 this.preSettings = preSettings; 389 this.preSettings = preSettings;
309 } 399 }
310 return null; 400 return null;
311 } 401 }
312 402
313 private void setSelection(InputData inputData, String uuid) { 403 private void setSelection(InputData inputData, String uuid) {
314 log.debug("StateBase.setSelection");
315 404
316 Object o = this.getDescribeData(inputData.getName(),uuid); 405 Object o = this.getDescribeData(inputData.getName(),uuid);
317 if (o != null) { 406 if (o != null) {
318 if (o instanceof Collection<?>) { 407 if (o instanceof Collection<?>) {
319 Collection<KeyValueDescibeData> values = (Collection<KeyValueDescibeData>) o; 408 Collection<KeyValueDescibeData> values = (Collection<KeyValueDescibeData>) o;
346 } 435 }
347 } 436 }
348 } 437 }
349 438
350 private Object getDescribeData(String name, String uuid) { 439 private Object getDescribeData(String name, String uuid) {
351 log.debug("StateBase.getDescribeData");
352 Collection<Object> descibeData = this.getDescibeData(uuid); 440 Collection<Object> descibeData = this.getDescibeData(uuid);
353 if (descibeData != null) { 441 if (descibeData != null) {
354 Iterator<Object> it = descibeData.iterator(); 442 Iterator<Object> it = descibeData.iterator();
355 while (it.hasNext()) { 443 while (it.hasNext()) {
356 Object o = it.next(); 444 Object o = it.next();
381 */ 469 */
382 public void advance(String uuid, CallContext context) 470 public void advance(String uuid, CallContext context)
383 throws StateException 471 throws StateException
384 { 472 {
385 } 473 }
386 474
387 public void initialize(String uuid, CallContext context) 475 public void initialize(String uuid, CallContext context)
388 throws StateException 476 throws StateException
389 { 477 {
390 log.debug("StateBase.initialize");
391 try { 478 try {
392 String[] filterValues = this 479 getDescibeData(uuid);
393 .generateFilterValuesFromInputData(); 480 }
394 Collection<Result> result = null; 481 catch (RuntimeException e) {
395 try {
396 if (this.queryID != null) {
397 QueryExecutor queryExecutor = QueryExecutorFactory
398 .getInstance().getQueryExecutor();
399 result = queryExecutor.executeQuery(this.queryID,
400 filterValues);
401 }
402 this.purifyResult(result, uuid);
403 } catch (RuntimeException e) {
404 log.error(e, e);
405 }
406 } catch (QueryException e) {
407 log.error(e, e); 482 log.error(e, e);
408 throw new StateException(e); 483 throw new StateException(e);
409 } 484 }
410 } 485 }
411 486
447 protected String prepareInputData4RegionDBQuery(String value){ 522 protected String prepareInputData4RegionDBQuery(String value){
448 return value; 523 return value;
449 } 524 }
450 525
451 private String prepareInputData4DateDBQuery(String value) { 526 private String prepareInputData4DateDBQuery(String value) {
452 log.debug("StateBase.prepareInputData4DateDBQuery");
453 if (value != null) { 527 if (value != null) {
454 String[] values = value.split(","); 528 String[] values = value.split(",");
455 String newValue = ""; 529 String newValue = "";
456 for (int i = 0; i < values.length; i++) { 530 for (int i = 0; i < values.length; i++) {
457 if (newValue.length() > 0) { 531 if (newValue.length() > 0) {
466 540
467 return value; 541 return value;
468 } 542 }
469 543
470 private String prepareInputData4DBQuery(String value) { 544 private String prepareInputData4DBQuery(String value) {
471 log.debug("StateBase.prepareInputData4DBQuery");
472 if (value != null) { 545 if (value != null) {
473 String[] values = value.split(","); 546 String[] values = value.split(",");
474 String newValue = ""; 547 String newValue = "";
475 for (int i = 0; i < values.length; i++) { 548 for (int i = 0; i < values.length; i++) {
476 if (newValue.length() > 0) { 549 if (newValue.length() > 0) {
486 } 559 }
487 560
488 /** 561 /**
489 * @param result 562 * @param result
490 */ 563 */
491 protected void purifyResult(Collection<Result> result, String uuid) { 564 protected List<Object> purifyResult(Collection<Result> result, String uuid) {
492 log.debug("StateBase.purifyResult"); 565 List<Object> describeData = new ArrayList<Object>();
493 List<Object> describeData = this.getDescibeData(uuid); 566
494 if (describeData == null) { 567 NamedCollection<KeyValueDescibeData> keyValueDescibeData =
495 describeData = new ArrayList<Object>(); 568 extractKVP(result, "KEY", "VALUE");
496 } 569
497 NamedCollection<KeyValueDescibeData> keyValueDescibeData = extractKVP(result, "KEY", "VALUE"); 570 describeData.add(keyValueDescibeData);
498 describeData.add(keyValueDescibeData); 571
499 this.setDescibeData(uuid, describeData); 572 return describeData;
500 } 573 }
501 574
502 /** 575 /**
503 * @param result 576 * @param result
504 * @return 577 * @return
506 protected NamedCollection<KeyValueDescibeData> extractKVP(Collection<Result> result, 579 protected NamedCollection<KeyValueDescibeData> extractKVP(Collection<Result> result,
507 String keyid, 580 String keyid,
508 String valueid) { 581 String valueid) {
509 Iterator<Result> rit = result.iterator(); 582 Iterator<Result> rit = result.iterator();
510 int dataSize = (this.dataNoSelect ? result.size()+1 : result.size()); 583 int dataSize = (this.dataNoSelect ? result.size()+1 : result.size());
511 584
512 NamedCollection<KeyValueDescibeData> keyValueDescibeData = new NamedArrayList<KeyValueDescibeData>( 585 NamedCollection<KeyValueDescibeData> keyValueDescibeData = new NamedArrayList<KeyValueDescibeData>(
513 this.dataName, dataSize); 586 this.dataName, dataSize);
514 keyValueDescibeData.setMultiSelect(this.dataMultiSelect); 587 keyValueDescibeData.setMultiSelect(this.dataMultiSelect);
515 588
516 if (this.dataNoSelect){ 589 if (this.dataNoSelect){
517 keyValueDescibeData.add(new DefaultKeyValueDescribeData( 590 keyValueDescibeData.add(new DefaultKeyValueDescribeData(
518 NODATASELECTIONKEY, 591 NODATASELECTIONKEY,
519 "No Selection", 592 "No Selection",
520 getID() 593 getID()
523 596
524 boolean initialized = false; 597 boolean initialized = false;
525 int keyPos = 0; 598 int keyPos = 0;
526 int valuePos = 1; 599 int valuePos = 1;
527 String previousKey = null; 600 String previousKey = null;
528 InputData preSettingsData = (this.preSettings != null && 601 InputData preSettingsData =
529 this.preSettingsName != null) ? 602 (this.preSettings != null && this.preSettingsName != null)
530 this.preSettings.get(this.preSettingsName) : 603 ? this.preSettings.get(this.preSettingsName)
531 null; 604 : null;
532 boolean filterWithPresettings = preSettingsData != null; 605 boolean filterWithPresettings = preSettingsData != null;
533 606
534 List<String> preSettingValues = null; 607 List<String> preSettingValues = null;
535 if(filterWithPresettings){ 608 if(filterWithPresettings){
536 preSettingValues = Arrays.asList(preSettingsData.splitValue()); 609 preSettingValues = Arrays.asList(preSettingsData.splitValue());
537 } 610 }
538 while (rit.hasNext()) { 611 while (rit.hasNext()) {
544 valuePos = 1; 617 valuePos = 1;
545 } 618 }
546 initialized = true; 619 initialized = true;
547 } 620 }
548 String tmpKey = resultValue.getString(keyPos); 621 String tmpKey = resultValue.getString(keyPos);
549 622
550 // TODO: FIXME: We have to do that because the arcsde does not 623 // TODO: FIXME: We have to do that because the arcsde does not
551 // support a distinct Query on Layers. 624 // support a distinct Query on Layers.
552 if (previousKey == null || !tmpKey.equals(previousKey)){ 625 if (previousKey == null || !tmpKey.equals(previousKey)){
553 previousKey = tmpKey; 626 previousKey = tmpKey;
554 if (!filterWithPresettings || preSettingValues.contains(tmpKey)){ 627 if (!filterWithPresettings || preSettingValues.contains(tmpKey)){
610 /** 683 /**
611 * @see de.intevation.gnv.state.State#describe(org.w3c.dom.Document, 684 * @see de.intevation.gnv.state.State#describe(org.w3c.dom.Document,
612 * org.w3c.dom.Node, de.intevation.artifacts.CallMeta, 685 * org.w3c.dom.Node, de.intevation.artifacts.CallMeta,
613 * java.lang.String) 686 * java.lang.String)
614 */ 687 */
615 /*
616 public void describe(
617 Document document,
618 Node rootNode,
619 CallContext context,
620 String uuid
621 ) {
622 log.debug("StateBase.describe");
623 CallMeta callMeta = context.getMeta();
624
625 List<Object> descibeData = this.getDescibeData(uuid);
626 if (descibeData != null) {
627 Iterator<Object> it = descibeData.iterator();
628
629 XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator(
630 document,
631 ArtifactNamespaceContext.NAMESPACE_URI,
632 ArtifactNamespaceContext.NAMESPACE_PREFIX
633 );
634
635 Node staticNode = (Node) XMLUtils.xpath(
636 rootNode,
637 XPATH_STATIC_UI,
638 XPathConstants.NODE,
639 ArtifactNamespaceContext.INSTANCE
640 );
641
642 Node dynamic = (Node) XMLUtils.xpath(
643 rootNode,
644 XPATH_DYNAMIC_UI,
645 XPathConstants.NODE,
646 ArtifactNamespaceContext.INSTANCE
647 );
648
649 XMLUtils.ElementCreator xCreator = new XMLUtils.ElementCreator(
650 document,
651 XMLUtils.XFORM_URL,
652 XMLUtils.XFORM_PREFIX
653 );
654
655 while (it.hasNext()) {
656 Object o = it.next();
657 if ((!it.hasNext() && dataName != null)) {
658 appendToDynamicNode(
659 creator, xCreator, document, dynamic, callMeta, o);
660 }
661 else {
662 appendToStaticNode(
663 creator, xCreator, document, staticNode, callMeta, o);
664 }
665 }
666 }
667 }
668 */
669
670
671 public void describe( 688 public void describe(
672 Document document, 689 Document document,
673 Node rootNode, 690 Node rootNode,
674 CallContext context, 691 CallContext context,
675 String uuid) 692 String uuid)
676 { 693 {
677 log.debug("StateBase.describe");
678
679 XMLUtils.ElementCreator xCreator = new XMLUtils.ElementCreator( 694 XMLUtils.ElementCreator xCreator = new XMLUtils.ElementCreator(
680 document, 695 document,
681 XMLUtils.XFORM_URL, 696 XMLUtils.XFORM_URL,
682 XMLUtils.XFORM_PREFIX 697 XMLUtils.XFORM_PREFIX
683 ); 698 );
718 Document document, 733 Document document,
719 Node dynamic, 734 Node dynamic,
720 CallContext context, 735 CallContext context,
721 String uuid) 736 String uuid)
722 { 737 {
723 log.debug("StateBase.describeDynamic");
724 CallMeta callMeta = context.getMeta(); 738 CallMeta callMeta = context.getMeta();
739
740 String newInput = findNewValue();
741 if (newInput == null && parent != null)
742 return;
725 743
726 List<Object> descibeData = getDescibeData(uuid); 744 List<Object> descibeData = getDescibeData(uuid);
727 if (descibeData != null) { 745 if (descibeData != null) {
728 Iterator<Object> it = descibeData.iterator(); 746 Iterator<Object> it = descibeData.iterator();
729 747
833 name = nc.getName(); 851 name = nc.getName();
834 multiselect = nc.isMultiSelect(); 852 multiselect = nc.isMultiSelect();
835 } else { 853 } else {
836 Object[] names = this.inputValueNames.toArray(); 854 Object[] names = this.inputValueNames.toArray();
837 name = names[names.length - 1].toString(); 855 name = names[names.length - 1].toString();
838 } 856 }
839 857
840 Element selectNode = creator.create(multiselect?"select":"select1"); 858 Element selectNode = creator.create(multiselect?"select":"select1");
841 creator.addAttr(selectNode, "ref", name); 859 creator.addAttr(selectNode, "ref", name);
842 860
843 Element lableNode = creator.create("label"); 861 Element lableNode = creator.create("label");
992 groupNode.appendChild(inputNode); 1010 groupNode.appendChild(inputNode);
993 1011
994 node.appendChild(groupNode); 1012 node.appendChild(groupNode);
995 } 1013 }
996 1014
1015
1016 protected String getHash(String uuid) {
1017 return uuid + id + inputData.hashCode();
1018 }
1019
1020
997 /** 1021 /**
998 * @see de.intevation.gnv.state.State#getDescibeData() 1022 * @see de.intevation.gnv.state.State#getDescibeData()
999 */ 1023 */
1000 protected List<Object> getDescibeData(String uuid) { 1024 public List<Object> getDescibeData(String uuid) {
1001 if (CacheFactory.getInstance().isInitialized()) { 1025 CacheFactory factory = CacheFactory.getInstance();
1002 String key = uuid + DESCRIBEDATAKEY; 1026 if (factory.isInitialized()) {
1003 log.debug("Hash for Queryelements: " + key); 1027 // we use a cache
1004 net.sf.ehcache.Element value = CacheFactory.getInstance().getCache().get(key); 1028 Cache cache = factory.getCache();
1029
1030 String key = getHash(uuid);
1031
1032 net.sf.ehcache.Element value = cache.get(key);
1005 if (value != null) { 1033 if (value != null) {
1034 // element already in cache, so return it.
1006 return (List<Object>) (value.getObjectValue()); 1035 return (List<Object>) (value.getObjectValue());
1007 } 1036 }
1008 } 1037 else {
1038 // element is not in cache yet, so we need to fetch data from
1039 // database and put it into cache right now
1040 try {
1041 String[] filterValues = generateFilterValuesFromInputData();
1042 List<Object> data = queryDatabase(filterValues, uuid);
1043
1044 cache.put(new net.sf.ehcache.Element(key, data));
1045
1046 return data;
1047 }
1048 catch (QueryException qe) {
1049 log.error(qe, qe);
1050 }
1051 }
1052 }
1053 else {
1054 // we don't use a cache, so we have to query the database every
1055 // single time
1056 String[] filterValues = generateFilterValuesFromInputData();
1057 Collection<Result> result = null;
1058 try {
1059 return queryDatabase(filterValues, uuid);
1060 }
1061 catch (RuntimeException e) {
1062 log.error(e, e);
1063 }
1064 catch (QueryException e) {
1065 log.error(e, e);
1066 }
1067 }
1068
1009 return null; 1069 return null;
1010 } 1070 }
1011 1071
1012 /** 1072 protected List<Object> queryDatabase(String[] filterValues, String uuid)
1013 * @see de.intevation.gnv.state.State#getDescibeData() 1073 throws QueryException {
1014 */ 1074 Collection<Result> result = null;
1015 protected void setDescibeData(String uuid, List<Object> describeData) { 1075
1016 1076 if (queryID != null) {
1017 if (CacheFactory.getInstance().isInitialized()) { 1077 QueryExecutor queryExecutor =
1018 String key = uuid + DESCRIBEDATAKEY; 1078 QueryExecutorFactory.getInstance().getQueryExecutor();
1019 log.debug("Hash for Queryelements: " + key); 1079
1020 CacheFactory.getInstance().getCache().put(new net.sf.ehcache.Element(key, describeData)); 1080 result = queryExecutor.executeQuery(queryID, filterValues);
1021 } 1081 }
1022 } 1082 return purifyResult(result, uuid);
1083 }
1084
1023 1085
1024 public Map<String, InputData> inputData() { 1086 public Map<String, InputData> inputData() {
1025 return inputData; 1087 return inputData;
1026 } 1088 }
1027 1089

http://dive4elements.wald.intevation.org