[Dive4elements-commits] [PATCH 3 of 8] Data structure and factory for hws

Wald Commits scm-commit at wald.intevation.org
Thu Mar 14 17:27:31 CET 2013


# HG changeset patch
# User Raimund Renkert <rrenkert at intevation.de>
# Date 1363277629 -3600
# Node ID 6131b352e5e4fe923afa523c3b07beba0dfdacbd
# Parent  cd8b84af2ebc0f78f3b6aa2c97c4f7c2bafc93ea
Data structure and factory for hws.

diff -r cd8b84af2ebc -r 6131b352e5e4 flys-artifacts/src/main/java/de/intevation/flys/artifacts/access/MapAccess.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/access/MapAccess.java	Thu Mar 14 17:13:49 2013 +0100
@@ -0,0 +1,27 @@
+package de.intevation.flys.artifacts.access;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import de.intevation.artifacts.CallContext;
+import de.intevation.flys.artifacts.FLYSArtifact;
+
+
+public class MapAccess
+extends RangeAccess
+{
+
+    public MapAccess(FLYSArtifact artifact, CallContext context) {
+        super(artifact, context);
+    }
+
+    public List<String> getHWS() {
+        String param = getString("uesk.hws");
+        if (param != null) {
+            String[] split = param.split(";");
+            return new ArrayList<String>(Arrays.asList(split));
+        }
+        return new ArrayList<String>();
+    }
+}
diff -r cd8b84af2ebc -r 6131b352e5e4 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/map/HWS.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/map/HWS.java	Thu Mar 14 17:13:49 2013 +0100
@@ -0,0 +1,153 @@
+package de.intevation.flys.artifacts.model.map;
+
+import org.geotools.feature.simple.SimpleFeatureBuilder;
+import org.opengis.feature.simple.SimpleFeature;
+import org.opengis.feature.simple.SimpleFeatureType;
+
+import com.vividsolutions.jts.geom.Geometry;
+
+import de.intevation.flys.artifacts.model.NamedObjectImpl;
+import de.intevation.flys.utils.GeometryUtils;
+
+
+public class HWS
+extends NamedObjectImpl
+{
+
+    public enum TYPE {LINE, POINT};
+
+    private Geometry geom;
+    private String id;
+    private int kind;
+    private int official;
+    private String fedState;
+    private String description;
+    private TYPE type;
+
+    public HWS() {
+        this.geom = null;
+        // TODO Auto-generated constructor stub
+    }
+
+    public HWS(String name) {
+        super(name);
+        this.geom = null;
+    }
+
+    public HWS(
+        String name,
+        Geometry geom,
+        String id,
+        int kind,
+        int official,
+        String fedState,
+        String description,
+        TYPE type
+    ) {
+        super(name);
+        this.geom = geom;
+        this.id = id;
+        this.kind = kind;
+        this.official = official;
+        this.fedState = fedState;
+        this.description = description;
+        this.type = type;
+    }
+
+    public Geometry getGeom() {
+        return geom;
+    }
+
+    public void setGeom(Geometry geom) {
+        this.geom = geom;
+    }
+
+    public String getId() {
+        return id;
+    }
+
+    public void setId(String id) {
+        this.id = id;
+    }
+
+    public int getKind() {
+        return kind;
+    }
+
+    public void setKind(int kind) {
+        this.kind = kind;
+    }
+
+    public boolean isOfficial() {
+        return official == 1;
+    }
+
+    public void setOfficial(boolean o) {
+        this.official = o ? 1 : 0;
+    }
+
+    public String getFedState() {
+        return fedState;
+    }
+
+    public void setFedState(String fedState) {
+        this.fedState = fedState;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public TYPE getType() {
+        return type;
+    }
+
+    public void setType(TYPE type) {
+        this.type = type;
+    }
+
+    public SimpleFeatureType getFeatureType() {
+        int srid = this.geom.getSRID();
+        String srs = "EPSG:" + srid;
+
+        Object[][] attrs = new Object[5][];
+        attrs[0] = new Object[] { "name", String.class };
+        attrs[1] = new Object[] { "description", String.class };
+        attrs[2] = new Object[] { "TYP", String.class };
+        attrs[3] = new Object[] { "fed_state", String.class };
+        attrs[4] = new Object[] { "official", Integer.class };
+        SimpleFeatureType ft =
+            GeometryUtils.buildFeatureType(
+                "hws", srs, this.geom.getClass(), attrs);
+        return ft;
+    }
+
+    public SimpleFeature getFeature() {
+        SimpleFeatureType ft = getFeatureType();
+
+        SimpleFeatureBuilder featureBuilder = new SimpleFeatureBuilder(ft);
+        featureBuilder.add(this.geom);
+        featureBuilder.add(this.name);
+        featureBuilder.add(this.description);
+        if (this.kind == 1) {
+            featureBuilder.add("Rohr 1");
+        }
+        else if (this.kind == 2) {
+            featureBuilder.add("Damm");
+        }
+        else if (this.kind == 3) {
+            featureBuilder.add("Graben");
+        }
+        else {
+            featureBuilder.add("");
+        }
+        featureBuilder.add(this.fedState);
+        featureBuilder.add(this.official);
+
+        return featureBuilder.buildFeature(null);
+    }
+}
diff -r cd8b84af2ebc -r 6131b352e5e4 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/map/HWSContainer.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/map/HWSContainer.java	Thu Mar 14 17:13:49 2013 +0100
@@ -0,0 +1,95 @@
+package de.intevation.flys.artifacts.model.map;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.hibernate.mapping.Array;
+
+
+public class HWSContainer
+{
+    private static Logger logger = Logger.getLogger(HWSContainer.class);
+    private String river;
+    private HWS.TYPE type;
+    private List<HWS> hws;
+
+    public HWSContainer() {
+        river = null;
+        hws = new ArrayList<HWS>();
+    }
+
+    public HWSContainer(String river, HWS.TYPE type, List<HWS> hws) {
+        this.river = river;
+        this.hws = hws;
+        this.type = type;
+    }
+
+    public void setRiver(String river) {
+        this.river = river;
+    }
+
+    public String getRiver() {
+        return this.river;
+    }
+
+    public HWS.TYPE getType() {
+        return type;
+    }
+
+    public void setType(HWS.TYPE type) {
+        this.type = type;
+    }
+
+    public List<HWS> getHws() {
+        return hws;
+    }
+
+    public void addHws(HWS hws) {
+        logger.debug("add hws: " + hws.getName());
+        this.hws.add(hws);
+    }
+
+    public void addHws(List<HWS> hws) {
+        this.hws.addAll(hws);
+    }
+
+    public List<HWS> getOfficialHWS() {
+        if (hws == null || hws.size() == 0) {
+            return new ArrayList<HWS>();
+        }
+        List<HWS> results = new ArrayList<HWS>();
+        for (HWS h: hws) {
+            if (h.isOfficial()) {
+                results.add(h);
+            }
+        }
+        return results;
+    }
+
+    public List<HWS> getHws(String name) {
+        logger.debug("find: " + name + " in " + hws.size() + " elements");
+        if (hws == null || hws.size() == 0) {
+            return new ArrayList<HWS>();
+        }
+        List<HWS> results = new ArrayList<HWS>();
+        for (HWS h: hws) {
+            if (h.getName().equals(name)) {
+                results.add(h);
+            }
+        }
+        logger.debug("found: " + results.size());
+        return results;
+    }
+
+    public List<HWS> getHws(List<String> list) {
+        if (hws == null || hws.size() == 0) {
+            return new ArrayList<HWS>();
+        }
+        List<HWS> results = new ArrayList<HWS>();
+        for (String name : list) {
+            results.addAll(getHws(name));
+        }
+        return results;
+    }
+}
diff -r cd8b84af2ebc -r 6131b352e5e4 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/map/HWSFactory.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/map/HWSFactory.java	Thu Mar 14 17:13:49 2013 +0100
@@ -0,0 +1,180 @@
+package de.intevation.flys.artifacts.model.map;
+
+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.hibernatespatial.GeometryUserType;
+
+import com.vividsolutions.jts.geom.Geometry;
+
+import de.intevation.flys.artifacts.cache.CacheFactory;
+import de.intevation.flys.backend.SessionHolder;
+
+
+public class HWSFactory
+{
+    /** Private logger to use here. */
+    private static Logger log = Logger.getLogger(HWSFactory.class);
+
+    private static final int HWS_LINES = 0;
+    private static final int HWS_POINTS = 1;
+
+    public static final String SQL_SELECT_LINES =
+        "SELECT hl.name, hl.geom, hl.id, hl.kind_id, hl.official, fs.name AS fed, hl.description " +
+        "   FROM hws_lines hl" +
+        "       JOIN rivers r ON hl.river_id = r.id" +
+        "       LEFT JOIN fed_states fs ON hl.fed_state_id = fs.id" +
+        "   WHERE r.name = :river";
+
+    public static final String SQL_SELECT_POINTS =
+        "SELECT hp.name, hp.geom, hp.id, hp.kind_id, hp.official, fs.name AS fed, hp.description " +
+        "   FROM hws_points hp" +
+        "       JOIN rivers r ON hp.river_id = r.id" +
+        "       LEFT JOIN fed_states fs ON hp.fed_state_id = fs.id" +
+        "   WHERE r.name = :river";
+
+
+    private HWSFactory() {
+    }
+
+
+    public static HWSContainer getHWSLines(String river) {
+        log.debug("HWSFactory.getHWS");
+        Cache cache = CacheFactory.getCache(StaticHWSCacheKey.CACHE_NAME);
+
+        StaticHWSCacheKey cacheKey;
+
+        if (cache != null) {
+            cacheKey = new StaticHWSCacheKey(river, HWS_LINES);
+            Element element = cache.get(cacheKey);
+            if (element != null) {
+                log.debug("Got static hws values from cache");
+                return (HWSContainer)element.getValue();
+            }
+        }
+        else {
+            cacheKey = null;
+        }
+
+        HWSContainer values = getHWSLinesUncached(river);
+
+        if (values != null && cacheKey != null) {
+            log.debug("Store static hws values in cache.");
+            Element element = new Element(cacheKey, values);
+            cache.put(element);
+        }
+        return values;
+    }
+
+    public static HWSContainer getHWSPoints(String river) {
+        log.debug("HWSFactory.getHWS");
+        Cache cache = CacheFactory.getCache(StaticHWSCacheKey.CACHE_NAME);
+
+        StaticHWSCacheKey cacheKey;
+
+        if (cache != null) {
+            cacheKey = new StaticHWSCacheKey(river, HWS_LINES);
+            Element element = cache.get(cacheKey);
+            if (element != null) {
+                log.debug("Got static hws values from cache");
+                return (HWSContainer)element.getValue();
+            }
+        }
+        else {
+            cacheKey = null;
+        }
+
+        HWSContainer values = getHWSPointsUncached(river);
+
+        if (values != null && cacheKey != null) {
+            log.debug("Store static hws values in cache.");
+            Element element = new Element(cacheKey, values);
+            cache.put(element);
+        }
+        return values;
+    }
+
+    private static HWSContainer getHWSLinesUncached(String river) {
+        if (log.isDebugEnabled()) {
+            log.debug("HWSFactory.getHWSLinesUncached");
+        }
+
+        Session session = SessionHolder.HOLDER.get();
+        SQLQuery sqlQuery = null;
+        HWSContainer container = new HWSContainer();
+        container.setRiver(river);
+        container.setType(HWS.TYPE.LINE);
+        sqlQuery = session.createSQLQuery(SQL_SELECT_LINES)
+            .addScalar("name", StandardBasicTypes.STRING)
+            .addScalar("geom", GeometryUserType.TYPE)
+            .addScalar("id", StandardBasicTypes.STRING)
+            .addScalar("kind_id", StandardBasicTypes.INTEGER)
+            .addScalar("official", StandardBasicTypes.INTEGER)
+            .addScalar("fed", StandardBasicTypes.STRING)
+            .addScalar("description", StandardBasicTypes.STRING);
+
+        sqlQuery.setString("river", river);
+        List<Object []> resultsLines = sqlQuery.list();
+
+        for (int i = 0; i < resultsLines.size(); i++) {
+            Object[] row = resultsLines.get(i);
+            container.addHws(
+                new HWS(
+                    (String) row[0],
+                    (Geometry) row[1],
+                    (String) row[2],
+                    (Integer) row[3],
+                    (Integer) row[4],
+                    (String) row[5],
+                    (String) row[6],
+                    HWS.TYPE.LINE));
+        }
+
+        return container;
+    }
+
+    private static HWSContainer getHWSPointsUncached(String river) {
+        if (log.isDebugEnabled()) {
+            log.debug("HWSFactory.getHWSLinesUncached");
+        }
+
+        Session session = SessionHolder.HOLDER.get();
+        SQLQuery sqlQuery = null;
+        HWSContainer container = new HWSContainer();
+        container.setRiver(river);
+        container.setType(HWS.TYPE.LINE);
+        sqlQuery = session.createSQLQuery(SQL_SELECT_POINTS)
+            .addScalar("name", StandardBasicTypes.STRING)
+            .addScalar("geom", GeometryUserType.TYPE)
+            .addScalar("id", StandardBasicTypes.STRING)
+            .addScalar("kind_id", StandardBasicTypes.INTEGER)
+            .addScalar("official", StandardBasicTypes.INTEGER)
+            .addScalar("fed", StandardBasicTypes.STRING)
+            .addScalar("description", StandardBasicTypes.STRING);
+
+        sqlQuery.setString("river", river);
+        List<Object []> resultsPoints = sqlQuery.list();
+
+        for (int i = 0; i < resultsPoints.size(); i++) {
+            Object[] row = resultsPoints.get(i);
+            container.addHws(
+                new HWS(
+                    (String) row[0],
+                    (Geometry) row[1],
+                    (String) row[2],
+                    (Integer) row[3],
+                    (Integer) row[4],
+                    (String) row[5],
+                    (String) row[6],
+                    HWS.TYPE.POINT));
+        }
+
+        return container;
+    }
+}
diff -r cd8b84af2ebc -r 6131b352e5e4 flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/map/StaticHWSCacheKey.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/map/StaticHWSCacheKey.java	Thu Mar 14 17:13:49 2013 +0100
@@ -0,0 +1,27 @@
+package de.intevation.flys.artifacts.model.map;
+
+
+public class StaticHWSCacheKey
+{
+    public static final String CACHE_NAME = "hws-value-table-static";
+
+    private String river;
+    private int type;
+    
+    public StaticHWSCacheKey(String river, int type) {
+        this.river = river;
+        this.type = type;
+    }
+
+    public int hashCode() {
+        return river.hashCode() | (type << 8);
+    }
+
+    public boolean equals(Object other) {
+        if (!(other instanceof StaticHWSCacheKey)) {
+            return false;
+        }
+        StaticHWSCacheKey o = (StaticHWSCacheKey) other;
+        return this.river == o.river;
+    }
+}


More information about the Dive4elements-commits mailing list