annotate flys-backend/src/main/java/de/intevation/flys/importer/ImportTimeInterval.java @ 5780:5e3c9027e09c

avoid NPE in time interval import (made by R. Renkert)
author Tom Gottfried <tom.gottfried@intevation.de>
date Mon, 22 Apr 2013 10:48:37 +0200
parents 300c0270c576
children 67e07c29dd29
rev   line source
486
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
1 package de.intevation.flys.importer;
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
2
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
3 import de.intevation.flys.model.TimeInterval;
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
4
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
5 import org.hibernate.Session;
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
6 import org.hibernate.Query;
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
7
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
8 import java.util.List;
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
9 import java.util.Date;
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10
493
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 487
diff changeset
11 import org.apache.log4j.Logger;
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 487
diff changeset
12
486
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13 public class ImportTimeInterval
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 {
493
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 487
diff changeset
15 private static Logger log = Logger.getLogger(ImportTimeInterval.class);
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 487
diff changeset
16
486
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 protected Date startTime;
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18 protected Date stopTime;
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20 protected TimeInterval peer;
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
21
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
22 public ImportTimeInterval() {
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 }
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24
1204
22858e7cca79 Integrated PRF parsing into importer. Needs testing!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
25 public ImportTimeInterval(Date startTime) {
22858e7cca79 Integrated PRF parsing into importer. Needs testing!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
26 this.startTime = startTime;
22858e7cca79 Integrated PRF parsing into importer. Needs testing!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
27 }
22858e7cca79 Integrated PRF parsing into importer. Needs testing!
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 497
diff changeset
28
486
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 public ImportTimeInterval(Date startTime, Date stopTime) {
5780
5e3c9027e09c avoid NPE in time interval import (made by R. Renkert)
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5775
diff changeset
30 Date start;
5e3c9027e09c avoid NPE in time interval import (made by R. Renkert)
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5775
diff changeset
31 Date stop;
5e3c9027e09c avoid NPE in time interval import (made by R. Renkert)
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5775
diff changeset
32 if (startTime == null) {
5e3c9027e09c avoid NPE in time interval import (made by R. Renkert)
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5775
diff changeset
33 start = stopTime;
5e3c9027e09c avoid NPE in time interval import (made by R. Renkert)
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5775
diff changeset
34 stop = null;
5e3c9027e09c avoid NPE in time interval import (made by R. Renkert)
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5775
diff changeset
35 }
5e3c9027e09c avoid NPE in time interval import (made by R. Renkert)
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5775
diff changeset
36 else {
5e3c9027e09c avoid NPE in time interval import (made by R. Renkert)
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5775
diff changeset
37 start = startTime;
5e3c9027e09c avoid NPE in time interval import (made by R. Renkert)
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5775
diff changeset
38 stop = stopTime;
5e3c9027e09c avoid NPE in time interval import (made by R. Renkert)
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5775
diff changeset
39 }
5e3c9027e09c avoid NPE in time interval import (made by R. Renkert)
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5775
diff changeset
40
5e3c9027e09c avoid NPE in time interval import (made by R. Renkert)
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5775
diff changeset
41 if (stop != null && start.after(stop)) {
5e3c9027e09c avoid NPE in time interval import (made by R. Renkert)
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5775
diff changeset
42 this.stopTime = start;
5e3c9027e09c avoid NPE in time interval import (made by R. Renkert)
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5775
diff changeset
43 this.startTime = stop;
5775
300c0270c576 Switch start- and stoptime if starttime is after stoptime in timeinterval
Raimund Renkert <rrenkert@intevation.de>
parents: 1204
diff changeset
44 }
300c0270c576 Switch start- and stoptime if starttime is after stoptime in timeinterval
Raimund Renkert <rrenkert@intevation.de>
parents: 1204
diff changeset
45 else {
5780
5e3c9027e09c avoid NPE in time interval import (made by R. Renkert)
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5775
diff changeset
46 this.startTime = start;
5e3c9027e09c avoid NPE in time interval import (made by R. Renkert)
Tom Gottfried <tom.gottfried@intevation.de>
parents: 5775
diff changeset
47 this.stopTime = stop;
5775
300c0270c576 Switch start- and stoptime if starttime is after stoptime in timeinterval
Raimund Renkert <rrenkert@intevation.de>
parents: 1204
diff changeset
48 }
486
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49 }
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
50
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
51 public Date getStartTime() {
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
52 return startTime;
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 }
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
55 public void setStartTime(Date startTime) {
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56 this.startTime = startTime;
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 }
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59 public Date getStopTime() {
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 return stopTime;
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
61 }
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 public void setStopTime(Date stopTime) {
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 this.stopTime = stopTime;
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65 }
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 public TimeInterval getPeer() {
493
b35c5dc0f8b7 Importer: Make import of historical discharge tables work.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 487
diff changeset
68 if (peer == null) {
497
67fd63e4ef66 Importer: centralized caching
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 493
diff changeset
69 Session session = ImporterSession.getInstance().getDatabaseSession();
486
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 Query query = session.createQuery(
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 "from TimeInterval where startTime=:a and stopTime=:b");
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 query.setParameter("a", startTime);
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73 query.setParameter("b", stopTime);
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 List<TimeInterval> intervals = query.list();
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 if (intervals.isEmpty()) {
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 peer = new TimeInterval(startTime, stopTime);
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77 session.save(peer);
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 }
487
cf513cbe88c3 Importer: forgot to fetch time interval peer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 486
diff changeset
79 else {
cf513cbe88c3 Importer: forgot to fetch time interval peer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 486
diff changeset
80 peer = intervals.get(0);
cf513cbe88c3 Importer: forgot to fetch time interval peer
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 486
diff changeset
81 }
486
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
82 }
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
83 return peer;
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
84 }
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
85 }
8ea09ec7f0c8 Importer: Attach a time interval to a discharge table if we find one during import.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
86 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org