comparison gwt-client/src/main/java/org/dive4elements/river/client/server/GaugeInfoServiceImpl.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/GaugeInfoServiceImpl.java@821a02bbfb4e
children 172338b1407f
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.client.server;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import javax.xml.xpath.XPathConstants;
7
8 import org.w3c.dom.Document;
9 import org.w3c.dom.Element;
10 import org.w3c.dom.NodeList;
11
12 import org.apache.log4j.Logger;
13
14 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
15
16 import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
17 import org.dive4elements.artifacts.common.utils.XMLUtils;
18
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.shared.exceptions.ServerException;
24 import org.dive4elements.river.client.client.services.GaugeInfoService;
25 import org.dive4elements.river.client.shared.model.Gauge;
26 import org.dive4elements.river.client.shared.model.GaugeImpl;
27
28
29 /**
30 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
31 */
32 public class GaugeInfoServiceImpl
33 extends RemoteServiceServlet
34 implements GaugeInfoService
35 {
36 private static final Logger logger =
37 Logger.getLogger(GaugeInfoServiceImpl.class);
38
39
40 public static final String ERROR_NO_GAUGES_FOUND =
41 "error_no_gaugeinfo_found";
42
43 public static final String XPATH_GAUGES = "art:service/art:gauge";
44
45
46 public List<Gauge> getGaugeInfo(String rivername, String refnumber)
47 throws ServerException
48 {
49 logger.info("GaugeInfoServiceImpl.getGaugeInfo");
50
51 String url = getServletContext().getInitParameter("server-url");
52
53 Document doc = XMLUtils.newDocument();
54
55 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(
56 doc,
57 ArtifactNamespaceContext.NAMESPACE_URI,
58 ArtifactNamespaceContext.NAMESPACE_PREFIX);
59
60 Element river = ec.create("river");
61 ec.addAttr(river, "name", rivername);
62
63 if (refnumber != null && refnumber.length() > 0) {
64 Element filter = ec.create("filter");
65 Element gauge = ec.create("gauge");
66 gauge.setTextContent(refnumber);
67
68 filter.appendChild(gauge);
69 river.appendChild(filter);
70 }
71
72 doc.appendChild(river);
73
74 HttpClient client = new HttpClientImpl(url);
75
76 try {
77 Document result = client.callService(url, "gaugeinfo", doc);
78
79 logger.debug("Extract gauge info now.");
80 List<Gauge> gauges = extractGauges(result);
81
82 if (gauges != null && gauges.size() > 0) {
83 return gauges;
84 }
85 }
86 catch (ConnectionException ce) {
87 logger.error(ce, ce);
88 }
89
90 throw new ServerException(ERROR_NO_GAUGES_FOUND);
91 }
92
93
94 /**
95 * Extracts all wq info objects from <i>result</i> document.
96 *
97 * @param result The document retrieved by the server.
98 *
99 * @return a list of WQInfoObjects.
100 */
101 protected List<Gauge> extractGauges(Document result)
102 throws ServerException
103 {
104 List<Gauge> gauges = new ArrayList<Gauge>();
105
106 NodeList list = (NodeList) XMLUtils.xpath(
107 result,
108 XPATH_GAUGES,
109 XPathConstants.NODESET,
110 ArtifactNamespaceContext.INSTANCE);
111
112 if (list == null || list.getLength() == 0) {
113 logger.warn("No gauges found.");
114
115 throw new ServerException(ERROR_NO_GAUGES_FOUND);
116 }
117
118 int num = list.getLength();
119 logger.debug("Response contains " + num + " objects.");
120
121 for (int i = 0; i < num; i++) {
122 Gauge obj = buildGauge((Element) list.item(i));
123
124 if (obj != null) {
125 gauges.add(obj);
126 }
127 }
128
129 logger.debug("Retrieved " + gauges.size() + " gauges.");
130
131 return gauges;
132 }
133
134
135 protected Gauge buildGauge(Element ele) {
136 String name = ele.getAttribute("name");
137 String lowerStr = ele.getAttribute("lower");
138 String upperStr = ele.getAttribute("upper");
139
140 if (lowerStr != null && upperStr != null) {
141 try {
142 return new GaugeImpl(
143 name,
144 Double.valueOf(lowerStr),
145 Double.valueOf(upperStr));
146 }
147 catch (NumberFormatException nfe) {
148 logger.warn("Error while Gauge creation: " + nfe.getMessage());
149 }
150 }
151
152 return null;
153 }
154 }
155 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org