comparison 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
comparison
equal deleted inserted replaced
5306:cd8b84af2ebc 5307:6131b352e5e4
1 package de.intevation.flys.artifacts.model.map;
2
3 import java.util.List;
4
5 import net.sf.ehcache.Cache;
6 import net.sf.ehcache.Element;
7
8 import org.apache.log4j.Logger;
9 import org.hibernate.SQLQuery;
10 import org.hibernate.Session;
11 import org.hibernate.type.StandardBasicTypes;
12 import org.hibernatespatial.GeometryUserType;
13
14 import com.vividsolutions.jts.geom.Geometry;
15
16 import de.intevation.flys.artifacts.cache.CacheFactory;
17 import de.intevation.flys.backend.SessionHolder;
18
19
20 public class HWSFactory
21 {
22 /** Private logger to use here. */
23 private static Logger log = Logger.getLogger(HWSFactory.class);
24
25 private static final int HWS_LINES = 0;
26 private static final int HWS_POINTS = 1;
27
28 public static final String SQL_SELECT_LINES =
29 "SELECT hl.name, hl.geom, hl.id, hl.kind_id, hl.official, fs.name AS fed, hl.description " +
30 " FROM hws_lines hl" +
31 " JOIN rivers r ON hl.river_id = r.id" +
32 " LEFT JOIN fed_states fs ON hl.fed_state_id = fs.id" +
33 " WHERE r.name = :river";
34
35 public static final String SQL_SELECT_POINTS =
36 "SELECT hp.name, hp.geom, hp.id, hp.kind_id, hp.official, fs.name AS fed, hp.description " +
37 " FROM hws_points hp" +
38 " JOIN rivers r ON hp.river_id = r.id" +
39 " LEFT JOIN fed_states fs ON hp.fed_state_id = fs.id" +
40 " WHERE r.name = :river";
41
42
43 private HWSFactory() {
44 }
45
46
47 public static HWSContainer getHWSLines(String river) {
48 log.debug("HWSFactory.getHWS");
49 Cache cache = CacheFactory.getCache(StaticHWSCacheKey.CACHE_NAME);
50
51 StaticHWSCacheKey cacheKey;
52
53 if (cache != null) {
54 cacheKey = new StaticHWSCacheKey(river, HWS_LINES);
55 Element element = cache.get(cacheKey);
56 if (element != null) {
57 log.debug("Got static hws values from cache");
58 return (HWSContainer)element.getValue();
59 }
60 }
61 else {
62 cacheKey = null;
63 }
64
65 HWSContainer values = getHWSLinesUncached(river);
66
67 if (values != null && cacheKey != null) {
68 log.debug("Store static hws values in cache.");
69 Element element = new Element(cacheKey, values);
70 cache.put(element);
71 }
72 return values;
73 }
74
75 public static HWSContainer getHWSPoints(String river) {
76 log.debug("HWSFactory.getHWS");
77 Cache cache = CacheFactory.getCache(StaticHWSCacheKey.CACHE_NAME);
78
79 StaticHWSCacheKey cacheKey;
80
81 if (cache != null) {
82 cacheKey = new StaticHWSCacheKey(river, HWS_LINES);
83 Element element = cache.get(cacheKey);
84 if (element != null) {
85 log.debug("Got static hws values from cache");
86 return (HWSContainer)element.getValue();
87 }
88 }
89 else {
90 cacheKey = null;
91 }
92
93 HWSContainer values = getHWSPointsUncached(river);
94
95 if (values != null && cacheKey != null) {
96 log.debug("Store static hws values in cache.");
97 Element element = new Element(cacheKey, values);
98 cache.put(element);
99 }
100 return values;
101 }
102
103 private static HWSContainer getHWSLinesUncached(String river) {
104 if (log.isDebugEnabled()) {
105 log.debug("HWSFactory.getHWSLinesUncached");
106 }
107
108 Session session = SessionHolder.HOLDER.get();
109 SQLQuery sqlQuery = null;
110 HWSContainer container = new HWSContainer();
111 container.setRiver(river);
112 container.setType(HWS.TYPE.LINE);
113 sqlQuery = session.createSQLQuery(SQL_SELECT_LINES)
114 .addScalar("name", StandardBasicTypes.STRING)
115 .addScalar("geom", GeometryUserType.TYPE)
116 .addScalar("id", StandardBasicTypes.STRING)
117 .addScalar("kind_id", StandardBasicTypes.INTEGER)
118 .addScalar("official", StandardBasicTypes.INTEGER)
119 .addScalar("fed", StandardBasicTypes.STRING)
120 .addScalar("description", StandardBasicTypes.STRING);
121
122 sqlQuery.setString("river", river);
123 List<Object []> resultsLines = sqlQuery.list();
124
125 for (int i = 0; i < resultsLines.size(); i++) {
126 Object[] row = resultsLines.get(i);
127 container.addHws(
128 new HWS(
129 (String) row[0],
130 (Geometry) row[1],
131 (String) row[2],
132 (Integer) row[3],
133 (Integer) row[4],
134 (String) row[5],
135 (String) row[6],
136 HWS.TYPE.LINE));
137 }
138
139 return container;
140 }
141
142 private static HWSContainer getHWSPointsUncached(String river) {
143 if (log.isDebugEnabled()) {
144 log.debug("HWSFactory.getHWSLinesUncached");
145 }
146
147 Session session = SessionHolder.HOLDER.get();
148 SQLQuery sqlQuery = null;
149 HWSContainer container = new HWSContainer();
150 container.setRiver(river);
151 container.setType(HWS.TYPE.LINE);
152 sqlQuery = session.createSQLQuery(SQL_SELECT_POINTS)
153 .addScalar("name", StandardBasicTypes.STRING)
154 .addScalar("geom", GeometryUserType.TYPE)
155 .addScalar("id", StandardBasicTypes.STRING)
156 .addScalar("kind_id", StandardBasicTypes.INTEGER)
157 .addScalar("official", StandardBasicTypes.INTEGER)
158 .addScalar("fed", StandardBasicTypes.STRING)
159 .addScalar("description", StandardBasicTypes.STRING);
160
161 sqlQuery.setString("river", river);
162 List<Object []> resultsPoints = sqlQuery.list();
163
164 for (int i = 0; i < resultsPoints.size(); i++) {
165 Object[] row = resultsPoints.get(i);
166 container.addHws(
167 new HWS(
168 (String) row[0],
169 (Geometry) row[1],
170 (String) row[2],
171 (Integer) row[3],
172 (Integer) row[4],
173 (String) row[5],
174 (String) row[6],
175 HWS.TYPE.POINT));
176 }
177
178 return container;
179 }
180 }

http://dive4elements.wald.intevation.org