Mercurial > dive4elements > river
comparison 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 |
comparison
equal
deleted
inserted
replaced
9587:d116a791b1f8 | 9588:c57caff9b00b |
---|---|
46 public ServiceException(final String message) { | 46 public ServiceException(final String message) { |
47 super(message); | 47 super(message); |
48 } | 48 } |
49 } | 49 } |
50 | 50 |
51 private static class GaugeInfoResult { | 51 public static class GaugeInfoResult { |
52 protected final String globalErrorMsg; | 52 protected final String globalErrorMsg; |
53 protected final List<GaugeInfo> gaugeInfos; | 53 protected final List<GaugeInfo> gaugeInfos; |
54 | 54 private final Date globalStartDate; |
55 protected GaugeInfoResult(final List<GaugeInfo> gaugeInfos, final String globalErrorMsg) { | 55 private final Date globalEndDate; |
56 | |
57 protected GaugeInfoResult(final List<GaugeInfo> gaugeInfos, final String globalErrorMsg, final Date min, final Date max) { | |
56 this.gaugeInfos = gaugeInfos; | 58 this.gaugeInfos = gaugeInfos; |
57 this.globalErrorMsg = globalErrorMsg; | 59 this.globalErrorMsg = globalErrorMsg; |
60 this.globalStartDate = min; | |
61 this.globalEndDate = max; | |
62 } | |
63 | |
64 public int getGlobalEndYear() { | |
65 return getYearFromDate(this.globalEndDate); | |
66 } | |
67 | |
68 public int getGlobalStartYear() { | |
69 return getYearFromDate(this.globalStartDate); | |
58 } | 70 } |
59 | 71 |
60 private static class GaugeInfo { | 72 private static class GaugeInfo { |
61 protected final String errorMsg; | 73 protected final String errorMsg; |
62 protected final Gauge gauge; | 74 protected final Gauge gauge; |
79 /** | 91 /** |
80 * Queries the available daily discharge time periods of a list of gauges from the database and checks the overlapping | 92 * Queries the available daily discharge time periods of a list of gauges from the database and checks the overlapping |
81 * | 93 * |
82 * @throws ServiceException | 94 * @throws ServiceException |
83 */ | 95 */ |
84 private GaugeInfoResult getCommonTimeRangeForGauges(final List<Gauge> gauges, final Date startTime, final Date endTime, final CallMeta meta) | 96 public static GaugeInfoResult getCommonTimeRangeForGauges(final List<Gauge> gauges, final int startYear, final int endYear, final CallMeta meta) { |
85 throws ServiceException { | 97 |
86 | 98 final Date startTime = getStartDateFromYear(startYear); |
99 final Date endTime = getEndDateFromYear(endYear); | |
87 // Query the gauge's daily Q values | 100 // Query the gauge's daily Q values |
88 String globalErrorMsg = ""; | 101 String globalErrorMsg = ""; |
89 final List<GaugeInfoResult.GaugeInfo> gaugeResults = new ArrayList<>(); | 102 final List<GaugeInfoResult.GaugeInfo> gaugeResults = new ArrayList<>(); |
90 final Date qStartTime = DateUtils.getAbflussYear(startTime)[0]; | 103 final Date qStartTime = DateUtils.getAbflussYear(startTime)[0]; |
91 Date min = qStartTime; | 104 Date min = qStartTime; |
120 // common Range and correct errorMsg | 133 // common Range and correct errorMsg |
121 min = DateUtils.getNextNewYear(min); | 134 min = DateUtils.getNextNewYear(min); |
122 final List<GaugeInfoResult.GaugeInfo> gaugeResultsSecondTurn = new ArrayList<>(); | 135 final List<GaugeInfoResult.GaugeInfo> gaugeResultsSecondTurn = new ArrayList<>(); |
123 for (final GaugeInfoResult.GaugeInfo gi : gaugeResults) { | 136 for (final GaugeInfoResult.GaugeInfo gi : gaugeResults) { |
124 gaugeResultsSecondTurn | 137 gaugeResultsSecondTurn |
125 .add(new GaugeInfoResult.GaugeInfo(gi.errorMsg, gi.gauge, gi.startdate != null ? min : null, gi.enddate != null ? max : null)); | 138 .add(new GaugeInfoResult.GaugeInfo(gi.errorMsg, gi.gauge, gi.startdate != null ? min : null, gi.enddate != null ? max : null)); |
126 } | 139 } |
127 if (globalErrorMsg.isEmpty() && (min.getTime() > max.getTime())) | 140 if (globalErrorMsg.isEmpty() && (min.getTime() > max.getTime())) |
128 globalErrorMsg = getMsg(meta, "bundu.wst.gauge_timeranges_disjoint"); | 141 globalErrorMsg = getMsg(meta, "bundu.wst.gauge_timeranges_disjoint"); |
129 | 142 |
130 // add "cannot calculate UD" to globalErrorMsg | 143 // add "cannot calculate UD" to globalErrorMsg |
131 // Eine Berechnung der UD ist nicht möglich. | 144 // Eine Berechnung der UD ist nicht möglich. |
132 if (!globalErrorMsg.isEmpty()) | 145 if (!globalErrorMsg.isEmpty()) |
133 globalErrorMsg = new StringBuilder().append(globalErrorMsg).append("\n").append(getMsg(meta, "bundu.wst.gauge_no_ud_calc_available")).toString(); | 146 globalErrorMsg = new StringBuilder().append(globalErrorMsg).append("\n").append(getMsg(meta, "bundu.wst.gauge_no_ud_calc_available")).toString(); |
134 | 147 |
135 final GaugeInfoResult result = new GaugeInfoResult(gaugeResultsSecondTurn, globalErrorMsg); | 148 final GaugeInfoResult result = new GaugeInfoResult(gaugeResultsSecondTurn, globalErrorMsg, min, max); |
136 | 149 |
137 return result; | 150 return result; |
138 } | 151 } |
139 | 152 |
140 private String makeDoesNotCoverErrorMsg(final int startYear, final int endYear, final CallMeta meta) { | 153 private static String makeDoesNotCoverErrorMsg(final int startYear, final int endYear, final CallMeta meta) { |
141 final String msgkey = "bundu.wst.range_does_not_cover"; | 154 final String msgkey = "bundu.wst.range_does_not_cover"; |
142 return Resources.getMsg(meta, msgkey, msgkey, startYear, endYear); | 155 return Resources.getMsg(meta, msgkey, msgkey, startYear, endYear); |
143 } | 156 } |
144 | 157 |
145 @Override | 158 @Override |
146 public Document doProcess(final Document data, final GlobalContext context, final CallMeta callMeta) { | 159 public Document doProcess(final Document data, final GlobalContext context, final CallMeta callMeta) { |
147 try { | 160 try { |
148 final River river = AbstractMainValuesService.getRequestedRiver(data, "/art:" + ROOT_NODE + "/art:river/text()"); | 161 final River river = AbstractMainValuesService.getRequestedRiver(data, "/art:" + ROOT_NODE + "/art:river/text()"); |
149 final List<Gauge> gauges = getRequestedGauges(data, river, callMeta); | 162 final List<Gauge> gauges = getRequestedGauges(data, river, callMeta); |
150 final Date start = getRequestedStartYear(data, "/art:" + ROOT_NODE + "/art:startYear/text()"); | 163 final int start = getRequestedStartYear(data, "/art:" + ROOT_NODE + "/art:startYear/text()"); |
151 final Date end = getRequestedEndYear(data, "/art:" + ROOT_NODE + "/art:endYear/text()"); | 164 final int end = getRequestedEndYear(data, "/art:" + ROOT_NODE + "/art:endYear/text()"); |
152 | 165 |
153 final GaugeInfoResult result = getCommonTimeRangeForGauges(gauges, start, end, callMeta); | 166 final GaugeInfoResult result = getCommonTimeRangeForGauges(gauges, start, end, callMeta); |
154 | 167 |
155 return buildDocument(result, context, callMeta); | 168 return buildDocument(result, context, callMeta); |
156 } | 169 } |
158 e.printStackTrace(); | 171 e.printStackTrace(); |
159 return AbstractMainValuesService.error(e.getMessage()); | 172 return AbstractMainValuesService.error(e.getMessage()); |
160 } | 173 } |
161 } | 174 } |
162 | 175 |
163 public static final Date getRequestedEndYear(final Document data, final String XPATH_END_YEAR) throws MainValuesServiceException { | 176 public static final int getRequestedEndYear(final Document data, final String XPATH_END_YEAR) throws MainValuesServiceException { |
164 | 177 |
165 final String endStr = XMLUtils.xpathString(data, XPATH_END_YEAR, ArtifactNamespaceContext.INSTANCE); | 178 final String endStr = XMLUtils.xpathString(data, XPATH_END_YEAR, ArtifactNamespaceContext.INSTANCE); |
166 | 179 |
167 if (endStr == null) | 180 if (endStr == null) |
168 throw new MainValuesServiceException("no end year"); // should not happen | 181 throw new MainValuesServiceException("no end year"); // should not happen |
169 | 182 |
170 try { | 183 try { |
171 final int year = Integer.parseInt(endStr); | 184 return Integer.parseInt(endStr); |
172 | |
173 // FIXME: timezone? probably must match timezone of database | |
174 final Calendar cal = Calendar.getInstance(); | |
175 cal.clear(); | |
176 cal.set(year, 11, 31); | |
177 return cal.getTime(); | |
178 } | 185 } |
179 catch (final NumberFormatException e) { | 186 catch (final NumberFormatException e) { |
180 e.printStackTrace(); | 187 e.printStackTrace(); |
181 throw new MainValuesServiceException("invalid end year"); // should not happen | 188 throw new MainValuesServiceException("invalid end year"); // should not happen |
182 } | 189 } |
183 } | 190 } |
184 | 191 |
185 public static final Date getRequestedStartYear(final Document data, final String XPATH_START_YEAR) throws MainValuesServiceException { | 192 public static final int getRequestedStartYear(final Document data, final String XPATH_START_YEAR) throws MainValuesServiceException { |
186 | 193 |
187 final String startStr = XMLUtils.xpathString(data, XPATH_START_YEAR, ArtifactNamespaceContext.INSTANCE); | 194 final String startStr = XMLUtils.xpathString(data, XPATH_START_YEAR, ArtifactNamespaceContext.INSTANCE); |
188 | 195 |
189 if (startStr == null) | 196 if (startStr == null) |
190 throw new MainValuesServiceException("no start year");// should not happen | 197 throw new MainValuesServiceException("no start year");// should not happen |
191 | 198 |
192 try { | 199 try { |
193 final int year = Integer.parseInt(startStr); | 200 return Integer.parseInt(startStr); |
194 | |
195 // FIXME: timezone? probably must match timezone of database | |
196 final Calendar cal = Calendar.getInstance(); | |
197 cal.clear(); | |
198 cal.set(year, 0, 1); | |
199 return cal.getTime(); | |
200 } | 201 } |
201 catch (final NumberFormatException e) { | 202 catch (final NumberFormatException e) { |
202 e.printStackTrace(); | 203 e.printStackTrace(); |
203 throw new MainValuesServiceException("invalid start year"); // should not happen | 204 throw new MainValuesServiceException("invalid start year"); // should not happen |
204 } | 205 } |
206 } | |
207 | |
208 private static Integer getYearFromDate(final Date date) { | |
209 // FIXME: timezone? probably must match timezone of database | |
210 final Calendar cal = Calendar.getInstance(); | |
211 cal.clear(); | |
212 cal.setTime(date); | |
213 return cal.get(Calendar.YEAR); | |
214 } | |
215 | |
216 private static Date getEndDateFromYear(final int year) { | |
217 // FIXME: timezone? probably must match timezone of database | |
218 final Calendar cal = Calendar.getInstance(); | |
219 cal.clear(); | |
220 cal.set(year, 11, 31); | |
221 return cal.getTime(); | |
222 } | |
223 | |
224 private static Date getStartDateFromYear(final int year) { | |
225 // FIXME: timezone? probably must match timezone of database | |
226 final Calendar cal = Calendar.getInstance(); | |
227 cal.clear(); | |
228 cal.set(year, 0, 1); | |
229 return cal.getTime(); | |
205 } | 230 } |
206 | 231 |
207 private Document buildDocument(final GaugeInfoResult result, final GlobalContext context, final CallMeta meta) { | 232 private Document buildDocument(final GaugeInfoResult result, final GlobalContext context, final CallMeta meta) { |
208 | 233 |
209 final Document doc = XMLUtils.newDocument(); | 234 final Document doc = XMLUtils.newDocument(); |