changeset 3296:45af081061e7

Added overview output for sq relation. Currently the overview generator is unused and the overview is build by the client. flys-artifacts/trunk@4979 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Fri, 13 Jul 2012 09:45:11 +0000
parents 4fc442f1b4f6
children 79c8e0d9fdea
files flys-artifacts/ChangeLog flys-artifacts/doc/conf/artifacts/minfo.xml flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FacetTypes.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQOverviewFacet.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/SQRelation.java flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java flys-artifacts/src/main/java/de/intevation/flys/exports/sq/SQOverviewGenerator.java
diffstat 8 files changed, 339 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Fri Jul 13 09:33:16 2012 +0000
+++ b/flys-artifacts/ChangeLog	Fri Jul 13 09:45:11 2012 +0000
@@ -1,3 +1,28 @@
+2012-07-13  Raimund Renkert <raimund.renkert@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/sq/SQOverviewFacet.java:
+	  New. Facet for chart overview in sq relation.
+
+	* src/main/java/de/intevation/flys/exports/sq/SQOverviewGenerator.java:
+	  New. Generator for chart overview. This generator is not used jet.
+	  Currently we generate the overview on client side by putting all charts in a
+	  simple grid, cause the charts rendered by this generator are empty.
+
+	* src/main/java/de/intevation/flys/artifacts/model/FacetTypes.java:
+	  Added new Facet type.
+
+	* src/main/java/de/intevation/flys/artifacts/states/SQRelation.java:
+	  Add facets for overview.
+
+	* src/main/java/de/intevation/flys/exports/ChartGenerator.java:
+	  Avoid a NPE in legend entry aggregation.
+
+	* src/main/java/de/intevation/flys/exports/XYChartGenerator.java:
+	  Moved log output to avoid NPE.
+
+	* doc/conf/artifacts/minfo.xml:
+	  Added new output for chart overview.
+
 2012-07-13  Raimund Renkert <raimund.renkert@intevation.de>
 
 	* src/main/java/de/intevation/flys/collections/FLYSArtifactCollection.java:
--- a/flys-artifacts/doc/conf/artifacts/minfo.xml	Fri Jul 13 09:33:16 2012 +0000
+++ b/flys-artifacts/doc/conf/artifacts/minfo.xml	Fri Jul 13 09:45:11 2012 +0000
@@ -179,6 +179,11 @@
                         <facet name="sq_f_curve" description="A facet for sq curve"/>
                     </facets>
                 </outputmode>
+                <outputmode name="sq_overview" description="output.sq_overview" type="overview">
+                    <facets>
+                        <facet name="sq_chart_overview" description="A facet for sq chart overview"/>
+                    </facets>
+                </outputmode>
                 <outputmode name="sq_relation_export" description="output.sq_relation_export" type="export">
                     <facets>
                         <facet name="csv" description="facet.sq_export.csv" />
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FacetTypes.java	Fri Jul 13 09:33:16 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FacetTypes.java	Fri Jul 13 09:45:11 2012 +0000
@@ -199,6 +199,8 @@
     String MIDDLE_BED_HEIGHT_EPOCH      = "bedheight_middle.epoch";
     String MIDDLE_BED_HEIGHT_ANNOTATION = "bedheight_middle.annotation";
 
+    String SQ_OVERVIEW       = "sq_overview";
+
     String SQ_A_CURVE       = "sq_a_curve";
     String SQ_A_MEASUREMENT = "sq_a_measurement";
     String SQ_A_OUTLIER     = "sq_a_outlier";
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/SQOverviewFacet.java	Fri Jul 13 09:45:11 2012 +0000
@@ -0,0 +1,62 @@
+package de.intevation.flys.artifacts.model.sq;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.CallContext;
+
+import de.intevation.flys.artifacts.FLYSArtifact;
+
+import de.intevation.flys.artifacts.model.DataFacet;
+import de.intevation.flys.artifacts.model.FacetTypes;
+
+import de.intevation.flys.artifacts.states.DefaultState.ComputeType;
+
+public class SQOverviewFacet
+extends DataFacet
+implements FacetTypes 
+{
+    private static final Logger logger = Logger.getLogger(SQOverviewFacet.class);
+
+    protected int fractionIdx;
+
+    public SQOverviewFacet() {
+    }
+
+    public SQOverviewFacet(
+        int    idx,
+        int    fractionIdx,
+        String name,
+        String description,
+        String hash,
+        String stateId
+    ) {
+        super(idx, name, description, ComputeType.ADVANCE, hash, stateId);
+        this.fractionIdx = fractionIdx;
+    }
+
+
+    @Override
+    public Object getData(Artifact artifact, CallContext context) {
+        logger.debug("SQOverviewFacet.getData");
+
+        switch(fractionIdx) {
+            case 0: return "sq_relation_a";
+            case 1: return "sq_relation_b";
+            case 2: return "sq_relation_c";
+            case 3: return "sq_relation_d";
+            case 4: return "sq_relation_e";
+            case 5: return "sq_relation_f";
+            default: return null;
+        }
+    }
+
+
+    @Override
+    public SQOverviewFacet deepCopy() {
+        SQOverviewFacet copy = new SQOverviewFacet();
+        copy.set(this);
+
+        return copy;
+    }
+}
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/SQRelation.java	Fri Jul 13 09:33:16 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/SQRelation.java	Fri Jul 13 09:45:11 2012 +0000
@@ -19,6 +19,7 @@
 import de.intevation.flys.artifacts.model.sq.SQOutlierFacet;
 import de.intevation.flys.artifacts.model.sq.SQRelationCalculation;
 import de.intevation.flys.artifacts.model.sq.SQResult;
+import de.intevation.flys.artifacts.model.sq.SQOverviewFacet;
 
 import de.intevation.flys.artifacts.resources.Resources;
 
@@ -97,7 +98,20 @@
     ) {
         CallMeta meta    = context.getMeta();
         String   stateId = getID();
-
+        for (int i = 0; i < 6; i++) {
+            container.add(new SQOverviewFacet(
+                    i,
+                    i,
+                    "sq_chart_overview",
+                    Resources.getMsg(
+                        context.getMeta(),
+                        I18N_FACET_CURVE,
+                        I18N_FACET_CURVE
+                    ),
+                    hash,
+                    getID()
+                ));
+        }
         for (int res = 0, n = sqr.length; res < n; res++) {
 
             for (int i = 0; i < 6; i++) {
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java	Fri Jul 13 09:33:16 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/ChartGenerator.java	Fri Jul 13 09:45:11 2012 +0000
@@ -1737,6 +1737,9 @@
     public void aggregateLegendEntries(XYPlot plot) {
         int AGGR_THRESHOLD = 0;
 
+        if (getChartSettings() == null) {
+            return;
+        }
         Integer threshold = getChartSettings().getLegendSection()
             .getAggregationThreshold();
 
--- a/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Fri Jul 13 09:33:16 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/XYChartGenerator.java	Fri Jul 13 09:45:11 2012 +0000
@@ -473,9 +473,10 @@
 
             Integer key = Integer.valueOf(i);
             Bounds  b   = getXBounds(key);
-            logger.debug("Check whether to expand a x axis.i ("+b.getLower() + "-" + b.getUpper()+")");
+
 
             if (b != null && b.getLower().equals(b.getUpper())) {
+                logger.debug("Check whether to expand a x axis.i ("+b.getLower() + "-" + b.getUpper()+")");
                 setXBounds(key, ChartHelper.expandBounds(b, 5));
             }
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/exports/sq/SQOverviewGenerator.java	Fri Jul 13 09:45:11 2012 +0000
@@ -0,0 +1,225 @@
+package de.intevation.flys.exports.sq;
+
+import java.awt.image.BufferedImage;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.imageio.ImageIO;
+import javax.xml.xpath.XPathConstants;
+
+import org.apache.log4j.Logger;
+import org.jfree.chart.ChartRenderingInfo;
+import org.jfree.chart.JFreeChart;
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.Node;
+
+import de.intevation.artifactdatabase.state.ArtifactAndFacet;
+import de.intevation.artifactdatabase.state.Settings;
+import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.ArtifactDatabaseException;
+import de.intevation.artifacts.CallContext;
+import de.intevation.artifacts.common.ArtifactNamespaceContext;
+import de.intevation.artifacts.common.utils.XMLUtils;
+import de.intevation.flys.artifacts.context.FLYSContext;
+import de.intevation.flys.collections.AttributeParser;
+import de.intevation.flys.collections.CollectionAttribute;
+import de.intevation.flys.exports.ChartGenerator;
+import de.intevation.flys.exports.OutGenerator;
+import de.intevation.flys.exports.OutputHelper;
+
+public class SQOverviewGenerator
+implements OutGenerator
+{
+    private static Logger logger = Logger.getLogger(SQOverviewGenerator.class);
+
+    public static final String XPATH_CHART_SIZE =
+        "/art:action/art:attributes/art:size";
+
+    protected Artifact master;
+
+    protected Settings settings;
+
+    protected Document request;
+
+    protected OutputStream out;
+
+    protected CallContext context;
+
+    protected List<JFreeChart> charts;
+
+    /**
+     * Produce output.
+     * @param artifactAndFacet current facet and artifact.
+     * @param attr  theme for facet
+     */
+    public void doOut(
+        ArtifactAndFacet artifactAndFacet,
+        Document         attr,
+        boolean          visible
+    ) {
+        logger.debug("doOut()");
+
+        logger.debug(XMLUtils.toString(attr));
+        String name = artifactAndFacet.getData(context).toString();
+        if(name != null) {
+            logger.debug("name: " + name);
+            ChartGenerator g =
+                (ChartGenerator)FLYSContext.getOutGenerator(
+                    context,
+                    name,
+                    null);
+            if (g == null) {
+                logger.debug("generator is null.");
+                return;
+            }
+            logger.debug(XMLUtils.toString(attr));
+            OutputHelper helper = new OutputHelper(master.identifier());
+            try {
+                AttributeParser parser    = new AttributeParser(request);
+                CollectionAttribute cAttr = parser.getCollectionAttribute();
+
+                g.init(request, out, context);
+                Document chartAttr = getAttribute(context, cAttr, name);
+                logger.debug(XMLUtils.toString(chartAttr));
+                helper.doOut(g, name, name, request, context);
+                charts.add(g.generateChart());
+            } catch (IOException e) {
+                logger.warn(e);
+            } catch (ArtifactDatabaseException e) {
+                // TODO Auto-generated catch block
+                logger.warn(e);
+            }
+        }
+    }
+
+    public void init(Document request, OutputStream out, CallContext context) {
+        this.request = request;
+        this.out = out;
+        this.context = context;
+        charts = new ArrayList<JFreeChart>();
+    }
+
+    public void setMasterArtifact(Artifact master) {
+        this.master = master;
+    }
+
+    public void generate() throws IOException {
+        logger.debug("SQOverviewGenerator.generate");
+
+        int[] size = getSize();
+
+        if (size == null) {
+            size = new int[] {400, 600};
+        }
+        BufferedImage result =
+            new BufferedImage(size[0], size[1], BufferedImage.TYPE_INT_RGB);
+        for (int i = 0; i < charts.size(); i++) {
+            logger.debug("index: " + i);
+            JFreeChart chart = charts.get(i);
+            ChartRenderingInfo info = new ChartRenderingInfo();
+            BufferedImage img =
+                chart.createBufferedImage(size[0]/2, size[1]/3, info);
+            int horPos = 0;
+            int vertPos = 0;
+            if (i % 2 == 1) {
+                horPos = size[0]/2;
+            }
+            if (i > 1) {
+                vertPos = (size[1] / 3) * (i / 2);
+            }
+            result.createGraphics().drawImage(img, horPos, vertPos, null);
+        }
+        ImageIO.write(result, "png", out);
+    }
+
+    public void setSettings(Settings settings) {
+        this.settings = settings;
+    }
+
+    public Settings getSettings() {
+        return this.settings;
+    }
+
+
+    /**
+     * Returns the "attribute" (part of description document) for a specific
+     * output type.
+     *
+     * @param context The CallContext object.
+     * @param cAttr The CollectionAttribute.
+     * @param output The name of the desired output type.
+     *
+     * @return the attribute for the desired output type.
+     */
+    protected Document getAttribute(
+        CallContext         context,
+        CollectionAttribute cAttr,
+        String              output)
+    throws    ArtifactDatabaseException
+    {
+        Document attr = cAttr.toXML();
+
+        Map<String, String> vars = new HashMap<String, String>();
+        vars.put("output", output);
+
+        Node out = (Node) XMLUtils.xpath(
+            attr,
+            "art:attribute/art:outputs/art:output[@name=$output]",
+            XPathConstants.NODE,
+            ArtifactNamespaceContext.INSTANCE,
+            vars);
+
+
+        if (out != null) {
+            Document o = XMLUtils.newDocument();
+
+            o.appendChild(o.importNode(out, true));
+
+            return o;
+        }
+
+        return null;
+    }
+
+
+    /**
+     * Returns the size of a chart export as array which has been specified by
+     * the incoming request document.
+     *
+     * @return the size of a chart as [width, height] or null if no width or
+     * height are given in the request document.
+     */
+    protected int[] getSize() {
+        int[] size = new int[2];
+
+        Element sizeEl = (Element)XMLUtils.xpath(
+            request,
+            XPATH_CHART_SIZE,
+            XPathConstants.NODE,
+            ArtifactNamespaceContext.INSTANCE);
+
+        if (sizeEl != null) {
+            String uri = ArtifactNamespaceContext.NAMESPACE_URI;
+
+            String w = sizeEl.getAttributeNS(uri, "width");
+            String h = sizeEl.getAttributeNS(uri, "height");
+
+            if (w.length() > 0 && h.length() > 0) {
+                try {
+                    size[0] = Integer.parseInt(w);
+                    size[1] = Integer.parseInt(h);
+                }
+                catch (NumberFormatException nfe) {
+                    logger.warn("Wrong values for chart width/height.");
+                }
+            }
+        }
+
+        return size[0] > 0 && size[1] > 0 ? size : null;
+    }
+}

http://dive4elements.wald.intevation.org