comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/StaticWKmsArtifact.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 77cdc344ef77
children 9d2a06c3a134
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 org.apache.log4j.Logger;
7
8 import org.w3c.dom.Document;
9
10 import java.awt.geom.Point2D;
11
12 import de.intevation.artifactdatabase.state.Facet;
13 import de.intevation.artifactdatabase.state.DefaultOutput;
14 import de.intevation.artifactdatabase.state.State;
15
16 import de.intevation.artifacts.Artifact;
17 import de.intevation.artifacts.ArtifactFactory;
18 import de.intevation.artifacts.ArtifactNamespaceContext;
19 import de.intevation.artifacts.CallMeta;
20
21 import de.intevation.flys.artifacts.model.CrossSectionWaterLineFacet;
22 import de.intevation.flys.artifacts.model.FacetTypes;
23 import de.intevation.flys.artifacts.model.WKms;
24 import de.intevation.flys.artifacts.model.WKmsFacet;
25 import de.intevation.flys.artifacts.model.WKmsFactory;
26
27 import de.intevation.flys.artifacts.states.StaticState;
28 import de.intevation.flys.artifacts.resources.Resources;
29
30 import de.intevation.artifacts.common.utils.XMLUtils;
31
32 import de.intevation.flys.geom.Lines;
33
34 import de.intevation.flys.model.FastCrossSectionLine;
35
36 /**
37 * Artifact to access additional "waterlevel"-type of data, like the height
38 * of protective measures (dikes).
39 *
40 * This artifact neglects (Static)FLYSArtifacts capabilities of interaction
41 * with the StateEngine by overriding the getState*-methods.
42 */
43 public class StaticWKmsArtifact
44 extends StaticFLYSArtifact
45 implements FacetTypes, WaterLineArtifact
46 {
47 /** The logger for this class. */
48 private static Logger logger =
49 Logger.getLogger(StaticWKmsArtifact.class);
50
51 /** XPath to access initial parameter. */
52 public static final String XPATH_DATA =
53 "/art:action/art:ids/@value";
54
55 public static final String STATIC_STATE_NAME =
56 "state.additional_wkms.static";
57
58 /** One and only state to be in. */
59 protected transient State state = null;
60
61
62 /**
63 * Trivial Constructor.
64 */
65 public StaticWKmsArtifact() {
66 logger.debug("StaticWKmsArtifact.StaticWKmsArtifact");
67 }
68
69
70 /**
71 * Gets called from factory, to set things up.
72 */
73 @Override
74 public void setup(
75 String identifier,
76 ArtifactFactory factory,
77 Object context,
78 CallMeta callMeta,
79 Document data)
80 {
81 logger.debug("StaticWKmsArtifact.setup");
82
83 state = new StaticState(STATIC_STATE_NAME);
84
85 List<Facet> fs = new ArrayList<Facet>();
86 logger.debug(XMLUtils.toString(data));
87 String code = XMLUtils.xpathString(
88 data, XPATH_DATA, ArtifactNamespaceContext.INSTANCE);
89
90 // TODO Go for JSON, one day.
91 //ex.: flood_protection-wstv-114-12
92 if (code != null) {
93 String [] parts = code.split("-");
94
95 if (parts.length >= 4) {
96 int col = -1;
97 int wst = Integer.valueOf(parts[3]);
98
99 if (!parts[2].equals("A")) {
100 col = Integer.valueOf(parts[2]);
101 }
102
103 addStringData("col_pos", parts[2]);
104 addStringData("wst_id", parts[3]);
105
106 String wkmsName;
107 if (col >= 0) {
108 wkmsName = WKmsFactory.getWKmsName(col, wst);
109 }
110 else {
111 wkmsName = WKmsFactory.getWKmsName(wst);
112 }
113
114 String name;
115 if (parts[0].equals(HEIGHTMARKS_POINTS)) {
116 name = HEIGHTMARKS_POINTS;
117 }
118 else {
119 name = STATIC_WKMS;
120 }
121
122 String facetDescription = Resources.getMsg(
123 callMeta, wkmsName, wkmsName);
124 Facet wKmsFacet = new WKmsFacet(
125 name,
126 facetDescription);
127 Facet csFacet = new CrossSectionWaterLineFacet(0,
128 facetDescription);
129 fs.add(wKmsFacet);
130 fs.add(csFacet);
131 facets.put(state.getID(), fs);
132 }
133 }
134
135 spawnState();
136 super.setup(identifier, factory, context, callMeta, data);
137 }
138
139
140 /**
141 * Initialize the static state with output.
142 * @return static state
143 */
144 protected State spawnState() {
145 state = new StaticState(STATIC_STATE_NAME);
146 List<Facet> fs = facets.get(STATIC_STATE_NAME);
147 DefaultOutput output = new DefaultOutput(
148 "general",
149 "general", "image/png",
150 fs,
151 "chart");
152
153 state.getOutputs().add(output);
154 return state;
155 }
156
157
158 /**
159 * Called via setup.
160 *
161 * @param artifact The master-artifact.
162 */
163 @Override
164 protected void initialize(
165 Artifact artifact,
166 Object context,
167 CallMeta meta)
168 {
169 logger.debug("StaticWKmsArtifact.initialize");
170 FLYSArtifact winfo = (FLYSArtifact) artifact;
171 // TODO: The river is of no interest, so far.
172 addData("river", winfo.getData("river"));
173 }
174
175
176 /**
177 * Get a list containing the one and only State.
178 * @param context ignored.
179 * @return list with one and only state.
180 */
181 @Override
182 protected List<State> getStates(Object context) {
183 ArrayList<State> states = new ArrayList<State>();
184 states.add(getState());
185 return states;
186 }
187
188
189 /**
190 * Get the "current" state (there is but one).
191 * @param cc ignored.
192 * @return the "current" (only possible) state.
193 */
194 @Override
195 public State getCurrentState(Object cc) {
196 return getState();
197 }
198
199
200 /**
201 * Get the only possible state.
202 * @return the state.
203 */
204 protected State getState() {
205 return getState(null, null);
206 }
207
208
209 /**
210 * Get the state.
211 * @param context ignored.
212 * @param stateID ignored.
213 * @return the state.
214 */
215 @Override
216 protected State getState(Object context, String stateID) {
217 return (state != null)
218 ? state
219 : spawnState();
220 }
221
222
223 /**
224 * Get WKms from factory.
225 * @param TODO idx param is not needed
226 * @return WKms according to parameterization (can be null);
227 */
228 public WKms getWKms(int idx) {
229 logger.debug("StaticWKmsArtifact.getWKms");
230
231 return WKmsFactory.getWKms(
232 Integer.valueOf(getDataAsString("col_pos")),
233 Integer.valueOf(getDataAsString("wst_id")));
234 }
235
236
237 /**
238 * Returns W at Km of WKms, searching linearly.
239 * Returns -1 if not found.
240 */
241 public double getWAtKm(WKms wkms, double km) {
242 // Uninformed search.
243 int size = wkms.size();
244 for (int i = 0; i < size; i++) {
245 if (wkms.getKm(i) == km) {
246 return wkms.getW(i);
247 }
248 }
249
250 return -1;
251 }
252
253
254 /**
255 * Get points of line describing the surface of water at cross section.
256 *
257 * @return an array holding coordinates of points of surface of water (
258 * in the form {{x1, x2} {y1, y2}} ).
259 */
260 @Override
261 public double [][] getWaterLines(int idx, FastCrossSectionLine csl) {
262 logger.debug("getWaterLines(" + idx + ")");
263
264 List<Point2D> points = csl.getPoints();
265
266 WKms wkms = getWKms(0);
267
268 double km = csl.getKm();
269
270 // Find W at km.
271 double wAtKm = getWAtKm(wkms, km);
272 if (wAtKm == -1) {
273 logger.warn("Waterlevel at km " + km + " unknown.");
274 return new double[][] {{}};
275 }
276
277 return Lines.createWaterLines(points, wAtKm);
278 }
279
280
281 /**
282 * Determines Facets initial disposition regarding activity (think of
283 * selection in Client ThemeList GUI). This will be checked one time
284 * when the facet enters a collections describe document.
285 *
286 * @param facetName name of the facet.
287 * @param index index of the facet.
288 *
289 * @return Always 0. Static Data will enter plots inactive.
290 */
291 @Override
292 public int getInitialFacetActivity(
293 String outputName,
294 String facetName,
295 int index)
296 {
297 return 0;
298 }
299 }
300 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org