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