[PATCH] Only the master-artifact should decide about injected CURRENT_KM et al. (and removed dependend obsolete class)

Wald Commits scm-commit at wald.intevation.org
Wed Oct 8 11:49:22 CEST 2014


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1412761747 -7200
# Node ID 994995baa32b7bde88834ea64b0b1d40f4699b01
# Parent  6b12045ac372cf6b7489cca2e740ab0d16fb9b78
Only the master-artifact should decide about injected CURRENT_KM et al. (and removed dependend obsolete class).

diff -r 6b12045ac372 -r 994995baa32b artifacts/doc/conf/generators/generators.xml
--- a/artifacts/doc/conf/generators/generators.xml	Wed Oct 08 11:02:16 2014 +0200
+++ b/artifacts/doc/conf/generators/generators.xml	Wed Oct 08 11:49:07 2014 +0200
@@ -35,7 +35,6 @@
     <output-generator names="bed_quality_export" class="org.dive4elements.river.exports.minfo.BedQualityExporter"/>
     <output-generator names="bedheight_difference_export" class="org.dive4elements.river.exports.minfo.BedDifferenceExporter"/>
     <output-generator names="sq_relation_export" class="org.dive4elements.river.exports.sq.SQRelationExporter"/>
-    <!--output-generator names="sq_overview" class="org.dive4elements.river.exports.sq.SQOverviewGenerator"/-->
     <output-generator names="fix_parameters_export" class="org.dive4elements.river.exports.fixings.ParametersExporter"/>
     <output-generator names="fix_deltawt_export" class="org.dive4elements.river.exports.fixings.DeltaWtExporter"/>
     <output-generator names="fix_deltawt_curve" class="org.dive4elements.river.exports.fixings.FixDeltaWtGenerator"/>
diff -r 6b12045ac372 -r 994995baa32b artifacts/src/main/java/org/dive4elements/river/collections/D4EArtifactCollection.java
--- a/artifacts/src/main/java/org/dive4elements/river/collections/D4EArtifactCollection.java	Wed Oct 08 11:02:16 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/collections/D4EArtifactCollection.java	Wed Oct 08 11:49:07 2014 +0200
@@ -329,11 +329,12 @@
         // because it has no OutputStream set!
         Document dummy = XMLUtils.newDocument();
         outGen.init(out, dummy, null, getContext());
-        prepareMasterArtifact(outGen);
+        D4EArtifact master = getMasterArtifact();
+        prepareMasterArtifact(master, outGen);
 
         try {
             Document outAttr = getAttribute(attr, out);
-            OutputHelper helper = new OutputHelper(identifier());
+            OutputHelper helper = new OutputHelper(identifier(), master);
             helper.doOut(outGen, out, out, outAttr, getContext(), dummy);
         }
         catch (ArtifactDatabaseException adbe) {
@@ -413,11 +414,13 @@
         generator.init(key, format, out, context);
         generator.setSettings(settings);
         generator.setCollection(this);
-        prepareMasterArtifact(generator);
+
+        D4EArtifact master = getMasterArtifact();
+        prepareMasterArtifact(master, generator);
 
         try {
             Document attr = getAttribute(cAttr, name);
-            OutputHelper helper = new OutputHelper(identifier());
+            OutputHelper helper = new OutputHelper(identifier(), master);
             helper.doOut(generator, name, subtype, attr, context, format);
             generator.generate();
         }
@@ -437,10 +440,8 @@
      *
      * @param generator The generator that gets a master Artifact.
      */
-    protected void prepareMasterArtifact(OutGenerator generator
-    ) {
+    protected void prepareMasterArtifact(D4EArtifact master, OutGenerator generator) {
         // Get master artifact.
-        D4EArtifact master = getMasterArtifact();
         if (master != null) {
             log.debug("Set master Artifact to uuid: " + master.identifier());
             generator.setMasterArtifact(master);
diff -r 6b12045ac372 -r 994995baa32b artifacts/src/main/java/org/dive4elements/river/exports/OutputHelper.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/OutputHelper.java	Wed Oct 08 11:02:16 2014 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/OutputHelper.java	Wed Oct 08 11:49:07 2014 +0200
@@ -50,9 +50,11 @@
     private static Logger log = Logger.getLogger(OutputHelper.class);
 
     protected String identifier;
+    private  D4EArtifact masterArtifact;
 
-    public OutputHelper(String identifier) {
+    public OutputHelper(String identifier, D4EArtifact masterArtifact) {
         this.identifier = identifier;
+        this.masterArtifact = masterArtifact;
     }
 
     private static List<ContextInjector> getContextInjectors(CallContext context, String out) {
@@ -104,6 +106,12 @@
 
         List<ContextInjector> cis = getContextInjectors(context, outName);
 
+        if (cis != null) {
+            for (ContextInjector ci: cis) {
+                ci.injectContext(context, masterArtifact, request);
+            }
+        }
+
         try {
             for (int i = 0, T = themeList.size(); i < T; i++) {
                 ManagedFacet theme = themeList.get(i);
@@ -131,23 +139,16 @@
                     continue;
                 }
 
-                if (cis != null) {
-                    for (ContextInjector ci: cis) {
-                        ci.injectContext(
-                            context, dataProviders.get(i).getArtifact(), request);
-                    }
-                }
-
                 generator.doOut(
-                        dataProviders.get(i),
-                        getFacetThemeFromAttribute(
-                            art,
-                            outName,
-                            facetName,
-                            theme.getDescription(),
-                            theme.getIndex(),
-                            context),
-                        theme.getActive() == 1);
+                    dataProviders.get(i),
+                    getFacetThemeFromAttribute(
+                        art,
+                        outName,
+                        facetName,
+                        theme.getDescription(),
+                        theme.getIndex(),
+                        context),
+                    theme.getActive() == 1);
             }
         }
         catch (ArtifactDatabaseException ade) {
diff -r 6b12045ac372 -r 994995baa32b artifacts/src/main/java/org/dive4elements/river/exports/sq/SQOverviewGenerator.java
--- a/artifacts/src/main/java/org/dive4elements/river/exports/sq/SQOverviewGenerator.java	Wed Oct 08 11:02:16 2014 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,254 +0,0 @@
-/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
- * Software engineering by Intevation GmbH
- *
- * This file is Free Software under the GNU AGPL (>=v3)
- * and comes with ABSOLUTELY NO WARRANTY! Check out the
- * documentation coming with Dive4Elements River for details.
- */
-
-package org.dive4elements.river.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 org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
-import org.dive4elements.artifactdatabase.state.Settings;
-import org.dive4elements.artifacts.Artifact;
-import org.dive4elements.artifacts.ArtifactDatabaseException;
-import org.dive4elements.artifacts.CallContext;
-import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
-import org.dive4elements.artifacts.common.utils.XMLUtils;
-import org.dive4elements.river.artifacts.context.RiverContext;
-import org.dive4elements.river.collections.D4EArtifactCollection;
-import org.dive4elements.river.exports.ChartGenerator2;
-import org.dive4elements.river.exports.OutGenerator;
-import org.dive4elements.river.exports.OutputHelper;
-import org.dive4elements.river.themes.ThemeDocument;
-
-public class SQOverviewGenerator
-implements OutGenerator
-{
-    private static Logger log = Logger.getLogger(SQOverviewGenerator.class);
-
-    public static final String XPATH_CHART_SIZE =
-        "/art:action/art:attributes/art:size";
-
-    protected D4EArtifactCollection collection;
-
-    protected Artifact master;
-
-    protected Settings settings;
-
-    protected Document request;
-
-    protected OutputStream out;
-
-    protected CallContext context;
-
-    protected List<JFreeChart> charts;
-
-    protected String outName;
-
-    @Override
-    public void setup(Object config) {
-        log.debug("SQOverviewGenerator.setup");
-    }
-
-    /**
-     * Produce output.
-     * @param artifactAndFacet current facet and artifact.
-     * @param attr  theme for facet
-     */
-    @Override
-    public void doOut(
-        ArtifactAndFacet artifactAndFacet,
-        ThemeDocument    attr,
-        boolean          visible
-    ) {
-        log.debug("doOut()");
-
-        // TODO: Why not using outName for this.
-
-        String name = artifactAndFacet.getData(context).toString();
-        if(name != null) {
-            log.debug("name: " + name);
-            ChartGenerator2 g =
-                (ChartGenerator2)RiverContext.getOutGenerator(
-                    context,
-                    name);
-            /* Make sure master is also set in those */
-            g.setMasterArtifact(master);
-
-            OutputHelper helper = new OutputHelper(master.identifier());
-            Document collectionAttribute = collection.getAttribute();
-
-            try {
-                Document cAttr = getAttribute(context, collectionAttribute, name);
-                g.init(name, request, out, context);
-
-                helper.doOut(g, name, name, cAttr, context, request);
-                JFreeChart chart = g.generateChart();
-                chart.removeLegend();
-                charts.add(chart);
-            }
-            catch (IOException e) {
-                log.warn(e);
-            }
-            catch (ArtifactDatabaseException e) {
-                log.warn(e);
-            }
-        }
-    }
-
-    @Override
-    public void init(String outName, Document request, OutputStream out, CallContext context) {
-        this.outName = outName;
-        this.request = request;
-        this.out = out;
-        this.context = context;
-        charts = new ArrayList<JFreeChart>();
-    }
-
-    @Override
-    public void setMasterArtifact(Artifact master) {
-        this.master = master;
-    }
-
-    @Override
-    public void setCollection(D4EArtifactCollection collection) {
-        this.collection = collection;
-    }
-
-    @Override
-    public void generate() throws IOException {
-        log.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, S = charts.size(); i < S; i++) {
-            log.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);
-            }
-            // TODO: Dispose Graphics object!
-            result.createGraphics().drawImage(img, horPos, vertPos, null);
-        }
-        ImageIO.write(result, "png", out);
-    }
-
-    @Override
-    public void setSettings(Settings settings) {
-        this.settings = settings;
-    }
-
-    @Override
-    public Settings getSettings() {
-        return this.settings;
-    }
-
-
-    /**
-     * Returns the "attribute" (part of description document) for a specific
-     * output type.
-     *
-     * @param context The CallContext object.
-     * @param attr The xml attribute saved at the collection.
-     * @param output The name of the desired output type.
-     *
-     * @return the attribute for the desired output type.
-     */
-    protected Document getAttribute(
-        CallContext context,
-        Document    attr,
-        String      output)
-    throws    ArtifactDatabaseException
-    {
-        log.debug("find specific XML node for Output: " + output);
-
-        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) {
-                    log.warn("Wrong values for chart width/height.");
-                }
-            }
-        }
-
-        return size[0] > 0 && size[1] > 0 ? size : null;
-    }
-}
-// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :


More information about the Dive4Elements-commits mailing list