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.Date; |
|
12 import java.util.List; |
|
13 |
|
14 import org.dive4elements.artifacts.CallMeta; |
|
15 import org.dive4elements.artifacts.GlobalContext; |
|
16 import org.dive4elements.artifacts.common.ArtifactNamespaceContext; |
|
17 import org.dive4elements.artifacts.common.utils.XMLUtils; |
|
18 import org.dive4elements.river.model.Gauge; |
|
19 import org.dive4elements.river.model.MainValue; |
|
20 import org.dive4elements.river.model.River; |
|
21 import org.w3c.dom.Document; |
|
22 |
|
23 /** |
|
24 * This service returns the main values of a river's gauge based on the start |
|
25 * and end point of the river. |
|
26 * |
|
27 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a> |
|
28 */ |
|
29 abstract public class AbstractDynamicMainValuesService extends AbstractMainValuesService { |
|
30 |
|
31 private static final long serialVersionUID = 1L; |
|
32 |
|
33 @Override |
|
34 public Document doProcess(final Document data, final GlobalContext context, final CallMeta callMeta) { |
|
35 try { |
|
36 |
|
37 final River river = getRequestedRiver(data, "/art:mainvalues/art:river/text()"); |
|
38 final Gauge gauge = getRequestedGauge(data, river); |
|
39 final Date startTime = getRequestedStartDate(data, "/art:mainvalues/art:startDate/text()"); |
|
40 final Date endTime = getRequestedEndDate(data, "/art:mainvalues/art:endDate/text()"); |
|
41 |
|
42 final List<MainValue> mainValues = getMainValues(river, gauge, startTime, endTime); |
|
43 |
|
44 return buildDocument(river, gauge, mainValues, context); |
|
45 } |
|
46 catch (final MainValuesServiceException e) { |
|
47 // e.printStackTrace(); |
|
48 return error(e.getMessage()); |
|
49 } |
|
50 catch (final Exception e) { |
|
51 e.printStackTrace(); |
|
52 return error(e.getMessage()); |
|
53 } |
|
54 } |
|
55 |
|
56 public static final Date getRequestedStartDate(final Document data, final String XPATH_END_YEAR) throws MainValuesServiceException { |
|
57 |
|
58 final String startStr = XMLUtils.xpathString(data, XPATH_END_YEAR, ArtifactNamespaceContext.INSTANCE); |
|
59 |
|
60 if (startStr == null) |
|
61 throw new MainValuesServiceException("no start date"); |
|
62 |
|
63 try { |
|
64 return new Date(Long.parseLong(startStr)); |
|
65 // FIXME: timezone? probably must match timezone of database |
|
66 } |
|
67 catch (final NumberFormatException e) { |
|
68 e.printStackTrace(); |
|
69 throw new MainValuesServiceException("invalid start date"); |
|
70 } |
|
71 } |
|
72 |
|
73 public static final Date getRequestedEndDate(final Document data, final String XPATH_END_DATE) throws MainValuesServiceException { |
|
74 |
|
75 final String endStr = XMLUtils.xpathString(data, XPATH_END_DATE, ArtifactNamespaceContext.INSTANCE); |
|
76 |
|
77 if (endStr == null) |
|
78 throw new MainValuesServiceException("no end date"); |
|
79 |
|
80 try { |
|
81 return new Date(Long.parseLong(endStr)); |
|
82 // FIXME: timezone? probably must match timezone of database |
|
83 } |
|
84 catch (final NumberFormatException e) { |
|
85 e.printStackTrace(); |
|
86 throw new MainValuesServiceException("invalid end date"); |
|
87 } |
|
88 } |
|
89 |
|
90 /** |
|
91 * Computes a gauge's main values for a period of time based on its daily discharges stored in the database |
|
92 */ |
|
93 protected abstract List<MainValue> getMainValues(final River river, final Gauge gauge, final Date startTime, final Date endTime) |
|
94 throws MainValuesServiceException; |
|
95 |
|
96 } |