Mercurial > dive4elements > river
comparison flys-client/src/main/java/de/intevation/flys/client/server/DistanceInfoServiceImpl.java @ 219:7523faf567e5
Implemented a service to fetch distance information of a river from the artifact server.
flys-client/trunk@1662 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Mon, 11 Apr 2011 14:32:36 +0000 |
parents | |
children | 924da6695800 |
comparison
equal
deleted
inserted
replaced
218:79fb4d900643 | 219:7523faf567e5 |
---|---|
1 package de.intevation.flys.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.Node; | |
11 import org.w3c.dom.NodeList; | |
12 | |
13 import com.google.gwt.user.server.rpc.RemoteServiceServlet; | |
14 | |
15 import de.intevation.artifacts.common.ArtifactNamespaceContext; | |
16 import de.intevation.artifacts.common.utils.XMLUtils; | |
17 | |
18 import de.intevation.artifacts.httpclient.exceptions.ConnectionException; | |
19 import de.intevation.artifacts.httpclient.http.HttpClient; | |
20 import de.intevation.artifacts.httpclient.http.HttpClientImpl; | |
21 | |
22 import de.intevation.flys.client.shared.exceptions.ServerException; | |
23 import de.intevation.flys.client.client.services.DistanceInfoService; | |
24 import de.intevation.flys.client.shared.model.DistanceInfoObject; | |
25 import de.intevation.flys.client.shared.model.DistanceInfoObjectImpl; | |
26 | |
27 | |
28 /** | |
29 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
30 */ | |
31 public class DistanceInfoServiceImpl | |
32 extends RemoteServiceServlet | |
33 implements DistanceInfoService | |
34 { | |
35 public static final String ERROR_NO_DISTANCEINFO_FOUND = | |
36 "error_no_distanceinfo_found"; | |
37 | |
38 public static final String XPATH_DISTANCES = "art:distances/art:distance"; | |
39 | |
40 | |
41 public DistanceInfoObject[] getDistanceInfo(String url, String river) | |
42 throws ServerException | |
43 { | |
44 System.out.println("DistanceInfoServiceImpl.getDistanceInfo"); | |
45 | |
46 Document doc = XMLUtils.newDocument(); | |
47 | |
48 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( | |
49 doc, | |
50 ArtifactNamespaceContext.NAMESPACE_URI, | |
51 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
52 | |
53 Element riverEl = ec.create("river"); | |
54 | |
55 riverEl.setTextContent(river); | |
56 | |
57 doc.appendChild(riverEl); | |
58 | |
59 HttpClient client = new HttpClientImpl(url); | |
60 | |
61 try { | |
62 Document result = client.callService(url, "distanceinfo", doc); | |
63 | |
64 System.out.println("Extract distance info objects now."); | |
65 DistanceInfoObject[] objects = extractDistanceInfoObjects(result); | |
66 | |
67 if (objects != null && objects.length > 0) { | |
68 return objects; | |
69 } | |
70 } | |
71 catch (ConnectionException ce) { | |
72 System.err.println(ce.getLocalizedMessage()); | |
73 } | |
74 | |
75 throw new ServerException(ERROR_NO_DISTANCEINFO_FOUND); | |
76 } | |
77 | |
78 | |
79 /** | |
80 * Extracts all distance info objects from <i>result</i> document. | |
81 * | |
82 * @param result The document retrieved by the server. | |
83 * | |
84 * @return a list of DistanceInfoObjects. | |
85 */ | |
86 protected DistanceInfoObject[] extractDistanceInfoObjects(Document result) | |
87 throws ServerException | |
88 { | |
89 NodeList list = (NodeList) XMLUtils.xpath( | |
90 result, | |
91 XPATH_DISTANCES, | |
92 XPathConstants.NODESET, | |
93 ArtifactNamespaceContext.INSTANCE); | |
94 | |
95 if (list == null || list.getLength() == 0) { | |
96 System.err.println("No distance info found."); | |
97 | |
98 throw new ServerException(ERROR_NO_DISTANCEINFO_FOUND); | |
99 } | |
100 | |
101 int num = list.getLength(); | |
102 System.out.println("Response contains " + num + " objects."); | |
103 | |
104 List<DistanceInfoObject> objects = | |
105 new ArrayList<DistanceInfoObject>(num); | |
106 | |
107 for (int i = 0; i < num; i++) { | |
108 DistanceInfoObject obj = buildDistanceInfoObject(list.item(i)); | |
109 | |
110 if (obj != null) { | |
111 objects.add(obj); | |
112 } | |
113 } | |
114 | |
115 System.out.println("Retrieved " + objects.size() + " distances."); | |
116 | |
117 return (DistanceInfoObject[]) | |
118 objects.toArray(new DistanceInfoObject[num]); | |
119 } | |
120 | |
121 | |
122 /** | |
123 * Extracts information for a single distance info object and intializes an | |
124 * DistanceInfoObject with them. | |
125 * | |
126 * @param node The node that contains the information. | |
127 * | |
128 * @return a valid DistanceInfoObject. | |
129 */ | |
130 protected DistanceInfoObject buildDistanceInfoObject(Node node) { | |
131 String desc = XMLUtils.xpathString( | |
132 node, "@art:description", ArtifactNamespaceContext.INSTANCE); | |
133 | |
134 String from = XMLUtils.xpathString( | |
135 node, "@art:from", ArtifactNamespaceContext.INSTANCE); | |
136 | |
137 String to = XMLUtils.xpathString( | |
138 node, "@art:to", ArtifactNamespaceContext.INSTANCE); | |
139 | |
140 String riverside = XMLUtils.xpathString( | |
141 node, "@art:riverside", ArtifactNamespaceContext.INSTANCE); | |
142 | |
143 if (desc != null && from != null) { | |
144 try { | |
145 return new DistanceInfoObjectImpl( | |
146 desc, | |
147 new Double(from), | |
148 to != null && to.trim().length() > 0 ? new Double(to) : null, | |
149 riverside); | |
150 } | |
151 catch (NumberFormatException nfe) { | |
152 System.err.println(nfe.getLocalizedMessage()); | |
153 } | |
154 } | |
155 | |
156 System.err.println("Invalid distance info object found."); | |
157 | |
158 return null; | |
159 } | |
160 } | |
161 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |