Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/services/DynamicMainValuesTimeRangeDeterminationService.java @ 9458:7369d6ae3f87
bundu bezugswst WQ-Input: UD disabled on global Error
author | gernotbelger |
---|---|
date | Tue, 28 Aug 2018 17:05:25 +0200 |
parents | a31cb674ddd1 |
children | 879c902c4a2d |
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; |
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 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
|
64 protected final Date enddate; |
9404 | 65 |
66 public GaugeInfo(final String errorMsg, final Gauge gauge, final Date startdate, final Date enddate) { | |
67 this.errorMsg = errorMsg; | |
68 this.gauge = gauge; | |
69 this.startdate = startdate; | |
70 this.enddate = enddate; | |
71 } | |
72 } | |
73 } | |
74 | |
9409
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
75 /** |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
76 * 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
|
77 * |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
78 * @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
|
79 */ |
9404 | 80 private GaugeInfoResult getCommonTimeRangeForGauges(final List<Gauge> gauges, final Date startTime, final Date endTime, final CallMeta meta) |
81 throws ServiceException { | |
82 | |
83 // 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
|
84 String globalErrorMsg = ""; |
9404 | 85 final List<GaugeInfoResult.GaugeInfo> gaugeResults = new ArrayList<>(); |
86 Date min = startTime; | |
87 Date max = endTime; | |
88 | |
89 for (final Gauge gauge : gauges) { | |
90 | |
9409
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
91 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
|
92 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
|
93 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
|
94 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
|
95 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
|
96 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
|
97 globalErrorMsg = msg; |
9404 | 98 continue; |
99 } | |
100 | |
9410 | 101 if (gaugeDates[0].getTime() > min.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
|
102 min = gaugeDates[0]; |
9404 | 103 |
9410 | 104 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
|
105 max = gaugeDates[1]; |
9404 | 106 |
107 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
|
108 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
|
109 errormsg = makeDoesNotCoverErrorMsg(gaugeDates[0], gaugeDates[1], meta); |
9404 | 110 |
9410 | 111 gaugeResults.add(new GaugeInfoResult.GaugeInfo(errormsg, gauge, gaugeDates[0], gaugeDates[1])); |
9404 | 112 } |
113 | |
114 // common Range and correct errorMsg | |
115 final List<GaugeInfoResult.GaugeInfo> gaugeResultsSecondTurn = new ArrayList<>(); | |
116 for (final GaugeInfoResult.GaugeInfo gi : gaugeResults) { | |
9458
7369d6ae3f87
bundu bezugswst WQ-Input: UD disabled on global Error
gernotbelger
parents:
9419
diff
changeset
|
117 gaugeResultsSecondTurn |
7369d6ae3f87
bundu bezugswst WQ-Input: UD disabled on global Error
gernotbelger
parents:
9419
diff
changeset
|
118 .add(new GaugeInfoResult.GaugeInfo(gi.errorMsg, gi.gauge, gi.startdate != null ? min : null, gi.enddate != null ? max : null)); |
9404 | 119 } |
9409
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
120 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
|
121 globalErrorMsg = getMsg(meta, "bundu.wst.gauge_timeranges_disjoint"); |
9458
7369d6ae3f87
bundu bezugswst WQ-Input: UD disabled on global Error
gernotbelger
parents:
9419
diff
changeset
|
122 |
7369d6ae3f87
bundu bezugswst WQ-Input: UD disabled on global Error
gernotbelger
parents:
9419
diff
changeset
|
123 // add "cannot calculate UD" to globalErrorMsg |
7369d6ae3f87
bundu bezugswst WQ-Input: UD disabled on global Error
gernotbelger
parents:
9419
diff
changeset
|
124 // Eine Berechnung der UD ist nicht möglich. |
7369d6ae3f87
bundu bezugswst WQ-Input: UD disabled on global Error
gernotbelger
parents:
9419
diff
changeset
|
125 if (!globalErrorMsg.isEmpty()) |
7369d6ae3f87
bundu bezugswst WQ-Input: UD disabled on global Error
gernotbelger
parents:
9419
diff
changeset
|
126 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
|
127 |
9404 | 128 final GaugeInfoResult result = new GaugeInfoResult(gaugeResultsSecondTurn, globalErrorMsg); |
129 | |
130 return result; | |
131 } | |
132 | |
133 private String makeDoesNotCoverErrorMsg(final Date start, final Date end, final CallMeta meta) { | |
9419
a31cb674ddd1
Fixed year extraction from date and message format for the years without group digit
mschaefer
parents:
9413
diff
changeset
|
134 final String msgkey = "bundu.wst.range_does_not_cover"; |
a31cb674ddd1
Fixed year extraction from date and message format for the years without group digit
mschaefer
parents:
9413
diff
changeset
|
135 return Resources.getMsg(meta, msgkey, msgkey, DateUtils.getYearFromDate(start), DateUtils.getYearFromDate(end)); |
9404 | 136 } |
137 | |
138 @Override | |
139 public Document doProcess(final Document data, final GlobalContext context, final CallMeta callMeta) { | |
140 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
|
141 final River river = AbstractMainValuesService.getRequestedRiver(data, "/art:" + ROOT_NODE + "/art:river/text()"); |
9404 | 142 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
|
143 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
|
144 final Date end = getRequestedEndYear(data, "/art:" + ROOT_NODE + "/art:endYear/text()"); |
9404 | 145 |
146 final GaugeInfoResult result = getCommonTimeRangeForGauges(gauges, start, end, callMeta); | |
147 | |
148 return buildDocument(result, context, callMeta); | |
149 } | |
150 catch (final ServiceException | MainValuesServiceException e) { | |
151 e.printStackTrace(); | |
152 return AbstractMainValuesService.error(e.getMessage()); | |
153 } | |
154 } | |
155 | |
156 public static final Date getRequestedEndYear(final Document data, final String XPATH_END_YEAR) throws MainValuesServiceException { | |
157 | |
158 final String endStr = XMLUtils.xpathString(data, XPATH_END_YEAR, ArtifactNamespaceContext.INSTANCE); | |
159 | |
160 if (endStr == null) | |
161 throw new MainValuesServiceException("no end year"); // should not happen | |
162 | |
163 try { | |
164 final int year = Integer.parseInt(endStr); | |
165 | |
166 // FIXME: timezone? probably must match timezone of database | |
167 final Calendar cal = Calendar.getInstance(); | |
168 cal.clear(); | |
169 cal.set(year, 11, 31); | |
170 return cal.getTime(); | |
171 } | |
172 catch (final NumberFormatException e) { | |
173 e.printStackTrace(); | |
174 throw new MainValuesServiceException("invalid end year"); // should not happen | |
175 } | |
176 } | |
177 | |
178 public static final Date getRequestedStartYear(final Document data, final String XPATH_START_YEAR) throws MainValuesServiceException { | |
179 | |
180 final String startStr = XMLUtils.xpathString(data, XPATH_START_YEAR, ArtifactNamespaceContext.INSTANCE); | |
181 | |
182 if (startStr == null) | |
183 throw new MainValuesServiceException("no start year");// should not happen | |
184 | |
185 try { | |
186 final int year = Integer.parseInt(startStr); | |
187 | |
188 // FIXME: timezone? probably must match timezone of database | |
189 final Calendar cal = Calendar.getInstance(); | |
190 cal.clear(); | |
191 cal.set(year, 0, 1); | |
192 return cal.getTime(); | |
193 } | |
194 catch (final NumberFormatException e) { | |
195 e.printStackTrace(); | |
196 throw new MainValuesServiceException("invalid start year"); // should not happen | |
197 } | |
198 } | |
199 | |
200 private Document buildDocument(final GaugeInfoResult result, final GlobalContext context, final CallMeta meta) { | |
201 | |
202 final Document doc = XMLUtils.newDocument(); | |
203 | |
204 final ElementCreator cr = new ElementCreator(doc, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
205 | |
206 final Element rootEl = cr.create(ROOT_NODE); | |
207 | |
208 doc.appendChild(rootEl); | |
209 | |
210 final Element globalErrElement = cr.create("global-error-msg"); | |
211 globalErrElement.setTextContent(result.globalErrorMsg); | |
212 rootEl.appendChild(globalErrElement); | |
213 | |
214 final List<GaugeInfoResult.GaugeInfo> values = result.gaugeInfos; | |
215 | |
216 for (final GaugeInfoResult.GaugeInfo gauge : values) { | |
217 final Element gaugeElement = cr.create("gauge"); | |
218 cr.addAttr(gaugeElement, "name", gauge.gauge.getName()); | |
219 if (gauge.startdate != null) | |
220 cr.addAttr(gaugeElement, "date-from", String.valueOf(gauge.startdate.getTime())); | |
221 | |
222 if (gauge.enddate != null) | |
223 cr.addAttr(gaugeElement, "date-to", String.valueOf(gauge.enddate.getTime())); | |
224 | |
225 if (gauge.errorMsg != null) | |
226 cr.addAttr(gaugeElement, "error-message", gauge.errorMsg); | |
227 | |
228 rootEl.appendChild(gaugeElement); | |
229 } | |
230 | |
231 return doc; | |
232 | |
233 } | |
234 | |
235 final Element buildElement(final ElementCreator cr, final String type, final Date date) { | |
236 final Element el = cr.create(type); | |
237 cr.addAttr(el, "value", String.valueOf(date.getTime())); | |
238 return el; | |
239 } | |
240 | |
241 private static final List<Gauge> getRequestedGauges(final Document data, final River river, final CallMeta meta) throws ServiceException { | |
242 | |
243 final NodeList gaugeNodes = data.getElementsByTagNameNS(ArtifactNamespaceContext.NAMESPACE_URI, "gauge"); | |
244 | |
245 final List<Gauge> gauges = new ArrayList<>(); | |
246 | |
247 for (int i = 0; i < gaugeNodes.getLength(); i++) { | |
248 final Element gaugeElt = (Element) gaugeNodes.item(i); | |
249 | |
250 final String gaugeName = (String) XMLUtils.xpath(gaugeElt, "text()", XPathConstants.STRING); | |
251 final Gauge gauge = Gauge.getGaugeByNameAndRiver(gaugeName, river); | |
252 if (gauge != null) | |
253 gauges.add(gauge); | |
254 else { | |
255 throw new ServiceException("bundu_wst_error_reading_gauges"); | |
256 } | |
257 } | |
258 | |
259 return gauges; | |
260 } | |
261 | |
262 private static String getMsg(final CallMeta meta, final String key) { | |
263 return Resources.getMsg(meta, key); | |
264 } | |
265 } |