changeset 13:2535158e2687

Chartoutput Added to the View gnv/trunk@95 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Wed, 16 Sep 2009 12:41:27 +0000
parents 4ebe57b170d3
children 9719cbd1a23f
files gnv/Changelog gnv/src/main/java/de/intevation/gnv/action/DoOutputAction.java gnv/src/main/java/de/intevation/gnv/action/NextArtifactStepAction.java gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClient.java gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java gnv/src/main/webapp/WEB-INF/config/struts-config.xml gnv/src/main/webapp/WEB-INF/config/templates/describe-ui.xsl
diffstat 7 files changed, 281 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/gnv/Changelog	Wed Sep 16 07:44:26 2009 +0000
+++ b/gnv/Changelog	Wed Sep 16 12:41:27 2009 +0000
@@ -1,3 +1,13 @@
+2009-09-16  Tim Englich  <tim.englich@intevation.de>
+
+    * src/main/webapp/WEB-INF/config/struts-config.xml Edited,
+      src/main/webapp/WEB-INF/config/templates/describe-ui.xsl Edited, 
+      src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java Edited,
+      src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClient.java (doOutput) (doFeed) Edited,
+      src/main/java/de/intevation/gnv/action/NextArtifactStepAction.java (execute) Edited, 
+      src/main/java/de/intevation/gnv/action/DoOutputAction.java Added:
+      Chartoutput Added to the View
+      
 2009-09-16  Tim Englich  <tim.englich@intevation.de>
 
     * src/test/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClientTestCase.java Edited:
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/action/DoOutputAction.java	Wed Sep 16 12:41:27 2009 +0000
@@ -0,0 +1,80 @@
+/**
+ *
+ */
+package de.intevation.gnv.action;
+
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.struts.action.ActionForm;
+import org.apache.struts.action.ActionForward;
+import org.apache.struts.action.ActionMapping;
+
+import de.intevation.gnv.action.sessionmodel.SessionModel;
+import de.intevation.gnv.action.sessionmodel.SessionModelFactory;
+import de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient;
+import de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClientFactory;
+import de.intevation.gnv.artifactdatabase.objects.ArtifactDescription;
+import de.intevation.gnv.artifactdatabase.objects.DefaultInputParameter;
+import de.intevation.gnv.artifactdatabase.objects.InputParameter;
+
+/**
+ * @author Tim Englich <tim.englich@intevation.de>
+ *
+ */
+public class DoOutputAction extends ArtifactDatabaseActionBase {
+
+    /**
+     * Constructor
+     */
+    public DoOutputAction() {
+        super();
+    }
+
+    
+    /**
+     * @see de.intevation.gnv.action.ArtifactDatabaseActionBase#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
+     */
+    @Override
+    public ActionForward execute(ActionMapping mapping, ActionForm form,
+            HttpServletRequest request, HttpServletResponse response)
+            throws Exception {
+        String target = request.getParameter("target");
+        String mimeType = request.getParameter("mimetype");
+        
+        // TODO Check if ist is propper;
+        
+        SessionModel sm = SessionModelFactory.getInstance().getSessionModel(request);
+        ArtifactDescription ad = (ArtifactDescription)sm.getCurrentArtifact();
+        Collection<String> inputParameter = ad.getInputParameter();
+        Collection<InputParameter> ips = null;
+        if (inputParameter != null){
+            ips = new ArrayList<InputParameter>(inputParameter.size());
+            Iterator<String> it = inputParameter.iterator();
+            while (it.hasNext()){
+                String name = it.next();
+                String[] values = request.getParameterValues(name);
+                InputParameter ip = new DefaultInputParameter(name, values);
+                ips.add(ip);
+            }
+        }
+        
+        ArtifactDatabaseClient adc = ArtifactDatabaseClientFactory.getInstance().getArtifactDatabaseClient();
+        
+        response.setContentType(mimeType);
+        OutputStream outputStream = response.getOutputStream();
+        adc.doOutput(sm.getSelectedArtifactFactory(), sm.getCurrentArtifact(), outputStream, target, mimeType, ips);
+        
+        outputStream.flush();
+        outputStream.close();
+        
+        return super.execute(mapping, form, request, response);
+    }
+    
+
+}
--- a/gnv/src/main/java/de/intevation/gnv/action/NextArtifactStepAction.java	Wed Sep 16 07:44:26 2009 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/action/NextArtifactStepAction.java	Wed Sep 16 12:41:27 2009 +0000
@@ -62,15 +62,28 @@
                 ips.add(ip);
             }
         }
-        // TODO: Woher kommt der zu erreichende Status;
-        String target = ad.getReachableStates().iterator().next();
         ArtifactDatabaseClient adc = ArtifactDatabaseClientFactory.getInstance().getArtifactDatabaseClient();
-        ArtifactDescription artifactDescription = adc.doNextStep(sm.getSelectedArtifactFactory(), sm.getCurrentArtifact(), target, ips);
-        
-        XSLTransformer transformer = new XSLTransformer();
-        String ui = transformer.transform(artifactDescription.getCurrentUI(), "UTF-8", request.getRealPath("WEB-INF/config/templates/describe-ui.xsl"));
-        
-        request.setAttribute("ui", ui);
+        if (ad.getReachableStates() != null && !ad.getReachableStates().isEmpty()){
+            // TODO: Woher kommt der zu erreichende Status;
+            String target = ad.getReachableStates().iterator().next();
+                    
+            
+            ArtifactDescription artifactDescription = adc.doNextStep(sm.getSelectedArtifactFactory(), sm.getCurrentArtifact(), target, ips);
+            
+            XSLTransformer transformer = new XSLTransformer();
+            String ui = transformer.transform(artifactDescription.getCurrentUI(), "UTF-8", request.getRealPath("WEB-INF/config/templates/describe-ui.xsl"));
+            
+            request.setAttribute("ui", ui);
+        }else{
+            
+            adc.doFeed(sm.getSelectedArtifactFactory(), sm.getCurrentArtifact(), ips);
+            String mimeType = "image/png";
+            String target = "chart";
+                
+            String src = "<img src='out.do?mimetype="+mimeType+"&target="+target+"&uid="+System.currentTimeMillis()+"' alt='Chart'>";
+            request.setAttribute("ui", src);
+            
+        }
         
         return super.execute(mapping, form, request, response);
     }
--- a/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClient.java	Wed Sep 16 07:44:26 2009 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/ArtifactDatabaseClient.java	Wed Sep 16 12:41:27 2009 +0000
@@ -3,6 +3,7 @@
  */
 package de.intevation.gnv.artifactdatabase.client;
 
+import java.io.OutputStream;
 import java.util.Collection;
 
 import de.intevation.gnv.artifactdatabase.client.exception.ArtifactDatabaseClientException;
@@ -43,9 +44,34 @@
     
     /**
      * 
+     * @param artifactFactory
      * @param currentArtifact
-     * @return the Current Artifact
+     * @param target
+     * @param inputParameter
+     * @return
      * @throws ArtifactDatabaseClientException
      */
-    public ArtifactDescription doNextStep(ArtifactObject artifactFactory, ArtifactObject currentArtifact, String state2reach, Collection<InputParameter> inputParameter) throws ArtifactDatabaseClientException;
+    public ArtifactDescription doNextStep(ArtifactObject artifactFactory, ArtifactObject currentArtifact, String target, Collection<InputParameter> inputParameter) throws ArtifactDatabaseClientException;
+    
+    
+    /**
+     * @param artifactFactory
+     * @param currentArtifact
+     * @param inputParameter
+     * @return
+     * @throws ArtifactDatabaseClientException
+     */
+    public void doFeed(ArtifactObject artifactFactory, ArtifactObject currentArtifact, Collection<InputParameter> inputParameter) throws ArtifactDatabaseClientException;
+    
+    
+    /**
+     * 
+     * @param artifactFactory
+     * @param currentArtifact
+     * @param stream
+     * @param targetName
+     * @param mimeType
+     * @throws ArtifactDatabaseClientException
+     */
+    public void doOutput(ArtifactObject artifactFactory, ArtifactObject currentArtifact, OutputStream stream, String targetName, String mimeType, Collection<InputParameter> inputParameter) throws ArtifactDatabaseClientException;
 }
--- a/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java	Wed Sep 16 07:44:26 2009 +0000
+++ b/gnv/src/main/java/de/intevation/gnv/artifactdatabase/client/DefaultArtifactDatabaseClient.java	Wed Sep 16 12:41:27 2009 +0000
@@ -5,6 +5,7 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -112,10 +113,14 @@
      * @throws IOException
      */
     private Document doGetRequest(String requestUrl) throws IOException {
+        return this.doGetRequest(requestUrl, null);
+    }
+    /**
+     * @throws IOException
+     */
+    private Document doGetRequest(String requestUrl, Document requestBody) throws IOException {
         XMLUtils xmlUtils = new XMLUtils();
-        Client client = new Client(Protocol.HTTP);
-        Response response = client.get(requestUrl);
-        Representation output = response.getEntity();
+        Representation output = doGetRequestInternal(requestUrl, requestBody);
         Document document = xmlUtils.readDocument(output.getStream());
         log.debug(xmlUtils.writeDocument2String(document));
         return document;
@@ -124,6 +129,39 @@
     /**
      * @throws IOException
      */
+    private void doGetRequest(OutputStream outputStream, String requestUrl, Document requestBody) throws IOException {
+        Representation output = doGetRequestInternal(requestUrl, requestBody);
+        InputStream inputStream = output.getStream();
+        
+        byte[] buffer = new byte[4096];
+        while (inputStream.read(buffer) > 0){
+            outputStream.write(buffer);
+        }
+        
+    }
+
+    /**
+     * @param requestUrl
+     * @param requestBody
+     * @return
+     */
+    private Representation doGetRequestInternal(String requestUrl,
+            Document requestBody) {
+        Client client = new Client(Protocol.HTTP);
+        Request request = new Request(Method.GET, requestUrl);
+        if (requestBody != null){
+            String documentBody = new XMLUtils().writeDocument2String(requestBody);
+            Representation representation = new StringRepresentation(documentBody);
+            request.setEntity(representation);
+        }
+        Response response = client.handle(request);
+        Representation output = response.getEntity();
+        return output;
+    }
+    
+    /**
+     * @throws IOException
+     */
     private InputStream doPostRequest(String requestUrl, Document requestBody) throws IOException {
         Client client = new Client(Protocol.HTTP);
         Request request = new Request(Method.POST, requestUrl);
@@ -314,11 +352,9 @@
         
         try {
             // 1 Feed
-            Document feedDocument = this.createFeedRequestBody(currentArtifact, inputParameter);
+            this.doFeed(artifactFactory, currentArtifact, inputParameter);
+             // 2 Advance
             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);
@@ -348,8 +384,20 @@
         hashNode.setAttribute("value", currentArtifact.getHash());
         rootNode.appendChild(hashNode);
         
-        Element dataNode = this.createArtifactElement(document, "data");
+        Node dataNode = this.createParameterNodes(inputParameter, document, "data");
         rootNode.appendChild(dataNode);
+        
+        return document;
+    }
+
+    /**
+     * @param inputParameter
+     * @param document
+     * @param rootNode
+     */
+    private Node createParameterNodes(Collection<InputParameter> inputParameter, Document document, String nodeName) {
+        Element dataNode = this.createArtifactElement(document, nodeName);
+        
         if (inputParameter != null){
             Iterator<InputParameter> it = inputParameter.iterator();
             while(it.hasNext()){
@@ -367,9 +415,7 @@
                 }
             }
         }
-        
-        
-        return document;
+        return dataNode;
     }
     
     private Document createAdvanceRequestBody(ArtifactObject currentArtifact, String target){
@@ -392,4 +438,80 @@
             rootNode.appendChild(targetNode);
             return document;
     }
+
+    /**
+     * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#doOutput(de.intevation.gnv.artifactdatabase.objects.ArtifactObject, de.intevation.gnv.artifactdatabase.objects.ArtifactObject, java.io.OutputStream, java.lang.String, java.lang.String, java.util.Collection)
+     */
+    public void doOutput(ArtifactObject artifactFactory,
+            ArtifactObject currentArtifact, OutputStream stream,
+            String targetName, String mimeType,
+            Collection<InputParameter> inputParameter)
+            throws ArtifactDatabaseClientException {
+        try {
+            XMLUtils xmlUtils = new XMLUtils();
+            Document requestBody = this.createOutRequestBody(currentArtifact, targetName, mimeType, inputParameter);
+            log.debug(xmlUtils.writeDocument2String(requestBody));
+            
+            String requestUrl = this.getArtifactUrl(artifactFactory, currentArtifact)+"/"+targetName;
+            InputStream is = this.doPostRequest( requestUrl, requestBody);
+            
+            byte[] b = new byte[4096];
+            int i = -1 ;
+            while ((i = is.read(b)) > 0)
+            {
+                stream.write(b, 0, i);
+            }
+            } catch (IOException e) {
+            log.error(e,e);
+            throw new ArtifactDatabaseClientException(e);
+        }
+    }
+    
+    private Document createOutRequestBody(ArtifactObject currentArtifact, String target, String mimeType, Collection<InputParameter> inputParameter){
+        Document document = new XMLUtils().newDocument();
+        Node rootNode  = this.createRootNode(document);
+        
+        Element typeNode = this.createArtifactElement(document, "type");
+        typeNode.setAttribute("name", "out");
+        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 outNode = this.createArtifactElement(document, "out");
+        outNode.setAttribute("name", target);
+        rootNode.appendChild(outNode);
+      
+        Element mimeTypeNode = this.createArtifactElement(document, "out");
+        mimeTypeNode.setAttribute("value", mimeType);
+        outNode.appendChild(mimeTypeNode);
+        
+        Node parameterNode = this.createParameterNodes(inputParameter, document, "params");
+        outNode.appendChild(parameterNode);
+        
+        return document;
 }
+
+    /**
+     * @see de.intevation.gnv.artifactdatabase.client.ArtifactDatabaseClient#doFeed(de.intevation.gnv.artifactdatabase.objects.ArtifactObject, de.intevation.gnv.artifactdatabase.objects.ArtifactObject, java.util.Collection)
+     */
+    public void doFeed(ArtifactObject artifactFactory,
+            ArtifactObject currentArtifact,
+            Collection<InputParameter> inputParameter)
+            throws ArtifactDatabaseClientException {
+        
+        try {
+            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.
+        } catch (IOException e) {
+            log.error(e,e);
+        }
+    }
+}
--- a/gnv/src/main/webapp/WEB-INF/config/struts-config.xml	Wed Sep 16 07:44:26 2009 +0000
+++ b/gnv/src/main/webapp/WEB-INF/config/struts-config.xml	Wed Sep 16 12:41:27 2009 +0000
@@ -29,6 +29,11 @@
                 name="success"
                 path="/WEB-INF/jsp/index.jsp"/>
         </action>
+                <action path="/out" 
+                type="de.intevation.gnv.action.DoOutputAction"
+                scope="request"
+                validate="false">
+        </action>
        </action-mappings>
 </struts-config>
 
--- a/gnv/src/main/webapp/WEB-INF/config/templates/describe-ui.xsl	Wed Sep 16 07:44:26 2009 +0000
+++ b/gnv/src/main/webapp/WEB-INF/config/templates/describe-ui.xsl	Wed Sep 16 12:41:27 2009 +0000
@@ -11,6 +11,10 @@
     </xsl:template>
     <xsl:template match="*[local-name() = 'select']">
         <xsl:variable name="selectName" select="@ref"/>
+        <select name="{$selectName}" multiple="multiple" size="6"> <xsl:apply-templates/></select>
+    </xsl:template>
+    <xsl:template match="*[local-name() = 'select1']">
+        <xsl:variable name="selectName" select="@ref"/>
         <select name="{$selectName}"> <xsl:apply-templates/></select>
     </xsl:template>
     <xsl:template match="*[local-name() = 'item']">
@@ -27,5 +31,4 @@
     
     <xsl:template match="*[local-name() = 'label']">
     </xsl:template>
-    
 </xsl:stylesheet>
\ No newline at end of file

http://dive4elements.wald.intevation.org