changeset 347:a63d8bdb2d79

Improved the out() operation of a Collection - the OutGenerator gets to know about the facets and its theme. flys-artifacts/trunk@1749 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 27 Apr 2011 14:35:44 +0000
parents 16161de47662
children 635af5381a4d
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java
diffstat 2 files changed, 173 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Tue Apr 26 13:29:18 2011 +0000
+++ b/flys-artifacts/ChangeLog	Wed Apr 27 14:35:44 2011 +0000
@@ -1,3 +1,11 @@
+2011-04-27  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java:
+	  Restructured the out() operation of a collection: Now, we collect a list
+	  of artifacts/facets and use this for the OutGenerator. Its doOut()
+	  method gets the attribute of an artifact - the position and the active
+	  state is managed by the Collection itself.
+
 2011-04-26  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/model/ManagedFacet.java: New.
--- a/flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java	Tue Apr 26 13:29:18 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java	Wed Apr 27 14:35:44 2011 +0000
@@ -4,6 +4,7 @@
 import java.io.OutputStream;
 import java.util.ArrayList;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
@@ -30,6 +31,7 @@
 import de.intevation.artifactdatabase.DefaultArtifactCollection;
 
 import de.intevation.flys.artifacts.context.FLYSContext;
+import de.intevation.flys.artifacts.model.ManagedFacet;
 import de.intevation.flys.exports.OutGenerator;
 
 
@@ -140,10 +142,54 @@
         generator.init(format, out, context);
 
         try {
-            Object[][] container = getArtifactsWithAttribute(context);
+            doOut(generator, getAttribute(context, name), context);
+        }
+        catch (ArtifactDatabaseException adbe) {
+            log.error(adbe, adbe);
+        }
+    }
 
-            for (Object[] obj: container) {
-                generator.doOut((Artifact) obj[0], (Document) obj[1]);
+
+    /**
+     * This method creates the concrete output.
+     *
+     * @param generator The OutGenerator that creates the output.
+     * @param attributes The collection's attributes for this concrete output
+     * type.
+     * @param context The context object.
+     */
+    protected void doOut(
+        OutGenerator generator,
+        Document     attributes,
+        CallContext  context)
+    throws IOException
+    {
+        log.debug("FLYSArtifactCollection.doOut");
+
+        ThemeList themeList = new ThemeList(attributes);
+
+        int size = themeList.size();
+        log.debug("Output will contain " + size + " elements.");
+
+        try {
+            for (int i = 0; i < size; i++) {
+                ManagedFacet theme = themeList.get(i);
+
+                if (theme == null) {
+                    continue;
+                }
+
+                String art = theme.getArtifact();
+
+                if (log.isDebugEnabled()) {
+                    log.debug("Do output for...");
+                    log.debug("... artifact: " + art);
+                    log.debug("... facet: " + theme.getName());
+                }
+
+                generator.doOut(
+                    getArtifact(art, context),
+                    getArtifactAttribute(art, context));
             }
         }
         catch (ArtifactDatabaseException ade) {
@@ -186,6 +232,41 @@
 
 
     /**
+     * Returns the attribute for a specific output type.
+     *
+     * @param output The name of the desired output type.
+     *
+     * @return the attribute for the desired output type.
+     */
+    protected Document getAttribute(CallContext context, String output)
+    throws    ArtifactDatabaseException
+    {
+        Document attr = buildAttributes(
+            context.getDatabase(),
+            context,
+            getAttribute(),
+            getArtifactUUIDs(context));
+
+        Node out = (Node) XMLUtils.xpath(
+            attr,
+            "art:output[@name='" + output + "']",
+            XPathConstants.NODE,
+            ArtifactNamespaceContext.INSTANCE);
+
+
+        if (out != null) {
+            Document o = XMLUtils.newDocument();
+
+            o.appendChild(o.importNode(out, true));
+
+            return o;
+        }
+
+        return null;
+    }
+
+
+    /**
      * This method returns the list of artifact UUIDs that this collections
      * contains.
      *
@@ -273,46 +354,6 @@
 
 
     /**
-     * Returns a list of Artifact/Attribute pairs.
-     *
-     * @param context The CallContext.
-     *
-     * @return a list of Artifact/Attribute pairs.
-     */
-    protected Object[][] getArtifactsWithAttribute(CallContext context)
-    throws    ArtifactDatabaseException
-    {
-        log.debug("FLYSArtifactCollection.getArtifactWithAttribute");
-
-        ArtifactDatabase db = context.getDatabase();
-        CallMeta meta       = context.getMeta();
-
-        String[] uuids = getArtifactUUIDs(context);
-        Object[][] awa = new Object[uuids.length][2];
-
-        for (int i = 0; i < uuids.length; i++) {
-            try {
-                Artifact artifact  = getArtifact(uuids[i], context);
-                Document attribute = getArtifactAttribute(uuids[i], context);
-
-                if (artifact == null) {
-                    log.warn("Artifact '" + uuids[i] + "' is not existing.");
-                    continue;
-                }
-
-                awa[i][0] = artifact;
-                awa[i][1] = attribute;
-            }
-            catch (ArtifactDatabaseException ade) {
-                log.warn(ade, ade);
-            }
-        }
-
-        return awa;
-    }
-
-
-    /**
      * Returns the OutGenerator for a specified <i>type</i>.
      *
      * @param name The name of the output type.
@@ -386,5 +427,86 @@
 
         return ci;
     }
+
+
+    /**
+     * Inner class to structure/order the themes of a chart.
+     */
+    private class ThemeList {
+        private Logger logger = Logger.getLogger(ThemeList.class);
+        protected Map<Integer, ManagedFacet> themes;
+
+        public ThemeList(Document output) {
+            themes = new HashMap<Integer, ManagedFacet>();
+            parse(output);
+        }
+
+        protected void parse(Document output) {
+            NodeList themes = (NodeList) XMLUtils.xpath(
+                output,
+                "art:output/art:theme",
+                XPathConstants.NODESET,
+                ArtifactNamespaceContext.INSTANCE);
+
+            int num = themes != null ? themes.getLength() : 0;
+
+            logger.debug("Output has " +  num + " elements.");
+
+            if (num == 0) {
+                return;
+            }
+
+            for (int i = 0; i < num; i++) {
+                Node theme = themes.item(i);
+
+                String name = XMLUtils.xpathString(
+                    theme, "@art:facet", ArtifactNamespaceContext.INSTANCE);
+
+                String uuid = XMLUtils.xpathString(
+                    theme, "@art:artifact", ArtifactNamespaceContext.INSTANCE);
+
+                String pos = XMLUtils.xpathString(
+                    theme, "@art:pos", ArtifactNamespaceContext.INSTANCE);
+
+                String active = XMLUtils.xpathString(
+                    theme, "@art:active", ArtifactNamespaceContext.INSTANCE);
+
+                addTheme(uuid, name, pos, active);
+            }
+        }
+
+        protected void addTheme(
+            String uuid,
+            String name,
+            String position,
+            String active)
+        {
+            if (logger.isDebugEnabled()) {
+                logger.debug("Add theme: ");
+                logger.debug(".. name: " + name);
+                logger.debug(".. uuid: " + uuid);
+                logger.debug(".. position: " + position);
+                logger.debug(".. active: " + active);
+            }
+
+            try {
+                Integer pos = new Integer(position);
+                Integer act = new Integer(active);
+
+                themes.put(pos, new ManagedFacet(name, null, uuid, pos, act));
+            }
+            catch (NumberFormatException nfe) {
+                // do nothing
+            }
+        }
+
+        public ManagedFacet get(int idx) {
+            return themes.get(new Integer(idx));
+        }
+
+        public int size() {
+            return themes.size();
+        }
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org