Mercurial > dive4elements > river
comparison flys-client/src/main/java/de/intevation/flys/client/server/CrossSectionKMServiceImpl.java @ 1426:fc8ff0ed64ca
Added client for CrossSectionKMService.
flys-client/trunk@3369 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Thu, 08 Dec 2011 12:09:58 +0000 |
parents | |
children | 9115b2a28be1 |
comparison
equal
deleted
inserted
replaced
1425:bc06a671ef60 | 1426:fc8ff0ed64ca |
---|---|
1 package de.intevation.flys.client.server; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.HashMap; | |
5 import java.util.Map; | |
6 | |
7 import javax.xml.xpath.XPathConstants; | |
8 | |
9 import org.w3c.dom.Document; | |
10 import org.w3c.dom.Element; | |
11 import org.w3c.dom.NodeList; | |
12 | |
13 import org.apache.log4j.Logger; | |
14 | |
15 import com.google.gwt.user.server.rpc.RemoteServiceServlet; | |
16 | |
17 import de.intevation.artifacts.common.ArtifactNamespaceContext; | |
18 import de.intevation.artifacts.common.utils.XMLUtils; | |
19 | |
20 import de.intevation.artifacts.httpclient.exceptions.ConnectionException; | |
21 import de.intevation.artifacts.httpclient.http.HttpClient; | |
22 import de.intevation.artifacts.httpclient.http.HttpClientImpl; | |
23 | |
24 import de.intevation.flys.client.shared.exceptions.ServerException; | |
25 import de.intevation.flys.client.client.services.CrossSectionKMService; | |
26 | |
27 /** | |
28 * Interact with not documented service. | |
29 */ | |
30 public class CrossSectionKMServiceImpl | |
31 extends RemoteServiceServlet | |
32 implements CrossSectionKMService | |
33 { | |
34 private static final Logger logger = | |
35 Logger.getLogger(CrossSectionKMServiceImpl.class); | |
36 | |
37 /** XPath that points to the found cross section measurements. */ | |
38 public static final String XPATH_CROSS_SECTIONS | |
39 = "/cross-sections/cross-section"; | |
40 | |
41 /** The error message key that is thrown if an error occured while getting | |
42 * new data. */ | |
43 public static final String ERROR_GET_CROSS_SECTION | |
44 = "error_get_cross_section"; | |
45 | |
46 | |
47 /** | |
48 * Fetches positions (kms) at which measurements for given cross-sections | |
49 * exists. | |
50 * | |
51 * @param data Map of Integer (cross-section-id) to km. | |
52 * | |
53 */ | |
54 public Map<Integer,Double[]> getCrossSectionKMs( | |
55 String locale, | |
56 Map<Integer, Double> data, | |
57 int nNeighbours) | |
58 throws ServerException | |
59 { | |
60 logger.info("CrossSectionKMService.getCrossSectionKMs"); | |
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 Element crossSection = ec.create("cross-sections"); | |
71 | |
72 doc.appendChild(crossSection); | |
73 | |
74 for(Map.Entry<Integer, Double> oneCrossSection : data.entrySet()) { | |
75 Element cs = ec.create("cross-section"); | |
76 cs.setAttribute("id", oneCrossSection.getKey().toString()); | |
77 cs.setAttribute("km", oneCrossSection.getValue().toString()); | |
78 cs.setAttribute("n", Integer.valueOf(nNeighbours).toString()); | |
79 crossSection.appendChild(cs); | |
80 } | |
81 | |
82 HttpClient client = new HttpClientImpl(url, locale); | |
83 logger.debug("Created httpclient"); | |
84 | |
85 try { | |
86 // Document should contain: | |
87 // crosse-sections: | |
88 // attribute(id), attribute(km) attribute(n) | |
89 Document response = client.callService(url, "cross-section-km", doc); | |
90 //<cross-sections><cross-section id="1"><line km="19.5" line-id="189"/>... | |
91 | |
92 NodeList nodeList = (NodeList) XMLUtils.xpath(response, | |
93 XPATH_CROSS_SECTIONS, | |
94 XPathConstants.NODESET); | |
95 | |
96 int num = nodeList.getLength(); | |
97 | |
98 Map<Integer, Double[]> result = new HashMap<Integer, Double[]>(); | |
99 | |
100 try{ | |
101 for (int i = 0; i < num; i++) { | |
102 Element csElement = (Element) nodeList.item(i); | |
103 | |
104 int idx = Integer.parseInt(csElement.getAttribute("id")); | |
105 ArrayList<Double> kms = new ArrayList<Double>(); | |
106 | |
107 NodeList lineNodes = csElement.getElementsByTagName("line"); | |
108 int numLines = lineNodes.getLength(); | |
109 for (int k = 0; k < numLines; k++) { | |
110 Element line = (Element) lineNodes.item(i); | |
111 double d = Double.parseDouble(line.getAttribute("km")); | |
112 kms.add(d); | |
113 } | |
114 | |
115 Double[] doubles = new Double[kms.size()]; | |
116 kms.toArray(doubles); | |
117 result.put(Integer.valueOf(idx), doubles); | |
118 } | |
119 } | |
120 catch(NumberFormatException nfe) { | |
121 logger.error("Response was not parsable"); | |
122 } | |
123 | |
124 return result; | |
125 } | |
126 catch (ConnectionException ce) { | |
127 logger.error("ConnectionExsp", ce); | |
128 } | |
129 | |
130 logger.warn("CrossSectionKMService.getCrossSectionKMS() - FAILED"); | |
131 throw new ServerException(ERROR_GET_CROSS_SECTION); | |
132 } | |
133 } | |
134 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |