Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/services/MainValuesService.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/services/MainValuesService.java@bd047b71ab37 |
children | 4897a58c8746 |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.artifacts.services; | |
2 | |
3 import java.util.List; | |
4 | |
5 import org.apache.log4j.Logger; | |
6 | |
7 import org.w3c.dom.Document; | |
8 import org.w3c.dom.Element; | |
9 | |
10 import org.dive4elements.artifacts.CallMeta; | |
11 import org.dive4elements.artifacts.GlobalContext; | |
12 | |
13 import org.dive4elements.artifacts.common.ArtifactNamespaceContext; | |
14 import org.dive4elements.artifacts.common.utils.XMLUtils; | |
15 import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator; | |
16 | |
17 import org.dive4elements.river.model.Gauge; | |
18 import org.dive4elements.river.model.MainValue; | |
19 import org.dive4elements.river.model.MainValueType; | |
20 import org.dive4elements.river.model.NamedMainValue; | |
21 import org.dive4elements.river.model.Range; | |
22 import org.dive4elements.river.model.River; | |
23 | |
24 import org.dive4elements.river.artifacts.model.RiverFactory; | |
25 | |
26 | |
27 /** | |
28 * This service returns the main values of a river's gauge based on the start | |
29 * and end point of the river. | |
30 * | |
31 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
32 */ | |
33 public class MainValuesService extends FLYSService { | |
34 | |
35 /** The logger that is used by this service.*/ | |
36 private static Logger logger = Logger.getLogger(MainValuesService.class); | |
37 | |
38 | |
39 /** The XPath that points to the river definition of the incoming request.*/ | |
40 public static final String XPATH_RIVER = "/art:mainvalues/art:river/text()"; | |
41 | |
42 /** The XPath that points to the start definition of the incoming request.*/ | |
43 public static final String XPATH_START = "/art:mainvalues/art:start/text()"; | |
44 | |
45 /** The XPath that points to the end definition of the incoming request.*/ | |
46 public static final String XPATH_END = "/art:mainvalues/art:end/text()"; | |
47 | |
48 /** | |
49 * The default constructor. | |
50 */ | |
51 public MainValuesService() { | |
52 } | |
53 | |
54 private static final Document error(String msg) { | |
55 logger.debug(msg); | |
56 return XMLUtils.newDocument(); | |
57 } | |
58 | |
59 | |
60 @Override | |
61 public Document doProcess( | |
62 Document data, | |
63 GlobalContext context, | |
64 CallMeta callMeta | |
65 ) { | |
66 logger.debug("MainValuesService.process"); | |
67 | |
68 River river = getRequestedRiver(data); | |
69 if (river == null) { | |
70 return error("no river found."); | |
71 } | |
72 | |
73 double[] minmax = getRequestedStartEnd(data, river); | |
74 Gauge gauge = river.determineGauge(minmax[0], minmax[1]); | |
75 | |
76 if (gauge == null) { | |
77 return error("no gauge found."); | |
78 } | |
79 | |
80 List<MainValue> mainValues = getMainValues(river, gauge); | |
81 | |
82 return buildDocument(river, gauge, mainValues, context); | |
83 } | |
84 | |
85 | |
86 /** | |
87 * This method extracts the river from the incoming request. If no river | |
88 * string was found or no river is found in the database based on this | |
89 * string a NullPointerException is thrown. | |
90 * | |
91 * @param data The incoming request data. | |
92 * | |
93 * @return the River object. | |
94 */ | |
95 protected River getRequestedRiver(Document data) | |
96 throws NullPointerException | |
97 { | |
98 logger.debug("MainValuesService.getRiver"); | |
99 | |
100 String riverStr = XMLUtils.xpathString( | |
101 data, XPATH_RIVER, ArtifactNamespaceContext.INSTANCE); | |
102 | |
103 return riverStr != null && (riverStr = riverStr.trim()).length() > 0 | |
104 ? RiverFactory.getRiver(riverStr) | |
105 : null; | |
106 } | |
107 | |
108 | |
109 /** | |
110 * This method extracts the start and end point from incoming request | |
111 * document and returns both values in an array. If no start and end strings | |
112 * are found in the document, the min/max values of the <i>river</i> are | |
113 * returned. | |
114 * | |
115 * @param data The incoming request data. | |
116 * @param river The river of the request. | |
117 * | |
118 * @return the start and end point. | |
119 */ | |
120 protected double[] getRequestedStartEnd(Document data, River river) { | |
121 logger.debug("MainValuesService.getStartEnd"); | |
122 | |
123 String startStr = XMLUtils.xpathString( | |
124 data, XPATH_START, ArtifactNamespaceContext.INSTANCE); | |
125 | |
126 String endStr = XMLUtils.xpathString( | |
127 data, XPATH_END, ArtifactNamespaceContext.INSTANCE); | |
128 | |
129 if (startStr == null || endStr == null) { | |
130 return river.determineMinMaxDistance(); | |
131 } | |
132 | |
133 try { | |
134 double start = Double.parseDouble(startStr); | |
135 double end = Double.parseDouble(endStr); | |
136 | |
137 if (logger.isDebugEnabled()) { | |
138 logger.debug("Found start: " + start); | |
139 logger.debug("Found end: " + end); | |
140 } | |
141 | |
142 return new double[] { start, end }; | |
143 } | |
144 catch (NumberFormatException nfe) { | |
145 logger.warn(nfe, nfe); | |
146 return river.determineMinMaxDistance(); | |
147 } | |
148 } | |
149 | |
150 | |
151 /** | |
152 * This method creates the result document that includes the main values of | |
153 * the specified <i>gauge</i>. | |
154 * | |
155 * @param river The river. | |
156 * @param gauge The gauge. | |
157 * | |
158 * @return a document that includes the main values of the specified river | |
159 * at the specified gauge. | |
160 */ | |
161 protected List<MainValue> getMainValues(River river, Gauge gauge) { | |
162 | |
163 if (logger.isDebugEnabled()) { | |
164 logger.debug("MainValuesService.buildMainValues"); | |
165 logger.debug("River: " + river.getName()); | |
166 logger.debug("Gauge: " + gauge.getName()); | |
167 } | |
168 | |
169 List<MainValue> mainValues = gauge.getMainValues(); | |
170 | |
171 if (logger.isDebugEnabled()) { | |
172 logger.debug(mainValues.size() + " main values found."); | |
173 } | |
174 | |
175 return mainValues; | |
176 } | |
177 | |
178 | |
179 protected Document buildDocument( | |
180 River river, | |
181 Gauge gauge, | |
182 List<MainValue> mainValues, | |
183 Object context) | |
184 { | |
185 logger.debug("MainValuesService.buildDocument"); | |
186 | |
187 Document doc = XMLUtils.newDocument(); | |
188 | |
189 ElementCreator cr = new ElementCreator( | |
190 doc, | |
191 ArtifactNamespaceContext.NAMESPACE_URI, | |
192 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
193 | |
194 Element rootEl = cr.create("service"); | |
195 cr.addAttr(rootEl, "name", "mainvalues"); | |
196 | |
197 doc.appendChild(rootEl); | |
198 | |
199 appendMetaInformation(doc, rootEl, river, gauge, context); | |
200 appendMainValues(doc, rootEl, mainValues, context); | |
201 | |
202 return doc; | |
203 } | |
204 | |
205 | |
206 /** | |
207 * This method appends some meta information to the result document. | |
208 * Currently, the river's and gauge's names and the gauge's range are | |
209 * appended. | |
210 * | |
211 * @param root The root element of the result document. | |
212 * @param river The river. | |
213 * @param gauge The gauge. | |
214 * @param context The context object. | |
215 */ | |
216 protected void appendMetaInformation( | |
217 Document doc, | |
218 Element root, | |
219 River river, | |
220 Gauge gauge, | |
221 Object context) | |
222 { | |
223 logger.debug("MainValuesService.appendMetaInformation"); | |
224 | |
225 ElementCreator cr = new ElementCreator( | |
226 doc, | |
227 ArtifactNamespaceContext.NAMESPACE_URI, | |
228 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
229 | |
230 Range range = gauge.getRange(); | |
231 | |
232 Element riverEl = cr.create("river"); | |
233 cr.addAttr(riverEl, "name", river.getName()); | |
234 | |
235 Element gaugeEl = cr.create("gauge"); | |
236 cr.addAttr(gaugeEl, "name", gauge.getName()); | |
237 cr.addAttr(gaugeEl, "from", range.getA().toString()); | |
238 cr.addAttr(gaugeEl, "to", range.getB().toString()); | |
239 | |
240 root.appendChild(riverEl); | |
241 root.appendChild(gaugeEl); | |
242 } | |
243 | |
244 | |
245 protected void appendMainValues( | |
246 Document doc, | |
247 Element root, | |
248 List<MainValue> mainValues, | |
249 Object context) | |
250 { | |
251 logger.debug("MainValuesService.appendMainValues"); | |
252 | |
253 ElementCreator cr = new ElementCreator( | |
254 doc, | |
255 ArtifactNamespaceContext.NAMESPACE_URI, | |
256 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
257 | |
258 Element list = cr.create("mainvalues"); | |
259 | |
260 for (MainValue mainValue: mainValues) { | |
261 Element newEl = buildMainValueElement(doc, mainValue, context); | |
262 | |
263 if (newEl != null) { | |
264 list.appendChild(newEl); | |
265 } | |
266 } | |
267 | |
268 root.appendChild(list); | |
269 } | |
270 | |
271 | |
272 /** | |
273 * This method builds a concrete mainvalue element. This element consists of | |
274 * three attributes: the value, its name and its type. | |
275 * | |
276 * @param doc The owner document. | |
277 * @param mainValue The mainvalue. | |
278 * @param context The context object. | |
279 * | |
280 * @return a mainvalue element. | |
281 */ | |
282 protected Element buildMainValueElement( | |
283 Document doc, | |
284 MainValue mainValue, | |
285 Object context) | |
286 { | |
287 ElementCreator cr = new ElementCreator( | |
288 doc, | |
289 ArtifactNamespaceContext.NAMESPACE_URI, | |
290 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
291 | |
292 NamedMainValue namedMainValue = mainValue.getMainValue(); | |
293 MainValueType mainValueType = namedMainValue.getType(); | |
294 | |
295 Element el = cr.create("mainvalue"); | |
296 | |
297 cr.addAttr(el, "value", mainValue.getValue().toString()); | |
298 cr.addAttr(el, "name", namedMainValue.getName()); | |
299 cr.addAttr(el, "type", mainValueType.getName()); | |
300 | |
301 return el; | |
302 } | |
303 } | |
304 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |