comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/states/ComputationRangeState.java @ 2424:092e519ff461

merged flys-artifacts/2.6.1
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:26 +0200
parents 16598bd04f70
children 28c3f6588011
comparison
equal deleted inserted replaced
2392:8112ec686a9a 2424:092e519ff461
1 package de.intevation.flys.artifacts.states;
2
3 import java.util.Date;
4 import java.util.List;
5
6 import org.apache.log4j.Logger;
7
8 import org.w3c.dom.Element;
9
10 import de.intevation.artifacts.Artifact;
11 import de.intevation.artifacts.CallContext;
12
13 import de.intevation.artifacts.common.utils.XMLUtils;
14
15 import de.intevation.artifactdatabase.ProtocolUtils;
16 import de.intevation.artifactdatabase.data.StateData;
17 import de.intevation.artifactdatabase.state.Facet;
18
19 import de.intevation.flys.model.DischargeTable;
20 import de.intevation.flys.model.Gauge;
21 import de.intevation.flys.model.TimeInterval;
22
23 import de.intevation.flys.artifacts.FLYSArtifact;
24 import de.intevation.flys.artifacts.WINFOArtifact;
25 import de.intevation.flys.artifacts.ChartArtifact;
26
27 import de.intevation.flys.artifacts.model.CalculationResult;
28 import de.intevation.flys.artifacts.model.FacetTypes;
29 import de.intevation.flys.artifacts.model.GaugesFactory;
30 import de.intevation.flys.artifacts.model.WaterlevelFacet;
31 import de.intevation.flys.artifacts.model.WQKms;
32 import de.intevation.flys.artifacts.resources.Resources;
33
34 import de.intevation.flys.utils.FLYSUtils;
35
36
37 /**
38 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
39 */
40 public class ComputationRangeState
41 extends RangeState
42 implements FacetTypes
43 {
44 private static Logger logger =
45 Logger.getLogger(ComputationRangeState.class);
46
47 /** The name of the 'from' field. */
48 public static final String FROM = "ld_from";
49
50 /** The name of the 'to' field. */
51 public static final String TO = "ld_to";
52
53 /** The name of the 'step' field. */
54 public static final String STEP = "ld_step";
55
56 /** The default step width. */
57 public static final int DEFAULT_STEP = 100;
58
59
60 public ComputationRangeState() {
61 }
62
63
64 @Override
65 protected Element createData(
66 XMLUtils.ElementCreator cr,
67 Artifact artifact,
68 StateData data,
69 CallContext context)
70 {
71 Element select = ProtocolUtils.createArtNode(
72 cr, "select", null, null);
73
74 cr.addAttr(select, "name", data.getName(), true);
75
76 Element label = ProtocolUtils.createArtNode(
77 cr, "label", null, null);
78
79 Element choices = ProtocolUtils.createArtNode(
80 cr, "choices", null, null);
81
82 label.setTextContent(Resources.getMsg(
83 context.getMeta(),
84 data.getName(),
85 data.getName()));
86
87 select.appendChild(label);
88
89 return select;
90 }
91
92
93 @Override
94 protected Element[] createItems(
95 XMLUtils.ElementCreator cr,
96 Artifact artifact,
97 String name,
98 CallContext context)
99 {
100 double[] minmax = getMinMax(artifact);
101
102 double minVal = Double.MIN_VALUE;
103 double maxVal = Double.MAX_VALUE;
104
105 if (minmax != null) {
106 minVal = minmax[0];
107 maxVal = minmax[1];
108 }
109 else {
110 logger.warn("Could not read min/max distance values!");
111 }
112
113 if (name.equals("ld_from")) {
114 Element min = createItem(
115 cr,
116 new String[] {"min", new Double(minVal).toString()});
117
118 return new Element[] { min };
119 }
120 else if (name.equals("ld_to")) {
121 Element max = createItem(
122 cr,
123 new String[] {"max", new Double(maxVal).toString()});
124
125 return new Element[] { max };
126 }
127 else {
128 Element step = createItem(
129 cr,
130 new String[] {"step", String.valueOf(getDefaultStep())});
131 return new Element[] { step };
132 }
133
134 }
135
136
137 protected Element createItem(XMLUtils.ElementCreator cr, Object obj) {
138 Element item = ProtocolUtils.createArtNode(cr, "item", null, null);
139 Element label = ProtocolUtils.createArtNode(cr, "label", null, null);
140 Element value = ProtocolUtils.createArtNode(cr, "value", null, null);
141
142 String[] arr = (String[]) obj;
143
144 label.setTextContent(arr[0]);
145 value.setTextContent(arr[1]);
146
147 item.appendChild(label);
148 item.appendChild(value);
149
150 return item;
151 }
152
153
154 @Override
155 public Object computeFeed(
156 FLYSArtifact artifact,
157 String hash,
158 CallContext context,
159 List<Facet> facets,
160 Object old
161 ) {
162 logger.debug("computeFeed");
163
164 if (artifact instanceof ChartArtifact) {
165 return null;
166 }
167 WINFOArtifact winfo = (WINFOArtifact)artifact;
168
169 CalculationResult res = old instanceof CalculationResult
170 ? (CalculationResult)old
171 : winfo.getDischargeCurveData();
172
173 if (facets == null) {
174 logger.debug("generate no facets");
175 return res;
176 }
177
178 WQKms [] wqkms = (WQKms [])res.getData();
179
180 logger.debug("generate " + wqkms.length + " facets.");
181
182 String stateID = winfo.getCurrentStateId();
183
184 for (int i = 0; i < wqkms.length; ++i) {
185 String name = getSeriesName(context, wqkms[i].getName());
186 facets.add(new WaterlevelFacet(
187 i, DISCHARGE_CURVE, name, ComputeType.FEED, stateID, hash));
188 }
189
190
191 return res;
192 }
193
194 protected String getSeriesName(CallContext cc, String gaugeName) {
195 Gauge gauge = GaugesFactory.getGauge(gaugeName);
196
197 if (gauge == null) {
198 logger.warn("Cannot determine Gauge for name: " + gaugeName);
199 return gaugeName;
200 }
201
202 List<DischargeTable> dts = gauge.getDischargeTables();
203
204 for (DischargeTable dt: dts) {
205 if (dt.getKind() == 0) {
206 TimeInterval ti = dt.getTimeInterval();
207
208 Date start = ti.getStartTime();
209 Date end = ti.getStopTime();
210
211 String name = gauge.getName();
212
213 if (end == null) {
214 Object[] args = new Object[] { name, start };
215 return Resources.getMsg(
216 cc.getMeta(),
217 "chart.discharge.curve.curve.valid.from",
218 "",
219 args);
220 }
221 else {
222 Object[] args = new Object[] { name, start, end };
223 return Resources.getMsg(
224 cc.getMeta(),
225 "chart.discharge.curve.curve.valid.range",
226 "",
227 args);
228 }
229 }
230 }
231
232 return gauge.getName();
233 }
234
235
236 @Override
237 protected double[] getMinMax(Artifact artifact) {
238 FLYSArtifact flysArtifact = (FLYSArtifact) artifact;
239 return FLYSUtils.getRiverMinMax(flysArtifact);
240 }
241
242
243 protected double getDefaultStep() {
244 return DEFAULT_STEP;
245 }
246
247
248 @Override
249 protected String getLowerField() {
250 return FROM;
251 }
252
253
254 @Override
255 protected String getUpperField() {
256 return TO;
257 }
258
259
260 @Override
261 protected String getStepField() {
262 return STEP;
263 }
264 }
265 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org