comparison artifacts/src/main/java/org/dive4elements/river/artifacts/WMSDBArtifact.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/artifacts/WMSDBArtifact.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.artifacts;
2
3 import com.vividsolutions.jts.geom.Envelope;
4
5 import org.dive4elements.artifactdatabase.data.DefaultStateData;
6 import org.dive4elements.artifactdatabase.state.Facet;
7 import org.dive4elements.artifactdatabase.state.State;
8 import org.dive4elements.artifacts.Artifact;
9 import org.dive4elements.artifacts.ArtifactFactory;
10 import org.dive4elements.artifacts.CallMeta;
11 import org.dive4elements.artifacts.common.utils.FileTools;
12 import org.dive4elements.river.artifacts.model.map.WMSDBLayerFacet;
13 import org.dive4elements.river.artifacts.resources.Resources;
14 import org.dive4elements.river.artifacts.states.DefaultState;
15 import org.dive4elements.river.utils.FLYSUtils;
16 import org.dive4elements.river.utils.MapUtils;
17
18 import java.io.File;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 import org.apache.log4j.Logger;
23 import org.w3c.dom.Document;
24
25
26 public abstract class WMSDBArtifact extends StaticFLYSArtifact {
27
28 private static final Logger logger = Logger.getLogger(WMSDBArtifact.class);
29
30 @Override
31 public void setup(
32 String identifier,
33 ArtifactFactory factory,
34 Object context,
35 CallMeta callMeta,
36 Document data)
37 {
38 logger.debug("WMSDBArtifact.setup");
39
40 super.setup(identifier, factory, context, callMeta, data);
41
42 String ids = getDatacageIDValue(data);
43
44 if (ids != null && ids.length() > 0) {
45 addData("ids", new DefaultStateData("ids", null, null, ids));
46 }
47 else {
48 throw new IllegalArgumentException("No attribute 'ids' found!");
49 }
50
51 List<Facet> fs = new ArrayList<Facet>();
52
53 WMSDBState state = (WMSDBState) getCurrentState(context);
54 state.computeInit(this, hash(), context, callMeta, fs);
55
56 if (!fs.isEmpty()) {
57 addFacets(getCurrentStateId(), fs);
58 }
59 }
60
61
62 @Override
63 protected void initialize(
64 Artifact artifact,
65 Object context,
66 CallMeta callMeta)
67 {
68 // do nothing
69 }
70
71
72 @Override
73 protected State getState(Object context, String stateID) {
74 return getCurrentState(context);
75 }
76
77
78 /**
79 * Get a list containing the one and only State.
80 * @param context ignored.
81 * @return list with one and only state.
82 */
83 @Override
84 protected List<State> getStates(Object context) {
85 ArrayList<State> states = new ArrayList<State>();
86 states.add(getCurrentState(context));
87
88 return states;
89 }
90
91
92
93 public static abstract class WMSDBState extends DefaultState {
94 private static final Logger logger = Logger.getLogger(WMSDBState.class);
95
96 protected FLYSArtifact artifact;
97
98 protected String name;
99 protected int riverId;
100
101
102 public WMSDBState() {}
103
104 public WMSDBState(FLYSArtifact artifact) {
105 this.artifact = artifact;
106 this.name = null;
107 this.riverId = 0;
108 }
109
110 @Override
111 public Object computeInit(
112 FLYSArtifact artifact,
113 String hash,
114 Object context,
115 CallMeta meta,
116 List<Facet> facets
117 ) {
118 logger.debug("WMSDBState.computeInit");
119
120 String type = getFacetType();
121
122 WMSDBLayerFacet facet = new WMSDBLayerFacet(
123 0,
124 type,
125 getTitle(meta),
126 ComputeType.INIT,
127 getID(), hash,
128 getUrl());
129
130 facet.addLayer(getLayer());
131 facet.setExtent(getExtent());
132 facet.setOriginalExtent(getExtent(true));
133 facet.setSrid(getSrid());
134 facet.setData(getDataString());
135 facet.setFilter(getFilter());
136 facet.setGeometryType(getGeometryType());
137 facet.setConnection(MapUtils.getConnection());
138 facet.setConnectionType(MapUtils.getConnectionType());
139 facet.setLabelItem(getLabelItem());
140
141 facets.add(facet);
142
143 return null;
144 }
145
146 protected String getLabelItem() {
147 return null;
148 }
149
150 public int getRiverId() {
151 if (riverId == 0) {
152 String rid = getIdPart(0);
153
154 try {
155 riverId = Integer.parseInt(rid);
156 }
157 catch (NumberFormatException nfe) {
158 logger.error("Cannot parse river id from '" +
159 artifact.getDataAsString("ids") + "'");
160 }
161 }
162 return riverId;
163 }
164
165 protected String getLayer() {
166 String type = getFacetType();
167 String name = type + "-" + artifact.identifier();
168 return name;
169 }
170
171 /**
172 * Returns the name of the WMS layer. This method extracts the name
173 * from 'ids' data string. It is expected, that the 'ids' string is
174 * seperated by ';' and that the name is placed at index 1.
175 *
176 * @return the name of the WMS layer.
177 */
178 public String getName() {
179 if (name == null) {
180 name = getIdPart(1);
181 }
182
183 return name;
184 }
185
186 /**
187 * Returns a part of the ID string. This method splits the
188 * 'ids' data string. It is expected, that the 'ids' string is
189 * seperated by ';'.
190 *
191 * @param number the position of the id data string
192 *
193 * @return the part of the id string at position number.
194 * Null if number was out of bounds.
195 */
196 public String getIdPart(int number) {
197 String ids = artifact.getDataAsString("ids");
198
199 String parts[] = ids != null ? ids.split(";") : null;
200
201 if (parts != null && parts.length >= number + 1) {
202 return parts[number];
203 }
204 return null;
205 }
206
207
208 /**
209 * Returns the name of the layer (returned by getName()) or the layer
210 * type if the name is empty. The layer type is created by an i18n
211 * string of getFacetType().
212 *
213 * @param meta A CallMeta used for i18n.
214 *
215 * @return the name of the layer or its type if name is empty.
216 */
217 protected String getTitle(CallMeta meta) {
218 String name = getName();
219
220 return name != null && name.length() > 0
221 ? name
222 : Resources.getMsg(
223 meta,
224 getFacetType(),
225 getFacetType());
226 }
227
228
229 @Override
230 public void endOfLife(Artifact owner, Object context) {
231 logger.info("Destroy WMSDBState: " + getID());
232
233 String p = FLYSUtils.getXPathString(FLYSUtils.XPATH_FLOODMAP_SHAPEFILE_DIR);
234 File dir = new File(p, owner.identifier());
235
236 if (dir != null && dir.exists()) {
237 logger.debug("Try to delete directory '" + dir + "'");
238
239 FileTools.deleteRecursive(dir);
240 }
241 }
242
243 /**
244 * This method returns the extent of a DB layer in the projection of the
245 * database.
246 *
247 * @return the extent of the DB layer in the projection of the database.
248 */
249 protected Envelope getExtent() {
250 return getExtent(false);
251 }
252
253
254 protected abstract String getFacetType();
255
256 protected abstract String getUrl();
257
258 protected abstract String getSrid();
259
260 /**
261 * This method returns the extent of the DB layer. The projection of the
262 * extent depends on the <i>reproject</i> parameter. If reproject is set,
263 * the extent is reprojected into the original projection which is
264 * specified in the configuration. Otherwise, the projection of the
265 * database is used.
266 *
267 * @param reproject True, to reproject the extent into the projection
268 * specified in the configuration.
269 *
270 * @return the extent of the database layer.
271 */
272 protected abstract Envelope getExtent(boolean reproject);
273
274 protected abstract String getFilter();
275
276 protected abstract String getDataString();
277
278 protected abstract String getGeometryType();
279 } // end of WMSDBState
280 }
281 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org