changeset 944:c256061287d7

Simplified the code to read all provided Outputs of an Artifact. flys-artifacts/trunk@2357 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Mon, 18 Jul 2011 17:09:00 +0000
parents 5de90b0cff8e
children 59ae2a823e73
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java flys-artifacts/src/main/java/de/intevation/flys/collections/OutputParser.java
diffstat 4 files changed, 102 insertions(+), 91 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Mon Jul 18 15:07:47 2011 +0000
+++ b/flys-artifacts/ChangeLog	Mon Jul 18 17:09:00 2011 +0000
@@ -1,3 +1,16 @@
+2011-07-18  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java: Added a
+	  method that returns the Outputs for the Artifact.
+
+	* src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java:
+	  Adapeted the call of OutputParser.
+
+	* src/main/java/de/intevation/flys/collections/OutputParser.java:
+	  Simplified the code to read the Outputs of Artifacts. This parser will
+	  now longer parse the DESCRIBE documents of the Artifacts, but query the
+	  Outputs via FLYSArtifact.getOutputs() directly.
+
 2011-07-18  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java,
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Mon Jul 18 15:07:47 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/FLYSArtifact.java	Mon Jul 18 17:09:00 2011 +0000
@@ -1111,6 +1111,48 @@
     }
 
 
+    public List<Output> getOutputs(CallContext context) {
+        List<String> stateIds = getPreviousStateIds();
+
+        DefaultState cur = (DefaultState) getCurrentState(context);
+        try {
+            if (cur.validate(this, context)) {
+                stateIds.add(cur.getID());
+            }
+        }
+        catch (IllegalArgumentException iae) { }
+
+        List<Output> generated = new ArrayList<Output>();
+
+        FLYSContext flysContext = getFlysContext(context);
+        StateEngine engine      = (StateEngine) flysContext.get(
+            FLYSContext.STATE_ENGINE_KEY);
+
+        for (String stateId: stateIds) {
+            DefaultState state = (DefaultState) engine.getState(stateId);
+
+            List<Output> list = state.getOutputs();
+            if (list == null || list.size() == 0) {
+                logger.debug("-> No output modes for this state.");
+                continue;
+            }
+
+            List<Facet>  fs = facets.get(stateId);
+            if (fs == null || fs.size() == 0) {
+                logger.debug("No facets found.");
+                continue;
+            }
+
+            List<Output> o = generateOutputs(list, fs);
+
+            generated.addAll(o);
+        }
+
+
+        return generated;
+    }
+
+
     protected List<Output> generateOutputs(List<Output> list, List<Facet> fs) {
         List<Output> generated = new ArrayList<Output>();
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java	Mon Jul 18 15:07:47 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java	Mon Jul 18 17:09:00 2011 +0000
@@ -265,7 +265,7 @@
             ArtifactNamespaceContext.NAMESPACE_PREFIX);
 
         AttributeParser aParser = new AttributeParser();
-        OutputParser    oParser = new OutputParser(db, context.getMeta());
+        OutputParser    oParser = new OutputParser(db, context);
 
         for (String uuid: items) {
             try {
--- a/flys-artifacts/src/main/java/de/intevation/flys/collections/OutputParser.java	Mon Jul 18 15:07:47 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/collections/OutputParser.java	Mon Jul 18 17:09:00 2011 +0000
@@ -1,27 +1,22 @@
 package de.intevation.flys.collections;
 
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
-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.artifacts.ArtifactDatabase;
 import de.intevation.artifacts.ArtifactDatabaseException;
-import de.intevation.artifacts.ArtifactNamespaceContext;
+import de.intevation.artifacts.CallContext;
 import de.intevation.artifacts.CallMeta;
 
 import de.intevation.artifactdatabase.state.DefaultOutput;
+import de.intevation.artifactdatabase.state.Facet;
 import de.intevation.artifactdatabase.state.Output;
 
-import de.intevation.artifacts.common.utils.XMLUtils;
-
+import de.intevation.flys.artifacts.FLYSArtifact;
 import de.intevation.flys.artifacts.model.ManagedFacet;
 
 
@@ -36,14 +31,16 @@
 
     protected ArtifactDatabase db;
     protected CallMeta         meta;
+    protected CallContext      context;
 
     protected Map<String, Output> outs;
 
 
-    public OutputParser(ArtifactDatabase db, CallMeta meta) {
-        this.db   = db;
-        this.meta = meta;
-        this.outs = new HashMap<String, Output>();
+    public OutputParser(ArtifactDatabase db, CallContext context) {
+        this.db      = db;
+        this.meta    = context.getMeta();
+        this.context = context;
+        this.outs    = new HashMap<String, Output>();
     }
 
 
@@ -52,24 +49,32 @@
     {
         logger.debug("OutputParser.parse: " + uuid);
 
-        // XXX I am not sure if it works well every time with an empty
-        // document in the describe operation of an artifact.
-        Document description = db.describe(uuid, null, meta);
+        FLYSArtifact flys = (FLYSArtifact) db.getRawArtifact(uuid);
 
-        NodeList outs = (NodeList) XMLUtils.xpath(
-            description,
-            XPATH_ARTIFACT_OUTPUTMODES,
-            XPathConstants.NODESET,
-            ArtifactNamespaceContext.INSTANCE);
+        List<Output> outList = flys.getOutputs(context);
 
-        int num = outs != null ? outs.getLength() : 0;
-
-        logger.debug("Artifact has " + num + " outputs.");
+        for (Output out: outList) {
+            String name = out.getName();
 
-        for (int i = 0; i < num; i++) {
-            Element out = (Element)outs.item(i);
+            Output o = outs.get(name);
+            int  pos = 1;
 
-            parseOutput(uuid, out);
+            if (o == null) {
+                o = new DefaultOutput(
+                    out.getName(),
+                    out.getDescription(),
+                    out.getMimeType(),
+                    new ArrayList<Facet>(),
+                    out.getType());
+
+                outs.put(name, o);
+            }
+            else {
+                pos = o.getFacets().size() + 1;
+            }
+
+            List<Facet> facets = facet2ManagedFacet(uuid, out.getFacets(), pos);
+            o.addFacets(facets);
         }
     }
 
@@ -79,71 +84,22 @@
     }
 
 
-    protected void addItem(String out, ManagedFacet item) {
-        Output o = outs.get(out);
-
-        if (o != null) {
-            int num = o.getFacets().size();
-            item.setPosition(num+1);
+    protected List<Facet> facet2ManagedFacet(
+        String      uuid,
+        List<Facet> old,
+        int         pos)
+    {
+        List<Facet> newFacets = new ArrayList<Facet>(old.size());
 
-            o.addFacet(item);
-        }
-    }
-
-
-    protected void parseOutput(String uuid, Element out) {
-
-        String uri = ArtifactNamespaceContext.NAMESPACE_URI;
-
-        String name = out.getAttributeNS(uri, "name");
-
-        if (outs.get(name) == null) {
-            logger.debug("Create new output: " + name);
-            newOutput(out, name);
+        for (Facet f: old) {
+            newFacets.add(new ManagedFacet(
+                f.getName(),
+                f.getIndex(),
+                f.getDescription(),
+                uuid, pos++, 1));
         }
 
-        parseItems(uuid, out, name);
-    }
-
-
-    protected void newOutput(Element out, String name) {
-
-        String uri = ArtifactNamespaceContext.NAMESPACE_URI;
-
-        String desc     = out.getAttributeNS(uri, "description");
-        String mimetype = out.getAttributeNS(uri, "mime-type");
-        String type     = out.getAttributeNS(uri, "type");
-
-        Output o = new DefaultOutput(name, desc, mimetype, type);
-
-        outs.put(name, o);
-    }
-
-
-    protected void parseItems(String uuid, Node out, String outname) {
-        NodeList facets = (NodeList) XMLUtils.xpath(
-            out, "art:facets/art:facet",
-            XPathConstants.NODESET,
-            ArtifactNamespaceContext.INSTANCE);
-
-        int num = facets != null ? facets.getLength() : 0;
-
-        String uri = ArtifactNamespaceContext.NAMESPACE_URI;
-
-        logger.debug("Output has " + num + " facets.");
-
-        for (int i = 0; i < num; i++) {
-            Element facet = (Element) facets.item(i);
-
-            String name  = facet.getAttributeNS(uri, "name");
-            String desc  = facet.getAttributeNS(uri, "description");
-            String index = facet.getAttributeNS(uri, "index");
-
-            ManagedFacet item = new ManagedFacet(
-                name, Integer.parseInt(index), desc, uuid, 1, 1);
-
-            addItem(outname, item);
-        }
+        return newFacets;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org