Mercurial > dive4elements > river
comparison gwt-client/src/main/java/org/dive4elements/river/client/server/AbstractMainValuesServiceImpl.java @ 9288:82c67b859aa7
bundu.bezugswst worklflow incl. service impl for mainValues to be calculated
author | gernotbelger |
---|---|
date | Tue, 24 Jul 2018 10:39:03 +0200 |
parents | |
children | bc9a45d2b1fa |
comparison
equal
deleted
inserted
replaced
9287:6c88ad449c83 | 9288:82c67b859aa7 |
---|---|
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde | |
2 * Software engineering by Intevation GmbH | |
3 * | |
4 * This file is Free Software under the GNU AGPL (>=v3) | |
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
6 * documentation coming with Dive4Elements River for details. | |
7 */ | |
8 | |
9 package org.dive4elements.river.client.server; | |
10 | |
11 import java.util.ArrayList; | |
12 import java.util.Arrays; | |
13 import java.util.Calendar; | |
14 import java.util.Comparator; | |
15 import java.util.List; | |
16 | |
17 import javax.xml.xpath.XPathConstants; | |
18 | |
19 import org.apache.log4j.Logger; | |
20 import org.dive4elements.artifacts.common.ArtifactNamespaceContext; | |
21 import org.dive4elements.artifacts.common.utils.XMLUtils; | |
22 import org.dive4elements.artifacts.httpclient.exceptions.ConnectionException; | |
23 import org.dive4elements.artifacts.httpclient.http.HttpClient; | |
24 import org.dive4elements.artifacts.httpclient.http.HttpClientImpl; | |
25 import org.dive4elements.river.client.shared.exceptions.ServerException; | |
26 import org.dive4elements.river.client.shared.model.WQInfoObject; | |
27 import org.dive4elements.river.client.shared.model.WQInfoObjectImpl; | |
28 import org.w3c.dom.Document; | |
29 import org.w3c.dom.Element; | |
30 import org.w3c.dom.Node; | |
31 import org.w3c.dom.NodeList; | |
32 | |
33 import com.google.gwt.user.server.rpc.RemoteServiceServlet; | |
34 | |
35 /** | |
36 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> | |
37 */ | |
38 abstract class AbstractMainValuesServiceImpl extends RemoteServiceServlet { | |
39 | |
40 private static final Logger log = Logger.getLogger(DynamicMainValuesServiceImpl.class); | |
41 | |
42 private static final Comparator<WQInfoObject> WQ_INFO_OBJECT_CMP = new Comparator<WQInfoObject>() { | |
43 @Override | |
44 public int compare(final WQInfoObject a, final WQInfoObject b) { | |
45 | |
46 // Descending by type: Qs before Ds | |
47 final int cmp = a.getType().compareTo(b.getType()); | |
48 if (cmp < 0) | |
49 return +1; | |
50 if (cmp > 0) | |
51 return -1; | |
52 | |
53 // Ascending by value | |
54 final double diff = a.getValue() - b.getValue(); | |
55 if (diff < 0d) | |
56 return -1; | |
57 if (diff > 0d) | |
58 return +1; | |
59 return 0; | |
60 } | |
61 }; | |
62 | |
63 private static final String ERROR_NO_WQINFO_FOUND = "error_no_wqinfo_found"; | |
64 | |
65 private static final String XPATH_WQS = "art:service/art:mainvalues/art:mainvalue"; | |
66 | |
67 private static final long serialVersionUID = 1L; | |
68 | |
69 protected final Document createInput(final String river, final double from, final double to, final Integer startYear, final Integer endYear) { | |
70 | |
71 final Document input = XMLUtils.newDocument(); | |
72 | |
73 final XMLUtils.ElementCreator ec = new XMLUtils.ElementCreator(input, ArtifactNamespaceContext.NAMESPACE_URI, | |
74 ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
75 | |
76 final Element mainvalues = ec.create("mainvalues"); | |
77 | |
78 final Element riverEl = ec.create("river"); | |
79 riverEl.setTextContent(river); | |
80 mainvalues.appendChild(riverEl); | |
81 | |
82 final Element startEl = ec.create("start"); | |
83 startEl.setTextContent(Double.valueOf(from).toString()); | |
84 mainvalues.appendChild(startEl); | |
85 | |
86 final Element endEl = ec.create("end"); | |
87 endEl.setTextContent(Double.valueOf(to).toString()); | |
88 mainvalues.appendChild(endEl); | |
89 | |
90 if (startYear != null) { | |
91 final Element startYearElement = ec.create("startYear"); | |
92 startYearElement.setTextContent(Integer.toString(startYear)); | |
93 mainvalues.appendChild(startYearElement); | |
94 } | |
95 | |
96 if (endYear != null) { | |
97 final Element endYearElement = ec.create("endYear"); | |
98 endYearElement.setTextContent(Integer.toString(endYear)); | |
99 mainvalues.appendChild(endYearElement); | |
100 } | |
101 | |
102 input.appendChild(mainvalues); | |
103 | |
104 return input; | |
105 } | |
106 | |
107 protected final WQInfoObject[] callService(final String serviceName, final Document input, final String locale) throws ServerException { | |
108 final String url = getServletContext().getInitParameter("server-url"); | |
109 | |
110 final HttpClient client = new HttpClientImpl(url, locale); | |
111 | |
112 try { | |
113 final Document result = client.callService(url, serviceName, input); | |
114 | |
115 log.debug("Extract wq info objects now."); | |
116 final WQInfoObject[] objects = extractWQInfoObjects(result); | |
117 | |
118 if (objects.length > 0) { | |
119 return objects; | |
120 } | |
121 } | |
122 catch (final ConnectionException ce) { | |
123 log.error(ce, ce); | |
124 } | |
125 | |
126 throw new ServerException(ERROR_NO_WQINFO_FOUND); | |
127 } | |
128 | |
129 /** | |
130 * Extracts all wq info objects from <i>result</i> document. | |
131 * | |
132 * @param result | |
133 * The document retrieved by the server. | |
134 * | |
135 * @return a list of WQInfoObjects. | |
136 */ | |
137 private WQInfoObject[] extractWQInfoObjects(final Document result) throws ServerException { | |
138 final NodeList list = (NodeList) XMLUtils.xpath(result, XPATH_WQS, XPathConstants.NODESET, ArtifactNamespaceContext.INSTANCE); | |
139 | |
140 if (list == null || list.getLength() == 0) { | |
141 log.warn("No wq info found."); | |
142 | |
143 throw new ServerException(ERROR_NO_WQINFO_FOUND); | |
144 } | |
145 | |
146 final boolean debug = log.isDebugEnabled(); | |
147 | |
148 final int num = list.getLength(); | |
149 if (debug) { | |
150 log.debug("Response contains " + num + " objects."); | |
151 } | |
152 | |
153 final List<WQInfoObject> objects = new ArrayList<WQInfoObject>(num); | |
154 | |
155 for (int i = 0; i < num; i++) { | |
156 final WQInfoObject obj = buildWQInfoObject(list.item(i)); | |
157 | |
158 if (obj != null) { | |
159 objects.add(obj); | |
160 } | |
161 } | |
162 | |
163 if (debug) { | |
164 log.debug("Retrieved " + objects.size() + " wq values"); | |
165 } | |
166 | |
167 final WQInfoObject[] array = objects.toArray(new WQInfoObject[objects.size()]); | |
168 | |
169 Arrays.sort(array, WQ_INFO_OBJECT_CMP); | |
170 | |
171 return array; | |
172 } | |
173 | |
174 /** | |
175 * Extracts information for a single wq info object and intializes an | |
176 * WQInfoObject with them. | |
177 * | |
178 * @param node | |
179 * The node that contains the information. | |
180 * | |
181 * @return a valid WQInfoObject. | |
182 */ | |
183 private static WQInfoObject buildWQInfoObject(final Node node) { | |
184 | |
185 final String name = XMLUtils.xpathString(node, "@name", ArtifactNamespaceContext.INSTANCE); | |
186 | |
187 final String type = XMLUtils.xpathString(node, "@type", ArtifactNamespaceContext.INSTANCE); | |
188 | |
189 final String value = XMLUtils.xpathString(node, "@value", ArtifactNamespaceContext.INSTANCE); | |
190 | |
191 final String official = XMLUtils.xpathString(node, "@official", ArtifactNamespaceContext.INSTANCE); | |
192 | |
193 final String starttime = XMLUtils.xpathString(node, "@starttime", ArtifactNamespaceContext.INSTANCE); | |
194 | |
195 final String stoptime = XMLUtils.xpathString(node, "@stoptime", ArtifactNamespaceContext.INSTANCE); | |
196 | |
197 if (name != null && type != null) { | |
198 try { | |
199 final Calendar cal = Calendar.getInstance(); | |
200 java.util.Date start = null; | |
201 java.util.Date stop = null; | |
202 if (!starttime.equals("")) { | |
203 cal.setTimeInMillis(Long.parseLong(starttime)); | |
204 start = cal.getTime(); | |
205 } | |
206 if (!stoptime.equals("")) { | |
207 cal.setTimeInMillis(Long.parseLong(stoptime)); | |
208 stop = cal.getTime(); | |
209 } | |
210 return new WQInfoObjectImpl(name, type, new Double(value), official != null && official.equalsIgnoreCase("true"), start, stop); | |
211 } | |
212 catch (final NumberFormatException nfe) { | |
213 log.warn(nfe.getLocalizedMessage()); | |
214 } | |
215 } | |
216 | |
217 log.warn("Invalid wq info object found."); | |
218 | |
219 return null; | |
220 } | |
221 } |