diff gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java @ 337:a887074460b6

Last Step of the Refactoring Work on the Transition Model. Splitted Businesslogic between States and Transitions. Splitted the ConfigurationElements of State and Transition in several XML-Fragments. gnv-artifacts/trunk@403 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Tue, 08 Dec 2009 13:38:21 +0000
parents e964a3d8f7bc
children 2f7a28f211c7
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java	Tue Dec 08 11:18:27 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java	Tue Dec 08 13:38:21 2009 +0000
@@ -5,6 +5,7 @@
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
@@ -34,6 +35,8 @@
 import de.intevation.gnv.state.State;
 import de.intevation.gnv.state.StateFactory;
 import de.intevation.gnv.state.exception.StateException;
+import de.intevation.gnv.transition.Transition;
+import de.intevation.gnv.transition.TransitionFactory;
 import de.intevation.gnv.utils.ArtifactXMLUtilities;
 
 /**
@@ -71,6 +74,11 @@
      * The States that can be used
      */
     protected Map<String, State> states = null;
+    
+    /**
+     * The Transitions which can switch between the different States.
+     */
+    protected Collection<Transition> transitions = null;
 
     /**
      * The Name of the Artifact
@@ -98,7 +106,7 @@
             if (this.current != null) {
                 String stateName = this.readStateName(target);
                 log.debug("Statename: " + stateName);
-                if (this.current.isStateReachable(stateName)) {
+                if (this.isStateCurrentlyReachable(stateName)) {
                     try {
                         State nextStep = this.states
                                 .get(stateName);
@@ -144,6 +152,21 @@
     }
     
     
+    private boolean isStateCurrentlyReachable(String stateid){
+        log.debug("GNVArtifactBase.isStateCurrentlyReachable "+stateid);
+        Iterator<Transition> it = this.transitions.iterator();
+        String from = this.current.getID();
+        while (it.hasNext()){
+            Transition transition = it.next();
+            if (transition.getFrom().equals(from)){
+                if (transition.getTo().equals(stateid) && transition.isValid(this.current)){
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+    
     public Document initialize (CallContext context) {
         Document result = XMLUtils.newDocument();
         try {
@@ -252,6 +275,17 @@
                     }
                 }
             }
+            
+            NodeList transitionList = Config.getNodeSetXPath(artifactNode,
+            "states/transition");
+            this.transitions = new ArrayList<Transition>(transitionList.getLength());
+            for (int i = 0; i < transitionList.getLength(); i++) {
+                Transition tmpTransition = TransitionFactory.getInstance()
+                        .createTransition(transitionList.item(i));
+                if (tmpTransition != null) {
+                    this.transitions.add(tmpTransition);
+                }
+            }
 
         }
     }
@@ -308,17 +342,20 @@
         Element stateNode = xmlUtilities.createArtifactElement(document,
                 "reachable-states");
         if (this.current != null) {
-            Iterator<String> states = this.current.reachableStates()
-                    .iterator();
-            while (states.hasNext()) {
-                String value = states.next();
-                Element currentNode = xmlUtilities.createArtifactElement(
-                        document, "state");
-                currentNode.setAttribute("name", value);
-                log.debug("Reachable State: " + value);
-                currentNode.setAttribute("description", this.states.get(value)
-                        .getDescription());
-                stateNode.appendChild(currentNode);
+            Iterator<Transition> transitions = this.transitions.iterator();
+            while (transitions.hasNext()) {
+                Transition tmpTransition = transitions.next();
+                if (tmpTransition.getFrom().equals(current.getID()) && 
+                    tmpTransition.isValid(this.current)){
+                    Element currentNode = xmlUtilities.createArtifactElement(
+                            document, "state");
+                    currentNode.setAttribute("name", tmpTransition.getTo());
+                    log.debug("Reachable State: " + tmpTransition.getTo());
+                    currentNode.setAttribute("description", 
+                                             this.states.get(tmpTransition.getTo())
+                                                             .getDescription());
+                    stateNode.appendChild(currentNode);
+                }
             }
         }
         parent.appendChild(stateNode);

http://dive4elements.wald.intevation.org