comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/transitions/ValueCompareTransition.java @ 356:22e9574c8b1b

Added a new Transition to compare values. flys-artifacts/trunk@1763 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 28 Apr 2011 11:22:45 +0000
parents
children eb5564662e19
comparison
equal deleted inserted replaced
355:d97982627596 356:22e9574c8b1b
1 package de.intevation.flys.artifacts.transitions;
2
3 import javax.xml.xpath.XPathConstants;
4
5 import org.apache.log4j.Logger;
6
7 import org.w3c.dom.Node;
8
9 import de.intevation.artifacts.Artifact;
10
11 import de.intevation.artifactdatabase.data.StateData;
12 import de.intevation.artifactdatabase.state.State;
13
14 import de.intevation.artifacts.common.utils.XMLUtils;
15
16 import de.intevation.flys.artifacts.FLYSArtifact;
17
18
19 /**
20 * This transition compares data objects with a <i>equal</i> or <i>notequal</i>
21 * operator.
22 *
23 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
24 */
25 public class ValueCompareTransition extends DefaultTransition {
26
27 /** The logger that is used in this transition.*/
28 private static Logger log = Logger.getLogger(ValueCompareTransition.class);
29
30
31 /** XPath that points to the condition's operator.*/
32 public static final String XPATH_OPERATOR = "condition/@operator";
33
34 /** XPath that points to the condition's value.*/
35 public static final String XPATH_VALUE = "condition/@value";
36
37 /** XPath that points to the condition's dataname.*/
38 public static final String XPATH_DATANAME = "condition/@data";
39
40 /** The value of the 'equal' operator.*/
41 public static final String OPERATOR_EQUAL = "equal";
42
43 /** The value of the 'not equal' operator.*/
44 public static final String OPERATOR_NOTEQUAL = "notequal";
45
46
47 /** The operator.*/
48 protected String operator;
49
50 /** The value used for comparison.*/
51 protected String value;
52
53 /** The name of the data used for the comparison.*/
54 protected String dataname;
55
56
57
58 public ValueCompareTransition() {
59 }
60
61
62 public ValueCompareTransition(String from, String to) {
63 super(from, to);
64 }
65
66
67 @Override
68 public void init(Node config) {
69 log.debug("ValueCompareTransition.init");
70
71 String tmp = (String) XMLUtils.xpath(
72 config,
73 XPATH_OPERATOR,
74 XPathConstants.STRING);
75 operator = tmp.trim().toLowerCase();
76
77 value = (String) XMLUtils.xpath(
78 config,
79 XPATH_VALUE,
80 XPathConstants.STRING);
81
82 dataname = (String) XMLUtils.xpath(
83 config,
84 XPATH_DATANAME,
85 XPathConstants.STRING);
86 }
87
88
89 @Override
90 public boolean isValid(Artifact artifact, State a, State b) {
91 log.debug("ValueCompareTransition.isValid");
92
93 FLYSArtifact flysArtifact = (FLYSArtifact) artifact;
94
95 StateData dataObj = flysArtifact.getData(dataname);
96 String dataVal = dataObj != null ? (String) dataObj.getValue() : "";
97
98 if (log.isDebugEnabled()) {
99 log.debug("Compare settings:");
100 log.debug("-- dataname: " + dataname);
101 log.debug("-- operator: " + operator);
102 log.debug("-- compare value: " + value);
103 log.debug("-- state value: " + dataVal);
104 }
105
106 if (operator.equals(OPERATOR_EQUAL)) {
107 return value.equals(dataVal);
108 }
109 else if (operator.equals(OPERATOR_NOTEQUAL)) {
110 return !(value.equals(dataVal));
111 }
112
113 log.error("Wrong operator set! No comparison takes place.");
114
115 return false;
116 }
117 }
118 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org