[Dive4elements-commits] [PATCH 1 of 6] Model, factory, facet and static artifact for morphologic width

Wald Commits scm-commit at wald.intevation.org
Thu Apr 18 14:19:43 CEST 2013


# HG changeset patch
# User Raimund Renkert <rrenkert at intevation.de>
# Date 1366287255 -7200
# Node ID 7ac111db8a0780058ce580e423050e9e3adc85d4
# Parent  dd652fcc9f0d2a5634ec1d278531d97b769af67c
Model, factory, facet and static artifact for morphologic width.

diff -r dd652fcc9f0d -r 7ac111db8a07 flys-artifacts/src/main/java/de/intevation/flys/artifacts/StaticMorphWidthArtifact.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/StaticMorphWidthArtifact.java	Thu Apr 18 14:14:15 2013 +0200
@@ -0,0 +1,110 @@
+package de.intevation.flys.artifacts;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.w3c.dom.Document;
+
+import de.intevation.artifactdatabase.state.DefaultOutput;
+import de.intevation.artifactdatabase.state.Facet;
+import de.intevation.artifactdatabase.state.FacetActivity;
+import de.intevation.artifacts.ArtifactFactory;
+import de.intevation.artifacts.Artifact;
+import de.intevation.artifacts.CallMeta;
+import de.intevation.artifacts.common.utils.XMLUtils;
+import de.intevation.flys.artifacts.model.minfo.BedHeightFacet;
+import de.intevation.flys.artifacts.model.minfo.BedHeightFactory;
+import de.intevation.flys.artifacts.model.minfo.MorphologicWidthFacet;
+import de.intevation.flys.artifacts.resources.Resources;
+import de.intevation.flys.artifacts.states.StaticState;
+
+public class StaticMorphWidthArtifact
+extends      AbstractStaticStateArtifact
+{
+    /** The logger for this class. */
+    private static Logger logger =
+        Logger.getLogger(StaticMorphWidthArtifact.class);
+
+    private static final String NAME = "morph-width";
+    private static final String STATIC_FACET_NAME = "morph-width";
+
+    static {
+        // TODO: Move to configuration.
+        FacetActivity.Registry.getInstance()
+            .register(NAME, FacetActivity.INACTIVE);
+    }
+
+    public static final String STATIC_STATE_NAME =
+        "state.morph-width.static";
+
+    /**
+     * Trivial Constructor.
+     */
+    public StaticMorphWidthArtifact() {
+    }
+
+    @Override
+    public String getName() {
+        return NAME;
+    }
+
+    /**
+     * Gets called from factory, to set things up.
+     */
+    @Override
+    public void setup(
+        String          identifier,
+        ArtifactFactory factory,
+        Object          context,
+        CallMeta        callMeta,
+        Document        data)
+    {
+        logger.debug("setup");
+
+        if (logger.isDebugEnabled()) {
+            logger.debug(XMLUtils.toString(data));
+        }
+
+        String code = getDatacageIDValue(data);
+
+        if (code != null) {
+                Facet facet = new MorphologicWidthFacet(
+                        STATIC_FACET_NAME,
+                        Resources.getMsg(
+                            callMeta,
+                            "facet.morphologic.width",
+                            "morphologische Breite"));
+                addStringData("width_id", code);
+                ArrayList<Facet> facets = new ArrayList<Facet>(1);
+                facets.add(facet);
+
+                addFacets(STATIC_STATE_NAME, facets);
+        }
+        super.setup(identifier, factory, context, callMeta, data);
+    }
+
+    @Override
+    protected void initStaticState() {
+
+        logger.debug("initStaticState " + getName() + " " + identifier());
+
+        StaticState state = new StaticState(STATIC_STATE_NAME);
+        DefaultOutput output = new DefaultOutput(
+                "general",
+                "general",
+                "image/png",
+                "chart");
+
+        List<Facet> facets = getFacets(STATIC_STATE_NAME);
+        output.addFacets(facets);
+        state.addOutput(output);
+
+        setStaticState(state);
+    }
+
+    @Override
+    protected void initialize(Artifact artifact, Object context, CallMeta meta) {
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :
diff -r dd652fcc9f0d -r 7ac111db8a07 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FacetTypes.java
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FacetTypes.java	Thu Apr 18 12:55:05 2013 +0200
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/FacetTypes.java	Thu Apr 18 14:14:15 2013 +0200
@@ -289,6 +289,7 @@
     String BED_DIFFERENCE_EPOCH_HEIGHT2 = "bedheight_difference.epoch.height2";
     String BED_DIFFERENCE_EPOCH_HEIGHT1_FILTERED = "bedheight_difference.epoch.height1.filtered";
     String BED_DIFFERENCE_EPOCH_HEIGHT2_FILTERED = "bedheight_difference.epoch.height2.filtered";
+    String MORPHOLOGIC_WIDTH = "morph-width";
 
     String SEDIMENT_LOAD_COARSE        = "sedimentload.coarse";
     String SEDIMENT_LOAD_SAND          = "sedimentload.sand";
diff -r dd652fcc9f0d -r 7ac111db8a07 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/StaticMorphoWidthCacheKey.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/StaticMorphoWidthCacheKey.java	Thu Apr 18 14:14:15 2013 +0200
@@ -0,0 +1,29 @@
+package de.intevation.flys.artifacts.model;
+
+
+public class StaticMorphoWidthCacheKey
+{
+   public static final String CACHE_NAME = "morpho-width-table-static";
+
+    private int width_id;
+
+    public StaticMorphoWidthCacheKey(
+        int width_id
+    ) {
+        this.width_id = width_id;
+    }
+
+    @Override
+    public int hashCode() {
+        return (String.valueOf(width_id)).hashCode();
+    }
+
+    @Override
+    public boolean equals(Object other) {
+        if (!(other instanceof StaticMorphoWidthCacheKey)) {
+            return false;
+        }
+        StaticMorphoWidthCacheKey o = (StaticMorphoWidthCacheKey) other;
+        return this.width_id == o.width_id;
+    }
+}
diff -r dd652fcc9f0d -r 7ac111db8a07 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/MorphologicWidth.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/MorphologicWidth.java	Thu Apr 18 14:14:15 2013 +0200
@@ -0,0 +1,50 @@
+package de.intevation.flys.artifacts.model.minfo;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import de.intevation.flys.jfree.DoubleBounds;
+
+
+public class MorphologicWidth
+{
+
+    private Map<Double, Double> pairs;
+
+
+    public MorphologicWidth() {
+        pairs = new HashMap<Double, Double>();
+    }
+
+    public void add(double station, double width) {
+        this.pairs.put(station, width);
+    }
+
+    public Map<Double, Double> getAll() {
+        return this.pairs;
+    }
+
+    public double[][] getAsArray() {
+        double [][] array = new double[2][pairs.size()];
+        Double[] kms = pairs.keySet().toArray(new Double[pairs.size()]);
+        Double[] width = pairs.values().toArray(new Double[pairs.size()]);
+        int realIndex = 0;
+        for (int i = 0; i < kms.length; i++) {
+            if (kms[i] == null || width[i] == null) {
+                continue;
+            }
+            array[0][realIndex] = kms[i];
+            array[1][realIndex] = width[i];
+            realIndex++;
+        }
+        return array;
+    }
+
+
+    public Double getWidth(double station) {
+        if (this.pairs.containsKey(station)) {
+            return this.pairs.get(station);
+        }
+        return null;
+    }
+}
diff -r dd652fcc9f0d -r 7ac111db8a07 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/MorphologicWidthFacet.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/MorphologicWidthFacet.java	Thu Apr 18 14:14:15 2013 +0200
@@ -0,0 +1,54 @@
+package de.intevation.flys.artifacts.model.minfo;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.artifactdatabase.state.Facet;
+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 MorphologicWidthFacet
+extends DataFacet
+implements   FacetTypes
+{
+    private static Logger logger = Logger.getLogger(SedimentLoadFacet.class);
+
+
+    public MorphologicWidthFacet() {
+    }
+
+    public MorphologicWidthFacet(String facetName, String description) {
+        super(facetName, description);
+    }
+
+    public MorphologicWidthFacet(int idx, String name, String description,
+        ComputeType type, String stateId, String hash) {
+        super(idx, name, description, type, hash, stateId);
+    }
+
+    public Object getData(Artifact artifact, CallContext context) {
+
+        FLYSArtifact flys = (FLYSArtifact) artifact;
+        String width_id = flys.getDataAsString("width_id");
+
+        MorphologicWidth width =
+            MorphologicWidthFactory.getWidth(Integer.valueOf(width_id));
+
+        return width;
+    }
+
+    /** Copy deeply. */
+    @Override
+    public Facet deepCopy() {
+        MorphologicWidthFacet copy = new MorphologicWidthFacet();
+        copy.set(this);
+        copy.type = type;
+        copy.hash = hash;
+        copy.stateId = stateId;
+        return copy;
+    }
+}
diff -r dd652fcc9f0d -r 7ac111db8a07 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/MorphologicWidthFactory.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/MorphologicWidthFactory.java	Thu Apr 18 14:14:15 2013 +0200
@@ -0,0 +1,87 @@
+package de.intevation.flys.artifacts.model.minfo;
+
+import java.util.List;
+
+import net.sf.ehcache.Cache;
+import net.sf.ehcache.Element;
+
+import org.apache.log4j.Logger;
+import org.hibernate.SQLQuery;
+import org.hibernate.Session;
+import org.hibernate.type.StandardBasicTypes;
+
+import de.intevation.flys.artifacts.cache.CacheFactory;
+import de.intevation.flys.artifacts.model.StaticMorphoWidthCacheKey;
+import de.intevation.flys.backend.SessionHolder;
+
+
+public class MorphologicWidthFactory
+{
+    /** Private logger to use here. */
+    private static Logger log = Logger.getLogger(MorphologicWidthFactory.class);
+
+    public static final String SQL_SELECT =
+        "SELECT mwv.station AS station, mwv.width AS width " +
+        "   FROM morphologic_width mw" +
+        "       JOIN morphologic_width_values mwv on mwv.morphologic_width_id = mw.id" +
+        "   WHERE mw.id = :width_id";
+
+    private MorphologicWidthFactory() {
+    }
+
+
+    /**
+     * Get WKms for given column and wst_id, caring about the cache.
+     */
+    public static MorphologicWidth getWidth(int width_id) {
+        log.debug("MorphologicWidthFactory.getWidth");
+        Cache cache = CacheFactory.getCache(StaticMorphoWidthCacheKey.CACHE_NAME);
+
+        StaticMorphoWidthCacheKey cacheKey;
+
+        if (cache != null) {
+            cacheKey = new StaticMorphoWidthCacheKey(width_id);
+            Element element = cache.get(cacheKey);
+            if (element != null) {
+                log.debug("Got static bedheight values from cache");
+                return (MorphologicWidth)element.getValue();
+            }
+        }
+        else {
+            cacheKey = null;
+        }
+
+        MorphologicWidth values = getWidthUncached(width_id);
+
+        if (values != null && cacheKey != null) {
+            log.debug("Store static morphologic width values in cache.");
+            Element element = new Element(cacheKey, values);
+            cache.put(element);
+        }
+        return values;
+    }
+
+    private static MorphologicWidth getWidthUncached(int width_id) {
+        if (log.isDebugEnabled()) {
+            log.debug("MorphologicWidthFactory.getWidthUncached");
+        }
+
+        Session session = SessionHolder.HOLDER.get();
+        SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT)
+                .addScalar("station", StandardBasicTypes.DOUBLE)
+                .addScalar("width", StandardBasicTypes.DOUBLE);
+        sqlQuery.setInteger("width_id", width_id);
+        List<Object []> results = sqlQuery.list();
+
+        MorphologicWidth widths = new MorphologicWidth();
+        for (int i = 0; i < results.size(); i++) {
+            Object[] row = results.get(i);
+            log.debug("got station: " + (Double)row[0]);
+            widths.add(
+                (Double) row[0],
+                (Double) row[1]);
+        }
+        return widths;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :


More information about the Dive4elements-commits mailing list