changeset 300:9a0e1289bab6

The FLYSArtifactCollection overrides out() and uses OutGenerators to create output for this operation. flys-artifacts/trunk@1649 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 06 Apr 2011 10:01:49 +0000 (2011-04-06)
parents 8940b0885865
children 1a00cb4998a8
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java
diffstat 3 files changed, 236 insertions(+), 111 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Wed Apr 06 09:56:38 2011 +0000
+++ b/flys-artifacts/ChangeLog	Wed Apr 06 10:01:49 2011 +0000
@@ -1,3 +1,14 @@
+2011-04-06  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java:
+	  This collection overrides the out() operation now. The incoming request
+	  document is read and the related OutGenerator is used to create the
+	  output.
+
+	* src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java: Removed
+	  the code to create discharge curves. It has moved to the
+	  DischargeCurveGenerator which now does this work.
+
 2011-04-06  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/exports/OutGenerator.java: The
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Wed Apr 06 09:56:38 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/WINFOArtifact.java	Wed Apr 06 10:01:49 2011 +0000
@@ -1,11 +1,6 @@
 package de.intevation.flys.artifacts;
 
-import java.awt.Color;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.util.ArrayList;
 import java.util.List;
-import java.util.Map;
 import java.util.Vector;
 
 import org.w3c.dom.Document;
@@ -14,12 +9,6 @@
 
 import org.apache.log4j.Logger;
 
-import org.jfree.data.xy.DefaultXYDataset;
-
-import org.jfree.chart.ChartFactory;
-import org.jfree.chart.JFreeChart;
-import org.jfree.chart.plot.PlotOrientation;
-
 import de.intevation.artifacts.ArtifactNamespaceContext;
 import de.intevation.artifacts.CallContext;
 
@@ -31,13 +20,8 @@
 
 import de.intevation.artifacts.common.utils.XMLUtils;
 
-import de.intevation.flys.model.Gauge;
-
-import de.intevation.flys.artifacts.model.DischargeTables;
-import de.intevation.flys.artifacts.model.GaugesFactory;
 import de.intevation.flys.artifacts.states.DefaultState;
 import de.intevation.flys.artifacts.context.FLYSContext;
-import de.intevation.flys.exports.ChartExportHelper;
 
 
 /**
@@ -139,76 +123,6 @@
     }
 
 
-    @Override
-    public void out(
-        Document     format,
-        OutputStream out,
-        CallContext  context)
-    throws IOException
-    {
-        logger.info("WINFOArtifact.out");
-        String river = (String) getData("river").getValue();
-        String from  = (String) getData("ld_from").getValue();
-        String to    = (String) getData("ld_to").getValue();
-
-        double f = Double.parseDouble(from);
-        double t = Double.parseDouble(to);
-
-        List<double[]> ranges = new ArrayList<double[]>();
-        ranges.add(new double [] { f, t });
-
-        List<Gauge> gauges   = GaugesFactory.getGauges(river);
-        List<Gauge> filtered = GaugesFactory.filterRanges(gauges, ranges);
-
-        if (logger.isDebugEnabled()) {
-            int numGauges   = gauges != null ? gauges.size() : 0;
-            int numFiltered = filtered != null ? filtered.size() : 0;
-
-            logger.debug("++++++++++++++++++++");
-            logger.debug("Search gauges for river: " + river);
-            logger.debug("-> ... from range: " + from);
-            logger.debug("-> ... to range: " + to);
-            logger.debug("-> Found " + numGauges + " gauges in total.");
-            logger.debug("-> Found " + numFiltered + " gauges after filtering.");
-            logger.debug("++++++++++++++++++++");
-        }
-
-        String[] gaugeNames = new String[filtered.size()];
-        int idx = 0;
-        for (Gauge gauge: filtered) {
-            gaugeNames[idx++] = gauge.getName();
-        }
-
-        DischargeTables dt = new DischargeTables(river, gaugeNames);
-        Map<String, double[][]> gaugeValues = dt.getValues(100d);
-
-        DefaultXYDataset dataset = new DefaultXYDataset();
-
-        for (String gauge: gaugeNames) {
-            double[][] values = gaugeValues.get(gauge);
-            dataset.addSeries(gauge, values);
-        }
-
-        JFreeChart chart = ChartFactory.createXYLineChart(
-            "Abflusskurven (" + river + ")",
-            "Q", "W",
-            dataset,
-            PlotOrientation.VERTICAL,
-            true,
-            false,
-            false);
-
-        chart.setBackgroundPaint(Color.WHITE);
-        chart.getPlot().setBackgroundPaint(Color.WHITE);
-
-        ChartExportHelper.exportImage(
-            out,
-            chart,
-            "png",
-            600, 400);
-    }
-
-
     /**
      * Returns the name of the concrete artifact.
      *
--- a/flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java	Wed Apr 06 09:56:38 2011 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java	Wed Apr 06 10:01:49 2011 +0000
@@ -1,6 +1,11 @@
 package de.intevation.flys.collections;
 
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.ArrayList;
 import java.util.Date;
+import java.util.List;
+import java.util.Map;
 
 import javax.xml.xpath.XPathConstants;
 
@@ -11,6 +16,7 @@
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
+import de.intevation.artifacts.Artifact;
 import de.intevation.artifacts.ArtifactDatabase;
 import de.intevation.artifacts.ArtifactDatabaseException;
 import de.intevation.artifacts.ArtifactNamespaceContext;
@@ -19,8 +25,13 @@
 
 import de.intevation.artifacts.common.utils.XMLUtils;
 
+import de.intevation.artifactdatabase.Backend;
+import de.intevation.artifactdatabase.Backend.PersistentArtifact;
 import de.intevation.artifactdatabase.DefaultArtifactCollection;
 
+import de.intevation.flys.artifacts.context.FLYSContext;
+import de.intevation.flys.exports.OutGenerator;
+
 
 /**
  * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
@@ -38,6 +49,11 @@
     public static final String XPATH_COLLECTION_ITEMS =
         "/art:result/art:artifact-collection/art:collection-item";
 
+    public static final String XPATH_OUT_NAME = "/art:action/@art:name";
+
+    public static final String XPATH_OUT_TYPE = "/art:action/@art:type";
+
+
 
     @Override
     public Document describe(CallContext context) {
@@ -71,30 +87,12 @@
             collection.appendChild(doc.importNode(child, true));
         }
 
-        try {
-            ArtifactDatabase db = context.getDatabase();
-            CallMeta meta       = context.getMeta();
-
-            Document itemList = db.listCollectionArtifacts(identifier(), meta);
-            NodeList items    = (NodeList) XMLUtils.xpath(
-                itemList,
-                XPATH_COLLECTION_ITEMS,
-                XPathConstants.NODESET,
-                ArtifactNamespaceContext.INSTANCE);
+        ArtifactDatabase db    = context.getDatabase();
 
-            if (items == null || items.getLength() == 0) {
-                log.debug("No collection items found.");
-                return doc;
-            }
+        try {
+            String[] artifactUUIDs = getArtifactUUIDs(context);
 
-            int num = items.getLength();
-
-            for (int i = 0; i < num; i++) {
-                String uuid = XMLUtils.xpathString(
-                    items.item(i),
-                    "@art:uuid",
-                    ArtifactNamespaceContext.INSTANCE);
-
+            for (String uuid: artifactUUIDs) {
                 try {
                     artifacts.appendChild(
                         buildArtifactNode(db, uuid, context, ec));
@@ -112,6 +110,211 @@
     }
 
 
+    @Override
+    public void out(Document format, OutputStream out, CallContext context)
+    throws IOException
+    {
+        log.info("FLYSArtifactCollection.out");
+
+        String name = XMLUtils.xpathString(
+            format, XPATH_OUT_NAME, ArtifactNamespaceContext.INSTANCE);
+
+        String type = XMLUtils.xpathString(
+            format, XPATH_OUT_TYPE, ArtifactNamespaceContext.INSTANCE);
+
+        OutGenerator generator = getOutGenerator(context, name, type);
+        if (generator == null) {
+            log.error("There is no generator specified for output: " + type);
+            // TODO throw an exception.
+
+            return;
+        }
+
+        generator.init(format, out, context);
+
+        try {
+            Object[][] container = getArtifactsWithAttribute(context);
+
+            for (Object[] obj: container) {
+                generator.doOut((Artifact) obj[0], (Document) obj[1]);
+            }
+        }
+        catch (ArtifactDatabaseException ade) {
+            log.error(ade, ade);
+        }
+
+        generator.generate();
+    }
+
+
+    /**
+     * This method returns the list of artifact UUIDs that this collections
+     * contains.
+     *
+     * @param context The CallContext that is necessary to get information about
+     * the ArtifactDatabase.
+     *
+     * @return a list of uuids.
+     */
+    protected String[] getArtifactUUIDs(CallContext context)
+    throws    ArtifactDatabaseException
+    {
+        log.debug("FLYSArtifactCollection.getArtifactUUIDs");
+
+        ArtifactDatabase db = context.getDatabase();
+        CallMeta meta       = context.getMeta();
+
+        Document itemList = db.listCollectionArtifacts(identifier(), meta);
+        NodeList items    = (NodeList) XMLUtils.xpath(
+            itemList,
+            XPATH_COLLECTION_ITEMS,
+            XPathConstants.NODESET,
+            ArtifactNamespaceContext.INSTANCE);
+
+        if (items == null || items.getLength() == 0) {
+            log.debug("No artifacts found in this collection.");
+            return null;
+        }
+
+        int num = items.getLength();
+
+        List<String> uuids = new ArrayList<String>(num);
+
+        for (int i = 0; i < num; i++) {
+            String uuid = XMLUtils.xpathString(
+                items.item(i),
+                "@art:uuid",
+                ArtifactNamespaceContext.INSTANCE);
+
+            if (uuid != null && uuid.trim().length() != 0) {
+                uuids.add(uuid);
+            }
+        }
+
+        return (String[]) uuids.toArray(new String[uuids.size()]);
+    }
+
+
+    /**
+     * Returns a concrete Artifact of this collection specified by its uuid.
+     *
+     * @param uuid The Artifact's uuid.
+     * @param context The CallContext.
+     *
+     * @return an Artifact.
+     */
+    protected Artifact getArtifact(String uuid, CallContext context)
+    throws    ArtifactDatabaseException
+    {
+        log.debug("FLYSArtifactCollection.getArtifact");
+
+        Backend backend               = Backend.getInstance();
+        PersistentArtifact persistent = backend.getArtifact(uuid);
+
+        return persistent != null ? persistent.getArtifact() : null;
+    }
+
+
+    /**
+     * Returns the attribute that belongs to an artifact stored in this
+     * collection.
+     *
+     * @param uuid The Artifact's uuid.
+     * @param context The CallContext.
+     *
+     * @return an attribute in form of a document.
+     */
+    protected Document getArtifactAttribute(String uuid, CallContext context)
+    throws    ArtifactDatabaseException
+    {
+        log.debug("FLYSArtifactCollection.getArtifactAttribute");
+
+        // TODO FILL ME
+        return null;
+    }
+
+
+    /**
+     * 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.
+     * @param type Defines the type of the desired OutGenerator.
+     *
+     * @return The OutGenerator specified by <i>type</i>.
+     */
+    protected OutGenerator getOutGenerator(
+        CallContext context,
+        String      name,
+        String      type)
+    {
+        FLYSContext flysContext = context instanceof FLYSContext
+            ? (FLYSContext) context
+            : (FLYSContext) context.globalContext();
+
+        Map<String, Class> generators = (Map<String, Class>)
+            flysContext.get(FLYSContext.OUTGENERATORS_KEY);
+
+        if (generators == null) {
+            log.error("No output generators found in the running application!");
+            return null;
+        }
+
+        Class clazz = generators.get(type);
+
+        try {
+            return clazz != null ? (OutGenerator) clazz.newInstance() : null;
+        }
+        catch (InstantiationException ie) {
+            log.error(ie, ie);
+        }
+        catch (IllegalAccessException iae) {
+            log.error(iae, iae);
+        }
+
+        return null;
+    }
+
+
     protected Element buildArtifactNode(
         ArtifactDatabase        database,
         String                  uuid,
@@ -125,9 +328,6 @@
         // in the describe operation of an artifact.
         Document description = database.describe(uuid, null, context.getMeta());
 
-        // FIXME: XMLDebug does not exists in version control.
-        // de.intevation.flys.artifacts.XMLDebug.out(description);
-
         String hash = "MYHASH";
         // TODO
 

http://dive4elements.wald.intevation.org