diff gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java @ 12:4ebe57b170d3

Integration of moving through the Artifact-States and rendering the UI. gnv/trunk@91 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Wed, 16 Sep 2009 07:44:26 +0000
parents 3cb753564552
children 2535158e2687
line wrap: on
line diff
--- a/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java	Tue Sep 15 14:19:55 2009 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java	Wed Sep 16 07:44:26 2009 +0000
@@ -29,6 +29,7 @@
 import de.intevation.gnv.artifactdatabase.objects.ArtifactDescription;
 import de.intevation.gnv.artifactdatabase.objects.ArtifactFactory;
 import de.intevation.gnv.artifactdatabase.objects.ArtifactObject;
+import de.intevation.gnv.artifactdatabase.objects.InputParameter;
 import de.intevation.gnv.util.XMLUtils;
 
 /**
@@ -245,7 +246,7 @@
         try {
             
             Document request = this.createDescribeRequestBody(currentArtifact);
-            String url = ((ArtifactFactory)artifactFactory).getDataBaseUrl()+"/artifact/"+ currentArtifact.getId();
+            String url = this.getArtifactUrl(artifactFactory, currentArtifact);
             Document result = this.doGetRequest(url);
             return this.readDescription(result,currentArtifact);
         } catch (IOException e) {
@@ -253,13 +254,24 @@
             throw new ArtifactDatabaseClientException(e);
         }
     }
+
+    /**
+     * @param artifactFactory
+     * @param currentArtifact
+     * @return
+     */
+    private String getArtifactUrl(ArtifactObject artifactFactory,
+            ArtifactObject currentArtifact) {
+        String url = ((ArtifactFactory)artifactFactory).getDataBaseUrl()+"/artifact/"+ currentArtifact.getId();
+        return url;
+    }
     
     private ArtifactDescription readDescription(Document document, ArtifactObject artifact) throws ArtifactDatabaseClientException{
         XMLUtils xmlUtils = new XMLUtils();
         if (artifact instanceof ArtifactDescription){
             ArtifactDescription ad = (ArtifactDescription)artifact;
             Node uiNode =  xmlUtils.getNodeXPath(document, "/result/ui");
-            uiNode = uiNode.getFirstChild();
+//            uiNode = uiNode.getFirstChild();
             Node outputNode = xmlUtils.getNodeXPath(document, "/result/outputs");
             String currentState = xmlUtils.getStringXPath(document, "/result/state/@name");
             NodeList statesList = xmlUtils.getNodeSetXPath(document, "/result/reachable-states/state/@name");
@@ -268,6 +280,17 @@
                 reachableStates.add(statesList.item(i).getNodeValue());
             }
             
+            NodeList inputNodes = xmlUtils.getNodeSetXPath(document, "/result/model/input");
+            if (inputNodes != null){
+                Collection<String> inputParameter = new ArrayList<String>(inputNodes.getLength());
+                for (int i = 0; i < inputNodes.getLength(); i++){
+                    Node inputNode = inputNodes.item(i);
+                    String name = xmlUtils.getStringXPath(inputNode, "@name");
+                    inputParameter.add(name);
+                }
+                ad.setInputParameter(inputParameter);
+            }
+            
             ad.setCurrentOut(outputNode);
             ad.setCurrentUI(uiNode);
             ad.setCurrentState(currentState);
@@ -280,7 +303,93 @@
        
         
     }
-    
+
+    /**
+     * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#doNextStep(de.intevation.gnv.artifactdatabase.objects.ArtifactObject, de.intevation.gnv.artifactdatabase.objects.ArtifactObject, java.lang.String, java.util.Collection)
+     */
+    public ArtifactDescription doNextStep(ArtifactObject artifactFactory,
+            ArtifactObject currentArtifact, String target,
+            Collection<InputParameter> inputParameter)
+            throws ArtifactDatabaseClientException {
+        
+        try {
+            // 1 Feed
+            Document feedDocument = this.createFeedRequestBody(currentArtifact, inputParameter);
+            String url = this.getArtifactUrl(artifactFactory, currentArtifact);
+            InputStream feedResult = this.doPostRequest(url, feedDocument);
+            // TODO feedResult auswerten und ggf. Fehler werfen.
+            // 2 Advance
+            Document advanceDocument = this.createAdvanceRequestBody(currentArtifact, target);
+            log.debug(new XMLUtils().writeDocument2String(advanceDocument));
+            InputStream advanceResult = this.doPostRequest(url, advanceDocument);
+            // TODO feedResult auswerten und ggf. Fehler werfen.
+            // 3 Descibe
+            return this.getCurrentStepDescription(artifactFactory, currentArtifact);
+        } catch (IOException e) {
+            log.error(e,e);
+            throw new ArtifactDatabaseClientException(e);
+        }
+    }
     
-   
+    private Document createFeedRequestBody(ArtifactObject currentArtifact, 
+                                           Collection<InputParameter> inputParameter){
+        Document document = new XMLUtils().newDocument();
+        Node rootNode  = this.createRootNode(document);
+        
+        Element typeNode = this.createArtifactElement(document, "type");
+        typeNode.setAttribute("name", "feed");
+        rootNode.appendChild(typeNode);
+             
+        Element uuidNode = this.createArtifactElement(document, "uuid");
+        uuidNode.setAttribute("value", currentArtifact.getId());
+        rootNode.appendChild(uuidNode);
+        
+        Element hashNode = this.createArtifactElement(document, "hash");
+        hashNode.setAttribute("value", currentArtifact.getHash());
+        rootNode.appendChild(hashNode);
+        
+        Element dataNode = this.createArtifactElement(document, "data");
+        rootNode.appendChild(dataNode);
+        if (inputParameter != null){
+            Iterator<InputParameter> it = inputParameter.iterator();
+            while(it.hasNext()){
+                InputParameter ip = it.next();
+                String name = ip.getName();
+                String[] values = ip.getValues();
+                if (values != null){
+                    for (int i = 0; i < values.length; i++){
+                        String value = values[i];
+                        Element inputNode = this.createArtifactElement(document, "input");
+                        inputNode.setAttribute("name", name);
+                        inputNode.setAttribute("value", value);
+                        dataNode.appendChild(inputNode);
+                    }
+                }
+            }
+        }
+        
+        
+        return document;
+    }
+    
+    private Document createAdvanceRequestBody(ArtifactObject currentArtifact, String target){
+            Document document = new XMLUtils().newDocument();
+            Node rootNode  = this.createRootNode(document);
+            
+            Element typeNode = this.createArtifactElement(document, "type");
+            typeNode.setAttribute("name", "advance");
+            rootNode.appendChild(typeNode);
+            
+            Element uuidNode = this.createArtifactElement(document, "uuid");
+            uuidNode.setAttribute("value", currentArtifact.getId());
+            rootNode.appendChild(uuidNode);
+            
+            Element hashNode = this.createArtifactElement(document, "hash");
+            hashNode.setAttribute("value", currentArtifact.getHash());
+            rootNode.appendChild(hashNode);
+            Element targetNode = this.createArtifactElement(document, "target");
+            targetNode.setAttribute("name", target);
+            rootNode.appendChild(targetNode);
+            return document;
+    }
 }

http://dive4elements.wald.intevation.org