comparison artifacts/src/main/java/org/dive4elements/river/artifacts/services/DynamicMainValuesService.java @ 9288:82c67b859aa7

bundu.bezugswst worklflow incl. service impl for mainValues to be calculated
author gernotbelger
date Tue, 24 Jul 2018 10:39:03 +0200
parents
children e4a6679b868f
comparison
equal deleted inserted replaced
9287:6c88ad449c83 9288:82c67b859aa7
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.math.BigDecimal;
12 import java.util.ArrayList;
13 import java.util.Calendar;
14 import java.util.Date;
15 import java.util.List;
16
17 import org.dive4elements.artifacts.CallMeta;
18 import org.dive4elements.artifacts.GlobalContext;
19 import org.dive4elements.artifacts.common.ArtifactNamespaceContext;
20 import org.dive4elements.artifacts.common.utils.XMLUtils;
21 import org.dive4elements.river.model.Gauge;
22 import org.dive4elements.river.model.MainValue;
23 import org.dive4elements.river.model.MainValueType;
24 import org.dive4elements.river.model.MainValueType.MainValueTypeKey;
25 import org.dive4elements.river.model.NamedMainValue;
26 import org.dive4elements.river.model.OfficialLine;
27 import org.dive4elements.river.model.River;
28 import org.dive4elements.river.model.TimeInterval;
29 import org.w3c.dom.Document;
30
31 /**
32 * This service returns the main values of a river's gauge based on the start
33 * and end point of the river.
34 *
35 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
36 */
37 public class DynamicMainValuesService extends AbstractMainValuesService {
38
39 private static final long serialVersionUID = 1L;
40
41 private static final String XPATH_START_YEAR = "/art:mainvalues/art:startYear/text()";
42
43 private static final String XPATH_END_YEAR = "/art:mainvalues/art:endYear/text()";
44
45 @Override
46 public Document doProcess(final Document data, final GlobalContext context, final CallMeta callMeta) {
47 try {
48
49 final River river = getRequestedRiver(data);
50 final Gauge gauge = getRequestedGauge(data, river);
51 final Date startTime = getRequestedStartYear(data);
52 final Date endTime = getRequestedEndYear(data);
53
54 final List<MainValue> mainValues = getMainValues(river, gauge, startTime, endTime);
55
56 return buildDocument(river, gauge, mainValues, context);
57 }
58 catch (final MainValuesServiceException e) {
59 e.printStackTrace();
60 return error(e.getMessage());
61 }
62 }
63
64 private Date getRequestedStartYear(final Document data) throws MainValuesServiceException {
65
66 final String startStr = XMLUtils.xpathString(data, XPATH_START_YEAR, ArtifactNamespaceContext.INSTANCE);
67
68 if (startStr == null)
69 throw new MainValuesServiceException("no start year");
70
71 try {
72 final int year = Integer.parseInt(startStr);
73
74 // FIXME: timezone? probably must match timezone of database
75 final Calendar cal = Calendar.getInstance();
76 cal.clear();
77 cal.set(year, 0, 1);
78 return cal.getTime();
79 }
80 catch (final NumberFormatException e) {
81 e.printStackTrace();
82 throw new MainValuesServiceException("invalid start year");
83 }
84 }
85
86 private Date getRequestedEndYear(final Document data) throws MainValuesServiceException {
87
88 final String endStr = XMLUtils.xpathString(data, XPATH_END_YEAR, ArtifactNamespaceContext.INSTANCE);
89
90 if (endStr == null)
91 throw new MainValuesServiceException("no end year");
92
93 try {
94 final int year = Integer.parseInt(endStr);
95
96 // FIXME: timezone? probably must match timezone of database
97 final Calendar cal = Calendar.getInstance();
98 cal.clear();
99 cal.set(year, 11, 31);
100 return cal.getTime();
101 }
102 catch (final NumberFormatException e) {
103 e.printStackTrace();
104 throw new MainValuesServiceException("invalid end year");
105 }
106 }
107
108 /**
109 * This method creates the result document that includes the main values of
110 * the specified <i>gauge</i>.
111 *
112 * @param river
113 * The river.
114 * @param gauge
115 * The gauge.
116 * @param endYear
117 * @param startYear
118 *
119 * @return a document that includes the main values of the specified river
120 * at the specified gauge.
121 */
122 protected List<MainValue> getMainValues(final River river, final Gauge gauge, final Date startTime, final Date endTime) {
123
124 // TODO: compute our own main values from the discharge timeseries.
125
126 // final List<MainValue> mainValues = gauge.getMainValues();
127 final List<MainValue> mainValues = new ArrayList<>();
128
129 final MainValue myMain = new MainValue();
130
131 // TODO: fetch real NamedMainValue from database: GLQ20, MNQ, MQ, MHQ, HQ5 + Dauerzahlen
132 final NamedMainValue mainValue = new NamedMainValue("Testname", new MainValueType(MainValueTypeKey.Q.getName()));
133 mainValue.setOfficialLines(new ArrayList<OfficialLine>());
134
135 myMain.setMainValue(mainValue);
136 // FIXME: compute value
137 myMain.setValue(new BigDecimal("1234.567"));
138
139 final TimeInterval timeInterval = new TimeInterval(startTime, endTime);
140 myMain.setTimeInterval(timeInterval);
141
142 mainValues.add(myMain);
143
144 return mainValues;
145 }
146 }

http://dive4elements.wald.intevation.org