comparison gwt-client/src/main/java/org/dive4elements/river/client/server/SedimentLoadInfoServiceImpl.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/SedimentLoadInfoServiceImpl.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.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 org.dive4elements.artifacts.common.ArtifactNamespaceContext;
14 import org.dive4elements.artifacts.common.utils.XMLUtils;
15 import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException;
16 import org.dive4elements.artifacts.httpclient.http.HttpClient;
17 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl;
18 import org.dive4elements.river.client.client.services.SedimentLoadInfoService;
19 import org.dive4elements.river.client.shared.exceptions.ServerException;
20 import org.dive4elements.river.client.shared.model.SedimentLoadInfoObject;
21 import org.dive4elements.river.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 }

http://dive4elements.wald.intevation.org