Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/services/SedimentLoadInfoService.java @ 8229:0bf888783683
(issue1448) Extend SL info service to handle sq_time_intervals
author | Andre Heinecke <andre.heinecke@intevation.de> |
---|---|
date | Mon, 08 Sep 2014 16:21:26 +0200 |
parents | e4606eae8ea5 |
children | b207eeb66edd |
comparison
equal
deleted
inserted
replaced
8228:2f63c6c84540 | 8229:0bf888783683 |
---|---|
8 | 8 |
9 package org.dive4elements.river.artifacts.services; | 9 package org.dive4elements.river.artifacts.services; |
10 | 10 |
11 import java.util.Calendar; | 11 import java.util.Calendar; |
12 import java.util.Collection; | 12 import java.util.Collection; |
13 import java.util.Iterator; | |
14 import java.util.Date; | |
13 | 15 |
14 import org.apache.log4j.Logger; | 16 import org.apache.log4j.Logger; |
15 import org.w3c.dom.Document; | 17 import org.w3c.dom.Document; |
16 import org.w3c.dom.Element; | 18 import org.w3c.dom.Element; |
17 | 19 |
30 /** The log used in this service. */ | 32 /** The log used in this service. */ |
31 private static Logger log = Logger.getLogger(SedimentLoadInfoService.class); | 33 private static Logger log = Logger.getLogger(SedimentLoadInfoService.class); |
32 | 34 |
33 public static final String RIVER_XPATH = "/art:river/text()"; | 35 public static final String RIVER_XPATH = "/art:river/text()"; |
34 public static final String TYPE_XPATH = "/art:river/art:type/text()"; | 36 public static final String TYPE_XPATH = "/art:river/art:type/text()"; |
37 public static final String SQ_TI_XPATH = "/art:river/art:sq_ti_id/text()"; | |
35 public static final String FROM_XPATH = "/art:river/art:location/art:from/text()"; | 38 public static final String FROM_XPATH = "/art:river/art:location/art:from/text()"; |
36 public static final String TO_XPATH = "/art:river/art:location/art:to/text()"; | 39 public static final String TO_XPATH = "/art:river/art:location/art:to/text()"; |
37 | 40 |
38 /** | 41 /** |
39 * Create document with sedimentload infos, | 42 * Create document with sedimentload infos, |
58 ArtifactNamespaceContext.INSTANCE); | 61 ArtifactNamespaceContext.INSTANCE); |
59 String to = XMLUtils.xpathString( | 62 String to = XMLUtils.xpathString( |
60 data, | 63 data, |
61 TO_XPATH, | 64 TO_XPATH, |
62 ArtifactNamespaceContext.INSTANCE); | 65 ArtifactNamespaceContext.INSTANCE); |
66 String sq_ti_id = XMLUtils.xpathString( | |
67 data, | |
68 SQ_TI_XPATH, | |
69 ArtifactNamespaceContext.INSTANCE); | |
63 double fromD, toD; | 70 double fromD, toD; |
64 try { | 71 try { |
65 fromD = Double.parseDouble(from); | 72 fromD = Double.parseDouble(from); |
66 toD = Double.parseDouble(to); | 73 toD = Double.parseDouble(to); |
67 } | 74 } |
73 /* This call initializes the sedimentloaddata for the river. Might be | 80 /* This call initializes the sedimentloaddata for the river. Might be |
74 * expensive but has to be done anyway for the calculation later on. */ | 81 * expensive but has to be done anyway for the calculation later on. */ |
75 SedimentLoadData allLoadData = SedimentLoadDataFactory.INSTANCE.getSedimentLoadData( | 82 SedimentLoadData allLoadData = SedimentLoadDataFactory.INSTANCE.getSedimentLoadData( |
76 river); | 83 river); |
77 | 84 |
78 Collection <Load> loads = allLoadData.findLoads(fromD, toD); | 85 log.debug("Requested type: " + type + " with sq_ti_id: " + sq_ti_id); |
86 Collection <Load> loads; | |
87 if (type.equals("off_epoch")) { | |
88 /* TODO */ | |
89 loads = null; | |
90 } else if (type.equals("sq_time_intervals")) { | |
91 loads = allLoadData.findUniqueTimeIntervalLoads(fromD, toD); | |
92 | |
93 for (Iterator<Load> it = loads.iterator(); it.hasNext();) { | |
94 /* Skip loads without time interval for this info type. */ | |
95 if (it.next().getSQRelationTimeIntervalId() == 0) { | |
96 it.remove(); | |
97 } | |
98 } | |
99 } else { | |
100 if (!sq_ti_id.isEmpty()) { | |
101 int id = Integer.parseInt(sq_ti_id); | |
102 log.debug("Finding only items for id"); | |
103 loads = allLoadData.findLoads(fromD, toD, id); | |
104 } else { | |
105 log.debug("Finding everything."); | |
106 loads = allLoadData.findLoads(fromD, toD); | |
107 } | |
108 } | |
79 | 109 |
80 return buildDocument(loads); | 110 return buildDocument(loads); |
81 } | 111 } |
82 | 112 |
83 protected Document buildDocument(Collection<Load> loads) { | 113 protected Document buildDocument(Collection<Load> loads) { |
102 calendar.setTime(load.getStartTime()); | 132 calendar.setTime(load.getStartTime()); |
103 ele.setAttribute( | 133 ele.setAttribute( |
104 "date", | 134 "date", |
105 String.valueOf(calendar.get(Calendar.YEAR))); | 135 String.valueOf(calendar.get(Calendar.YEAR))); |
106 } | 136 } |
137 /* SQ Time interval */ | |
138 if (load.getSQRelationTimeIntervalId() != 0) { | |
139 ele.setAttribute("sq_ti_id", String.valueOf(load.getSQRelationTimeIntervalId())); | |
140 Date start = load.getSQStartTime(); | |
141 Date stop = load.getSQStopTime(); | |
142 if (start != null && stop != null) { | |
143 Calendar calendarS = Calendar.getInstance(); | |
144 calendarS.setTime(start); | |
145 Calendar calendarE = Calendar.getInstance(); | |
146 calendarE.setTime(stop); | |
147 ele.setAttribute( | |
148 "sq_date", | |
149 calendarS.get(Calendar.YEAR) + | |
150 " - " + | |
151 calendarE.get(Calendar.YEAR)); | |
152 } else if (start != null) { | |
153 Calendar calendar = Calendar.getInstance(); | |
154 calendar.setTime(start); | |
155 ele.setAttribute( | |
156 "sq_date", | |
157 String.valueOf(calendar.get(Calendar.YEAR))); | |
158 } else { | |
159 log.warn("Load: " + load.getSQRelationTimeIntervalId() + | |
160 " has no beginning."); | |
161 } | |
162 } | |
163 | |
107 all.appendChild(ele); | 164 all.appendChild(ele); |
108 } | 165 } |
109 result.appendChild(all); | 166 result.appendChild(all); |
110 return result; | 167 return result; |
111 } | 168 } |