comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/LocationDistanceSelect.java @ 921:610d0e0f4f85

#159 Modifications in the transition model to support a state with a kilometer range input only. flys-artifacts/trunk@2270 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Thu, 30 Jun 2011 11:32:17 +0000
parents c09c9e05ecfa
children eccf966fb677
comparison
equal deleted inserted replaced
920:a618dd6d80ea 921:610d0e0f4f85
1 package de.intevation.flys.artifacts.states; 1 package de.intevation.flys.artifacts.states;
2 2
3 import java.util.List;
4
5 import org.apache.log4j.Logger; 3 import org.apache.log4j.Logger;
6
7 import org.w3c.dom.Element;
8 4
9 import gnu.trove.TDoubleArrayList; 5 import gnu.trove.TDoubleArrayList;
10 6
11 import de.intevation.artifacts.Artifact; 7 import de.intevation.artifacts.Artifact;
12 import de.intevation.artifacts.CallContext; 8 import de.intevation.artifacts.CallContext;
13 9
14 import de.intevation.artifacts.common.utils.XMLUtils;
15
16 import de.intevation.artifactdatabase.ProtocolUtils;
17 import de.intevation.artifactdatabase.data.StateData; 10 import de.intevation.artifactdatabase.data.StateData;
18 import de.intevation.artifactdatabase.state.Facet;
19
20 import de.intevation.flys.model.River;
21 11
22 import de.intevation.flys.artifacts.FLYSArtifact; 12 import de.intevation.flys.artifacts.FLYSArtifact;
23 import de.intevation.flys.artifacts.WINFOArtifact;
24 13
25 import de.intevation.flys.artifacts.model.RiverFactory;
26 import de.intevation.flys.artifacts.model.WQKms;
27 import de.intevation.flys.artifacts.model.CalculationResult;
28 import de.intevation.flys.artifacts.model.WaterlevelFacet;
29 import de.intevation.flys.artifacts.model.FacetTypes;
30
31 import de.intevation.flys.artifacts.resources.Resources;
32 14
33 /** 15 /**
34 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> 16 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
35 */ 17 */
36 public class LocationDistanceSelect 18 public class LocationDistanceSelect
37 extends RangeState 19 extends ComputationRangeState
38 implements FacetTypes
39 { 20 {
40 21
41 /** The logger used in this class.*/ 22 /** The logger used in this class.*/
42 private static Logger logger = Logger.getLogger(LocationDistanceSelect.class); 23 private static Logger logger = Logger.getLogger(LocationDistanceSelect.class);
43 24
44
45 /** The default step width.*/
46 public static final String DEFAULT_STEP = "100";
47 25
48 /** The name of the 'mode' field. */ 26 /** The name of the 'mode' field. */
49 public static final String MODE = "ld_mode"; 27 public static final String MODE = "ld_mode";
50 28
51 /** The name of the 'locations' field.*/ 29 /** The name of the 'locations' field.*/
52 public static final String LOCATIONS = "ld_locations"; 30 public static final String LOCATIONS = "ld_locations";
53 31
54 /** The name of the 'from' field. */
55 public static final String FROM = "ld_from";
56
57 /** The name of the 'to' field. */
58 public static final String TO = "ld_to";
59
60 /** The name of the 'step' field. */
61 public static final String STEP = "ld_step";
62 32
63 /** 33 /**
64 * The default constructor that initializes an empty State object. 34 * The default constructor that initializes an empty State object.
65 */ 35 */
66 public LocationDistanceSelect() { 36 public LocationDistanceSelect() {
67 } 37 }
68 38
69 protected Element createData(
70 XMLUtils.ElementCreator cr,
71 Artifact artifact,
72 StateData data,
73 CallContext context)
74 {
75 Element select = ProtocolUtils.createArtNode(
76 cr, "select", null, null);
77
78 cr.addAttr(select, "name", data.getName(), true);
79
80 Element label = ProtocolUtils.createArtNode(
81 cr, "label", null, null);
82
83 Element choices = ProtocolUtils.createArtNode(
84 cr, "choices", null, null);
85
86 label.setTextContent(Resources.getMsg(
87 context.getMeta(),
88 data.getName(),
89 data.getName()));
90
91 select.appendChild(label);
92
93 return select;
94 }
95
96
97 @Override
98 protected Element[] createItems(
99 XMLUtils.ElementCreator cr,
100 Artifact artifact,
101 String name,
102 CallContext context)
103 {
104 double[] minmax = getMinMaxDistance(artifact);
105
106 double minVal = Double.MIN_VALUE;
107 double maxVal = Double.MAX_VALUE;
108
109 if (minmax != null) {
110 minVal = minmax[0];
111 maxVal = minmax[1];
112 }
113 else {
114 logger.warn("Could not read min/max distance values!");
115 }
116
117 if (name.equals("ld_from")) {
118 Element min = createItem(
119 cr,
120 new String[] {"min", new Double(minVal).toString()});
121
122 return new Element[] { min };
123 }
124 else if (name.equals("ld_to")) {
125 Element max = createItem(
126 cr,
127 new String[] {"max", new Double(maxVal).toString()});
128
129 return new Element[] { max };
130 }
131 else {
132 Element step = createItem(cr, new String[] {"step", DEFAULT_STEP});
133 return new Element[] { step };
134 }
135
136 }
137
138
139 protected Element createItem(XMLUtils.ElementCreator cr, Object obj) {
140 Element item = ProtocolUtils.createArtNode(cr, "item", null, null);
141 Element label = ProtocolUtils.createArtNode(cr, "label", null, null);
142 Element value = ProtocolUtils.createArtNode(cr, "value", null, null);
143
144 String[] arr = (String[]) obj;
145
146 label.setTextContent(arr[0]);
147 value.setTextContent(arr[1]);
148
149 item.appendChild(label);
150 item.appendChild(value);
151
152 return item;
153 }
154
155 39
156 @Override 40 @Override
157 protected String getUIProvider() { 41 protected String getUIProvider() {
158 return "location_distance_panel"; 42 return "location_distance_panel";
159 }
160
161
162 protected double[] getMinMaxDistance(Artifact artifact) {
163 FLYSArtifact flysArtifact = (FLYSArtifact) artifact;
164 StateData data = getData(flysArtifact, "river");
165
166 String name = data != null ? (String) data.getValue() : "";
167
168 logger.debug("Search for the min/max distances of '" + name + "'");
169
170 River river = RiverFactory.getRiver(name);
171
172 return river != null ? river.determineMinMaxDistance() : null;
173 } 43 }
174 44
175 45
176 @Override 46 @Override
177 public boolean validate(Artifact artifact, CallContext context) 47 public boolean validate(Artifact artifact, CallContext context)
180 logger.debug("LocationDistanceSelect.validate"); 50 logger.debug("LocationDistanceSelect.validate");
181 51
182 FLYSArtifact flys = (FLYSArtifact) artifact; 52 FLYSArtifact flys = (FLYSArtifact) artifact;
183 53
184 if (flys.isRange()) { 54 if (flys.isRange()) {
185 return validateRange(flys, context); 55 return super.validate(flys, context);
186 } 56 }
187 else { 57 else {
188 return validateLocations(flys, context); 58 return validateLocations(flys, context);
189 } 59 }
190 } 60 }
198 68
199 if (values == null || values.length() == 0) { 69 if (values == null || values.length() == 0) {
200 throw new IllegalArgumentException("error_empty_state"); 70 throw new IllegalArgumentException("error_empty_state");
201 } 71 }
202 72
203 double[] absMinMax = getMinMaxDistance(flys); 73 double[] absMinMax = getMinMax(flys);
204 double[] relMinMax = getMinMaxFromString(values); 74 double[] relMinMax = getMinMaxFromString(values);
205 75
206 if (relMinMax[0] < absMinMax[0] || relMinMax[0] > absMinMax[1]) { 76 if (relMinMax[0] < absMinMax[0] || relMinMax[0] > absMinMax[1]) {
207 throw new IllegalArgumentException("error_feed_from_out_of_range"); 77 throw new IllegalArgumentException("error_feed_from_out_of_range");
208 } 78 }
210 if (relMinMax[1] > absMinMax[1] || relMinMax[1] < absMinMax[0]) { 80 if (relMinMax[1] > absMinMax[1] || relMinMax[1] < absMinMax[0]) {
211 throw new IllegalArgumentException("error_feed_to_out_of_range"); 81 throw new IllegalArgumentException("error_feed_to_out_of_range");
212 } 82 }
213 83
214 return true; 84 return true;
215 }
216
217
218 protected boolean validateRange(FLYSArtifact flys, CallContext context)
219 throws IllegalArgumentException
220 {
221 StateData dFrom = getData(flys, FROM);
222 StateData dTo = getData(flys, TO);
223 StateData dStep = getData(flys, STEP);
224
225 String fromStr = dFrom != null ? (String) dFrom.getValue() : null;
226 String toStr = dTo != null ? (String) dTo.getValue() : null;
227 String stepStr = dStep != null ? (String) dStep.getValue() : null;
228
229 if (fromStr == null || toStr == null || stepStr == null) {
230 throw new IllegalArgumentException("error_empty_state");
231 }
232
233 try {
234 double from = Double.parseDouble(fromStr);
235 double to = Double.parseDouble(toStr);
236 double step = Double.parseDouble(stepStr);
237
238 double[] minmaxDist = getMinMaxDistance(flys);
239
240 return validateBounds(minmaxDist[0], minmaxDist[1], from, to, step);
241 }
242 catch (NumberFormatException nfe) {
243 throw new IllegalArgumentException("error_invalid_double_value");
244 }
245 } 85 }
246 86
247 87
248 /** 88 /**
249 * Extracts the min/max values from String <i>s</i>. An 89 * Extracts the min/max values from String <i>s</i>. An
301 } 141 }
302 } 142 }
303 143
304 return values.toNativeArray(); 144 return values.toNativeArray();
305 } 145 }
306
307
308 @Override
309 public Object computeFeed(
310 FLYSArtifact artifact,
311 String hash,
312 CallContext context,
313 List<Facet> facets,
314 Object old
315 ) {
316 logger.debug("computeFeed");
317
318 WINFOArtifact winfo = (WINFOArtifact)artifact;
319
320 CalculationResult res = old instanceof CalculationResult
321 ? (CalculationResult)old
322 : winfo.getDischargeCurveData();
323
324 if (facets == null) {
325 logger.debug("generate no facets");
326 return res;
327 }
328
329 WQKms [] wqkms = (WQKms [])res.getData();
330
331 logger.debug("generate " + wqkms.length + " facets.");
332
333 String stateID = winfo.getCurrentStateId();
334
335 for (int i = 0; i < wqkms.length; ++i) {
336 String name = wqkms[i].getName();
337 facets.add(new WaterlevelFacet(
338 i, DISCHARGE_CURVE, name, ComputeType.FEED, stateID, hash));
339 }
340
341
342 return res;
343 }
344 } 146 }
345 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : 147 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org