diff gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java @ 735:d47901df0a14

Integrated the handling of AutoResumeStates into the initialization Process of an Artifact. gnv-artifacts/trunk@773 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Mon, 15 Mar 2010 12:50:08 +0000
parents dde7f51dbe1e
children 9a828e5a2390
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java	Mon Mar 15 12:39:35 2010 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/artifacts/GNVArtifactBase.java	Mon Mar 15 12:50:08 2010 +0000
@@ -1,41 +1,7 @@
 package de.intevation.gnv.artifacts;
 
-import de.intevation.artifactdatabase.Config;
-import de.intevation.artifactdatabase.ProxyArtifact;
-import de.intevation.artifactdatabase.XMLUtils;
-
-import de.intevation.artifacts.Artifact;
-import de.intevation.artifacts.ArtifactDatabase;
-import de.intevation.artifacts.ArtifactFactory;
-import de.intevation.artifacts.ArtifactNamespaceContext;
-import de.intevation.artifacts.CallContext;
-import de.intevation.artifacts.CallMeta;
-
-import de.intevation.gnv.artifacts.context.GNVArtifactContext;
-
-import de.intevation.gnv.artifacts.fis.product.Product;
-
-import de.intevation.gnv.artifacts.ressource.RessourceFactory;
-
-import de.intevation.gnv.state.DefaultInputData;
-import de.intevation.gnv.state.ExportMode;
-import de.intevation.gnv.state.InputData;
-import de.intevation.gnv.state.InputValue;
-import de.intevation.gnv.state.OutputMode;
-import de.intevation.gnv.state.OutputState;
-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;
-
 import java.io.IOException;
 import java.io.OutputStream;
-
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -47,12 +13,37 @@
 import javax.xml.xpath.XPathConstants;
 
 import org.apache.log4j.Logger;
-
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
+import de.intevation.artifactdatabase.Config;
+import de.intevation.artifactdatabase.ProxyArtifact;
+import de.intevation.artifactdatabase.XMLUtils;
+import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.ArtifactDatabase;
+import de.intevation.artifacts.ArtifactFactory;
+import de.intevation.artifacts.ArtifactNamespaceContext;
+import de.intevation.artifacts.CallContext;
+import de.intevation.artifacts.CallMeta;
+import de.intevation.gnv.artifacts.context.GNVArtifactContext;
+import de.intevation.gnv.artifacts.fis.product.Product;
+import de.intevation.gnv.artifacts.ressource.RessourceFactory;
+import de.intevation.gnv.state.AutoResumeState;
+import de.intevation.gnv.state.DefaultInputData;
+import de.intevation.gnv.state.ExportMode;
+import de.intevation.gnv.state.InputData;
+import de.intevation.gnv.state.InputValue;
+import de.intevation.gnv.state.OutputMode;
+import de.intevation.gnv.state.OutputState;
+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;
+
 /**
  * @author Tim Englich         (tim.englich@intevation.de)
  * @author Ingo Weinzierl      (ingo.weinzierl@intevation.de)
@@ -168,24 +159,17 @@
 
                 next = states.get(targetState);
 
-                // 2. Transfer Results
-                next.putInputData(current.getInputData(), identifier);
-                next.setParent(current);
-                next.setPreSettings(this.preSettings);
-
-                if (current != null) {
-                    current.endOfLife(context.globalContext());
+                boolean success = go2NextState(context, result, next);
+                
+                if (success){
+                    result = createReport(
+                            result, "result", "success", "Advance success"
+                        );
+                }else{
+                    result =  ArtifactXMLUtilities.
+                    createExceptionReport("Error while going to next State.", 
+                            XMLUtils.newDocument());
                 }
-
-                // 3. Switch to next State
-                current = next;
-
-                // 4. Initialize next Step
-                current.initialize(identifier, context);
-
-                result = createReport(
-                    result, "result", "success", "Advance success"
-                );
             }
 
             // step backward
@@ -246,6 +230,34 @@
     }
 
 
+    /**
+     * @param context
+     * @param result
+     * @param next
+     * @return
+     * @throws StateException
+     */
+    private boolean go2NextState(CallContext context, Document result,
+                                  State next) throws StateException {
+        // 2. Transfer Results
+        next.putInputData(current.getInputData(), identifier);
+        next.setParent(current);
+        next.setPreSettings(this.preSettings);
+
+        if (current != null) {
+            current.endOfLife(context.globalContext());
+        }
+
+        // 3. Switch to next State
+        current = next;
+
+        // 4. Initialize next Step
+        current.initialize(identifier, context);
+
+        return true;
+    }
+
+
     protected Document createReport(
         Document document,
         String   nodeName,
@@ -302,16 +314,53 @@
         Document result = XMLUtils.newDocument();
         try {
             this.current.initialize(super.identifier, context);
-            result = new ArtifactXMLUtilities()
-            .createSuccessReport("Initialize success",
-                    XMLUtils.newDocument());
+            
+            if (this.current instanceof AutoResumeState){
+                // Jump to next State using the Transitions.
+                State next = this.getNextReachableState(this.current);
+                if (next != null){
+                    boolean success = go2NextState(context, result, next);
+                    if (success){
+                        result = ArtifactXMLUtilities
+                        .createSuccessReport("Initialize success",
+                                XMLUtils.newDocument());
+                    }else{
+                        result =  ArtifactXMLUtilities.
+                                    createExceptionReport(
+                                            "Error while going to next State.", 
+                                            XMLUtils.newDocument());
+                    }
+                }else{
+                    result = ArtifactXMLUtilities.
+                                     createExceptionReport("No propper State found.", 
+                                                           XMLUtils.newDocument());
+                }
+            }else{
+                result = ArtifactXMLUtilities
+                            .createSuccessReport("Initialize success",
+                                    XMLUtils.newDocument());
+            }
         } catch (StateException e) {
             log.error(e,e);
-            result = new ArtifactXMLUtilities().createExceptionReport(e
+            result = ArtifactXMLUtilities.createExceptionReport(e
                     .getLocalizedMessage(), XMLUtils.newDocument());
         }
         return result;
     }
+    
+    protected State getNextReachableState(State current){
+        Iterator<Transition> it = this.transitions.iterator();
+        String from = current.getID();
+        while (it.hasNext()){
+            Transition transition = it.next();
+            if (transition.getFrom().equals(from)){
+                if (transition.isValid(current)){
+                    return this.states.get(transition.getTo());
+                }
+            }
+        }
+        return null;
+    }
 
     protected String readStateName(Document document) {
         String returnValue = XMLUtils.xpathString(
@@ -363,19 +412,19 @@
                         EXCEPTION_NO_INPUT);
 
                     log.warn(msg);
-                    result = new ArtifactXMLUtilities().createInputExceptionReport(
+                    result = ArtifactXMLUtilities.createInputExceptionReport(
                         msg,
                         XMLUtils.newDocument());
                 }
             } else {
                 String msg = "No State instantiated";
                 log.warn(msg);
-                result = new ArtifactXMLUtilities().createExceptionReport(msg,
+                result = ArtifactXMLUtilities.createExceptionReport(msg,
                         XMLUtils.newDocument());
             }
         } catch (StateException e) {
             log.error(e, e);
-            result = new ArtifactXMLUtilities().createExceptionReport(e
+            result = ArtifactXMLUtilities.createExceptionReport(e
                     .getLocalizedMessage(), XMLUtils.newDocument());
         }
         return result;

http://dive4elements.wald.intevation.org