Mercurial > dive4elements > river
comparison flys-client/src/main/java/de/intevation/flys/client/server/SedimentLoadInfoServiceImpl.java @ 4298:1f98a3a89e13
New client side service for sediment load infos.
The service requests an sediment load overview and creates info objects for the
received data.
author | Raimund Renkert <rrenkert@intevation.de> |
---|---|
date | Mon, 29 Oct 2012 13:13:23 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
4297:6d44914b2dd4 | 4298:1f98a3a89e13 |
---|---|
1 package de.intevation.flys.client.server; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.List; | |
5 | |
6 import org.apache.log4j.Logger; | |
7 import org.w3c.dom.Document; | |
8 import org.w3c.dom.Element; | |
9 import org.w3c.dom.NodeList; | |
10 | |
11 import com.google.gwt.user.server.rpc.RemoteServiceServlet; | |
12 | |
13 import de.intevation.artifacts.common.ArtifactNamespaceContext; | |
14 import de.intevation.artifacts.common.utils.XMLUtils; | |
15 import de.intevation.artifacts.httpclient.exceptions.ConnectionException; | |
16 import de.intevation.artifacts.httpclient.http.HttpClient; | |
17 import de.intevation.artifacts.httpclient.http.HttpClientImpl; | |
18 import de.intevation.flys.client.client.services.SedimentLoadInfoService; | |
19 import de.intevation.flys.client.shared.exceptions.ServerException; | |
20 import de.intevation.flys.client.shared.model.SedimentLoadInfoObject; | |
21 import de.intevation.flys.client.shared.model.SedimentLoadInfoObjectImpl; | |
22 | |
23 | |
24 public class SedimentLoadInfoServiceImpl | |
25 extends RemoteServiceServlet | |
26 implements SedimentLoadInfoService | |
27 { | |
28 private static final Logger logger = | |
29 Logger.getLogger(SedimentLoadInfoServiceImpl.class); | |
30 | |
31 public static final String ERROR_NO_SEDIMENTLOADINFO_FOUND = | |
32 "error_no_sedimentloadinfo_found"; | |
33 | |
34 @Override | |
35 public SedimentLoadInfoObject[] getSedimentLoadInfo( | |
36 String locale, | |
37 String river, | |
38 String type, | |
39 double startKm, | |
40 double endKm) | |
41 throws ServerException | |
42 { | |
43 logger.info("SedimentLoadInfoServiceImpl.getSedimentLoadInfo"); | |
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 riverEl = ec.create("river"); | |
55 Element location = ec.create("location"); | |
56 Element from = ec.create("from"); | |
57 Element to = ec.create("to"); | |
58 Element typeEl = ec.create("type"); | |
59 riverEl.setTextContent(river); | |
60 from.setTextContent(String.valueOf(startKm)); | |
61 to.setTextContent(String.valueOf(endKm)); | |
62 typeEl.setTextContent(type); | |
63 | |
64 location.appendChild(from); | |
65 location.appendChild(to); | |
66 riverEl.appendChild(location); | |
67 riverEl.appendChild(typeEl); | |
68 doc.appendChild(riverEl); | |
69 | |
70 HttpClient client = new HttpClientImpl(url, locale); | |
71 | |
72 try { | |
73 Document result = client.callService(url, "sedimentloadinfo", doc); | |
74 | |
75 logger.debug("Extract sedimentload info objects now."); | |
76 SedimentLoadInfoObject[] objects = | |
77 extractSedimentLoadInfoObjects(result); | |
78 | |
79 if (objects != null && objects.length > 0) { | |
80 return objects; | |
81 } | |
82 } | |
83 catch (ConnectionException ce) { | |
84 logger.error(ce, ce); | |
85 } | |
86 | |
87 throw new ServerException(ERROR_NO_SEDIMENTLOADINFO_FOUND); | |
88 } | |
89 | |
90 | |
91 /** | |
92 * Extracts all distance info objects from <i>result</i> document. | |
93 * | |
94 * @param result The document retrieved by the server. | |
95 * | |
96 * @return a list of DistanceInfoObjects. | |
97 */ | |
98 protected SedimentLoadInfoObject[] extractSedimentLoadInfoObjects( | |
99 Document result) | |
100 throws ServerException | |
101 { | |
102 NodeList list = result.getElementsByTagName("sedimentload"); | |
103 | |
104 if (list == null || list.getLength() == 0) { | |
105 logger.warn("No sedimentload info found."); | |
106 throw new ServerException(ERROR_NO_SEDIMENTLOADINFO_FOUND); | |
107 } | |
108 | |
109 int num = list.getLength(); | |
110 logger.debug("Response contains " + num + " objects."); | |
111 | |
112 List<SedimentLoadInfoObject> objects = | |
113 new ArrayList<SedimentLoadInfoObject>(num); | |
114 | |
115 for (int i = 0; i < num; i++) { | |
116 SedimentLoadInfoObject obj = buildSedimentLoadInfoObject( | |
117 (Element)list.item(i)); | |
118 | |
119 if (obj != null) { | |
120 objects.add(obj); | |
121 } | |
122 } | |
123 | |
124 logger.debug("Retrieved " + objects.size() + " sediment loads."); | |
125 | |
126 return (SedimentLoadInfoObject[]) | |
127 objects.toArray(new SedimentLoadInfoObject[num]); | |
128 } | |
129 | |
130 | |
131 /** | |
132 * Extracts information for a single distance info object and intializes an | |
133 * DistanceInfoObject with them. | |
134 * | |
135 * @param node The node that contains the information. | |
136 * | |
137 * @return a valid DistanceInfoObject. | |
138 */ | |
139 protected SedimentLoadInfoObject buildSedimentLoadInfoObject(Element node) { | |
140 | |
141 String desc = node.getAttribute("description").trim(); | |
142 String date = node.getAttribute("date").trim(); | |
143 | |
144 if (desc.length() > 0 && date.length() > 0) { | |
145 return new SedimentLoadInfoObjectImpl(desc, date); | |
146 } | |
147 | |
148 logger.warn("Invalid sediment load info object found."); | |
149 | |
150 return null; | |
151 } | |
152 } |