annotate artifacts/src/main/java/org/dive4elements/river/artifacts/services/DynamicMainValuesTimeRangeDeterminationService.java @ 9405:34cd4faf43f4

minMax queries
author gernotbelger
date Wed, 15 Aug 2018 14:34:23 +0200
parents bc9a45d2b1fa
children 38201f5b0dd9
rev   line source
9404
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
2 * Software engineering by Intevation GmbH
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
3 *
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
4 * This file is Free Software under the GNU AGPL (>=v3)
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
6 * documentation coming with Dive4Elements River for details.
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
7 */
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
8
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
9 package org.dive4elements.river.artifacts.services;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
10
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
11 import java.util.ArrayList;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
12 import java.util.Calendar;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
13 import java.util.Date;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
14 import java.util.List;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
15
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
16 import javax.xml.xpath.XPathConstants;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
17
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
18 import org.dive4elements.artifacts.CallMeta;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
19 import org.dive4elements.artifacts.GlobalContext;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
20 import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
21 import org.dive4elements.artifacts.common.utils.XMLUtils;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
22 import org.dive4elements.artifacts.common.utils.XMLUtils.ElementCreator;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
23 import org.dive4elements.river.artifacts.resources.Resources;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
24 import org.dive4elements.river.artifacts.services.AbstractMainValuesService.MainValuesServiceException;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
25 import org.dive4elements.river.model.Gauge;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
26 import org.dive4elements.river.model.River;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
27 import org.dive4elements.river.model.sinfo.DailyDischargeValue;
9405
34cd4faf43f4 minMax queries
gernotbelger
parents: 9404
diff changeset
28 import org.dive4elements.river.model.sinfo.DailyDischargeValue.MinMax;
9404
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
29 import org.w3c.dom.Document;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
30 import org.w3c.dom.Element;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
31 import org.w3c.dom.NodeList;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
32
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
33 /**
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
34 * This service returns the main values of a river's gauge based on the start
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
35 * and end point of the river.
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
36 *
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
37 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
38 */
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
39 public class DynamicMainValuesTimeRangeDeterminationService extends D4EService {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
40
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
41 private static final long serialVersionUID = 1L;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
42
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
43 private static final String ROOT_NODE = "dynamic-mainvalues-input";
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
44
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
45 private static final Long DATE_DELTA_ERROR_MSG = (long) (60 * 60 * 24 * 1000);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
46
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
47 public static final class ServiceException extends Exception {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
48
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
49 private static final long serialVersionUID = 1L;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
50
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
51 public ServiceException(final String message) {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
52 super(message);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
53 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
54 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
55
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
56 /**
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
57 * Computes a gauge's main values for a period of time based on its daily discharges stored in the database
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
58 *
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
59 * @throws Exception
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
60 */
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
61
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
62 private static class GaugeInfoResult {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
63 private final String globalErrorMsg;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
64 private final List<GaugeInfo> gaugeInfos;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
65
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
66 private GaugeInfoResult(final List<GaugeInfo> gaugeInfos, final String globalErrorMsg) {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
67 this.gaugeInfos = gaugeInfos;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
68 this.globalErrorMsg = globalErrorMsg;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
69 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
70
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
71 private static class GaugeInfo {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
72 private final String errorMsg;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
73 private final Gauge gauge;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
74 private final Date startdate;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
75 private final Date enddate;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
76
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
77 public GaugeInfo(final String errorMsg, final Gauge gauge, final Date startdate, final Date enddate) {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
78 this.errorMsg = errorMsg;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
79 this.gauge = gauge;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
80 this.startdate = startdate;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
81 this.enddate = enddate;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
82 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
83 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
84 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
85
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
86 private GaugeInfoResult getCommonTimeRangeForGauges(final List<Gauge> gauges, final Date startTime, final Date endTime, final CallMeta meta)
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
87 throws ServiceException {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
88
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
89 // Query the gauge's daily Q values
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
90 final List<GaugeInfoResult.GaugeInfo> gaugeResults = new ArrayList<>();
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
91 Date min = startTime;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
92 Date max = endTime;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
93
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
94 for (final Gauge gauge : gauges) {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
95
9405
34cd4faf43f4 minMax queries
gernotbelger
parents: 9404
diff changeset
96 final Date minGlobalForGauge = DailyDischargeValue.getGlobalMinMax(gauge, MinMax.min);
34cd4faf43f4 minMax queries
gernotbelger
parents: 9404
diff changeset
97 final Date maxGlobalForGauge = DailyDischargeValue.getGlobalMinMax(gauge, MinMax.max);
34cd4faf43f4 minMax queries
gernotbelger
parents: 9404
diff changeset
98 if (minGlobalForGauge == null || maxGlobalForGauge == null) { // der Fall, dass nur eins von beiden null ist, kann eigentlich nciht vorkommen
9404
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
99 gaugeResults.add(new GaugeInfoResult.GaugeInfo(getMsg(meta, "bundu.wst_no_data_at_all"), gauge, null, null));
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
100 // TODO : wenn der Workflow abgebrochen werden soll, GlobalErrorMsg setzen, dass mind. ein Pegel überhaupt keine Daten
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
101 // hat (der Mechnismus auf Client-Seite ist schon implementiert)
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
102
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
103 continue;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
104 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
105
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
106 if (minGlobalForGauge.getTime() > startTime.getTime())
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
107 min = minGlobalForGauge;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
108
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
109 if (maxGlobalForGauge.getTime() < endTime.getTime())
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
110 max = maxGlobalForGauge;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
111
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
112 String errormsg = null;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
113 if ((maxGlobalForGauge.getTime() < endTime.getTime()) || (minGlobalForGauge.getTime() > startTime.getTime()))
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
114 errormsg = makeDoesNotCoverErrorMsg(minGlobalForGauge, maxGlobalForGauge, meta);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
115
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
116 gaugeResults.add(new GaugeInfoResult.GaugeInfo(errormsg, gauge, min, max));
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
117 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
118
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
119 // common Range and correct errorMsg
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
120 final List<GaugeInfoResult.GaugeInfo> gaugeResultsSecondTurn = new ArrayList<>();
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
121 for (final GaugeInfoResult.GaugeInfo gi : gaugeResults) {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
122 gaugeResultsSecondTurn
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
123 .add(new GaugeInfoResult.GaugeInfo(gi.errorMsg, gi.gauge, gi.startdate != null ? min : null, gi.enddate != null ? max : null));
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
124 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
125 final String globalErrorMsg = (min.getTime() > max.getTime()) ? getMsg(meta, "bundu.wst.gauge_timeranges_disjoint") : "";
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
126 final GaugeInfoResult result = new GaugeInfoResult(gaugeResultsSecondTurn, globalErrorMsg);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
127
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
128 return result;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
129 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
130
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
131 private String makeDoesNotCoverErrorMsg(final Date start, final Date end, final CallMeta meta) {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
132 final Calendar cal = Calendar.getInstance();
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
133 cal.setTime(start);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
134 final String startyear = String.valueOf(cal.get(Calendar.YEAR));
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
135 cal.setTime(end);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
136 final String endyear = String.valueOf(cal.get(Calendar.YEAR));
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
137 return Resources.getMsg(meta, "bundu.wst.range_does_not_cover", new Object[] { startyear, endyear });
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
138 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
139
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
140 @Override
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
141 public Document doProcess(final Document data, final GlobalContext context, final CallMeta callMeta) {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
142 try {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
143 final River river = AbstractMainValuesService.getRequestedRiver(data, "/art:" + this.ROOT_NODE + "/art:river/text()");
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
144 final List<Gauge> gauges = getRequestedGauges(data, river, callMeta);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
145 final Date start = getRequestedStartYear(data, "/art:" + this.ROOT_NODE + "/art:startYear/text()");
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
146 final Date end = getRequestedEndYear(data, "/art:" + this.ROOT_NODE + "/art:endYear/text()");
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
147
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
148 final GaugeInfoResult result = getCommonTimeRangeForGauges(gauges, start, end, callMeta);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
149
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
150 return buildDocument(result, context, callMeta);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
151 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
152 catch (final ServiceException | MainValuesServiceException e) {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
153 e.printStackTrace();
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
154 return AbstractMainValuesService.error(e.getMessage());
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
155 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
156 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
157
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
158 public static final Date getRequestedEndYear(final Document data, final String XPATH_END_YEAR) throws MainValuesServiceException {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
159
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
160 final String endStr = XMLUtils.xpathString(data, XPATH_END_YEAR, ArtifactNamespaceContext.INSTANCE);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
161
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
162 if (endStr == null)
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
163 throw new MainValuesServiceException("no end year"); // should not happen
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
164
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
165 try {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
166 final int year = Integer.parseInt(endStr);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
167
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
168 // FIXME: timezone? probably must match timezone of database
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
169 final Calendar cal = Calendar.getInstance();
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
170 cal.clear();
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
171 cal.set(year, 11, 31);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
172 return cal.getTime();
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
173 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
174 catch (final NumberFormatException e) {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
175 e.printStackTrace();
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
176 throw new MainValuesServiceException("invalid end year"); // should not happen
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
177 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
178 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
179
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
180 public static final Date getRequestedStartYear(final Document data, final String XPATH_START_YEAR) throws MainValuesServiceException {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
181
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
182 final String startStr = XMLUtils.xpathString(data, XPATH_START_YEAR, ArtifactNamespaceContext.INSTANCE);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
183
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
184 if (startStr == null)
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
185 throw new MainValuesServiceException("no start year");// should not happen
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
186
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
187 try {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
188 final int year = Integer.parseInt(startStr);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
189
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
190 // FIXME: timezone? probably must match timezone of database
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
191 final Calendar cal = Calendar.getInstance();
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
192 cal.clear();
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
193 cal.set(year, 0, 1);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
194 return cal.getTime();
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
195 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
196 catch (final NumberFormatException e) {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
197 e.printStackTrace();
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
198 throw new MainValuesServiceException("invalid start year"); // should not happen
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
199 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
200 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
201
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
202 private Document buildDocument(final GaugeInfoResult result, final GlobalContext context, final CallMeta meta) {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
203
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
204 final Document doc = XMLUtils.newDocument();
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
205
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
206 final ElementCreator cr = new ElementCreator(doc, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
207
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
208 final Element rootEl = cr.create(ROOT_NODE);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
209
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
210 doc.appendChild(rootEl);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
211
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
212 final Element globalErrElement = cr.create("global-error-msg");
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
213 globalErrElement.setTextContent(result.globalErrorMsg);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
214 rootEl.appendChild(globalErrElement);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
215
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
216 final List<GaugeInfoResult.GaugeInfo> values = result.gaugeInfos;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
217
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
218 for (final GaugeInfoResult.GaugeInfo gauge : values) {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
219 final Element gaugeElement = cr.create("gauge");
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
220 cr.addAttr(gaugeElement, "name", gauge.gauge.getName());
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
221 if (gauge.startdate != null)
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
222 cr.addAttr(gaugeElement, "date-from", String.valueOf(gauge.startdate.getTime()));
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
223
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
224 if (gauge.enddate != null)
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
225 cr.addAttr(gaugeElement, "date-to", String.valueOf(gauge.enddate.getTime()));
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
226
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
227 if (gauge.errorMsg != null)
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
228 cr.addAttr(gaugeElement, "error-message", gauge.errorMsg);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
229
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
230 rootEl.appendChild(gaugeElement);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
231 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
232
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
233 return doc;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
234
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
235 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
236
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
237 final Element buildElement(final ElementCreator cr, final String type, final Date date) {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
238 final Element el = cr.create(type);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
239 cr.addAttr(el, "value", String.valueOf(date.getTime()));
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
240 return el;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
241 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
242
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
243 private static final List<Gauge> getRequestedGauges(final Document data, final River river, final CallMeta meta) throws ServiceException {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
244
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
245 final NodeList gaugeNodes = data.getElementsByTagNameNS(ArtifactNamespaceContext.NAMESPACE_URI, "gauge");
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
246
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
247 final List<Gauge> gauges = new ArrayList<>();
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
248
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
249 for (int i = 0; i < gaugeNodes.getLength(); i++) {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
250 final Element gaugeElt = (Element) gaugeNodes.item(i);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
251
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
252 final String gaugeName = (String) XMLUtils.xpath(gaugeElt, "text()", XPathConstants.STRING);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
253 final Gauge gauge = Gauge.getGaugeByNameAndRiver(gaugeName, river);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
254 if (gauge != null)
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
255 gauges.add(gauge);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
256 else {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
257 throw new ServiceException("bundu_wst_error_reading_gauges");
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
258 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
259 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
260
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
261 return gauges;
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
262 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
263
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
264 private static String getMsg(final CallMeta meta, final String key) {
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
265 return Resources.getMsg(meta, key);
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
266 }
bc9a45d2b1fa common time range for gauges incl. error messages
gernotbelger
parents:
diff changeset
267 }

http://dive4elements.wald.intevation.org