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