comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/WMSDBArtifact.java @ 3812:f788d2d901d6

merged flys-artifacts/pre2.6-2011-12-05
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:53 +0200
parents 71139016cd0f
children 007a8f5ed9f1
comparison
equal deleted inserted replaced
3808:5fab0fe3c445 3812:f788d2d901d6
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 WMSDBArtifact artifact;
107
108 public WMSDBState(WMSDBArtifact artifact) {
109 this.artifact = artifact;
110 }
111
112 @Override
113 public Object computeInit(
114 FLYSArtifact artifact,
115 String hash,
116 Object context,
117 CallMeta meta,
118 List<Facet> facets
119 ) {
120 logger.debug("WMSDBState.computeInit");
121
122 String type = getFacetType();
123
124 WMSDBLayerFacet facet = new WMSDBLayerFacet(
125 0,
126 type,
127 getTitle(meta),
128 ComputeType.INIT,
129 getID(), hash,
130 getUrl());
131
132 String name = type + "-" + artifact.identifier();
133
134 facet.addLayer(name);
135 facet.setExtent(getExtent());
136 facet.setSrid(getSrid());
137 facet.setData(getDataString());
138 facet.setFilter(getFilter());
139 facet.setGeometryType(getGeometryType());
140 facet.setConnection(getConnection());
141 facet.setConnectionType(getConnectionType());
142 facet.setLabelItem(getLabelItem());
143
144 facets.add(facet);
145
146 return null;
147 }
148
149 /**
150 * This method returns a connection string for databases used by
151 * Mapserver's Mapfile.
152 *
153 * @return A connection string for Mapserver.
154 */
155 protected String getConnection() {
156 SessionFactoryImpl sf = (SessionFactoryImpl)
157 SessionFactoryProvider.getSessionFactory();
158
159 String user = SessionFactoryProvider.getUser(sf);
160 String pass = SessionFactoryProvider.getPass(sf);
161 String url = SessionFactoryProvider.getURL(sf);
162
163 logger.debug("Parse connection url: " + url);
164
165 Matcher m = DB_URL_PATTERN.matcher(url);
166 if (!m.matches()) {
167 logger.warn("Could not parse Connection string.");
168 return null;
169 }
170
171 logger.debug("Groups for connection string: " + m.groupCount());
172 int groups = m.groupCount();
173
174 for (int i = 0; i <= m.groupCount(); i++) {
175 logger.debug("Group " + i + ": " + m.group(i));
176 }
177
178 String connection = null;
179
180 if (FLYSUtils.isUsingOracle()) {
181 if (groups < 3) {
182 logger.warn("Could only partially parse connection string.");
183 return null;
184 }
185
186 String host = m.group(2);
187 String port = m.group(3);
188
189 connection = user + "/" + pass + "@" + host;
190 }
191 else {
192 if (groups < 4) {
193 logger.warn("Could only partially parse connection string.");
194 return null;
195 }
196
197 String host = m.group(2);
198 String port = m.group(3);
199 String db = m.group(4);
200
201 StringBuilder sb = new StringBuilder();
202 sb.append("dbname=" + db);
203 sb.append("host='" + host + "'");
204 sb.append("port=" + port);
205 sb.append("password='" + pass + "'");
206 sb.append("sslmode=disable");
207
208 connection = sb.toString();
209 }
210
211 logger.debug("Created connection: '" + connection + "'");
212
213 return connection;
214 }
215
216 protected String getConnectionType() {
217 return FLYSUtils.isUsingOracle() ? "oraclespatial" : "postgis";
218 }
219
220 protected String getLabelItem() {
221 return null;
222 }
223
224 protected abstract String getFacetType();
225
226 protected abstract String getTitle(CallMeta meta);
227
228 protected abstract String getUrl();
229
230 protected abstract String getSrid();
231
232 protected abstract Envelope getExtent();
233
234 protected abstract String getFilter();
235
236 protected abstract String getDataString();
237
238 protected abstract String getGeometryType();
239 } // end of WMSDBState
240 }
241 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org