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