comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSDBArtifact.java @ 3818:dc18457b1cef

merged flys-artifacts/pre2.7-2012-03-16
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:59 +0200
parents 0da8874bd378
children 5afccab9aac1
comparison
equal deleted inserted replaced
2456:60ab1054069d 3818:dc18457b1cef
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 import de.intevation.artifacts.common.ArtifactNamespaceContext;
26 import de.intevation.artifacts.common.utils.XMLUtils;
27
28 import de.intevation.flys.backend.SessionFactoryProvider;
29
30 import de.intevation.flys.artifacts.states.DefaultState;
31 import de.intevation.flys.artifacts.model.WMSDBLayerFacet;
32 import de.intevation.flys.utils.FLYSUtils;
33
34
35 public abstract class WMSDBArtifact extends StaticFLYSArtifact {
36
37 private static final Logger logger = Logger.getLogger(WMSDBArtifact.class);
38
39 public static final String XPATH_IDS = "/art:action/art:ids/@value";
40
41 public static final Pattern DB_URL_PATTERN =
42 Pattern.compile("(.*)\\/\\/(.*):([0-9]+)\\/([a-zA-Z]+)");
43
44
45 @Override
46 public void setup(
47 String identifier,
48 ArtifactFactory factory,
49 Object context,
50 CallMeta callMeta,
51 Document data)
52 {
53 logger.debug("WMSDBArtifact.setup");
54
55 super.setup(identifier, factory, context, callMeta, data);
56
57 String ids = XMLUtils.xpathString(
58 data, XPATH_IDS, ArtifactNamespaceContext.INSTANCE);
59
60 if (ids != null && ids.length() > 0) {
61 addData("ids", new DefaultStateData("ids", null, null, ids));
62 }
63 else {
64 throw new IllegalArgumentException("No attribute 'ids' found!");
65 }
66
67 List<Facet> fs = new ArrayList<Facet>();
68
69 WMSDBState state = (WMSDBState) getCurrentState(context);
70 state.computeInit(this, hash(), context, callMeta, fs);
71
72 if (!fs.isEmpty()) {
73 facets.put(getCurrentStateId(), fs);
74 }
75 }
76
77
78 @Override
79 protected void initialize(
80 Artifact artifact,
81 Object context,
82 CallMeta callMeta)
83 {
84 // do nothing
85 }
86
87
88 /**
89 * Get a list containing the one and only State.
90 * @param context ignored.
91 * @return list with one and only state.
92 */
93 @Override
94 protected List<State> getStates(Object context) {
95 ArrayList<State> states = new ArrayList<State>();
96 states.add(getCurrentState(context));
97
98 return states;
99 }
100
101
102
103 public static abstract class WMSDBState extends DefaultState {
104 private static final Logger logger = Logger.getLogger(WMSDBState.class);
105
106 protected FLYSArtifact artifact;
107
108 public WMSDBState() {}
109
110 public WMSDBState(FLYSArtifact artifact) {
111 this.artifact = artifact;
112 }
113
114 @Override
115 public Object computeInit(
116 FLYSArtifact artifact,
117 String hash,
118 Object context,
119 CallMeta meta,
120 List<Facet> facets
121 ) {
122 logger.debug("WMSDBState.computeInit");
123
124 String type = getFacetType();
125
126 WMSDBLayerFacet facet = new WMSDBLayerFacet(
127 0,
128 type,
129 getTitle(meta),
130 ComputeType.INIT,
131 getID(), hash,
132 getUrl());
133
134 String name = type + "-" + artifact.identifier();
135
136 facet.addLayer(name);
137 facet.setExtent(getExtent());
138 facet.setSrid(getSrid());
139 facet.setData(getDataString());
140 facet.setFilter(getFilter());
141 facet.setGeometryType(getGeometryType());
142 facet.setConnection(getConnection());
143 facet.setConnectionType(getConnectionType());
144 facet.setLabelItem(getLabelItem());
145
146 facets.add(facet);
147
148 return null;
149 }
150
151 /**
152 * This method returns a connection string for databases used by
153 * Mapserver's Mapfile.
154 *
155 * @return A connection string for Mapserver.
156 */
157 protected String getConnection() {
158 SessionFactoryImpl sf = (SessionFactoryImpl)
159 SessionFactoryProvider.getSessionFactory();
160
161 String user = SessionFactoryProvider.getUser(sf);
162 String pass = SessionFactoryProvider.getPass(sf);
163 String url = SessionFactoryProvider.getURL(sf);
164
165 logger.debug("Parse connection url: " + url);
166
167 Matcher m = DB_URL_PATTERN.matcher(url);
168 if (!m.matches()) {
169 logger.warn("Could not parse Connection string.");
170 return null;
171 }
172
173 logger.debug("Groups for connection string: " + m.groupCount());
174 int groups = m.groupCount();
175
176 for (int i = 0; i <= m.groupCount(); i++) {
177 logger.debug("Group " + i + ": " + m.group(i));
178 }
179
180 String connection = null;
181
182 if (FLYSUtils.isUsingOracle()) {
183 if (groups < 3) {
184 logger.warn("Could only partially parse connection string.");
185 return null;
186 }
187
188 String host = m.group(2);
189 String port = m.group(3);
190
191 connection = user + "/" + pass + "@" + host;
192 }
193 else {
194 if (groups < 4) {
195 logger.warn("Could only partially parse connection string.");
196 return null;
197 }
198
199 String host = m.group(2);
200 String port = m.group(3);
201 String db = m.group(4);
202
203 StringBuilder sb = new StringBuilder();
204 sb.append("dbname=" + db);
205 sb.append("host='" + host + "'");
206 sb.append("port=" + port);
207 sb.append("password='" + pass + "'");
208 sb.append("sslmode=disable");
209
210 connection = sb.toString();
211 }
212
213 logger.debug("Created connection: '" + connection + "'");
214
215 return connection;
216 }
217
218 protected String getConnectionType() {
219 return FLYSUtils.isUsingOracle() ? "oraclespatial" : "postgis";
220 }
221
222 protected String getLabelItem() {
223 return null;
224 }
225
226 protected abstract String getFacetType();
227
228 protected abstract String getTitle(CallMeta meta);
229
230 protected abstract String getUrl();
231
232 protected abstract String getSrid();
233
234 protected abstract Envelope getExtent();
235
236 protected abstract String getFilter();
237
238 protected abstract String getDataString();
239
240 protected abstract String getGeometryType();
241 } // end of WMSDBState
242 }
243 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org