comparison gwt-client/src/main/java/org/dive4elements/river/client/server/RiverInfoServiceImpl.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-client/src/main/java/org/dive4elements/river/client/server/RiverInfoServiceImpl.java@821a02bbfb4e
children 172338b1407f
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.client.server;
2
3 import java.text.DateFormat;
4 import java.text.ParseException;
5 import java.util.ArrayList;
6 import java.util.Date;
7 import java.util.List;
8 import java.util.Locale;
9 import javax.xml.xpath.XPathConstants;
10
11 import org.apache.log4j.Logger;
12
13 import org.w3c.dom.Document;
14 import org.w3c.dom.Element;
15 import org.w3c.dom.NodeList;
16
17 import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
18 import org.dive4elements.artifacts.common.utils.XMLUtils;
19 import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException;
20 import org.dive4elements.artifacts.httpclient.http.HttpClient;
21 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl;
22
23 import org.dive4elements.river.client.client.services.RiverInfoService;
24 import org.dive4elements.river.client.shared.exceptions.ServerException;
25 import org.dive4elements.river.client.shared.model.DefaultGaugeInfo;
26 import org.dive4elements.river.client.shared.model.DefaultMeasurementStation;
27 import org.dive4elements.river.client.shared.model.DefaultRiverInfo;
28 import org.dive4elements.river.client.shared.model.GaugeInfo;
29 import org.dive4elements.river.client.shared.model.MeasurementStation;
30 import org.dive4elements.river.client.shared.model.RiverInfo;
31
32
33 /**
34 * GWT Service to serve the gauge and measurement station info
35 *
36 * @author <a href="mailto:bjoern.ricks@intevation.de">Björn Ricks</a>
37 */
38 public class RiverInfoServiceImpl
39 extends RemoteServiceServlet
40 implements RiverInfoService
41 {
42 private static final Logger logger =
43 Logger.getLogger(RiverInfoServiceImpl.class);
44
45 public static final String ERROR_NO_RIVER_INFO_FOUND =
46 "error_no_riverinfo_found";
47
48 private static final String XPATH_RIVER =
49 "/art:river-info/art:river";
50
51 private static final String XPATH_STATIONS =
52 "/art:river-info/art:measurement-stations/art:measurement-station";
53
54 private static final String XPATH_GAUGES = "/art:river-info/art:gauges/art:gauge";
55
56 public static final DateFormat DATE_FORMAT = DateFormat.getDateInstance(
57 DateFormat.SHORT, Locale.GERMANY);
58
59 public RiverInfo getGauges(String river) throws ServerException {
60 logger.info("RiverInfoServiceImpl.getRiverInfo");
61
62 String url = getServletContext().getInitParameter("server-url");
63
64 Document doc = XMLUtils.newDocument();
65
66 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
67 doc,
68 ArtifactNamespaceContext.NAMESPACE_URI,
69 ArtifactNamespaceContext.NAMESPACE_PREFIX);
70
71 Element riverele = ec.create("river");
72 riverele.setTextContent(river);
73
74 doc.appendChild(riverele);
75
76 HttpClient client = new HttpClientImpl(url);
77
78 try {
79 Document result = client.callService(url, "gaugeoverviewinfo", doc);
80
81 DefaultRiverInfo riverinfo = getRiverInfo(result);
82 List<GaugeInfo>gauges = createGauges(result, riverinfo.getName(),
83 riverinfo.isKmUp(), riverinfo.getWstUnit());
84
85
86 riverinfo.setGauges(gauges);
87
88 logger.debug("Finished RiverInfoService.getGauges.");
89
90 return riverinfo;
91 }
92 catch (ConnectionException ce) {
93 logger.error(ce, ce);
94 }
95
96 logger.warn("No gauge found");
97 throw new ServerException(ERROR_NO_RIVER_INFO_FOUND);
98 }
99
100 public RiverInfo getMeasurementStations(String river) throws ServerException {
101 logger.info("RiverInfoServiceImpl.getMeasurementStations");
102
103 String url = getServletContext().getInitParameter("server-url");
104
105 Document doc = XMLUtils.newDocument();
106
107 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
108 doc,
109 ArtifactNamespaceContext.NAMESPACE_URI,
110 ArtifactNamespaceContext.NAMESPACE_PREFIX);
111
112 Element riverele = ec.create("river");
113 riverele.setTextContent(river);
114
115 doc.appendChild(riverele);
116
117 HttpClient client = new HttpClientImpl(url);
118
119 try {
120 Document result = client.callService(url, "measurementstationinfo", doc);
121
122 DefaultRiverInfo riverinfo = getRiverInfo(result);
123 List<MeasurementStation> mstations = createMeasurementStations(
124 result, riverinfo.getName(), riverinfo.isKmUp());
125
126 riverinfo.setMeasurementStations(mstations);
127
128 logger.debug("Finished MeasurementStationInfoService.");
129
130 return riverinfo;
131 }
132 catch (ConnectionException ce) {
133 logger.error(ce, ce);
134 }
135
136 logger.warn("No measurement station found");
137 throw new ServerException(ERROR_NO_RIVER_INFO_FOUND);
138 }
139
140 /**
141 * Avoids NullPointerException when parsing double value
142 */
143 private Double parseDouble(String value) {
144 if (value == null || value.isEmpty()) {
145 return null;
146 }
147 try {
148 return Double.valueOf(value);
149 }
150 catch(NumberFormatException e) {
151 logger.error(e, e);
152 return null;
153 }
154 }
155
156 private Long parseLong(String value) {
157 if (value == null || value.isEmpty()) {
158 return null;
159 }
160 try {
161 return Long.valueOf(value);
162 }
163 catch(NumberFormatException e) {
164 logger.error(e, e);
165 return null;
166 }
167 }
168
169 private Integer parseInteger(String value) {
170 if (value == null || value.isEmpty()) {
171 return null;
172 }
173 try {
174 return Integer.valueOf(value);
175 }
176 catch(NumberFormatException e) {
177 logger.error(e, e);
178 return null;
179 }
180 }
181
182 private Date parseDate(String value) {
183 if (value == null || value.isEmpty()) {
184 return null;
185 }
186 try {
187 return DATE_FORMAT.parse(value);
188 }
189 catch(ParseException e) {
190 logger.error(e, e);
191 return null;
192 }
193 }
194
195 private List<MeasurementStation> createMeasurementStations(
196 Document result, String rivername, boolean kmup) {
197
198 NodeList stationnodes = (NodeList) XMLUtils.xpath(
199 result,
200 XPATH_STATIONS,
201 XPathConstants.NODESET,
202 ArtifactNamespaceContext.INSTANCE);
203
204 int num = stationnodes == null ? 0 : stationnodes.getLength();
205
206 ArrayList<MeasurementStation> mstations = new ArrayList<MeasurementStation>(num);
207
208 if (num == 0) {
209 logger.warn("No measurement station found.");
210 }
211 else {
212 logger.debug("Found " + num + " measurement stations.");
213
214 for (int i = 0; i < num; i++) {
215 Element stationele = (Element)stationnodes.item(i);
216
217 String mname = stationele.getAttributeNS(
218 ArtifactNamespaceContext.NAMESPACE_URI, "name");
219 String mstart = stationele.getAttributeNS(
220 ArtifactNamespaceContext.NAMESPACE_URI, "start");
221 String mend = stationele.getAttributeNS(
222 ArtifactNamespaceContext.NAMESPACE_URI, "end");
223 String mstation = stationele.getAttributeNS(
224 ArtifactNamespaceContext.NAMESPACE_URI, "station");
225 String mtype = stationele.getAttributeNS(
226 ArtifactNamespaceContext.NAMESPACE_URI, "type");
227 String riverside = stationele.getAttributeNS(
228 ArtifactNamespaceContext.NAMESPACE_URI, "riverside");
229 String mid = stationele.getAttributeNS(
230 ArtifactNamespaceContext.NAMESPACE_URI, "id");
231 String moperator = stationele.getAttributeNS(
232 ArtifactNamespaceContext.NAMESPACE_URI, "operator");
233 String mstarttime = stationele.getAttributeNS(
234 ArtifactNamespaceContext.NAMESPACE_URI, "starttime");
235 String mstoptime = stationele.getAttributeNS(
236 ArtifactNamespaceContext.NAMESPACE_URI, "stoptime");
237
238 String gaugename = null;
239
240 Element gaugeele = (Element)stationele.getFirstChild();
241 if (gaugeele != null) {
242 gaugename = gaugeele.getAttributeNS(
243 ArtifactNamespaceContext.NAMESPACE_URI, "name");
244 }
245
246
247 logger.debug("Found measurement station with name " + mname);
248
249 MeasurementStation station = new DefaultMeasurementStation(
250 rivername,
251 mname,
252 parseInteger(mid),
253 parseDouble(mstation),
254 parseDouble(mstart),
255 parseDouble(mend),
256 kmup,
257 riverside,
258 mtype,
259 moperator,
260 parseDate(mstarttime),
261 parseDate(mstoptime),
262 gaugename
263 );
264
265 mstations.add(station);
266 }
267 }
268 return mstations;
269 }
270
271 private List<GaugeInfo> createGauges(
272 Document result, String rivername, Boolean kmup, String rwstunit) {
273 NodeList gaugenodes = (NodeList) XMLUtils.xpath(
274 result,
275 XPATH_GAUGES,
276 XPathConstants.NODESET,
277 ArtifactNamespaceContext.INSTANCE);
278
279 int num = gaugenodes == null ? 0 : gaugenodes.getLength();
280
281 ArrayList<GaugeInfo> gauges = new ArrayList<GaugeInfo>(num);
282
283 if (num == 0) {
284 logger.warn("No gauge info found.");
285 }
286 else {
287 logger.debug("Found " + num + " gauges.");
288
289 for (int i = 0; i < num; i++) {
290 Element gaugeele = (Element)gaugenodes.item(i);
291
292 String gname = gaugeele.getAttributeNS(
293 ArtifactNamespaceContext.NAMESPACE_URI, "name");
294 String gstart = gaugeele.getAttributeNS(
295 ArtifactNamespaceContext.NAMESPACE_URI, "start");
296 String gend = gaugeele.getAttributeNS(
297 ArtifactNamespaceContext.NAMESPACE_URI, "end");
298 String gdatum = gaugeele.getAttributeNS(
299 ArtifactNamespaceContext.NAMESPACE_URI, "datum");
300 String gaeo = gaugeele.getAttributeNS(
301 ArtifactNamespaceContext.NAMESPACE_URI, "aeo");
302 String gminq = gaugeele.getAttributeNS(
303 ArtifactNamespaceContext.NAMESPACE_URI, "minq");
304 String gminw = gaugeele.getAttributeNS(
305 ArtifactNamespaceContext.NAMESPACE_URI, "minw");
306 String gmaxq = gaugeele.getAttributeNS(
307 ArtifactNamespaceContext.NAMESPACE_URI, "maxq");
308 String gmaxw = gaugeele.getAttributeNS(
309 ArtifactNamespaceContext.NAMESPACE_URI, "maxw");
310 String gstation = gaugeele.getAttributeNS(
311 ArtifactNamespaceContext.NAMESPACE_URI, "station");
312 String gofficial = gaugeele.getAttributeNS(
313 ArtifactNamespaceContext.NAMESPACE_URI, "official");
314
315 logger.debug("Found gauge with name " + gname);
316
317 GaugeInfo gaugeinfo = new DefaultGaugeInfo(
318 rivername,
319 gname,
320 kmup,
321 parseDouble(gstation),
322 parseDouble(gstart),
323 parseDouble(gend),
324 parseDouble(gdatum),
325 parseDouble(gaeo),
326 parseDouble(gminq),
327 parseDouble(gmaxq),
328 parseDouble(gminw),
329 parseDouble(gmaxw),
330 rwstunit,
331 parseLong(gofficial)
332 );
333
334 gauges.add(gaugeinfo);
335 }
336 }
337 return gauges;
338 }
339
340 private DefaultRiverInfo getRiverInfo(Document result) {
341 Element riverresp = (Element) XMLUtils.xpath(
342 result,
343 XPATH_RIVER,
344 XPathConstants.NODE,
345 ArtifactNamespaceContext.INSTANCE);
346
347 String rname = riverresp.getAttributeNS(
348 ArtifactNamespaceContext.NAMESPACE_URI, "name");
349 String rkmup = riverresp.getAttributeNS(
350 ArtifactNamespaceContext.NAMESPACE_URI, "kmup");
351 String rstart = riverresp.getAttributeNS(
352 ArtifactNamespaceContext.NAMESPACE_URI, "start");
353 String rend = riverresp.getAttributeNS(
354 ArtifactNamespaceContext.NAMESPACE_URI, "end");
355 String rwstunit = riverresp.getAttributeNS(
356 ArtifactNamespaceContext.NAMESPACE_URI, "wstunit");
357 String rminq = riverresp.getAttributeNS(
358 ArtifactNamespaceContext.NAMESPACE_URI, "minq");
359 String rmaxq = riverresp.getAttributeNS(
360 ArtifactNamespaceContext.NAMESPACE_URI, "maxq");
361 String rofficial = riverresp.getAttributeNS(
362 ArtifactNamespaceContext.NAMESPACE_URI, "official");
363
364 logger.debug("River is " + rname);
365
366 boolean kmup = rkmup.equalsIgnoreCase("true");
367 DefaultRiverInfo riverinfo = new DefaultRiverInfo(
368 rname,
369 kmup,
370 parseDouble(rstart),
371 parseDouble(rend),
372 rwstunit,
373 parseDouble(rminq),
374 parseDouble(rmaxq),
375 parseLong(rofficial)
376 );
377
378 return riverinfo;
379 }
380 }

http://dive4elements.wald.intevation.org