9404
|
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.artifacts.services; |
|
10 |
|
11 import java.util.ArrayList; |
|
12 import java.util.Calendar; |
|
13 import java.util.Date; |
|
14 import java.util.List; |
|
15 |
|
16 import javax.xml.xpath.XPathConstants; |
|
17 |
|
18 import org.dive4elements.artifacts.CallMeta; |
|
19 import org.dive4elements.artifacts.GlobalContext; |
|
20 import org.dive4elements.artifacts.common.ArtifactNamespaceContext; |
|
21 import org.dive4elements.artifacts.common.utils.XMLUtils; |
|
22 import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator; |
|
23 import org.dive4elements.river.artifacts.resources.Resources; |
|
24 import org.dive4elements.river.artifacts.services.AbstractMainValuesService.MainValuesServiceException; |
|
25 import org.dive4elements.river.model.Gauge; |
|
26 import org.dive4elements.river.model.River; |
|
27 import org.dive4elements.river.model.sinfo.DailyDischargeValue; |
9405
|
28 import org.dive4elements.river.model.sinfo.DailyDischargeValue.MinMax; |
9404
|
29 import org.w3c.dom.Document; |
|
30 import org.w3c.dom.Element; |
|
31 import org.w3c.dom.NodeList; |
|
32 |
|
33 /** |
|
34 * This service returns the main values of a river's gauge based on the start |
|
35 * and end point of the river. |
|
36 * |
|
37 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
|
38 */ |
|
39 public class DynamicMainValuesTimeRangeDeterminationService extends D4EService { |
|
40 |
|
41 private static final long serialVersionUID = 1L; |
|
42 |
|
43 private static final String ROOT_NODE = "dynamic-mainvalues-input"; |
|
44 |
|
45 private static final Long DATE_DELTA_ERROR_MSG = (long) (60 * 60 * 24 * 1000); |
|
46 |
|
47 public static final class ServiceException extends Exception { |
|
48 |
|
49 private static final long serialVersionUID = 1L; |
|
50 |
|
51 public ServiceException(final String message) { |
|
52 super(message); |
|
53 } |
|
54 } |
|
55 |
|
56 /** |
|
57 * Computes a gauge's main values for a period of time based on its daily discharges stored in the database |
|
58 * |
|
59 * @throws Exception |
|
60 */ |
|
61 |
|
62 private static class GaugeInfoResult { |
|
63 private final String globalErrorMsg; |
|
64 private final List<GaugeInfo> gaugeInfos; |
|
65 |
|
66 private GaugeInfoResult(final List<GaugeInfo> gaugeInfos, final String globalErrorMsg) { |
|
67 this.gaugeInfos = gaugeInfos; |
|
68 this.globalErrorMsg = globalErrorMsg; |
|
69 } |
|
70 |
|
71 private static class GaugeInfo { |
|
72 private final String errorMsg; |
|
73 private final Gauge gauge; |
|
74 private final Date startdate; |
|
75 private final Date enddate; |
|
76 |
|
77 public GaugeInfo(final String errorMsg, final Gauge gauge, final Date startdate, final Date enddate) { |
|
78 this.errorMsg = errorMsg; |
|
79 this.gauge = gauge; |
|
80 this.startdate = startdate; |
|
81 this.enddate = enddate; |
|
82 } |
|
83 } |
|
84 } |
|
85 |
|
86 private GaugeInfoResult getCommonTimeRangeForGauges(final List<Gauge> gauges, final Date startTime, final Date endTime, final CallMeta meta) |
|
87 throws ServiceException { |
|
88 |
|
89 // Query the gauge's daily Q values |
|
90 final List<GaugeInfoResult.GaugeInfo> gaugeResults = new ArrayList<>(); |
|
91 Date min = startTime; |
|
92 Date max = endTime; |
|
93 |
|
94 for (final Gauge gauge : gauges) { |
|
95 |
9405
|
96 final Date minGlobalForGauge = DailyDischargeValue.getGlobalMinMax(gauge, MinMax.min); |
|
97 final Date maxGlobalForGauge = DailyDischargeValue.getGlobalMinMax(gauge, MinMax.max); |
|
98 if (minGlobalForGauge == null || maxGlobalForGauge == null) { // der Fall, dass nur eins von beiden null ist, kann eigentlich nciht vorkommen |
9404
|
99 gaugeResults.add(new GaugeInfoResult.GaugeInfo(getMsg(meta, "bundu.wst_no_data_at_all"), gauge, null, null)); |
|
100 // TODO : wenn der Workflow abgebrochen werden soll, GlobalErrorMsg setzen, dass mind. ein Pegel überhaupt keine Daten |
|
101 // hat (der Mechnismus auf Client-Seite ist schon implementiert) |
|
102 |
|
103 continue; |
|
104 } |
|
105 |
|
106 if (minGlobalForGauge.getTime() > startTime.getTime()) |
|
107 min = minGlobalForGauge; |
|
108 |
|
109 if (maxGlobalForGauge.getTime() < endTime.getTime()) |
|
110 max = maxGlobalForGauge; |
|
111 |
|
112 String errormsg = null; |
|
113 if ((maxGlobalForGauge.getTime() < endTime.getTime()) || (minGlobalForGauge.getTime() > startTime.getTime())) |
|
114 errormsg = makeDoesNotCoverErrorMsg(minGlobalForGauge, maxGlobalForGauge, meta); |
|
115 |
|
116 gaugeResults.add(new GaugeInfoResult.GaugeInfo(errormsg, gauge, min, max)); |
|
117 } |
|
118 |
|
119 // common Range and correct errorMsg |
|
120 final List<GaugeInfoResult.GaugeInfo> gaugeResultsSecondTurn = new ArrayList<>(); |
|
121 for (final GaugeInfoResult.GaugeInfo gi : gaugeResults) { |
|
122 gaugeResultsSecondTurn |
|
123 .add(new GaugeInfoResult.GaugeInfo(gi.errorMsg, gi.gauge, gi.startdate != null ? min : null, gi.enddate != null ? max : null)); |
|
124 } |
|
125 final String globalErrorMsg = (min.getTime() > max.getTime()) ? getMsg(meta, "bundu.wst.gauge_timeranges_disjoint") : ""; |
|
126 final GaugeInfoResult result = new GaugeInfoResult(gaugeResultsSecondTurn, globalErrorMsg); |
|
127 |
|
128 return result; |
|
129 } |
|
130 |
|
131 private String makeDoesNotCoverErrorMsg(final Date start, final Date end, final CallMeta meta) { |
|
132 final Calendar cal = Calendar.getInstance(); |
|
133 cal.setTime(start); |
|
134 final String startyear = String.valueOf(cal.get(Calendar.YEAR)); |
|
135 cal.setTime(end); |
|
136 final String endyear = String.valueOf(cal.get(Calendar.YEAR)); |
|
137 return Resources.getMsg(meta, "bundu.wst.range_does_not_cover", new Object[] { startyear, endyear }); |
|
138 } |
|
139 |
|
140 @Override |
|
141 public Document doProcess(final Document data, final GlobalContext context, final CallMeta callMeta) { |
|
142 try { |
|
143 final River river = AbstractMainValuesService.getRequestedRiver(data, "/art:" + this.ROOT_NODE + "/art:river/text()"); |
|
144 final List<Gauge> gauges = getRequestedGauges(data, river, callMeta); |
|
145 final Date start = getRequestedStartYear(data, "/art:" + this.ROOT_NODE + "/art:startYear/text()"); |
|
146 final Date end = getRequestedEndYear(data, "/art:" + this.ROOT_NODE + "/art:endYear/text()"); |
|
147 |
|
148 final GaugeInfoResult result = getCommonTimeRangeForGauges(gauges, start, end, callMeta); |
|
149 |
|
150 return buildDocument(result, context, callMeta); |
|
151 } |
|
152 catch (final ServiceException | MainValuesServiceException e) { |
|
153 e.printStackTrace(); |
|
154 return AbstractMainValuesService.error(e.getMessage()); |
|
155 } |
|
156 } |
|
157 |
|
158 public static final Date getRequestedEndYear(final Document data, final String XPATH_END_YEAR) throws MainValuesServiceException { |
|
159 |
|
160 final String endStr = XMLUtils.xpathString(data, XPATH_END_YEAR, ArtifactNamespaceContext.INSTANCE); |
|
161 |
|
162 if (endStr == null) |
|
163 throw new MainValuesServiceException("no end year"); // should not happen |
|
164 |
|
165 try { |
|
166 final int year = Integer.parseInt(endStr); |
|
167 |
|
168 // FIXME: timezone? probably must match timezone of database |
|
169 final Calendar cal = Calendar.getInstance(); |
|
170 cal.clear(); |
|
171 cal.set(year, 11, 31); |
|
172 return cal.getTime(); |
|
173 } |
|
174 catch (final NumberFormatException e) { |
|
175 e.printStackTrace(); |
|
176 throw new MainValuesServiceException("invalid end year"); // should not happen |
|
177 } |
|
178 } |
|
179 |
|
180 public static final Date getRequestedStartYear(final Document data, final String XPATH_START_YEAR) throws MainValuesServiceException { |
|
181 |
|
182 final String startStr = XMLUtils.xpathString(data, XPATH_START_YEAR, ArtifactNamespaceContext.INSTANCE); |
|
183 |
|
184 if (startStr == null) |
|
185 throw new MainValuesServiceException("no start year");// should not happen |
|
186 |
|
187 try { |
|
188 final int year = Integer.parseInt(startStr); |
|
189 |
|
190 // FIXME: timezone? probably must match timezone of database |
|
191 final Calendar cal = Calendar.getInstance(); |
|
192 cal.clear(); |
|
193 cal.set(year, 0, 1); |
|
194 return cal.getTime(); |
|
195 } |
|
196 catch (final NumberFormatException e) { |
|
197 e.printStackTrace(); |
|
198 throw new MainValuesServiceException("invalid start year"); // should not happen |
|
199 } |
|
200 } |
|
201 |
|
202 private Document buildDocument(final GaugeInfoResult result, final GlobalContext context, final CallMeta meta) { |
|
203 |
|
204 final Document doc = XMLUtils.newDocument(); |
|
205 |
|
206 final ElementCreator cr = new ElementCreator(doc, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX); |
|
207 |
|
208 final Element rootEl = cr.create(ROOT_NODE); |
|
209 |
|
210 doc.appendChild(rootEl); |
|
211 |
|
212 final Element globalErrElement = cr.create("global-error-msg"); |
|
213 globalErrElement.setTextContent(result.globalErrorMsg); |
|
214 rootEl.appendChild(globalErrElement); |
|
215 |
|
216 final List<GaugeInfoResult.GaugeInfo> values = result.gaugeInfos; |
|
217 |
|
218 for (final GaugeInfoResult.GaugeInfo gauge : values) { |
|
219 final Element gaugeElement = cr.create("gauge"); |
|
220 cr.addAttr(gaugeElement, "name", gauge.gauge.getName()); |
|
221 if (gauge.startdate != null) |
|
222 cr.addAttr(gaugeElement, "date-from", String.valueOf(gauge.startdate.getTime())); |
|
223 |
|
224 if (gauge.enddate != null) |
|
225 cr.addAttr(gaugeElement, "date-to", String.valueOf(gauge.enddate.getTime())); |
|
226 |
|
227 if (gauge.errorMsg != null) |
|
228 cr.addAttr(gaugeElement, "error-message", gauge.errorMsg); |
|
229 |
|
230 rootEl.appendChild(gaugeElement); |
|
231 } |
|
232 |
|
233 return doc; |
|
234 |
|
235 } |
|
236 |
|
237 final Element buildElement(final ElementCreator cr, final String type, final Date date) { |
|
238 final Element el = cr.create(type); |
|
239 cr.addAttr(el, "value", String.valueOf(date.getTime())); |
|
240 return el; |
|
241 } |
|
242 |
|
243 private static final List<Gauge> getRequestedGauges(final Document data, final River river, final CallMeta meta) throws ServiceException { |
|
244 |
|
245 final NodeList gaugeNodes = data.getElementsByTagNameNS(ArtifactNamespaceContext.NAMESPACE_URI, "gauge"); |
|
246 |
|
247 final List<Gauge> gauges = new ArrayList<>(); |
|
248 |
|
249 for (int i = 0; i < gaugeNodes.getLength(); i++) { |
|
250 final Element gaugeElt = (Element) gaugeNodes.item(i); |
|
251 |
|
252 final String gaugeName = (String) XMLUtils.xpath(gaugeElt, "text()", XPathConstants.STRING); |
|
253 final Gauge gauge = Gauge.getGaugeByNameAndRiver(gaugeName, river); |
|
254 if (gauge != null) |
|
255 gauges.add(gauge); |
|
256 else { |
|
257 throw new ServiceException("bundu_wst_error_reading_gauges"); |
|
258 } |
|
259 } |
|
260 |
|
261 return gauges; |
|
262 } |
|
263 |
|
264 private static String getMsg(final CallMeta meta, final String key) { |
|
265 return Resources.getMsg(meta, key); |
|
266 } |
|
267 } |