comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/access/FixationArtifactAccess.java @ 3203:1b9f791937c3

Moved FixationArtifactAccess to new access package. flys-artifacts/trunk@4820 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 28 Jun 2012 08:16:20 +0000
parents
children e11dbf8baf69
comparison
equal deleted inserted replaced
3202:307842cf8d9e 3203:1b9f791937c3
1 package de.intevation.flys.artifacts.access;
2
3 import de.intevation.artifactdatabase.data.StateData;
4
5 import de.intevation.flys.artifacts.FLYSArtifact;
6
7 import de.intevation.flys.artifacts.model.fixings.DateRange;
8
9 import de.intevation.flys.utils.FLYSUtils;
10
11 import gnu.trove.TDoubleArrayList;
12
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.Date;
16
17 import org.apache.log4j.Logger;
18
19 public class FixationArtifactAccess
20 {
21 private static Logger log = Logger.getLogger(FixationArtifactAccess.class);
22
23 protected FLYSArtifact artifact;
24
25 protected String river;
26
27 protected String calculationMode;
28
29 protected Double from;
30 protected Double to;
31 protected Double step;
32
33 protected Long start;
34 protected Long end;
35
36 protected Integer qSectorStart;
37 protected Integer qSectorEnd;
38
39 protected DateRange referencePeriod;
40 protected DateRange [] analysisPeriods;
41
42 protected int [] events;
43
44 protected Boolean preprocessing;
45
46 protected String function;
47
48 protected double [] qs;
49
50 public FixationArtifactAccess() {
51 }
52
53 public FixationArtifactAccess(FLYSArtifact artifact) {
54 this.artifact = artifact;
55 }
56
57 public FLYSArtifact getArtifact() {
58 return artifact;
59 }
60
61 public String getRiver() {
62 if (river == null) {
63 StateData sd = artifact.getData("river");
64 if (sd == null) {
65 log.warn("missing 'river' value");
66 return null;
67 }
68 river = (String)sd.getValue();
69 }
70 if (log.isDebugEnabled()) {
71 log.debug("river: '" + river + "'");
72 }
73 return river;
74 }
75
76 public String getCalculationMode() {
77 if (calculationMode == null) {
78 StateData sd = artifact.getData("calculation.mode");
79 if (sd == null) {
80 log.warn("missing 'calculation.mode' value");
81 return null;
82 }
83 calculationMode = (String)sd.getValue();
84 }
85
86 if (log.isDebugEnabled()) {
87 log.debug("calculationMode: '" + calculationMode + "'");
88 }
89 return calculationMode;
90 }
91
92 public Double getFrom() {
93
94 if (from == null) {
95 StateData sd = artifact.getData("from");
96 if (sd == null) {
97 log.warn("missing 'from' value");
98 return null;
99 }
100 try {
101 from = Double.valueOf((String)sd.getValue());
102 }
103 catch (NumberFormatException nfe) {
104 log.warn("from '" + sd.getValue() + "' is not numeric.");
105 }
106 }
107
108 if (log.isDebugEnabled()) {
109 log.debug("from: '" + from + "'");
110 }
111
112 return from;
113 }
114
115 public Double getTo() {
116
117 if (to == null) {
118 StateData sd = artifact.getData("to");
119 if (sd == null) {
120 log.warn("missing 'to' value");
121 return null;
122 }
123 try {
124 to = Double.valueOf((String)sd.getValue());
125 }
126 catch (NumberFormatException nfe) {
127 log.warn("to '" + sd.getValue() + "' is not numeric.");
128 }
129 }
130
131 if (log.isDebugEnabled()) {
132 log.debug("to: '" + to + "'");
133 }
134
135 return to;
136 }
137
138 public Double getStep() {
139
140 if (step == null) {
141 StateData sd = artifact.getData("step");
142 if (sd == null) {
143 log.warn("missing 'step' value");
144 return null;
145 }
146 try {
147 step = Double.valueOf((String)sd.getValue());
148 }
149 catch (NumberFormatException nfe) {
150 log.warn("step '" + sd.getValue() + "' is not numeric.");
151 }
152 }
153
154 if (log.isDebugEnabled()) {
155 log.debug("step: '" + step + "'");
156 }
157
158 return step;
159 }
160
161 public Long getStart() {
162
163 if (start == null) {
164 StateData sd = artifact.getData("start");
165 if (sd == null) {
166 log.warn("missing 'start' value");
167 return null;
168 }
169 try {
170 start = Long.valueOf((String)sd.getValue());
171 }
172 catch (NumberFormatException nfe) {
173 log.warn("start '" + sd.getValue() + "' is not an integer.");
174 }
175 }
176
177 if (log.isDebugEnabled()) {
178 log.debug("start: '" + start + "'");
179 }
180
181 return start;
182 }
183
184 public Long getEnd() {
185
186 if (end == null) {
187 StateData sd = artifact.getData("end");
188 if (sd == null) {
189 log.warn("missing 'end' value");
190 return null;
191 }
192 try {
193 end = Long.valueOf((String)sd.getValue());
194 }
195 catch (NumberFormatException nfe) {
196 log.warn("end '" + sd.getValue() + "' is not an integer.");
197 }
198 }
199
200 if (log.isDebugEnabled()) {
201 log.debug("end: '" + end + "'");
202 }
203
204 return end;
205 }
206
207 public Integer getQSectorStart() {
208
209 if (qSectorStart == null) {
210 StateData sd = artifact.getData("q1");
211 if (sd == null) {
212 log.warn("missing 'q1' value");
213 return null;
214 }
215 try {
216 qSectorStart = Integer.valueOf((String)sd.getValue());
217 }
218 catch (NumberFormatException nfe) {
219 log.warn("q1 '" + sd.getValue() + "' is not an integer.");
220 }
221 }
222
223 return qSectorStart;
224 }
225
226 public Integer getQSectorEnd() {
227
228 if (qSectorEnd == null) {
229 StateData sd = artifact.getData("q2");
230 if (sd == null) {
231 log.warn("missing 'q2' value");
232 return null;
233 }
234 try {
235 qSectorEnd = Integer.valueOf((String)sd.getValue());
236 }
237 catch (NumberFormatException nfe) {
238 log.warn("q2 '" + sd.getValue() + "' is not an integer.");
239 }
240 }
241
242 return qSectorEnd;
243 }
244
245 public int [] getEvents() {
246 if (events == null) {
247 StateData sd = artifact.getData("events");
248 if (sd == null) {
249 log.warn("missing 'events' value");
250 return null;
251 }
252 events = FLYSUtils.intArrayFromString((String)sd.getValue());
253 }
254 return events;
255 }
256
257 public DateRange getReferencePeriod() {
258 if (referencePeriod == null) {
259 StateData refStart = artifact.getData("ref_start");
260 StateData refEnd = artifact.getData("ref_end");
261
262 if (refStart == null || refEnd == null) {
263 log.warn("missing 'ref_start' or 'ref_start' value");
264 return null;
265 }
266
267 try {
268 long rs = Long.parseLong((String)refStart.getValue());
269 long re = Long.parseLong((String)refEnd .getValue());
270
271 if (rs > re) { long t = rs; rs = re; re = t; }
272
273 Date from = new Date(rs);
274 Date to = new Date(re);
275 referencePeriod = new DateRange(from, to);
276 }
277 catch (NumberFormatException nfe) {
278 log.warn("ref_start or ref_end is not an integer.");
279 }
280 }
281
282 return referencePeriod;
283 }
284
285 public DateRange [] getAnalysisPeriods() {
286 if (analysisPeriods == null) {
287 StateData sd = artifact.getData("ana_data");
288
289 if (sd == null) {
290 log.warn("missing 'ana_data'");
291 return null;
292 }
293
294 String data = (String)sd.getValue();
295 String[] pairs = data.split("\\s*;\\s*");
296
297 ArrayList<DateRange> aPs = new ArrayList<DateRange>(pairs.length);
298
299 for (int i = 0; i < pairs.length; i++) {
300 String[] fromTo = pairs[i].split("\\s*,\\s*");
301 if (fromTo.length >= 2) {
302 try {
303 Date from = new Date(Long.parseLong(fromTo[0]));
304 Date to = new Date(Long.parseLong(fromTo[1]));
305 DateRange aP = new DateRange(from, to);
306 if (!aPs.contains(aP)) {
307 aPs.add(aP);
308 }
309 }
310 catch (NumberFormatException nfe) {
311 log.warn("ana_data contains no long values.", nfe);
312 }
313 }
314 }
315
316 analysisPeriods = aPs.toArray(new DateRange[aPs.size()]);
317 }
318
319 if (log.isDebugEnabled()) {
320 for (int i = 0; i < analysisPeriods.length; ++i) {
321 DateRange ap = analysisPeriods[i];
322 log.debug("analysis period " +
323 ap.getFrom() + " - " + ap.getTo());
324 }
325 }
326
327 return analysisPeriods;
328 }
329
330 public Boolean getPreprocessing() {
331 if (preprocessing == null) {
332 StateData sd = artifact.getData("preprocessing");
333 if (sd == null) {
334 log.warn("missing 'preprocessing'");
335 return null;
336 }
337 preprocessing = Boolean.valueOf((String)sd.getValue());
338 }
339 return preprocessing;
340 }
341
342 public String getFunction() {
343 if (function == null) {
344 StateData sd = artifact.getData("function");
345 if (sd == null) {
346 log.warn("missing 'function'");
347 return null;
348 }
349 function = (String)sd.getValue();
350 }
351 if (log.isDebugEnabled()) {
352 log.debug("function: " + function);
353 }
354 return function;
355 }
356
357 public double [] getQs() {
358 if (qs == null) {
359 StateData sd = artifact.getData("qs");
360 if (sd == null) {
361 log.warn("missing 'qs'");
362 return null;
363 }
364 String [] parts = ((String)sd.getValue()).split("[\\s;]");
365 TDoubleArrayList list = new TDoubleArrayList(parts.length);
366 for (String part: parts) {
367 try {
368 list.add(Double.parseDouble(part));
369 }
370 catch (NumberFormatException nfe) {
371 log.warn("'" + part + "' is not numeric.");
372 }
373 }
374 qs = list.toNativeArray();
375 }
376 if (log.isDebugEnabled()) {
377 log.debug("qs: " + Arrays.toString(qs));
378 }
379 return qs;
380 }
381
382
383 public double getCurrentKm() {
384 //TODO: get the current km.
385 return getFrom().doubleValue();
386 }
387 }
388 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org