comparison flys-client/src/main/java/de/intevation/flys/client/server/RiverInfoServiceImpl.java @ 4250:ef154fbf58f1

Add method to get the MeasurementStations from the RiverInfoService Also refactor the RiverInfoServiceImpl to use common code for getting GaugeInfo and MeasurementStations.
author Björn Ricks <bjoern.ricks@intevation.de>
date Wed, 24 Oct 2012 17:09:04 +0200
parents flys-client/src/main/java/de/intevation/flys/client/server/GaugeOverviewInfoServiceImpl.java@22cd60315e08
children 06d9098a609a
comparison
equal deleted inserted replaced
4249:7a889098bcc6 4250:ef154fbf58f1
1 package de.intevation.flys.client.server; 1 package de.intevation.flys.client.server;
2 2
3 import java.util.ArrayList; 3 import java.util.ArrayList;
4 import java.util.List;
4 import javax.xml.xpath.XPathConstants; 5 import javax.xml.xpath.XPathConstants;
5 6
6 import org.apache.log4j.Logger; 7 import org.apache.log4j.Logger;
7 8
8 import org.w3c.dom.Document; 9 import org.w3c.dom.Document;
16 import de.intevation.artifacts.httpclient.http.HttpClientImpl; 17 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
17 18
18 import de.intevation.flys.client.client.services.RiverInfoService; 19 import de.intevation.flys.client.client.services.RiverInfoService;
19 import de.intevation.flys.client.shared.exceptions.ServerException; 20 import de.intevation.flys.client.shared.exceptions.ServerException;
20 import de.intevation.flys.client.shared.model.DefaultGaugeInfo; 21 import de.intevation.flys.client.shared.model.DefaultGaugeInfo;
22 import de.intevation.flys.client.shared.model.DefaultMeasurementStation;
21 import de.intevation.flys.client.shared.model.DefaultRiverInfo; 23 import de.intevation.flys.client.shared.model.DefaultRiverInfo;
22 import de.intevation.flys.client.shared.model.GaugeInfo; 24 import de.intevation.flys.client.shared.model.GaugeInfo;
25 import de.intevation.flys.client.shared.model.MeasurementStation;
23 import de.intevation.flys.client.shared.model.RiverInfo; 26 import de.intevation.flys.client.shared.model.RiverInfo;
27
24 28
25 /** 29 /**
26 * @author <a href="mailto:bjoern.ricks@intevation.de">Björn Ricks</a> 30 * @author <a href="mailto:bjoern.ricks@intevation.de">Björn Ricks</a>
27 */ 31 */
28 public class RiverInfoServiceImpl 32 public class RiverInfoServiceImpl
30 implements RiverInfoService 34 implements RiverInfoService
31 { 35 {
32 private static final Logger logger = 36 private static final Logger logger =
33 Logger.getLogger(RiverInfoServiceImpl.class); 37 Logger.getLogger(RiverInfoServiceImpl.class);
34 38
35 public static final String ERROR_NO_RIVERINFO_FOUND = 39 public static final String ERROR_NO_RIVER_INFO_FOUND =
36 "error_no_gaugeoverviewinfo_found"; 40 "error_no_riverinfo_found";
37 41
38 private static final String XPATH_RIVER = "/art:gauge-info/art:river"; 42 private static final String XPATH_RIVER =
39 43 "/art:river-info/art:river";
40 private static final String XPATH_GAUGES = "/art:gauge-info/art:gauges/art:gauge"; 44
45 private static final String XPATH_STATIONS =
46 "/art:river-info/art:measurement-stations/art:measurement-station";
47
48 private static final String XPATH_GAUGES = "/art:river-info/art:gauges/art:gauge";
41 49
42 public RiverInfo getGauges(String river) throws ServerException { 50 public RiverInfo getGauges(String river) throws ServerException {
43 logger.info("RiverInfoServiceImpl.getRiverInfo"); 51 logger.info("RiverInfoServiceImpl.getRiverInfo");
44 52
45 String url = getServletContext().getInitParameter("server-url"); 53 String url = getServletContext().getInitParameter("server-url");
59 HttpClient client = new HttpClientImpl(url); 67 HttpClient client = new HttpClientImpl(url);
60 68
61 try { 69 try {
62 Document result = client.callService(url, "gaugeoverviewinfo", doc); 70 Document result = client.callService(url, "gaugeoverviewinfo", doc);
63 71
64 Element riverresp = (Element) XMLUtils.xpath( 72 DefaultRiverInfo riverinfo = getRiverInfo(result);
65 result, 73 List<GaugeInfo>gauges = createGauges(result, riverinfo.getName(),
66 XPATH_RIVER, 74 riverinfo.isKmUp(), riverinfo.getWstUnit());
67 XPathConstants.NODE, 75
68 ArtifactNamespaceContext.INSTANCE); 76
69
70 String rname = riverresp.getAttributeNS(
71 ArtifactNamespaceContext.NAMESPACE_URI, "name");
72 String rkmup = riverresp.getAttributeNS(
73 ArtifactNamespaceContext.NAMESPACE_URI, "kmup");
74 String rstart = riverresp.getAttributeNS(
75 ArtifactNamespaceContext.NAMESPACE_URI, "start");
76 String rend = riverresp.getAttributeNS(
77 ArtifactNamespaceContext.NAMESPACE_URI, "end");
78 String rwstunit = riverresp.getAttributeNS(
79 ArtifactNamespaceContext.NAMESPACE_URI, "wstunit");
80 String rminq = riverresp.getAttributeNS(
81 ArtifactNamespaceContext.NAMESPACE_URI, "minq");
82 String rmaxq = riverresp.getAttributeNS(
83 ArtifactNamespaceContext.NAMESPACE_URI, "maxq");
84 String rofficial = riverresp.getAttributeNS(
85 ArtifactNamespaceContext.NAMESPACE_URI, "official");
86
87 logger.debug("River is " + rname);
88
89 boolean kmup = rkmup.equalsIgnoreCase("true");
90
91 NodeList gaugenodes = (NodeList) XMLUtils.xpath(
92 result,
93 XPATH_GAUGES,
94 XPathConstants.NODESET,
95 ArtifactNamespaceContext.INSTANCE);
96
97 int num = gaugenodes == null ? 0 : gaugenodes.getLength();
98
99 ArrayList<GaugeInfo> gauges = new ArrayList<GaugeInfo>(num);
100
101 if (num == 0) {
102 logger.warn("No gauge info found.");
103 }
104 else {
105 logger.debug("Found " + num + " gauges.");
106
107 for (int i = 0; i < num; i++) {
108 Element gaugeele = (Element)gaugenodes.item(i);
109
110 String gname = gaugeele.getAttributeNS(
111 ArtifactNamespaceContext.NAMESPACE_URI, "name");
112 String gstart = gaugeele.getAttributeNS(
113 ArtifactNamespaceContext.NAMESPACE_URI, "start");
114 String gend = gaugeele.getAttributeNS(
115 ArtifactNamespaceContext.NAMESPACE_URI, "end");
116 String gdatum = gaugeele.getAttributeNS(
117 ArtifactNamespaceContext.NAMESPACE_URI, "datum");
118 String gaeo = gaugeele.getAttributeNS(
119 ArtifactNamespaceContext.NAMESPACE_URI, "aeo");
120 String gminq = gaugeele.getAttributeNS(
121 ArtifactNamespaceContext.NAMESPACE_URI, "minq");
122 String gminw = gaugeele.getAttributeNS(
123 ArtifactNamespaceContext.NAMESPACE_URI, "minw");
124 String gmaxq = gaugeele.getAttributeNS(
125 ArtifactNamespaceContext.NAMESPACE_URI, "maxq");
126 String gmaxw = gaugeele.getAttributeNS(
127 ArtifactNamespaceContext.NAMESPACE_URI, "maxw");
128 String gstation = gaugeele.getAttributeNS(
129 ArtifactNamespaceContext.NAMESPACE_URI, "station");
130 String gofficial = gaugeele.getAttributeNS(
131 ArtifactNamespaceContext.NAMESPACE_URI, "official");
132
133 logger.debug("Found gauge with name " + gname);
134
135 GaugeInfo gaugeinfo = new DefaultGaugeInfo(
136 rname,
137 gname,
138 kmup,
139 parseDouble(gstation),
140 parseDouble(gstart),
141 parseDouble(gend),
142 parseDouble(gdatum),
143 parseDouble(gaeo),
144 parseDouble(gminq),
145 parseDouble(gmaxq),
146 parseDouble(gminw),
147 parseDouble(gmaxw),
148 rwstunit,
149 parseLong(gofficial)
150 );
151
152 gauges.add(gaugeinfo);
153 }
154 }
155
156 DefaultRiverInfo riverinfo = new DefaultRiverInfo(
157 rname,
158 kmup,
159 parseDouble(rstart),
160 parseDouble(rend),
161 rwstunit,
162 parseDouble(rminq),
163 parseDouble(rmaxq),
164 parseLong(rofficial)
165 );
166 riverinfo.setGauges(gauges); 77 riverinfo.setGauges(gauges);
167 78
168 logger.debug("Finished RiverInfoService."); 79 logger.debug("Finished RiverInfoService.getGauges.");
169 80
170 return riverinfo; 81 return riverinfo;
171 } 82 }
172 catch (ConnectionException ce) { 83 catch (ConnectionException ce) {
173 logger.error(ce, ce); 84 logger.error(ce, ce);
174 } 85 }
175 86
176 logger.warn("No gauge found"); 87 logger.warn("No gauge found");
177 throw new ServerException(ERROR_NO_RIVERINFO_FOUND); 88 throw new ServerException(ERROR_NO_RIVER_INFO_FOUND);
89 }
90
91 public RiverInfo getMeasurementStations(String river) throws ServerException {
92 logger.info("RiverInfoServiceImpl.getMeasurementStations");
93
94 String url = getServletContext().getInitParameter("server-url");
95
96 Document doc = XMLUtils.newDocument();
97
98 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
99 doc,
100 ArtifactNamespaceContext.NAMESPACE_URI,
101 ArtifactNamespaceContext.NAMESPACE_PREFIX);
102
103 Element riverele = ec.create("river");
104 riverele.setTextContent(river);
105
106 doc.appendChild(riverele);
107
108 HttpClient client = new HttpClientImpl(url);
109
110 try {
111 Document result = client.callService(url, "measurementstationinfo", doc);
112
113 DefaultRiverInfo riverinfo = getRiverInfo(result);
114 List<MeasurementStation> mstations = createMeasurementStations(
115 result, riverinfo.getName());
116
117 riverinfo.setMeasurementStations(mstations);
118
119 logger.debug("Finished MeasurementStationInfoService.");
120
121 return riverinfo;
122 }
123 catch (ConnectionException ce) {
124 logger.error(ce, ce);
125 }
126
127 logger.warn("No measurement station found");
128 throw new ServerException(ERROR_NO_RIVER_INFO_FOUND);
178 } 129 }
179 130
180 /** 131 /**
181 * Avoids NullPointerException when parsing double value 132 * Avoids NullPointerException when parsing double value
182 */ 133 */
203 catch(NumberFormatException e) { 154 catch(NumberFormatException e) {
204 logger.error(e, e); 155 logger.error(e, e);
205 return null; 156 return null;
206 } 157 }
207 } 158 }
159
160 private List<MeasurementStation> createMeasurementStations(
161 Document result, String rivername) {
162 NodeList stationnodes = (NodeList) XMLUtils.xpath(
163 result,
164 XPATH_STATIONS,
165 XPathConstants.NODESET,
166 ArtifactNamespaceContext.INSTANCE);
167
168 int num = stationnodes == null ? 0 : stationnodes.getLength();
169
170 ArrayList<MeasurementStation> mstations = new ArrayList<MeasurementStation>(num);
171
172 if (num == 0) {
173 logger.warn("No measurement station found.");
174 }
175 else {
176 logger.debug("Found " + num + " measurement stations.");
177
178 for (int i = 0; i < num; i++) {
179 Element stationele = (Element)stationnodes.item(i);
180
181 String mname = stationele.getAttributeNS(
182 ArtifactNamespaceContext.NAMESPACE_URI, "name");
183 String mstart = stationele.getAttributeNS(
184 ArtifactNamespaceContext.NAMESPACE_URI, "start");
185 String mend = stationele.getAttributeNS(
186 ArtifactNamespaceContext.NAMESPACE_URI, "end");
187 String mstation = stationele.getAttributeNS(
188 ArtifactNamespaceContext.NAMESPACE_URI, "station");
189 String mtype = stationele.getAttributeNS(
190 ArtifactNamespaceContext.NAMESPACE_URI, "type");
191 String riverside = stationele.getAttributeNS(
192 ArtifactNamespaceContext.NAMESPACE_URI, "riverside");
193
194 logger.debug("Found measurement station with name " + mname);
195
196 MeasurementStation station = new DefaultMeasurementStation(
197 rivername,
198 mname,
199 parseDouble(mstation),
200 parseDouble(mstart),
201 parseDouble(mend),
202 riverside,
203 mtype
204 );
205
206 mstations.add(station);
207 }
208 }
209 return mstations;
210 }
211
212 private List<GaugeInfo> createGauges(
213 Document result, String rivername, Boolean kmup, String rwstunit) {
214 NodeList gaugenodes = (NodeList) XMLUtils.xpath(
215 result,
216 XPATH_GAUGES,
217 XPathConstants.NODESET,
218 ArtifactNamespaceContext.INSTANCE);
219
220 int num = gaugenodes == null ? 0 : gaugenodes.getLength();
221
222 ArrayList<GaugeInfo> gauges = new ArrayList<GaugeInfo>(num);
223
224 if (num == 0) {
225 logger.warn("No gauge info found.");
226 }
227 else {
228 logger.debug("Found " + num + " gauges.");
229
230 for (int i = 0; i < num; i++) {
231 Element gaugeele = (Element)gaugenodes.item(i);
232
233 String gname = gaugeele.getAttributeNS(
234 ArtifactNamespaceContext.NAMESPACE_URI, "name");
235 String gstart = gaugeele.getAttributeNS(
236 ArtifactNamespaceContext.NAMESPACE_URI, "start");
237 String gend = gaugeele.getAttributeNS(
238 ArtifactNamespaceContext.NAMESPACE_URI, "end");
239 String gdatum = gaugeele.getAttributeNS(
240 ArtifactNamespaceContext.NAMESPACE_URI, "datum");
241 String gaeo = gaugeele.getAttributeNS(
242 ArtifactNamespaceContext.NAMESPACE_URI, "aeo");
243 String gminq = gaugeele.getAttributeNS(
244 ArtifactNamespaceContext.NAMESPACE_URI, "minq");
245 String gminw = gaugeele.getAttributeNS(
246 ArtifactNamespaceContext.NAMESPACE_URI, "minw");
247 String gmaxq = gaugeele.getAttributeNS(
248 ArtifactNamespaceContext.NAMESPACE_URI, "maxq");
249 String gmaxw = gaugeele.getAttributeNS(
250 ArtifactNamespaceContext.NAMESPACE_URI, "maxw");
251 String gstation = gaugeele.getAttributeNS(
252 ArtifactNamespaceContext.NAMESPACE_URI, "station");
253 String gofficial = gaugeele.getAttributeNS(
254 ArtifactNamespaceContext.NAMESPACE_URI, "official");
255
256 logger.debug("Found gauge with name " + gname);
257
258 GaugeInfo gaugeinfo = new DefaultGaugeInfo(
259 rivername,
260 gname,
261 kmup,
262 parseDouble(gstation),
263 parseDouble(gstart),
264 parseDouble(gend),
265 parseDouble(gdatum),
266 parseDouble(gaeo),
267 parseDouble(gminq),
268 parseDouble(gmaxq),
269 parseDouble(gminw),
270 parseDouble(gmaxw),
271 rwstunit,
272 parseLong(gofficial)
273 );
274
275 gauges.add(gaugeinfo);
276 }
277 }
278 return gauges;
279 }
280
281 private DefaultRiverInfo getRiverInfo(Document result) {
282 Element riverresp = (Element) XMLUtils.xpath(
283 result,
284 XPATH_RIVER,
285 XPathConstants.NODE,
286 ArtifactNamespaceContext.INSTANCE);
287
288 String rname = riverresp.getAttributeNS(
289 ArtifactNamespaceContext.NAMESPACE_URI, "name");
290 String rkmup = riverresp.getAttributeNS(
291 ArtifactNamespaceContext.NAMESPACE_URI, "kmup");
292 String rstart = riverresp.getAttributeNS(
293 ArtifactNamespaceContext.NAMESPACE_URI, "start");
294 String rend = riverresp.getAttributeNS(
295 ArtifactNamespaceContext.NAMESPACE_URI, "end");
296 String rwstunit = riverresp.getAttributeNS(
297 ArtifactNamespaceContext.NAMESPACE_URI, "wstunit");
298 String rminq = riverresp.getAttributeNS(
299 ArtifactNamespaceContext.NAMESPACE_URI, "minq");
300 String rmaxq = riverresp.getAttributeNS(
301 ArtifactNamespaceContext.NAMESPACE_URI, "maxq");
302 String rofficial = riverresp.getAttributeNS(
303 ArtifactNamespaceContext.NAMESPACE_URI, "official");
304
305 logger.debug("River is " + rname);
306
307 boolean kmup = rkmup.equalsIgnoreCase("true");
308 DefaultRiverInfo riverinfo = new DefaultRiverInfo(
309 rname,
310 kmup,
311 parseDouble(rstart),
312 parseDouble(rend),
313 rwstunit,
314 parseDouble(rminq),
315 parseDouble(rmaxq),
316 parseLong(rofficial)
317 );
318
319 return riverinfo;
320 }
208 } 321 }

http://dive4elements.wald.intevation.org