Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/services/DynamicMainValuesTimeRangeDeterminationService.java @ 9588:c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
author | gernotbelger |
---|---|
date | Thu, 10 Jan 2019 11:56:39 +0100 |
parents | 879c902c4a2d |
children |
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 | |
9588
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
51 public static class GaugeInfoResult { |
9409
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; |
9588
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
54 private final Date globalStartDate; |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
55 private final Date globalEndDate; |
9404 | 56 |
9588
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
57 protected GaugeInfoResult(final List<GaugeInfo> gaugeInfos, final String globalErrorMsg, final Date min, final Date max) { |
9404 | 58 this.gaugeInfos = gaugeInfos; |
59 this.globalErrorMsg = globalErrorMsg; | |
9588
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
60 this.globalStartDate = min; |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
61 this.globalEndDate = max; |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
62 } |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
63 |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
64 public int getGlobalEndYear() { |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
65 return getYearFromDate(this.globalEndDate); |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
66 } |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
67 |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
68 public int getGlobalStartYear() { |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
69 return getYearFromDate(this.globalStartDate); |
9404 | 70 } |
71 | |
72 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
|
73 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
|
74 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
|
75 /** |
879c902c4a2d
Changed main value calculations from calendar year to Abflussjahr, modified time range determination accordingly
mschaefer
parents:
9458
diff
changeset
|
76 * 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
|
77 * before |
879c902c4a2d
Changed main value calculations from calendar year to Abflussjahr, modified time range determination accordingly
mschaefer
parents:
9458
diff
changeset
|
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 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
|
80 protected final Date enddate; |
9404 | 81 |
82 public GaugeInfo(final String errorMsg, final Gauge gauge, final Date startdate, final Date enddate) { | |
83 this.errorMsg = errorMsg; | |
84 this.gauge = gauge; | |
85 this.startdate = startdate; | |
86 this.enddate = enddate; | |
87 } | |
88 } | |
89 } | |
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 /** |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
92 * 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
|
93 * |
38201f5b0dd9
Changed bundu bzws workflow to stop in case of missing daily discharge values and other minor changes
mschaefer
parents:
9405
diff
changeset
|
94 * @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
|
95 */ |
9588
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
96 public static GaugeInfoResult getCommonTimeRangeForGauges(final List<Gauge> gauges, final int startYear, final int endYear, final CallMeta meta) { |
9404 | 97 |
9588
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
98 final Date startTime = getStartDateFromYear(startYear); |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
99 final Date endTime = getEndDateFromYear(endYear); |
9404 | 100 // 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
|
101 String globalErrorMsg = ""; |
9404 | 102 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
|
103 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
|
104 Date min = qStartTime; |
9404 | 105 Date max = endTime; |
106 | |
107 for (final Gauge gauge : gauges) { | |
108 | |
9494
879c902c4a2d
Changed main value calculations from calendar year to Abflussjahr, modified time range determination accordingly
mschaefer
parents:
9458
diff
changeset
|
109 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
|
110 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
|
111 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
|
112 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
|
113 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
|
114 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
|
115 globalErrorMsg = msg; |
9404 | 116 continue; |
117 } | |
118 | |
9494
879c902c4a2d
Changed main value calculations from calendar year to Abflussjahr, modified time range determination accordingly
mschaefer
parents:
9458
diff
changeset
|
119 final Date gaugeCalcStartDate = DateUtils.getNextAbflussYear(gaugeDates[0])[0]; |
9410 | 120 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
|
121 min = gaugeCalcStartDate; |
9404 | 122 |
9410 | 123 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
|
124 max = gaugeDates[1]; |
9404 | 125 |
126 String errormsg = null; | |
9494
879c902c4a2d
Changed main value calculations from calendar year to Abflussjahr, modified time range determination accordingly
mschaefer
parents:
9458
diff
changeset
|
127 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
|
128 errormsg = makeDoesNotCoverErrorMsg(DateUtils.getAbflussYearFromDate(gaugeCalcStartDate), DateUtils.getYearFromDate(gaugeDates[1]), meta); |
9404 | 129 |
9494
879c902c4a2d
Changed main value calculations from calendar year to Abflussjahr, modified time range determination accordingly
mschaefer
parents:
9458
diff
changeset
|
130 gaugeResults.add(new GaugeInfoResult.GaugeInfo(errormsg, gauge, DateUtils.getNextNewYear(gaugeCalcStartDate), gaugeDates[1])); |
9404 | 131 } |
132 | |
133 // 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
|
134 min = DateUtils.getNextNewYear(min); |
9404 | 135 final List<GaugeInfoResult.GaugeInfo> gaugeResultsSecondTurn = new ArrayList<>(); |
136 for (final GaugeInfoResult.GaugeInfo gi : gaugeResults) { | |
9458
7369d6ae3f87
bundu bezugswst WQ-Input: UD disabled on global Error
gernotbelger
parents:
9419
diff
changeset
|
137 gaugeResultsSecondTurn |
9588
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
138 .add(new GaugeInfoResult.GaugeInfo(gi.errorMsg, gi.gauge, gi.startdate != null ? min : null, gi.enddate != null ? max : null)); |
9404 | 139 } |
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 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
|
141 globalErrorMsg = getMsg(meta, "bundu.wst.gauge_timeranges_disjoint"); |
9458
7369d6ae3f87
bundu bezugswst WQ-Input: UD disabled on global Error
gernotbelger
parents:
9419
diff
changeset
|
142 |
7369d6ae3f87
bundu bezugswst WQ-Input: UD disabled on global Error
gernotbelger
parents:
9419
diff
changeset
|
143 // add "cannot calculate UD" to globalErrorMsg |
7369d6ae3f87
bundu bezugswst WQ-Input: UD disabled on global Error
gernotbelger
parents:
9419
diff
changeset
|
144 // Eine Berechnung der UD ist nicht möglich. |
7369d6ae3f87
bundu bezugswst WQ-Input: UD disabled on global Error
gernotbelger
parents:
9419
diff
changeset
|
145 if (!globalErrorMsg.isEmpty()) |
7369d6ae3f87
bundu bezugswst WQ-Input: UD disabled on global Error
gernotbelger
parents:
9419
diff
changeset
|
146 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
|
147 |
9588
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
148 final GaugeInfoResult result = new GaugeInfoResult(gaugeResultsSecondTurn, globalErrorMsg, min, max); |
9404 | 149 |
150 return result; | |
151 } | |
152 | |
9588
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
153 private static 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
|
154 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
|
155 return Resources.getMsg(meta, msgkey, msgkey, startYear, endYear); |
9404 | 156 } |
157 | |
158 @Override | |
159 public Document doProcess(final Document data, final GlobalContext context, final CallMeta callMeta) { | |
160 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
|
161 final River river = AbstractMainValuesService.getRequestedRiver(data, "/art:" + ROOT_NODE + "/art:river/text()"); |
9404 | 162 final List<Gauge> gauges = getRequestedGauges(data, river, callMeta); |
9588
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
163 final int start = getRequestedStartYear(data, "/art:" + ROOT_NODE + "/art:startYear/text()"); |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
164 final int end = getRequestedEndYear(data, "/art:" + ROOT_NODE + "/art:endYear/text()"); |
9404 | 165 |
166 final GaugeInfoResult result = getCommonTimeRangeForGauges(gauges, start, end, callMeta); | |
167 | |
168 return buildDocument(result, context, callMeta); | |
169 } | |
170 catch (final ServiceException | MainValuesServiceException e) { | |
171 e.printStackTrace(); | |
172 return AbstractMainValuesService.error(e.getMessage()); | |
173 } | |
174 } | |
175 | |
9588
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
176 public static final int getRequestedEndYear(final Document data, final String XPATH_END_YEAR) throws MainValuesServiceException { |
9404 | 177 |
178 final String endStr = XMLUtils.xpathString(data, XPATH_END_YEAR, ArtifactNamespaceContext.INSTANCE); | |
179 | |
180 if (endStr == null) | |
181 throw new MainValuesServiceException("no end year"); // should not happen | |
182 | |
183 try { | |
9588
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
184 return Integer.parseInt(endStr); |
9404 | 185 } |
186 catch (final NumberFormatException e) { | |
187 e.printStackTrace(); | |
188 throw new MainValuesServiceException("invalid end year"); // should not happen | |
189 } | |
190 } | |
191 | |
9588
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
192 public static final int getRequestedStartYear(final Document data, final String XPATH_START_YEAR) throws MainValuesServiceException { |
9404 | 193 |
194 final String startStr = XMLUtils.xpathString(data, XPATH_START_YEAR, ArtifactNamespaceContext.INSTANCE); | |
195 | |
196 if (startStr == null) | |
197 throw new MainValuesServiceException("no start year");// should not happen | |
198 | |
199 try { | |
9588
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
200 return Integer.parseInt(startStr); |
9404 | 201 } |
202 catch (final NumberFormatException e) { | |
203 e.printStackTrace(); | |
204 throw new MainValuesServiceException("invalid start year"); // should not happen | |
205 } | |
206 } | |
207 | |
9588
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
208 private static Integer getYearFromDate(final Date date) { |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
209 // FIXME: timezone? probably must match timezone of database |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
210 final Calendar cal = Calendar.getInstance(); |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
211 cal.clear(); |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
212 cal.setTime(date); |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
213 return cal.get(Calendar.YEAR); |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
214 } |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
215 |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
216 private static Date getEndDateFromYear(final int year) { |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
217 // FIXME: timezone? probably must match timezone of database |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
218 final Calendar cal = Calendar.getInstance(); |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
219 cal.clear(); |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
220 cal.set(year, 11, 31); |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
221 return cal.getTime(); |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
222 } |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
223 |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
224 private static Date getStartDateFromYear(final int year) { |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
225 // FIXME: timezone? probably must match timezone of database |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
226 final Calendar cal = Calendar.getInstance(); |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
227 cal.clear(); |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
228 cal.set(year, 0, 1); |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
229 return cal.getTime(); |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
230 } |
c57caff9b00b
Punkt 10.6 CSV-Ausgabe Abflusszeitreihenlänge
gernotbelger
parents:
9494
diff
changeset
|
231 |
9404 | 232 private Document buildDocument(final GaugeInfoResult result, final GlobalContext context, final CallMeta meta) { |
233 | |
234 final Document doc = XMLUtils.newDocument(); | |
235 | |
236 final ElementCreator cr = new ElementCreator(doc, ArtifactNamespaceContext.NAMESPACE_URI, ArtifactNamespaceContext.NAMESPACE_PREFIX); | |
237 | |
238 final Element rootEl = cr.create(ROOT_NODE); | |
239 | |
240 doc.appendChild(rootEl); | |
241 | |
242 final Element globalErrElement = cr.create("global-error-msg"); | |
243 globalErrElement.setTextContent(result.globalErrorMsg); | |
244 rootEl.appendChild(globalErrElement); | |
245 | |
246 final List<GaugeInfoResult.GaugeInfo> values = result.gaugeInfos; | |
247 | |
248 for (final GaugeInfoResult.GaugeInfo gauge : values) { | |
249 final Element gaugeElement = cr.create("gauge"); | |
250 cr.addAttr(gaugeElement, "name", gauge.gauge.getName()); | |
251 if (gauge.startdate != null) | |
252 cr.addAttr(gaugeElement, "date-from", String.valueOf(gauge.startdate.getTime())); | |
253 | |
254 if (gauge.enddate != null) | |
255 cr.addAttr(gaugeElement, "date-to", String.valueOf(gauge.enddate.getTime())); | |
256 | |
257 if (gauge.errorMsg != null) | |
258 cr.addAttr(gaugeElement, "error-message", gauge.errorMsg); | |
259 | |
260 rootEl.appendChild(gaugeElement); | |
261 } | |
262 | |
263 return doc; | |
264 | |
265 } | |
266 | |
267 final Element buildElement(final ElementCreator cr, final String type, final Date date) { | |
268 final Element el = cr.create(type); | |
269 cr.addAttr(el, "value", String.valueOf(date.getTime())); | |
270 return el; | |
271 } | |
272 | |
273 private static final List<Gauge> getRequestedGauges(final Document data, final River river, final CallMeta meta) throws ServiceException { | |
274 | |
275 final NodeList gaugeNodes = data.getElementsByTagNameNS(ArtifactNamespaceContext.NAMESPACE_URI, "gauge"); | |
276 | |
277 final List<Gauge> gauges = new ArrayList<>(); | |
278 | |
279 for (int i = 0; i < gaugeNodes.getLength(); i++) { | |
280 final Element gaugeElt = (Element) gaugeNodes.item(i); | |
281 | |
282 final String gaugeName = (String) XMLUtils.xpath(gaugeElt, "text()", XPathConstants.STRING); | |
283 final Gauge gauge = Gauge.getGaugeByNameAndRiver(gaugeName, river); | |
284 if (gauge != null) | |
285 gauges.add(gauge); | |
286 else { | |
287 throw new ServiceException("bundu_wst_error_reading_gauges"); | |
288 } | |
289 } | |
290 | |
291 return gauges; | |
292 } | |
293 | |
294 private static String getMsg(final CallMeta meta, final String key) { | |
295 return Resources.getMsg(meta, key); | |
296 } | |
297 } |