tim@337: package de.intevation.gnv.transition;
tim@337:
tim@337: import java.util.Iterator;
tim@337:
tim@337: import org.apache.log4j.Logger;
sascha@779: import org.w3c.dom.Node;
tim@337:
tim@824: import de.intevation.artifactdatabase.Config;
tim@824: import de.intevation.gnv.state.InputData;
tim@824: import de.intevation.gnv.state.State;
tim@824: import de.intevation.gnv.state.exception.StateException;
tim@824:
tim@337: /**
tim@824: * Class which supports the possibility to validate if a
tim@824: * Transition could be done using simple Valuecompare Operations.
tim@824: * At this Time equal and notequal-operations are supported.
tim@824: * The Comparation is casesensitive.
sascha@780: * @author Tim Englich
tim@337: *
tim@337: */
tim@337: public class ValueCompareTransition extends TransitionBase {
tim@337:
tim@337: /**
tim@337: * the logger, used to log exceptions and additonaly information
tim@337: */
tim@337: private static Logger log = Logger.getLogger(ValueCompareTransition.class);
tim@337: /**
tim@337: * The UID of this Class.
tim@337: */
tim@337: private static final long serialVersionUID = -7846722158776823205L;
tim@337:
tim@337: /**
sascha@835: * The Name of the Datafield the Value that should be compared
tim@824: * should be fetched from.
tim@824: */
tim@824: private String dataName = null;
tim@824: /**
tim@824: * The Value that should be set to the State.
tim@824: */
tim@824: private String dataValue = null;
tim@824: /**
tim@824: * The Operation that should be used (equal notequal)
tim@824: */
tim@824: private String operator = null;
tim@824:
tim@824:
tim@824: /**
tim@337: * Constructor
tim@337: */
tim@337: public ValueCompareTransition() {
tim@337: super();
tim@337: }
tim@337:
tim@824: /**
tim@824: * validates if a Transition is valid and could be used using simple
tim@824: * Comparevalue-Operations.
tim@824: * @see de.intevation.gnv.transition.Transition#isValid(de.intevation.gnv.state.State)
tim@824: */
tim@337: public boolean isValid(State state) {
sascha@778:
tim@337: try {
tim@337: Iterator it = state.getInputData().iterator();
tim@337: while (it.hasNext()){
tim@337: InputData inputData = it.next();
tim@337: if (inputData.getName().equals(this.dataName)){
tim@337: boolean returnValue = false;
tim@337: if (operator.equals("equal")){
tim@337: returnValue = this.dataValue.equals(inputData.getValue());
tim@337: }else if (operator.equals("notequal")){
tim@337: returnValue = !this.dataValue.equals(inputData.getValue());
tim@337: }
tim@337: return returnValue;
tim@337: }
tim@337: }
tim@337: if (operator.equals("notequal")){
tim@337: // data is not given. So the constraint not Equals is fullfilled.
tim@337: return true;
tim@337: }
tim@337: } catch (StateException e) {
tim@337: log.error(e,e);
tim@337: return false;
tim@337: }
tim@337: return false;
tim@337: }
tim@337:
sascha@835:
tim@337: @Override
tim@337: public void setup(Node configuration) {
tim@337: super.setup(configuration);
tim@337: this.dataName = Config.getStringXPath(configuration,"condition/@inputvalue");
tim@337: this.dataValue = Config.getStringXPath(configuration,"condition/@value");
tim@337: this.operator = Config.getStringXPath(configuration,"condition/@operator");
tim@337: }
tim@337: }
sascha@836: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :