changeset 7199:baf04164fcc6

issue1435: Stubs for SedimentDensityFacet and -Artifact.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Mon, 30 Sep 2013 09:38:09 +0200 (2013-09-30)
parents f707ee04ac80
children 483cae8915f6
files artifacts/src/main/java/org/dive4elements/river/artifacts/SedimentDensityArtifact.java artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentDensityFacet.java
diffstat 2 files changed, 309 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/SedimentDensityArtifact.java	Mon Sep 30 09:38:09 2013 +0200
@@ -0,0 +1,226 @@
+/* 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.artifacts;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.w3c.dom.Document;
+
+import org.dive4elements.artifactdatabase.state.DefaultOutput;
+import org.dive4elements.artifactdatabase.state.Facet;
+import org.dive4elements.artifactdatabase.state.FacetActivity;
+import org.dive4elements.artifactdatabase.state.State;
+import org.dive4elements.artifacts.Artifact;
+import org.dive4elements.artifacts.ArtifactFactory;
+import org.dive4elements.artifacts.CallMeta;
+import org.dive4elements.artifacts.common.utils.XMLUtils;
+import org.dive4elements.river.artifacts.states.DefaultState.ComputeType;
+import org.dive4elements.river.artifacts.model.Calculation;
+import org.dive4elements.river.artifacts.model.CalculationResult;
+import org.dive4elements.river.artifacts.model.minfo.SedimentDensity;
+import org.dive4elements.river.artifacts.model.minfo.SedimentDensityFacet;
+import org.dive4elements.river.artifacts.model.minfo.SedimentDensityFactory;
+import org.dive4elements.river.artifacts.states.StaticState;
+
+import org.dive4elements.river.artifacts.model.FacetTypes;
+
+import org.dive4elements.river.utils.Formatter;
+
+
+/** Artifact to access sediment density measurements. */
+public class SedimentDensityArtifact
+extends      StaticD4EArtifact
+implements   FacetTypes
+{
+    /** The logger for this class. */
+    private static Logger logger =
+        Logger.getLogger(SedimentDensityArtifact.class);
+
+    /** Artifact key name. */
+    private static final String NAME = "sedimentdensity";
+
+    /** Spawn only inactive facets. */
+    static {
+        // TODO: Move to configuration.
+        FacetActivity.Registry.getInstance()
+            .register(NAME, FacetActivity.INACTIVE);
+    }
+
+    /** Need to give the state an id. */
+    public static final String STATIC_STATE_NAME =
+        "state.sedimentdensity.static";
+
+    /** One and only state to be in. */
+    protected transient State state = null;
+
+    protected String DATA_ID = "ID";
+    protected String DATA_YEAR = "YEAR";
+
+    /**
+     * Trivial Constructor.
+     */
+    public SedimentDensityArtifact() {
+        logger.debug("SedimentDensityArtifact.SedimentDensityArtifact");
+    }
+
+
+    /** Get artifact key name. */
+    @Override
+    public String getName() {
+        return NAME;
+    }
+
+
+    private Object getSedimentDensity() {
+        logger.debug("SedimentDensityArtifact.getSedimentDensity");
+        Integer id = getDataAsInteger(DATA_ID);
+        Integer year = getDataAsInteger(DATA_YEAR);
+
+        // TODO use cache if possible
+        return SedimentDensityFactory.getSedimentDensityUncached(id, year);
+    }
+
+
+    private State newDensityState() {
+        return new StaticState(STATIC_STATE_NAME) {
+            public Object staticCompute(List<Facet> facets) {
+                 return getSedimentDensity();
+            }
+        };
+    }
+
+
+    /** Create a new state with bogus output. */
+    protected State spawnState() {
+        state = newDensityState();
+        List<Facet> fs = getFacets(STATIC_STATE_NAME);
+        DefaultOutput output = new DefaultOutput(
+            "general",
+            "general",
+            "image/png",
+            fs,
+            "chart");
+
+        state.getOutputs().add(output);
+
+        return state;
+    }
+
+
+    /**
+     * Gets called from factory, to set things up.
+     */
+    @Override
+    public void setup(
+        String          identifier,
+        ArtifactFactory factory,
+        Object          context,
+        CallMeta        callMeta,
+        Document        data)
+    {
+        logger.debug("SedimentDensityArtifact.setup");
+
+        // Store id, yield yields.
+        state = newDensityState();
+        if (logger.isDebugEnabled()) {
+            logger.debug(XMLUtils.toString(data));
+        }
+
+        List<Facet> fs = new ArrayList<Facet>();
+
+        String code = getDatacageIDValue(data);
+
+        if (code != null) {
+            //String name = SedimentDensityFactory.getSedimentDensityDescription(Integer.valueOf(code));
+            //id: and year?
+
+            String name = "facet";
+
+            Facet facet = new SedimentDensityFacet(
+                0,
+                SEDIMENT_LOAD_COARSE,
+                name,
+                //????
+                ComputeType.ADVANCE, state.getID(), "hash"
+                );
+            fs.add(facet);
+            addFacets(state.getID(), fs);
+            addStringData(DATA_ID, code);
+            addStringData(DATA_YEAR, code);
+        }
+
+        spawnState();
+        super.setup(identifier, factory, context, callMeta, data);
+    }
+
+
+    /**
+     * Get a list containing the one and only State.
+     * @param  context ignored.
+     * @return list with one and only state.
+     */
+    @Override
+    protected List<State> getStates(Object context) {
+        ArrayList<State> states = new ArrayList<State>();
+        states.add(getState());
+        return states;
+    }
+
+
+    /**
+     * Get the "current" state (there is but one).
+     * @param cc ignored.
+     * @return the "current" (only possible) state.
+     */
+    @Override
+    public State getCurrentState(Object cc) {
+        return getState();
+    }
+
+
+    /**
+     * Get the only possible state.
+     * @return the state.
+     */
+    protected State getState() {
+        return getState(null, null);
+    }
+
+
+    /**
+     * Get the state.
+     * @param context ignored.
+     * @param stateID ignored.
+     * @return the state.
+     */
+    @Override
+    protected State getState(Object context, String stateID) {
+        return (state != null)
+            ? state
+            : spawnState();
+    }
+
+
+    /**
+     * Called via setup. Overridden to avoid cloning all data.
+     *
+     * @param artifact The master-artifact.
+     */
+    @Override
+    protected void initialize(
+        Artifact artifact,
+        Object context,
+        CallMeta meta)
+    {
+        logger.debug("SedimentDensityArtifact.initialize");
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentDensityFacet.java	Mon Sep 30 09:38:09 2013 +0200
@@ -0,0 +1,83 @@
+/* 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.artifacts.model.minfo;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import gnu.trove.TDoubleArrayList;
+
+import org.dive4elements.artifactdatabase.state.Facet;
+
+import org.dive4elements.artifacts.Artifact;
+import org.dive4elements.artifacts.CallContext;
+
+import org.dive4elements.river.artifacts.D4EArtifact;
+
+import org.dive4elements.river.artifacts.model.CalculationResult;
+import org.dive4elements.river.artifacts.model.DataFacet;
+import org.dive4elements.river.artifacts.model.FacetTypes;
+
+import org.dive4elements.river.artifacts.states.DefaultState.ComputeType;
+
+import org.dive4elements.river.utils.RiverUtils;
+
+import org.apache.log4j.Logger;
+
+
+/** Facet to access sediment density values measured in one year. */
+public class SedimentDensityFacet
+extends DataFacet
+{
+    /** Very own logger. */
+    private static Logger logger = Logger.getLogger(SedimentDensityFacet.class);
+
+    /** Used as tolerance value when fetching measurement stations. */
+    private static double EPSILON = 1e-5;
+
+
+    public SedimentDensityFacet() {
+    }
+
+    public SedimentDensityFacet(int idx, String name, String description,
+        ComputeType type, String stateId, String hash) {
+        super(idx, name, description, type, hash, stateId);
+    }
+
+    @Override
+    public Object getData(Artifact artifact, CallContext context) {
+        logger.debug("Get data for sediment density at index: " + index);
+
+        D4EArtifact flys = (D4EArtifact) artifact;
+
+        CalculationResult res = (CalculationResult) flys.compute(context, hash,
+            stateId, type, false);
+
+        if (res == null) {
+            logger.error("No CalculationResult");
+        }
+
+        return null;
+    }
+
+
+    /** Copy deeply. */
+    @Override
+    public Facet deepCopy() {
+        SedimentDensityFacet copy = new SedimentDensityFacet();
+        copy.set(this);
+        copy.type = type;
+        copy.hash = hash;
+        copy.stateId = stateId;
+        return copy;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org