Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/services/DischargeInfoService.java @ 3818:dc18457b1cef
merged flys-artifacts/pre2.7-2012-03-16
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:59 +0200 |
parents | 2966787b5188 |
children | e96bf6c47c12 |
comparison
equal
deleted
inserted
replaced
2456:60ab1054069d | 3818:dc18457b1cef |
---|---|
1 package de.intevation.flys.artifacts.services; | |
2 | |
3 import java.util.List; | |
4 import java.util.Date; | |
5 import java.util.Calendar; | |
6 import java.util.GregorianCalendar; | |
7 | |
8 import org.apache.log4j.Logger; | |
9 | |
10 import org.w3c.dom.Document; | |
11 import org.w3c.dom.Element; | |
12 | |
13 import de.intevation.artifacts.CallMeta; | |
14 import de.intevation.artifacts.GlobalContext; | |
15 | |
16 import de.intevation.artifacts.common.ArtifactNamespaceContext; | |
17 import de.intevation.artifacts.common.utils.XMLUtils; | |
18 | |
19 import de.intevation.flys.model.Gauge; | |
20 import de.intevation.flys.model.DischargeTable; | |
21 import de.intevation.flys.model.TimeInterval; | |
22 | |
23 /** | |
24 * This service provides information about discharges at a defined gauge. | |
25 * | |
26 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a> | |
27 */ | |
28 public class DischargeInfoService extends FLYSService { | |
29 | |
30 /** The logger used in this service. */ | |
31 private static Logger logger = Logger.getLogger(DischargeInfoService.class); | |
32 | |
33 public static final String GAUGE_XPATH = "/art:gauge/text()"; | |
34 | |
35 public DischargeInfoService() { | |
36 } | |
37 | |
38 | |
39 @Override | |
40 public Document doProcess( | |
41 Document data, | |
42 GlobalContext globalContext, | |
43 CallMeta callMeta | |
44 ) { | |
45 logger.debug("DischargeInfoService.process"); | |
46 | |
47 String gaugeNumber = XMLUtils.xpathString( | |
48 data, GAUGE_XPATH, ArtifactNamespaceContext.INSTANCE); | |
49 | |
50 if(gaugeNumber == null || | |
51 (gaugeNumber = gaugeNumber.trim()).length() == 0) { | |
52 logger.warn("No gauge specified. Cannot return discharge info."); | |
53 return XMLUtils.newDocument(); | |
54 } | |
55 | |
56 logger.debug("Getting discharge for gauge: " + gaugeNumber); | |
57 | |
58 long gn; | |
59 try { | |
60 gn = Long.parseLong(gaugeNumber); | |
61 } | |
62 catch (NumberFormatException nfe) { | |
63 logger.warn("Invalid gauge number. Cannot return discharg info."); | |
64 return XMLUtils.newDocument(); | |
65 } | |
66 | |
67 Gauge gauge = Gauge.getGaugeByOfficialNumber(gn); | |
68 | |
69 logger.debug("Found gauge: " + gauge.getName()); | |
70 | |
71 return buildDocument(gauge); | |
72 } | |
73 | |
74 | |
75 protected Document buildDocument(Gauge gauge) { | |
76 Document result = XMLUtils.newDocument(); | |
77 | |
78 List<DischargeTable> tables =gauge.getDischargeTables(); | |
79 | |
80 Element all = result.createElement("discharges"); | |
81 for (DischargeTable dt: tables) { | |
82 Element discharge = result.createElement("discharge"); | |
83 discharge.setAttribute("description", dt.getDescription()); | |
84 | |
85 // Get time interval. | |
86 TimeInterval ti = dt.getTimeInterval(); | |
87 Date startTime = ti.getStartTime(); | |
88 Date stopTime = ti.getStopTime(); | |
89 | |
90 // Get the year for start end end date. | |
91 int startYear; | |
92 int stopYear; | |
93 Calendar c = new GregorianCalendar(); | |
94 if (startTime != null) { | |
95 c.setTime(startTime); | |
96 startYear = c.get(Calendar.YEAR); | |
97 } | |
98 else { | |
99 startYear = -1; | |
100 } | |
101 if (stopTime != null) { | |
102 c.setTime(stopTime); | |
103 stopYear = c.get(Calendar.YEAR); | |
104 } | |
105 else { | |
106 stopYear = -1; | |
107 } | |
108 | |
109 discharge.setAttribute("start", String.valueOf(startYear)); | |
110 discharge.setAttribute("end", String.valueOf(stopYear)); | |
111 | |
112 all.appendChild(discharge); | |
113 } | |
114 result.appendChild(all); | |
115 return result; | |
116 } | |
117 } |