ingo@356: package de.intevation.flys.artifacts.transitions; ingo@356: ingo@356: import javax.xml.xpath.XPathConstants; ingo@356: ingo@356: import org.apache.log4j.Logger; ingo@356: ingo@356: import org.w3c.dom.Node; ingo@356: ingo@356: import de.intevation.artifacts.Artifact; ingo@356: ingo@356: import de.intevation.artifactdatabase.data.StateData; ingo@356: import de.intevation.artifactdatabase.state.State; ingo@356: ingo@356: import de.intevation.artifacts.common.utils.XMLUtils; ingo@356: ingo@356: import de.intevation.flys.artifacts.FLYSArtifact; ingo@356: ingo@356: ingo@356: /** ingo@356: * This transition compares data objects with a equal or notequal ingo@356: * operator. ingo@356: * ingo@356: * @author Ingo Weinzierl ingo@356: */ ingo@356: public class ValueCompareTransition extends DefaultTransition { ingo@356: ingo@356: /** The logger that is used in this transition.*/ ingo@356: private static Logger log = Logger.getLogger(ValueCompareTransition.class); ingo@356: ingo@356: ingo@356: /** XPath that points to the condition's operator.*/ ingo@356: public static final String XPATH_OPERATOR = "condition/@operator"; ingo@356: ingo@356: /** XPath that points to the condition's value.*/ ingo@356: public static final String XPATH_VALUE = "condition/@value"; ingo@356: ingo@356: /** XPath that points to the condition's dataname.*/ ingo@356: public static final String XPATH_DATANAME = "condition/@data"; ingo@356: ingo@356: /** The value of the 'equal' operator.*/ ingo@356: public static final String OPERATOR_EQUAL = "equal"; ingo@356: ingo@356: /** The value of the 'not equal' operator.*/ ingo@356: public static final String OPERATOR_NOTEQUAL = "notequal"; ingo@356: ingo@356: ingo@356: /** The operator.*/ ingo@356: protected String operator; ingo@356: ingo@356: /** The value used for comparison.*/ ingo@356: protected String value; ingo@356: ingo@356: /** The name of the data used for the comparison.*/ ingo@356: protected String dataname; ingo@356: ingo@356: ingo@356: ingo@356: public ValueCompareTransition() { ingo@356: } ingo@356: ingo@356: ingo@356: public ValueCompareTransition(String from, String to) { ingo@356: super(from, to); ingo@356: } ingo@356: ingo@356: felix@4043: /** Setup member values from the document. */ ingo@356: @Override ingo@356: public void init(Node config) { ingo@356: log.debug("ValueCompareTransition.init"); ingo@356: ingo@356: String tmp = (String) XMLUtils.xpath( ingo@356: config, ingo@356: XPATH_OPERATOR, ingo@356: XPathConstants.STRING); ingo@356: operator = tmp.trim().toLowerCase(); ingo@356: ingo@356: value = (String) XMLUtils.xpath( ingo@356: config, ingo@356: XPATH_VALUE, ingo@356: XPathConstants.STRING); ingo@356: ingo@356: dataname = (String) XMLUtils.xpath( ingo@356: config, ingo@356: XPATH_DATANAME, ingo@356: XPathConstants.STRING); ingo@356: } ingo@356: ingo@356: ingo@356: @Override ingo@356: public boolean isValid(Artifact artifact, State a, State b) { ingo@356: log.debug("ValueCompareTransition.isValid"); ingo@356: ingo@356: FLYSArtifact flysArtifact = (FLYSArtifact) artifact; ingo@356: ingo@356: StateData dataObj = flysArtifact.getData(dataname); ingo@356: String dataVal = dataObj != null ? (String) dataObj.getValue() : ""; ingo@356: ingo@356: if (log.isDebugEnabled()) { ingo@356: log.debug("Compare settings:"); ingo@356: log.debug("-- dataname: " + dataname); ingo@356: log.debug("-- operator: " + operator); ingo@356: log.debug("-- compare value: " + value); ingo@356: log.debug("-- state value: " + dataVal); ingo@356: } ingo@356: ingo@356: if (operator.equals(OPERATOR_EQUAL)) { ingo@356: return value.equals(dataVal); ingo@356: } ingo@356: else if (operator.equals(OPERATOR_NOTEQUAL)) { ingo@356: return !(value.equals(dataVal)); ingo@356: } ingo@356: ingo@356: log.error("Wrong operator set! No comparison takes place."); ingo@356: ingo@356: return false; ingo@356: } ingo@356: } ingo@356: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :