changeset 140:43f03f6047b9

Implemented the step-back part of the advance() operation. flys-artifacts/trunk@1522 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 21 Mar 2011 08:17:15 +0000
parents 05207cc4a084
children 7f909429ccc8
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java
diffstat 2 files changed, 48 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Mon Mar 21 06:11:01 2011 +0000
+++ b/flys-artifacts/ChangeLog	Mon Mar 21 08:17:15 2011 +0000
@@ -1,3 +1,8 @@
+2011-03-21  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java:
+	  Implemented the step-back part of the advance() operation.
+
 2011-03-21  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/resources/messages_de.properties: Fixed a german umlaut.
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Mon Mar 21 06:11:01 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Mon Mar 21 08:17:15 2011 +0000
@@ -182,8 +182,6 @@
      * @return a document that contains a SUCCESS or FAILURE message.
      */
     public Document advance(Document target, CallContext context) {
-        logger.info("FLYSArtifact.advance()");
-
         Document doc = XMLUtils.newDocument();
 
         XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
@@ -196,8 +194,10 @@
         String targetState = XMLUtils.xpathString(
             target, XPATH_ADVANCE_TARGET, ArtifactNamespaceContext.INSTANCE);
 
+        logger.info("FLYSArtifact.advance() to '" + targetState + "'");
+
         if (isStateReachable(targetState, context)) {
-            logger.debug("Advance: Step forward.");
+            logger.info("Advance: Step forward");
 
             Vector<String> prev = getPreviousStateIds();
             prev.add(getCurrentStateId());
@@ -206,8 +206,23 @@
 
             return describe(target, context);
         }
+        else if (isPreviousState(targetState, context)) {
+            logger.info("Advance: Step back to");
 
-        // TODO IMPLEMENT STEP BACK!
+            Vector<String> prevs = getPreviousStateIds();
+            int targetIdx        = prevs.indexOf(targetState);
+            int start            = prevs.size() - 1;
+
+            for (int i = start; i >= targetIdx; i--) {
+                String prev = prevs.get(i);
+                logger.debug("Remove state id '" + prev + "'");
+                prevs.remove(prev);
+            }
+
+            setCurrentStateId(targetState);
+
+            return describe(target, context);
+        }
 
         logger.warn("Advance: Cannot advance to '" + targetState + "'");
         ec.addAttr(result, "type", OPERATION_FAILED, true);
@@ -354,6 +369,11 @@
      */
     protected State fillState(State state) {
         Map<String, StateData> stateData = state.getData();
+
+        if (stateData == null) {
+            return state;
+        }
+
         Set<String>                 keys = stateData.keySet();
 
         for (String key: keys) {
@@ -394,5 +414,24 @@
 
         return tEngine.isStateReachable(stateId, currentState, sEngine);
     }
+
+
+    /**
+     * Determines if the state with the identifier <i>stateId</i> is a previous
+     * state of the current state.
+     *
+     * @param stateId The target state identifier.
+     * @param context The context object.
+     */
+    protected boolean isPreviousState(String stateId, Object context) {
+        logger.debug("Determine if the state '" + stateId + "' is old.");
+
+        Vector<String> prevs = getPreviousStateIds();
+        if (prevs.contains(stateId)) {
+            return true;
+        }
+
+        return false;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org