comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSDBArtifact.java @ 2793:6310b1582f2d

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

http://dive4elements.wald.intevation.org