comparison flys-client/src/main/java/de/intevation/flys/client/server/RiverInfoServiceImpl.java @ 4243:e68a710d9652

Convert GaugeOverviewInfoService into RiverInfoService The service will be reused for the MeasurementStation info too and it returns a RiverInfo in both cases. Therefore rename the current GaugeOverviewInfoService into RiverInfoService.
author Björn Ricks <bjoern.ricks@intevation.de>
date Wed, 24 Oct 2012 15:50:41 +0200
parents flys-client/src/main/java/de/intevation/flys/client/server/GaugeOverviewInfoServiceImpl.java@22cd60315e08
children
comparison
equal deleted inserted replaced
4242:448cbd8708d5 4243:e68a710d9652
1 package de.intevation.flys.client.server;
2
3 import java.util.ArrayList;
4 import javax.xml.xpath.XPathConstants;
5
6 import org.apache.log4j.Logger;
7
8 import org.w3c.dom.Document;
9 import org.w3c.dom.Element;
10 import org.w3c.dom.NodeList;
11
12 import de.intevation.artifacts.common.ArtifactNamespaceContext;
13 import de.intevation.artifacts.common.utils.XMLUtils;
14 import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
15 import de.intevation.artifacts.httpclient.http.HttpClient;
16 import de.intevation.artifacts.httpclient.http.HttpClientImpl;
17
18 import de.intevation.flys.client.client.services.RiverInfoService;
19 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.DefaultRiverInfo;
22 import de.intevation.flys.client.shared.model.GaugeInfo;
23 import de.intevation.flys.client.shared.model.RiverInfo;
24
25 /**
26 * @author <a href="mailto:bjoern.ricks@intevation.de">Björn Ricks</a>
27 */
28 public class RiverInfoServiceImpl
29 extends RemoteServiceServlet
30 implements RiverInfoService
31 {
32 private static final Logger logger =
33 Logger.getLogger(RiverInfoServiceImpl.class);
34
35 public static final String ERROR_NO_RIVERINFO_FOUND =
36 "error_no_gaugeoverviewinfo_found";
37
38 private static final String XPATH_RIVER = "/art:gauge-info/art:river";
39
40 private static final String XPATH_GAUGES = "/art:gauge-info/art:gauges/art:gauge";
41
42 public RiverInfo getGauges(String river) throws ServerException {
43 logger.info("RiverInfoServiceImpl.getRiverInfo");
44
45 String url = getServletContext().getInitParameter("server-url");
46
47 Document doc = XMLUtils.newDocument();
48
49 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
50 doc,
51 ArtifactNamespaceContext.NAMESPACE_URI,
52 ArtifactNamespaceContext.NAMESPACE_PREFIX);
53
54 Element riverele = ec.create("river");
55 riverele.setTextContent(river);
56
57 doc.appendChild(riverele);
58
59 HttpClient client = new HttpClientImpl(url);
60
61 try {
62 Document result = client.callService(url, "gaugeoverviewinfo", doc);
63
64 Element riverresp = (Element) XMLUtils.xpath(
65 result,
66 XPATH_RIVER,
67 XPathConstants.NODE,
68 ArtifactNamespaceContext.INSTANCE);
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);
167
168 logger.debug("Finished RiverInfoService.");
169
170 return riverinfo;
171 }
172 catch (ConnectionException ce) {
173 logger.error(ce, ce);
174 }
175
176 logger.warn("No gauge found");
177 throw new ServerException(ERROR_NO_RIVERINFO_FOUND);
178 }
179
180 /**
181 * Avoids NullPointerException when parsing double value
182 */
183 private Double parseDouble(String value) {
184 if (value == null || value.isEmpty()) {
185 return null;
186 }
187 try {
188 return Double.valueOf(value);
189 }
190 catch(NumberFormatException e) {
191 logger.error(e, e);
192 return null;
193 }
194 }
195
196 private Long parseLong(String value) {
197 if (value == null || value.isEmpty()) {
198 return null;
199 }
200 try {
201 return Long.valueOf(value);
202 }
203 catch(NumberFormatException e) {
204 logger.error(e, e);
205 return null;
206 }
207 }
208 }

http://dive4elements.wald.intevation.org