Mercurial > dive4elements > river
comparison flys-client/src/main/java/org/dive4elements/river/client/server/WQInfoServiceImpl.java @ 5834:f507086aa94b
Repaired internal references.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 12:31:32 +0200 |
parents | flys-client/src/main/java/de/intevation/flys/client/server/WQInfoServiceImpl.java@8fce35702908 |
children | 821a02bbfb4e |
comparison
equal
deleted
inserted
replaced
5833:a2bdc0f524e8 | 5834:f507086aa94b |
---|---|
1 package de.intevation.flys.client.server; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.Arrays; | |
5 import java.util.Comparator; | |
6 import java.util.List; | |
7 | |
8 import javax.xml.xpath.XPathConstants; | |
9 | |
10 import org.w3c.dom.Document; | |
11 import org.w3c.dom.Element; | |
12 import org.w3c.dom.Node; | |
13 import org.w3c.dom.NodeList; | |
14 | |
15 import org.apache.log4j.Logger; | |
16 | |
17 import com.google.gwt.user.server.rpc.RemoteServiceServlet; | |
18 | |
19 import de.intevation.artifacts.common.ArtifactNamespaceContext; | |
20 import de.intevation.artifacts.common.utils.XMLUtils; | |
21 | |
22 import de.intevation.artifacts.httpclient.exceptions.ConnectionException; | |
23 import de.intevation.artifacts.httpclient.http.HttpClient; | |
24 import de.intevation.artifacts.httpclient.http.HttpClientImpl; | |
25 | |
26 import de.intevation.flys.client.shared.exceptions.ServerException; | |
27 import de.intevation.flys.client.client.services.WQInfoService; | |
28 import de.intevation.flys.client.shared.model.WQInfoObject; | |
29 import de.intevation.flys.client.shared.model.WQInfoObjectImpl; | |
30 | |
31 | |
32 /** | |
33 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
34 */ | |
35 public class WQInfoServiceImpl | |
36 extends RemoteServiceServlet | |
37 implements WQInfoService | |
38 { | |
39 private static final Logger logger = | |
40 Logger.getLogger(WQInfoServiceImpl.class); | |
41 | |
42 public static final String ERROR_NO_WQINFO_FOUND = | |
43 "error_no_wqinfo_found"; | |
44 | |
45 public static final String XPATH_WQS = "art:service/art:mainvalues/art:mainvalue"; | |
46 | |
47 | |
48 public WQInfoObject[] getWQInfo( | |
49 String locale, | |
50 String river, | |
51 double from, | |
52 double to) | |
53 throws ServerException | |
54 { | |
55 logger.info("WQInfoServiceImpl.getWQInfo"); | |
56 | |
57 String url = getServletContext().getInitParameter("server-url"); | |
58 | |
59 Document doc = XMLUtils.newDocument(); | |
60 | |
61 XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator( | |
62 doc, | |
63 ArtifactNamespaceContext.NAMESPACE_URI, | |
64 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
65 | |
66 Element mainvalues = ec.create("mainvalues"); | |
67 Element riverEl = ec.create("river"); | |
68 Element startEl = ec.create("start"); | |
69 Element endEl = ec.create("end"); | |
70 | |
71 riverEl.setTextContent(river); | |
72 startEl.setTextContent(Double.valueOf(from).toString()); | |
73 endEl.setTextContent(Double.valueOf(to).toString()); | |
74 | |
75 mainvalues.appendChild(riverEl); | |
76 mainvalues.appendChild(startEl); | |
77 mainvalues.appendChild(endEl); | |
78 | |
79 doc.appendChild(mainvalues); | |
80 | |
81 HttpClient client = new HttpClientImpl(url, locale); | |
82 | |
83 try { | |
84 Document result = client.callService(url, "mainvalues", doc); | |
85 | |
86 logger.debug("Extract wq info objects now."); | |
87 WQInfoObject[] objects = extractWQInfoObjects(result); | |
88 | |
89 if (objects.length > 0) { | |
90 return objects; | |
91 } | |
92 } | |
93 catch (ConnectionException ce) { | |
94 logger.error(ce, ce); | |
95 } | |
96 | |
97 throw new ServerException(ERROR_NO_WQINFO_FOUND); | |
98 } | |
99 | |
100 | |
101 /** | |
102 * Extracts all wq info objects from <i>result</i> document. | |
103 * | |
104 * @param result The document retrieved by the server. | |
105 * | |
106 * @return a list of WQInfoObjects. | |
107 */ | |
108 protected WQInfoObject[] extractWQInfoObjects(Document result) | |
109 throws ServerException | |
110 { | |
111 NodeList list = (NodeList) XMLUtils.xpath( | |
112 result, | |
113 XPATH_WQS, | |
114 XPathConstants.NODESET, | |
115 ArtifactNamespaceContext.INSTANCE); | |
116 | |
117 if (list == null || list.getLength() == 0) { | |
118 logger.warn("No wq info found."); | |
119 | |
120 throw new ServerException(ERROR_NO_WQINFO_FOUND); | |
121 } | |
122 | |
123 boolean debug = logger.isDebugEnabled(); | |
124 | |
125 int num = list.getLength(); | |
126 if (debug) { | |
127 logger.debug("Response contains " + num + " objects."); | |
128 } | |
129 | |
130 List<WQInfoObject> objects = | |
131 new ArrayList<WQInfoObject>(num); | |
132 | |
133 for (int i = 0; i < num; i++) { | |
134 WQInfoObject obj = buildWQInfoObject(list.item(i)); | |
135 | |
136 if (obj != null) { | |
137 objects.add(obj); | |
138 } | |
139 } | |
140 | |
141 if (debug) { | |
142 logger.debug("Retrieved " + objects.size() + " wq values"); | |
143 } | |
144 | |
145 WQInfoObject [] array = (WQInfoObject[]) | |
146 objects.toArray(new WQInfoObject[objects.size()]); | |
147 | |
148 Arrays.sort(array, WQ_INFO_OBJECT_CMP); | |
149 | |
150 return array; | |
151 } | |
152 | |
153 public static final Comparator<WQInfoObject> WQ_INFO_OBJECT_CMP = | |
154 new Comparator<WQInfoObject>() { | |
155 @Override | |
156 public int compare(WQInfoObject a, WQInfoObject b) { | |
157 | |
158 // Descending by type: Qs before Ds | |
159 int cmp = a.getType().compareTo(b.getType()); | |
160 if (cmp < 0) return +1; | |
161 if (cmp > 0) return -1; | |
162 | |
163 // Ascending by value | |
164 double diff = a.getValue() - b.getValue(); | |
165 if (diff < 0d) return -1; | |
166 if (diff > 0d) return +1; | |
167 return 0; | |
168 } | |
169 }; | |
170 | |
171 /** | |
172 * Extracts information for a single wq info object and intializes an | |
173 * WQInfoObject with them. | |
174 * | |
175 * @param node The node that contains the information. | |
176 * | |
177 * @return a valid WQInfoObject. | |
178 */ | |
179 protected static WQInfoObject buildWQInfoObject(Node node) { | |
180 | |
181 String name = XMLUtils.xpathString( | |
182 node, "@name", ArtifactNamespaceContext.INSTANCE); | |
183 | |
184 String type = XMLUtils.xpathString( | |
185 node, "@type", ArtifactNamespaceContext.INSTANCE); | |
186 | |
187 String value = XMLUtils.xpathString( | |
188 node, "@value", ArtifactNamespaceContext.INSTANCE); | |
189 | |
190 if (name != null && type != null) { | |
191 try { | |
192 return new WQInfoObjectImpl( | |
193 name, | |
194 type, | |
195 new Double(value)); | |
196 } | |
197 catch (NumberFormatException nfe) { | |
198 logger.warn(nfe.getLocalizedMessage()); | |
199 } | |
200 } | |
201 | |
202 logger.warn("Invalid wq info object found."); | |
203 | |
204 return null; | |
205 } | |
206 } | |
207 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |