Mercurial > dive4elements > river
diff flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/map/HWSFactory.java @ 5307:6131b352e5e4
Data structure and factory for hws.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Thu, 14 Mar 2013 17:13:49 +0100 |
parents | |
children | 029b5f5f1dcc |
line wrap: on
line diff
--- /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; + } +}