Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/services/DynamicMainValuesTimeRangeDeterminationService.java @ 9409:38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
author | mschaefer |
---|---|
date | Thu, 16 Aug 2018 08:47:41 +0200 |
parents | 34cd4faf43f4 |
children | 52314c4ab3be |
rev | line source |
---|---|
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; | |
28 import org.w3c.dom.Document; | |
29 import org.w3c.dom.Element; | |
30 import org.w3c.dom.NodeList; | |
31 | |
32 /** | |
9409
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
33 * This service returns the list of gauges with daily discharge time periods and error messages |
9404 | 34 */ |
35 public class DynamicMainValuesTimeRangeDeterminationService extends D4EService { | |
36 | |
37 private static final long serialVersionUID = 1L; | |
38 | |
39 private static final String ROOT_NODE = "dynamic-mainvalues-input"; | |
40 | |
41 private static final Long DATE_DELTA_ERROR_MSG = (long) (60 * 60 * 24 * 1000); | |
42 | |
43 public static final class ServiceException extends Exception { | |
44 | |
45 private static final long serialVersionUID = 1L; | |
46 | |
47 public ServiceException(final String message) { | |
48 super(message); | |
49 } | |
50 } | |
51 | |
9409
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
52 private static class GaugeInfoResult { |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
53 protected final String globalErrorMsg; |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
54 protected final List<GaugeInfo> gaugeInfos; |
9404 | 55 |
9409
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
56 protected GaugeInfoResult(final List<GaugeInfo> gaugeInfos, final String globalErrorMsg) { |
9404 | 57 this.gaugeInfos = gaugeInfos; |
58 this.globalErrorMsg = globalErrorMsg; | |
59 } | |
60 | |
61 private static class GaugeInfo { | |
9409
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
62 protected final String errorMsg; |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
63 protected final Gauge gauge; |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
64 protected final Date startdate; |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
65 protected final Date enddate; |
9404 | 66 |
67 public GaugeInfo(final String errorMsg, final Gauge gauge, final Date startdate, final Date enddate) { | |
68 this.errorMsg = errorMsg; | |
69 this.gauge = gauge; | |
70 this.startdate = startdate; | |
71 this.enddate = enddate; | |
72 } | |
73 } | |
74 } | |
75 | |
9409
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
76 /** |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
77 * Queries the available daily discharge time periods of a list of gauges from the database and checks the overlapping |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
78 * |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
79 * @throws ServiceException |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
80 */ |
9404 | 81 private GaugeInfoResult getCommonTimeRangeForGauges(final List<Gauge> gauges, final Date startTime, final Date endTime, final CallMeta meta) |
82 throws ServiceException { | |
83 | |
84 // Query the gauge's daily Q values | |
9409
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
85 String globalErrorMsg = ""; |
9404 | 86 final List<GaugeInfoResult.GaugeInfo> gaugeResults = new ArrayList<>(); |
87 Date min = startTime; | |
88 Date max = endTime; | |
89 | |
90 for (final Gauge gauge : gauges) { | |
91 | |
9409
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
92 final Date[] gaugeDates = DailyDischargeValue.getTimePeriod(gauge, startTime, endTime); |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
93 if (gaugeDates[0] == null) { |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
94 final String msg = Resources.getMsg(meta, "bundu.wst_no_data_at_all", "bundu.wst_no_data_at_all", gauge.getName()); |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
95 final GaugeInfoResult.GaugeInfo gi = new GaugeInfoResult.GaugeInfo(msg, gauge, null, null); |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
96 gaugeResults.add(gi); |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
97 if (globalErrorMsg.isEmpty()) |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
98 globalErrorMsg = msg; |
9404 | 99 continue; |
100 } | |
101 | |
9409
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
102 if (gaugeDates[0].getTime() > startTime.getTime()) |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
103 min = gaugeDates[0]; |
9404 | 104 |
9409
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
105 if (gaugeDates[1].getTime() < endTime.getTime()) |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
106 max = gaugeDates[1]; |
9404 | 107 |
108 String errormsg = null; | |
9409
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
109 if ((gaugeDates[1].getTime() < endTime.getTime()) || (gaugeDates[0].getTime() > startTime.getTime())) |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
110 errormsg = makeDoesNotCoverErrorMsg(gaugeDates[0], gaugeDates[1], meta); |
9404 | 111 |
112 gaugeResults.add(new GaugeInfoResult.GaugeInfo(errormsg, gauge, min, max)); | |
113 } | |
114 | |
115 // common Range and correct errorMsg | |
116 final List<GaugeInfoResult.GaugeInfo> gaugeResultsSecondTurn = new ArrayList<>(); | |
117 for (final GaugeInfoResult.GaugeInfo gi : gaugeResults) { | |
9409
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
118 gaugeResultsSecondTurn.add(new GaugeInfoResult.GaugeInfo(gi.errorMsg, gi.gauge, gi.startdate != null ? min : null, |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
119 gi.enddate != null ? max : null)); |
9404 | 120 } |
9409
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
121 if (globalErrorMsg.isEmpty() && (min.getTime() > max.getTime())) |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
122 globalErrorMsg = getMsg(meta, "bundu.wst.gauge_timeranges_disjoint"); |
9404 | 123 final GaugeInfoResult result = new GaugeInfoResult(gaugeResultsSecondTurn, globalErrorMsg); |
124 | |
125 return result; | |
126 } | |
127 | |
128 private String makeDoesNotCoverErrorMsg(final Date start, final Date end, final CallMeta meta) { | |
129 final Calendar cal = Calendar.getInstance(); | |
130 cal.setTime(start); | |
131 final String startyear = String.valueOf(cal.get(Calendar.YEAR)); | |
132 cal.setTime(end); | |
133 final String endyear = String.valueOf(cal.get(Calendar.YEAR)); | |
134 return Resources.getMsg(meta, "bundu.wst.range_does_not_cover", new Object[] { startyear, endyear }); | |
135 } | |
136 | |
137 @Override | |
138 public Document doProcess(final Document data, final GlobalContext context, final CallMeta callMeta) { | |
139 try { | |
9409
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
140 final River river = AbstractMainValuesService.getRequestedRiver(data, "/art:" + ROOT_NODE + "/art:river/text()"); |
9404 | 141 final List<Gauge> gauges = getRequestedGauges(data, river, callMeta); |
9409
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
142 final Date start = getRequestedStartYear(data, "/art:" + ROOT_NODE + "/art:startYear/text()"); |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
143 final Date end = getRequestedEndYear(data, "/art:" + ROOT_NODE + "/art:endYear/text()"); |
9404 | 144 |
145 final GaugeInfoResult result = getCommonTimeRangeForGauges(gauges, start, end, callMeta); | |
146 | |
147 return buildDocument(result, context, callMeta); | |
148 } | |
149 catch (final ServiceException | MainValuesServiceException e) { | |
150 e.printStackTrace(); | |
151 return AbstractMainValuesService.error(e.getMessage()); | |
152 } | |
153 } | |
154 | |
155 public static final Date getRequestedEndYear(final Document data, final String XPATH_END_YEAR) throws MainValuesServiceException { | |
156 | |
157 final String endStr = XMLUtils.xpathString(data, XPATH_END_YEAR, ArtifactNamespaceContext.INSTANCE); | |
158 | |
159 if (endStr == null) | |
160 throw new MainValuesServiceException("no end year"); // should not happen | |
161 | |
162 try { | |
163 final int year = Integer.parseInt(endStr); | |
164 | |
165 // FIXME: timezone? probably must match timezone of database | |
166 final Calendar cal = Calendar.getInstance(); | |
167 cal.clear(); | |
168 cal.set(year, 11, 31); | |
169 return cal.getTime(); | |
170 } | |
171 catch (final NumberFormatException e) { | |
172 e.printStackTrace(); | |
173 throw new MainValuesServiceException("invalid end year"); // should not happen | |
174 } | |
175 } | |
176 | |
177 public static final Date getRequestedStartYear(final Document data, final String XPATH_START_YEAR) throws MainValuesServiceException { | |
178 | |
179 final String startStr = XMLUtils.xpathString(data, XPATH_START_YEAR, ArtifactNamespaceContext.INSTANCE); | |
180 | |
181 if (startStr == null) | |
182 throw new MainValuesServiceException("no start year");// should not happen | |
183 | |
184 try { | |
185 final int year = Integer.parseInt(startStr); | |
186 | |
187 // FIXME: timezone? probably must match timezone of database | |
188 final Calendar cal = Calendar.getInstance(); | |
189 cal.clear(); | |
190 cal.set(year, 0, 1); | |
191 return cal.getTime(); | |
192 } | |
193 catch (final NumberFormatException e) { | |
194 e.printStackTrace(); | |
195 throw new MainValuesServiceException("invalid start year"); // should not happen | |
196 } | |
197 } | |
198 | |
199 private Document buildDocument(final GaugeInfoResult result, final GlobalContext context, final CallMeta meta) { | |
200 | |
201 final Document doc = XMLUtils.newDocument(); | |
202 | |
203 final ElementCreator cr = new ElementCreator(doc, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
204 | |
205 final Element rootEl = cr.create(ROOT_NODE); | |
206 | |
207 doc.appendChild(rootEl); | |
208 | |
209 final Element globalErrElement = cr.create("global-error-msg"); | |
210 globalErrElement.setTextContent(result.globalErrorMsg); | |
211 rootEl.appendChild(globalErrElement); | |
212 | |
213 final List<GaugeInfoResult.GaugeInfo> values = result.gaugeInfos; | |
214 | |
215 for (final GaugeInfoResult.GaugeInfo gauge : values) { | |
216 final Element gaugeElement = cr.create("gauge"); | |
217 cr.addAttr(gaugeElement, "name", gauge.gauge.getName()); | |
218 if (gauge.startdate != null) | |
219 cr.addAttr(gaugeElement, "date-from", String.valueOf(gauge.startdate.getTime())); | |
220 | |
221 if (gauge.enddate != null) | |
222 cr.addAttr(gaugeElement, "date-to", String.valueOf(gauge.enddate.getTime())); | |
223 | |
224 if (gauge.errorMsg != null) | |
225 cr.addAttr(gaugeElement, "error-message", gauge.errorMsg); | |
226 | |
227 rootEl.appendChild(gaugeElement); | |
228 } | |
229 | |
230 return doc; | |
231 | |
232 } | |
233 | |
234 final Element buildElement(final ElementCreator cr, final String type, final Date date) { | |
235 final Element el = cr.create(type); | |
236 cr.addAttr(el, "value", String.valueOf(date.getTime())); | |
237 return el; | |
238 } | |
239 | |
240 private static final List<Gauge> getRequestedGauges(final Document data, final River river, final CallMeta meta) throws ServiceException { | |
241 | |
242 final NodeList gaugeNodes = data.getElementsByTagNameNS(ArtifactNamespaceContext.NAMESPACE_URI, "gauge"); | |
243 | |
244 final List<Gauge> gauges = new ArrayList<>(); | |
245 | |
246 for (int i = 0; i < gaugeNodes.getLength(); i++) { | |
247 final Element gaugeElt = (Element) gaugeNodes.item(i); | |
248 | |
249 final String gaugeName = (String) XMLUtils.xpath(gaugeElt, "text()", XPathConstants.STRING); | |
250 final Gauge gauge = Gauge.getGaugeByNameAndRiver(gaugeName, river); | |
251 if (gauge != null) | |
252 gauges.add(gauge); | |
253 else { | |
254 throw new ServiceException("bundu_wst_error_reading_gauges"); | |
255 } | |
256 } | |
257 | |
258 return gauges; | |
259 } | |
260 | |
261 private static String getMsg(final CallMeta meta, final String key) { | |
262 return Resources.getMsg(meta, key); | |
263 } | |
264 } |