Mercurial > dive4elements > river
comparison flys-client/src/main/java/de/intevation/flys/client/server/DischargeInfoServiceImpl.java @ 1600:67468c90ca68
Added service to request discharge infos at a specific gauge.
flys-client/trunk@3938 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Mon, 06 Feb 2012 16:01:22 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1599:4ba6f1fb3a03 | 1600:67468c90ca68 |
---|---|
1 package de.intevation.flys.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 de.intevation.artifacts.common.ArtifactNamespaceContext; | |
15 import de.intevation.artifacts.common.utils.XMLUtils; | |
16 | |
17 import de.intevation.artifacts.httpclient.exceptions.ConnectionException; | |
18 import de.intevation.artifacts.httpclient.http.HttpClient; | |
19 import de.intevation.artifacts.httpclient.http.HttpClientImpl; | |
20 | |
21 import de.intevation.flys.client.shared.exceptions.ServerException; | |
22 import de.intevation.flys.client.client.services.DischargeInfoService; | |
23 import de.intevation.flys.client.shared.model.DischargeInfoObject; | |
24 import de.intevation.flys.client.shared.model.DischargeInfoObjectImpl; | |
25 | |
26 | |
27 /** | |
28 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> | |
29 */ | |
30 public class DischargeInfoServiceImpl | |
31 extends RemoteServiceServlet | |
32 implements DischargeInfoService | |
33 { | |
34 private static final Logger logger = | |
35 Logger.getLogger(DischargeInfoServiceImpl.class); | |
36 | |
37 public static final String ERROR_NO_DISCHARGEINFO_FOUND = | |
38 "error_no_dischargeinfo_found"; | |
39 | |
40 public static final String XPATH_DISTANCES = "art:discharges/art:discharge"; | |
41 | |
42 | |
43 public DischargeInfoObject[] getDischargeInfo( | |
44 String locale, | |
45 long gauge) | |
46 throws ServerException | |
47 { | |
48 logger.info("DichargeInfoServiceImpl.getDischargeInfo"); | |
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 gaugeEl = ec.create("gauge"); | |
60 gaugeEl.setTextContent(String.valueOf(gauge)); | |
61 | |
62 doc.appendChild(gaugeEl); | |
63 | |
64 HttpClient client = new HttpClientImpl(url, locale); | |
65 | |
66 try { | |
67 Document result = client.callService(url, "dischargeinfo", doc); | |
68 | |
69 logger.debug("Extract discharge info objects now."); | |
70 DischargeInfoObject[] objects = extractDischargeInfoObjects(result); | |
71 | |
72 if (objects != null && objects.length > 0) { | |
73 return objects; | |
74 } | |
75 } | |
76 catch (ConnectionException ce) { | |
77 logger.error(ce, ce); | |
78 } | |
79 | |
80 throw new ServerException(ERROR_NO_DISCHARGEINFO_FOUND); | |
81 } | |
82 | |
83 protected DischargeInfoObject[] extractDischargeInfoObjects( | |
84 Document result | |
85 ) | |
86 throws ServerException { | |
87 NodeList list = result.getElementsByTagName("discharge"); | |
88 | |
89 if (list == null || list.getLength() == 0) { | |
90 logger.warn("No discharge info found."); | |
91 throw new ServerException(ERROR_NO_DISCHARGEINFO_FOUND); | |
92 } | |
93 | |
94 int num = list.getLength(); | |
95 logger.debug("Response contains " + num + " objects."); | |
96 | |
97 List<DischargeInfoObject> objects = | |
98 new ArrayList<DischargeInfoObject>(num); | |
99 | |
100 for (int i = 0; i < num; i++) { | |
101 DischargeInfoObject obj = buildDischargeInfoObject( | |
102 (Element)list.item(i)); | |
103 | |
104 if (obj != null) { | |
105 objects.add(obj); | |
106 } | |
107 } | |
108 | |
109 logger.debug("Retrieved " + objects.size() + " discharges."); | |
110 | |
111 return (DischargeInfoObject[]) | |
112 objects.toArray(new DischargeInfoObject[num]); | |
113 | |
114 } | |
115 | |
116 protected DischargeInfoObject buildDischargeInfoObject(Element node) { | |
117 | |
118 String desc = node.getAttribute("description").trim(); | |
119 String start = node.getAttribute("start").trim(); | |
120 String end = node.getAttribute("end").trim(); | |
121 | |
122 if (start.length() > 0 && end.length() > 0) { | |
123 try { | |
124 Integer startYear = new Integer(start); | |
125 Integer endYear = new Integer(end); | |
126 return new DischargeInfoObjectImpl(desc, startYear, endYear); | |
127 } | |
128 catch (NumberFormatException nfe) { | |
129 logger.warn(nfe.getLocalizedMessage()); | |
130 } | |
131 } | |
132 | |
133 logger.warn("Invalid distance info object found."); | |
134 | |
135 return null; | |
136 } | |
137 } | |
138 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |