Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/services/DynamicMainValuesTimeRangeDeterminationService.java @ 9419:a31cb674ddd1
Fixed year extraction from date and message format for the years without group digit
author | mschaefer |
---|---|
date | Fri, 17 Aug 2018 09:22:16 +0200 |
parents | b534a4f4e4f6 |
children | 7369d6ae3f87 |
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) { | |
9419
a31cb674ddd1
Fixed year extraction from date and message format for the years without group digit
mschaefer
parents:
9413
diff
changeset
|
117 gaugeResultsSecondTurn.add(new GaugeInfoResult.GaugeInfo(gi.errorMsg, gi.gauge, gi.startdate != null ? min : null, |
a31cb674ddd1
Fixed year extraction from date and message format for the years without group digit
mschaefer
parents:
9413
diff
changeset
|
118 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"); |
9404 | 122 final GaugeInfoResult result = new GaugeInfoResult(gaugeResultsSecondTurn, globalErrorMsg); |
123 | |
124 return result; | |
125 } | |
126 | |
127 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
|
128 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
|
129 return Resources.getMsg(meta, msgkey, msgkey, DateUtils.getYearFromDate(start), DateUtils.getYearFromDate(end)); |
9404 | 130 } |
131 | |
132 @Override | |
133 public Document doProcess(final Document data, final GlobalContext context, final CallMeta callMeta) { | |
134 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
|
135 final River river = AbstractMainValuesService.getRequestedRiver(data, "/art:" + ROOT_NODE + "/art:river/text()"); |
9404 | 136 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
|
137 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
|
138 final Date end = getRequestedEndYear(data, "/art:" + ROOT_NODE + "/art:endYear/text()"); |
9404 | 139 |
140 final GaugeInfoResult result = getCommonTimeRangeForGauges(gauges, start, end, callMeta); | |
141 | |
142 return buildDocument(result, context, callMeta); | |
143 } | |
144 catch (final ServiceException | MainValuesServiceException e) { | |
145 e.printStackTrace(); | |
146 return AbstractMainValuesService.error(e.getMessage()); | |
147 } | |
148 } | |
149 | |
150 public static final Date getRequestedEndYear(final Document data, final String XPATH_END_YEAR) throws MainValuesServiceException { | |
151 | |
152 final String endStr = XMLUtils.xpathString(data, XPATH_END_YEAR, ArtifactNamespaceContext.INSTANCE); | |
153 | |
154 if (endStr == null) | |
155 throw new MainValuesServiceException("no end year"); // should not happen | |
156 | |
157 try { | |
158 final int year = Integer.parseInt(endStr); | |
159 | |
160 // FIXME: timezone? probably must match timezone of database | |
161 final Calendar cal = Calendar.getInstance(); | |
162 cal.clear(); | |
163 cal.set(year, 11, 31); | |
164 return cal.getTime(); | |
165 } | |
166 catch (final NumberFormatException e) { | |
167 e.printStackTrace(); | |
168 throw new MainValuesServiceException("invalid end year"); // should not happen | |
169 } | |
170 } | |
171 | |
172 public static final Date getRequestedStartYear(final Document data, final String XPATH_START_YEAR) throws MainValuesServiceException { | |
173 | |
174 final String startStr = XMLUtils.xpathString(data, XPATH_START_YEAR, ArtifactNamespaceContext.INSTANCE); | |
175 | |
176 if (startStr == null) | |
177 throw new MainValuesServiceException("no start year");// should not happen | |
178 | |
179 try { | |
180 final int year = Integer.parseInt(startStr); | |
181 | |
182 // FIXME: timezone? probably must match timezone of database | |
183 final Calendar cal = Calendar.getInstance(); | |
184 cal.clear(); | |
185 cal.set(year, 0, 1); | |
186 return cal.getTime(); | |
187 } | |
188 catch (final NumberFormatException e) { | |
189 e.printStackTrace(); | |
190 throw new MainValuesServiceException("invalid start year"); // should not happen | |
191 } | |
192 } | |
193 | |
194 private Document buildDocument(final GaugeInfoResult result, final GlobalContext context, final CallMeta meta) { | |
195 | |
196 final Document doc = XMLUtils.newDocument(); | |
197 | |
198 final ElementCreator cr = new ElementCreator(doc, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
199 | |
200 final Element rootEl = cr.create(ROOT_NODE); | |
201 | |
202 doc.appendChild(rootEl); | |
203 | |
204 final Element globalErrElement = cr.create("global-error-msg"); | |
205 globalErrElement.setTextContent(result.globalErrorMsg); | |
206 rootEl.appendChild(globalErrElement); | |
207 | |
208 final List<GaugeInfoResult.GaugeInfo> values = result.gaugeInfos; | |
209 | |
210 for (final GaugeInfoResult.GaugeInfo gauge : values) { | |
211 final Element gaugeElement = cr.create("gauge"); | |
212 cr.addAttr(gaugeElement, "name", gauge.gauge.getName()); | |
213 if (gauge.startdate != null) | |
214 cr.addAttr(gaugeElement, "date-from", String.valueOf(gauge.startdate.getTime())); | |
215 | |
216 if (gauge.enddate != null) | |
217 cr.addAttr(gaugeElement, "date-to", String.valueOf(gauge.enddate.getTime())); | |
218 | |
219 if (gauge.errorMsg != null) | |
220 cr.addAttr(gaugeElement, "error-message", gauge.errorMsg); | |
221 | |
222 rootEl.appendChild(gaugeElement); | |
223 } | |
224 | |
225 return doc; | |
226 | |
227 } | |
228 | |
229 final Element buildElement(final ElementCreator cr, final String type, final Date date) { | |
230 final Element el = cr.create(type); | |
231 cr.addAttr(el, "value", String.valueOf(date.getTime())); | |
232 return el; | |
233 } | |
234 | |
235 private static final List<Gauge> getRequestedGauges(final Document data, final River river, final CallMeta meta) throws ServiceException { | |
236 | |
237 final NodeList gaugeNodes = data.getElementsByTagNameNS(ArtifactNamespaceContext.NAMESPACE_URI, "gauge"); | |
238 | |
239 final List<Gauge> gauges = new ArrayList<>(); | |
240 | |
241 for (int i = 0; i < gaugeNodes.getLength(); i++) { | |
242 final Element gaugeElt = (Element) gaugeNodes.item(i); | |
243 | |
244 final String gaugeName = (String) XMLUtils.xpath(gaugeElt, "text()", XPathConstants.STRING); | |
245 final Gauge gauge = Gauge.getGaugeByNameAndRiver(gaugeName, river); | |
246 if (gauge != null) | |
247 gauges.add(gauge); | |
248 else { | |
249 throw new ServiceException("bundu_wst_error_reading_gauges"); | |
250 } | |
251 } | |
252 | |
253 return gauges; | |
254 } | |
255 | |
256 private static String getMsg(final CallMeta meta, final String key) { | |
257 return Resources.getMsg(meta, key); | |
258 } | |
259 } |