changeset 7846:e84726b48484

New Facet, datatype and data factory for porosities.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 30 Apr 2014 15:20:11 +0200
parents 4e264d6c6e06
children a95f1265da8e
files artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/Porosity.java artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/PorosityFacet.java artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/PorosityFactory.java
diffstat 3 files changed, 240 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/model/minfo/Porosity.java	Wed Apr 30 15:20:11 2014 +0200
@@ -0,0 +1,56 @@
+/* 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.HashMap;
+import java.util.Map;
+
+
+public class Porosity
+{
+
+    private Map<Double, Double> pairs;
+
+
+    public Porosity() {
+        pairs = new HashMap<Double, Double>();
+    }
+
+    public void add(double station, double porosity) {
+        this.pairs.put(station, porosity);
+    }
+
+    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[] porosity = pairs.values().toArray(new Double[pairs.size()]);
+        int realIndex = 0;
+        for (int i = 0; i < kms.length; i++) {
+            if (kms[i] == null || porosity[i] == null) {
+                continue;
+            }
+            array[0][realIndex] = kms[i];
+            array[1][realIndex] = porosity[i];
+            realIndex++;
+        }
+        return array;
+    }
+
+
+    public Double getWidth(double station) {
+        if (this.pairs.containsKey(station)) {
+            return this.pairs.get(station);
+        }
+        return null;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/PorosityFacet.java	Wed Apr 30 15:20:11 2014 +0200
@@ -0,0 +1,89 @@
+/* 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 org.apache.log4j.Logger;
+
+import org.dive4elements.artifactdatabase.state.Facet;
+import org.dive4elements.artifactdatabase.state.StaticFacet;
+import org.dive4elements.artifacts.Artifact;
+import org.dive4elements.artifacts.CallContext;
+import org.dive4elements.artifacts.CallMeta;
+import org.dive4elements.artifacts.common.utils.XMLUtils;
+import org.dive4elements.river.artifacts.D4EArtifact;
+import org.dive4elements.river.artifacts.model.DataFacet;
+import org.dive4elements.river.artifacts.model.FacetTypes;
+import org.dive4elements.river.artifacts.resources.Resources;
+import org.dive4elements.river.artifacts.states.DefaultState.ComputeType;
+import org.w3c.dom.Document;
+
+
+public class PorosityFacet
+extends DataFacet
+implements   FacetTypes, StaticFacet
+{
+    private static Logger logger = Logger.getLogger(PorosityFacet.class);
+
+    private static final String NAME = "porosity";
+
+    public PorosityFacet() {
+    }
+
+    public PorosityFacet(String facetName, String description) {
+        super(facetName, description);
+    }
+
+    public PorosityFacet(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) {
+
+        D4EArtifact flys = (D4EArtifact) artifact;
+        String porosity_id = flys.getDataAsString("porosity_id");
+
+        Porosity porosity =
+            PorosityFactory.getPorosity(Integer.valueOf(porosity_id));
+
+        return porosity;
+    }
+
+    /** Copy deeply. */
+    @Override
+    public Facet deepCopy() {
+        PorosityFacet copy = new PorosityFacet();
+        copy.set(this);
+        copy.type = type;
+        copy.hash = hash;
+        copy.stateId = stateId;
+        return copy;
+    }
+
+    @Override
+    public void setup(Artifact artifact, Document data, CallMeta callMeta) {
+        logger.debug("setup");
+
+        if (logger.isDebugEnabled()) {
+            logger.debug(XMLUtils.toString(data));
+        }
+
+        String code = D4EArtifact.getDatacageIDValue(data);
+
+        if (code != null) {
+            this.name = NAME;
+            this.description = Resources.getMsg(
+                callMeta,
+                "facet.porosity",
+                "Porosity");
+            D4EArtifact d4e = (D4EArtifact) artifact;
+            d4e.addStringData("porosity_id", code);
+        }
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/PorosityFactory.java	Wed Apr 30 15:20:11 2014 +0200
@@ -0,0 +1,95 @@
+/* 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.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 org.dive4elements.river.artifacts.cache.CacheFactory;
+import org.dive4elements.river.artifacts.model.StaticMorphoWidthCacheKey;
+import org.dive4elements.river.artifacts.model.StaticPorosityCacheKey;
+import org.dive4elements.river.backend.SessionHolder;
+
+
+public class PorosityFactory
+{
+    /** Private logger to use here. */
+    private static Logger log = Logger.getLogger(PorosityFactory.class);
+
+    public static final String SQL_SELECT =
+        "SELECT pv.station AS station, pv.porosity AS porosity " +
+        "   FROM porosity p" +
+        "       JOIN porosity_values pv on pv.porosity_id = p.id" +
+        "   WHERE p.id = :porosity_id";
+
+    private PorosityFactory() {
+    }
+
+
+    /**
+     * Get WKms for given column and wst_id, caring about the cache.
+     */
+    public static Porosity getPorosity(int porosity_id) {
+        log.debug("PorosityFactory.getPorosity");
+        Cache cache = CacheFactory.getCache(StaticPorosityCacheKey.CACHE_NAME);
+
+        StaticPorosityCacheKey cacheKey;
+
+        if (cache != null) {
+            cacheKey = new StaticPorosityCacheKey(porosity_id);
+            Element element = cache.get(cacheKey);
+            if (element != null) {
+                log.debug("Got static porosity values from cache");
+                return (Porosity)element.getValue();
+            }
+        }
+        else {
+            cacheKey = null;
+        }
+
+        Porosity values = getPorosityUncached(porosity_id);
+
+        if (values != null && cacheKey != null) {
+            log.debug("Store static porosity values in cache.");
+            Element element = new Element(cacheKey, values);
+            cache.put(element);
+        }
+        return values;
+    }
+
+    private static Porosity getPorosityUncached(int porosity_id) {
+        if (log.isDebugEnabled()) {
+            log.debug("PorosityFactory.getPorosityUncached");
+        }
+
+        Session session = SessionHolder.HOLDER.get();
+        SQLQuery sqlQuery = session.createSQLQuery(SQL_SELECT)
+                .addScalar("station", StandardBasicTypes.DOUBLE)
+                .addScalar("porosity", StandardBasicTypes.DOUBLE);
+        sqlQuery.setInteger("porosity_id", porosity_id);
+        List<Object []> results = sqlQuery.list();
+
+        Porosity porosities = new Porosity();
+        for (Object [] row: results) {
+            log.debug("got station: " + (Double)row[0]);
+            porosities.add(
+                (Double) row[0],
+                (Double) row[1]);
+        }
+        return porosities;
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org