Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSDBArtifact.java @ 3318:dbe2f85bf160
merged flys-artifacts/2.8
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:35 +0200 |
parents | 453d2d0c4258 |
children | b0ba96bbf01d |
comparison
equal
deleted
inserted
replaced
2987:98c7a46ec5ae | 3318:dbe2f85bf160 |
---|---|
1 package de.intevation.flys.artifacts; | |
2 | |
3 import java.io.File; | |
4 | |
5 import java.util.ArrayList; | |
6 import java.util.List; | |
7 | |
8 import java.util.regex.Pattern; | |
9 import java.util.regex.Matcher; | |
10 | |
11 import org.w3c.dom.Document; | |
12 | |
13 import org.apache.log4j.Logger; | |
14 | |
15 import org.hibernate.impl.SessionFactoryImpl; | |
16 | |
17 import com.vividsolutions.jts.geom.Envelope; | |
18 | |
19 import de.intevation.artifacts.Artifact; | |
20 import de.intevation.artifacts.ArtifactFactory; | |
21 import de.intevation.artifacts.CallMeta; | |
22 | |
23 import de.intevation.artifacts.common.utils.FileTools; | |
24 | |
25 import de.intevation.artifactdatabase.data.DefaultStateData; | |
26 import de.intevation.artifactdatabase.state.Facet; | |
27 import de.intevation.artifactdatabase.state.State; | |
28 | |
29 | |
30 import de.intevation.flys.backend.SessionFactoryProvider; | |
31 | |
32 import de.intevation.flys.artifacts.resources.Resources; | |
33 import de.intevation.flys.artifacts.states.DefaultState; | |
34 import de.intevation.flys.artifacts.model.map.WMSDBLayerFacet; | |
35 import de.intevation.flys.utils.FLYSUtils; | |
36 import de.intevation.flys.utils.MapfileGenerator; | |
37 | |
38 | |
39 public abstract class WMSDBArtifact extends StaticFLYSArtifact { | |
40 | |
41 private static final Logger logger = Logger.getLogger(WMSDBArtifact.class); | |
42 | |
43 public static final Pattern DB_URL_PATTERN = | |
44 Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z]+)"); | |
45 | |
46 | |
47 @Override | |
48 public void setup( | |
49 String identifier, | |
50 ArtifactFactory factory, | |
51 Object context, | |
52 CallMeta callMeta, | |
53 Document data) | |
54 { | |
55 logger.debug("WMSDBArtifact.setup"); | |
56 | |
57 super.setup(identifier, factory, context, callMeta, data); | |
58 | |
59 String ids = getDatacageIDValue(data); | |
60 | |
61 if (ids != null && ids.length() > 0) { | |
62 addData("ids", new DefaultStateData("ids", null, null, ids)); | |
63 } | |
64 else { | |
65 throw new IllegalArgumentException("No attribute 'ids' found!"); | |
66 } | |
67 | |
68 List<Facet> fs = new ArrayList<Facet>(); | |
69 | |
70 WMSDBState state = (WMSDBState) getCurrentState(context); | |
71 state.computeInit(this, hash(), context, callMeta, fs); | |
72 | |
73 if (!fs.isEmpty()) { | |
74 facets.put(getCurrentStateId(), fs); | |
75 } | |
76 } | |
77 | |
78 | |
79 @Override | |
80 protected void initialize( | |
81 Artifact artifact, | |
82 Object context, | |
83 CallMeta callMeta) | |
84 { | |
85 // do nothing | |
86 } | |
87 | |
88 | |
89 @Override | |
90 protected State getState(Object context, String stateID) { | |
91 return getCurrentState(context); | |
92 } | |
93 | |
94 | |
95 /** | |
96 * Get a list containing the one and only State. | |
97 * @param context ignored. | |
98 * @return list with one and only state. | |
99 */ | |
100 @Override | |
101 protected List<State> getStates(Object context) { | |
102 ArrayList<State> states = new ArrayList<State>(); | |
103 states.add(getCurrentState(context)); | |
104 | |
105 return states; | |
106 } | |
107 | |
108 | |
109 | |
110 public static abstract class WMSDBState extends DefaultState { | |
111 private static final Logger logger = Logger.getLogger(WMSDBState.class); | |
112 | |
113 protected FLYSArtifact artifact; | |
114 | |
115 protected String name; | |
116 protected int riverId; | |
117 | |
118 | |
119 public WMSDBState() {} | |
120 | |
121 public WMSDBState(FLYSArtifact artifact) { | |
122 this.artifact = artifact; | |
123 this.name = null; | |
124 this.riverId = 0; | |
125 } | |
126 | |
127 @Override | |
128 public Object computeInit( | |
129 FLYSArtifact artifact, | |
130 String hash, | |
131 Object context, | |
132 CallMeta meta, | |
133 List<Facet> facets | |
134 ) { | |
135 logger.debug("WMSDBState.computeInit"); | |
136 | |
137 String type = getFacetType(); | |
138 | |
139 WMSDBLayerFacet facet = new WMSDBLayerFacet( | |
140 0, | |
141 type, | |
142 getTitle(meta), | |
143 ComputeType.INIT, | |
144 getID(), hash, | |
145 getUrl()); | |
146 | |
147 String name = type + "-" + artifact.identifier(); | |
148 | |
149 facet.addLayer(name); | |
150 facet.setExtent(getExtent()); | |
151 facet.setSrid(getSrid()); | |
152 facet.setData(getDataString()); | |
153 facet.setFilter(getFilter()); | |
154 facet.setGeometryType(getGeometryType()); | |
155 facet.setConnection(getConnection()); | |
156 facet.setConnectionType(getConnectionType()); | |
157 facet.setLabelItem(getLabelItem()); | |
158 | |
159 facets.add(facet); | |
160 | |
161 return null; | |
162 } | |
163 | |
164 /** | |
165 * This method returns a connection string for databases used by | |
166 * Mapserver's Mapfile. | |
167 * | |
168 * @return A connection string for Mapserver. | |
169 */ | |
170 protected String getConnection() { | |
171 SessionFactoryImpl sf = (SessionFactoryImpl) | |
172 SessionFactoryProvider.getSessionFactory(); | |
173 | |
174 String user = SessionFactoryProvider.getUser(sf); | |
175 String pass = SessionFactoryProvider.getPass(sf); | |
176 String url = SessionFactoryProvider.getURL(sf); | |
177 | |
178 logger.debug("Parse connection url: " + url); | |
179 | |
180 Matcher m = DB_URL_PATTERN.matcher(url); | |
181 if (!m.matches()) { | |
182 logger.warn("Could not parse Connection string."); | |
183 return null; | |
184 } | |
185 | |
186 logger.debug("Groups for connection string: " + m.groupCount()); | |
187 int groups = m.groupCount(); | |
188 | |
189 for (int i = 0; i <= groups; i++) { | |
190 logger.debug("Group " + i + ": " + m.group(i)); | |
191 } | |
192 | |
193 String connection = null; | |
194 | |
195 if (FLYSUtils.isUsingOracle()) { | |
196 if (groups < 3) { | |
197 logger.warn("Could only partially parse connection string."); | |
198 return null; | |
199 } | |
200 | |
201 String host = m.group(2); | |
202 String port = m.group(3); | |
203 | |
204 connection = user + "/" + pass + "@" + host; | |
205 } | |
206 else { | |
207 if (groups < 4) { | |
208 logger.warn("Could only partially parse connection string."); | |
209 return null; | |
210 } | |
211 | |
212 String host = m.group(2); | |
213 String port = m.group(3); | |
214 String db = m.group(4); | |
215 | |
216 StringBuilder sb = new StringBuilder(); | |
217 sb.append("dbname=" + db); | |
218 sb.append("host='" + host + "'"); | |
219 sb.append("port=" + port); | |
220 sb.append("password='" + pass + "'"); | |
221 sb.append("sslmode=disable"); | |
222 | |
223 connection = sb.toString(); | |
224 } | |
225 | |
226 logger.debug("Created connection: '" + connection + "'"); | |
227 | |
228 return connection; | |
229 } | |
230 | |
231 protected String getConnectionType() { | |
232 return FLYSUtils.isUsingOracle() ? "oraclespatial" : "postgis"; | |
233 } | |
234 | |
235 protected String getLabelItem() { | |
236 return null; | |
237 } | |
238 | |
239 public int getRiverId() { | |
240 if (riverId == 0) { | |
241 String ids = artifact.getDataAsString("ids"); | |
242 String[] parts = ids.split(";"); | |
243 | |
244 try { | |
245 riverId = Integer.valueOf(parts[0]); | |
246 } | |
247 catch (NumberFormatException nfe) { | |
248 logger.error("Cannot parse river id from '" + parts[0] + "'"); | |
249 } | |
250 } | |
251 | |
252 return riverId; | |
253 } | |
254 | |
255 /** | |
256 * Returns the name of the WMS layer. This method extracts the name | |
257 * from 'ids' data string. It is expected, that the 'ids' string is | |
258 * seperated by ';' and that the name is placed at index 1. | |
259 * | |
260 * @return the name of the WMS layer. | |
261 */ | |
262 public String getName() { | |
263 if (name == null) { | |
264 String ids = artifact.getDataAsString("ids"); | |
265 | |
266 String parts[] = ids != null ? ids.split(";") : null; | |
267 | |
268 if (parts != null && parts.length >= 2) { | |
269 name = parts[1]; | |
270 } | |
271 } | |
272 | |
273 return name; | |
274 } | |
275 | |
276 | |
277 /** | |
278 * Returns the name of the layer (returned by getName()) or the layer | |
279 * type if the name is empty. The layer type is created by an i18n | |
280 * string of getFacetType(). | |
281 * | |
282 * @param meta A CallMeta used for i18n. | |
283 * | |
284 * @return the name of the layer or its type if name is empty. | |
285 */ | |
286 protected String getTitle(CallMeta meta) { | |
287 String name = getName(); | |
288 | |
289 return name != null && name.length() > 0 | |
290 ? name | |
291 : Resources.getMsg( | |
292 meta, | |
293 getFacetType(), | |
294 getFacetType()); | |
295 } | |
296 | |
297 | |
298 @Override | |
299 public void endOfLife(Artifact owner, Object context) { | |
300 logger.info("Destroy WMSDBState: " + getID()); | |
301 | |
302 String p = FLYSUtils.getXPathString(FLYSUtils.XPATH_SHAPEFILE_DIR); | |
303 File dir = new File(p, owner.identifier()); | |
304 | |
305 if (dir != null && dir.exists()) { | |
306 logger.debug("Try to delete directory '" + dir + "'"); | |
307 | |
308 FileTools.deleteRecursive(dir); | |
309 MapfileGenerator.getInstance().update(); | |
310 } | |
311 } | |
312 | |
313 | |
314 protected abstract String getFacetType(); | |
315 | |
316 protected abstract String getUrl(); | |
317 | |
318 protected abstract String getSrid(); | |
319 | |
320 protected abstract Envelope getExtent(); | |
321 | |
322 protected abstract String getFilter(); | |
323 | |
324 protected abstract String getDataString(); | |
325 | |
326 protected abstract String getGeometryType(); | |
327 } // end of WMSDBState | |
328 } | |
329 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |