comparison flys-client/src/main/java/de/intevation/flys/client/server/GaugeOverviewInfoServiceImpl.java @ 3713:79ad33f41977

Implement a gauge service flys-client/trunk@5469 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Bjoern Ricks <bjoern.ricks@intevation.de>
date Fri, 14 Sep 2012 13:14:45 +0000
parents
children e82acd5c86f7 7a34af684ed4
comparison
equal deleted inserted replaced
3712:738010779c74 3713:79ad33f41977
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.GaugeOverviewInfoService;
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 GaugeOverviewInfoServiceImpl
29 extends RemoteServiceServlet
30 implements GaugeOverviewInfoService
31 {
32 private static final Logger logger =
33 Logger.getLogger(GaugeOverviewInfoServiceImpl.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 getRiverInfo(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
85 logger.debug("River is " + rname);
86
87 boolean kmup = rkmup.equalsIgnoreCase("true");
88
89 NodeList gaugenodes = (NodeList) XMLUtils.xpath(
90 result,
91 XPATH_GAUGES,
92 XPathConstants.NODESET,
93 ArtifactNamespaceContext.INSTANCE);
94
95 int num = gaugenodes == null ? 0 : gaugenodes.getLength();
96
97 ArrayList<GaugeInfo> gauges = new ArrayList<GaugeInfo>(num);
98
99 if (num == 0) {
100 logger.warn("No gauge info found.");
101 }
102 else {
103 logger.debug("Found " + num + " gauges.");
104
105 for (int i = 0; i < num; i++) {
106 Element gaugeele = (Element)gaugenodes.item(i);
107
108 String gname = gaugeele.getAttributeNS(
109 ArtifactNamespaceContext.NAMESPACE_URI, "name");
110 String gstart = gaugeele.getAttributeNS(
111 ArtifactNamespaceContext.NAMESPACE_URI, "start");
112 String gend = gaugeele.getAttributeNS(
113 ArtifactNamespaceContext.NAMESPACE_URI, "end");
114 String gdatum = gaugeele.getAttributeNS(
115 ArtifactNamespaceContext.NAMESPACE_URI, "datum");
116 String gaeo = gaugeele.getAttributeNS(
117 ArtifactNamespaceContext.NAMESPACE_URI, "aeo");
118 String gminq = gaugeele.getAttributeNS(
119 ArtifactNamespaceContext.NAMESPACE_URI, "minq");
120 String gminw = gaugeele.getAttributeNS(
121 ArtifactNamespaceContext.NAMESPACE_URI, "minw");
122 String gmaxq = gaugeele.getAttributeNS(
123 ArtifactNamespaceContext.NAMESPACE_URI, "maxq");
124 String gmaxw = gaugeele.getAttributeNS(
125 ArtifactNamespaceContext.NAMESPACE_URI, "maxw");
126
127 logger.debug("Found gauge with name " + gname);
128
129 GaugeInfo gaugeinfo = new DefaultGaugeInfo(
130 gname,
131 kmup,
132 parseDouble(gstart),
133 parseDouble(gend),
134 parseDouble(gdatum),
135 parseDouble(gaeo),
136 parseDouble(gminq),
137 parseDouble(gmaxq),
138 parseDouble(gminw),
139 parseDouble(gmaxw)
140 );
141
142 gauges.add(gaugeinfo);
143 }
144 }
145
146 RiverInfo riverinfo = new DefaultRiverInfo(
147 rname,
148 kmup,
149 parseDouble(rstart),
150 parseDouble(rend),
151 rwstunit,
152 parseDouble(rminq),
153 parseDouble(rmaxq),
154 gauges);
155
156 logger.debug("Finished RiverInfoService.");
157
158 return riverinfo;
159 }
160 catch (ConnectionException ce) {
161 logger.error(ce, ce);
162 }
163
164 logger.warn("No gauge found");
165 throw new ServerException(ERROR_NO_RIVERINFO_FOUND);
166 }
167
168 /**
169 * Avoids NullPointerException when parsing double value
170 */
171 private Double parseDouble(String value) {
172 if (value == null || value.isEmpty()) {
173 return null;
174 }
175 try {
176 return Double.valueOf(value);
177 }
178 catch(NumberFormatException e) {
179 logger.error(e, e);
180 return null;
181 }
182 }
183 }

http://dive4elements.wald.intevation.org