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