Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/ImportBedHeightEpoch.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-backend/src/main/java/org/dive4elements/river/importer/ImportBedHeightEpoch.java@18619c1e7c2a |
children | 4dd33b86dc61 |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.importer; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.List; | |
5 | |
6 import org.apache.log4j.Logger; | |
7 import org.hibernate.Query; | |
8 import org.hibernate.Session; | |
9 | |
10 import org.dive4elements.river.model.BedHeightEpoch; | |
11 import org.dive4elements.river.model.ElevationModel; | |
12 import org.dive4elements.river.model.Range; | |
13 import org.dive4elements.river.model.River; | |
14 import org.dive4elements.river.model.TimeInterval; | |
15 | |
16 | |
17 /** Import Bed Height Data, 'epoch' type from csv file. */ | |
18 public class ImportBedHeightEpoch implements ImportBedHeight { | |
19 | |
20 /** Private logger. */ | |
21 private static Logger log = Logger.getLogger(ImportBedHeightEpoch.class); | |
22 | |
23 protected String evaluationBy; | |
24 | |
25 /** De facto the file name. */ | |
26 protected String description; | |
27 | |
28 protected ImportTimeInterval timeInterval; | |
29 protected ImportRange range; | |
30 protected ImportElevationModel curElevationModel; | |
31 protected ImportElevationModel oldElevationModel; | |
32 | |
33 protected List<ImportBedHeightEpochValue> values; | |
34 | |
35 protected BedHeightEpoch peer; | |
36 | |
37 public ImportBedHeightEpoch(String description) { | |
38 this.description = description; | |
39 this.values = new ArrayList<ImportBedHeightEpochValue>(); | |
40 } | |
41 | |
42 public String getDescription() { | |
43 return description; | |
44 } | |
45 | |
46 public int getValueCount() { | |
47 return values.size(); | |
48 } | |
49 | |
50 public void setTimeInterval(ImportTimeInterval timeInterval) { | |
51 this.timeInterval = timeInterval; | |
52 } | |
53 | |
54 public void setEvaluationBy(String evaluationBy) { | |
55 this.evaluationBy = evaluationBy; | |
56 } | |
57 | |
58 public void setDescription(String description) { | |
59 this.description = description; | |
60 } | |
61 | |
62 public void setRange(ImportRange range) { | |
63 this.range = range; | |
64 } | |
65 | |
66 public void setCurElevationModel(ImportElevationModel curElevationModel) { | |
67 this.curElevationModel = curElevationModel; | |
68 } | |
69 | |
70 public void setOldElevationModel(ImportElevationModel oldElevationModel) { | |
71 this.oldElevationModel = oldElevationModel; | |
72 } | |
73 | |
74 /** Does nothing. */ | |
75 public void setYear(int year) { | |
76 // do nothing | |
77 } | |
78 | |
79 /** Does nothing. */ | |
80 public void setSoundingWidth(int soundingWidth) { | |
81 // do nothing | |
82 } | |
83 | |
84 | |
85 /** Does nothing. */ | |
86 public void setLocationSystem(ImportLocationSystem locationSystem) { | |
87 // do nothing | |
88 } | |
89 | |
90 | |
91 /** Does nothing. */ | |
92 public void setType(ImportBedHeightType type) { | |
93 // do nothing | |
94 } | |
95 | |
96 @Override | |
97 public void addValue(ImportBedHeightValue value) { | |
98 values.add((ImportBedHeightEpochValue) value); | |
99 } | |
100 | |
101 @Override | |
102 public void storeDependencies(River river) { | |
103 log.info("Store dependencies for epoch: '" + getDescription() + "'"); | |
104 | |
105 BedHeightEpoch peer = getPeer(river); | |
106 | |
107 if (curElevationModel != null) { | |
108 curElevationModel.storeDependencies(); | |
109 } | |
110 | |
111 if (oldElevationModel != null) { | |
112 oldElevationModel.storeDependencies(); | |
113 } | |
114 | |
115 if (peer != null) { | |
116 log.debug("store values now..."); | |
117 | |
118 for (ImportBedHeightEpochValue value : values) { | |
119 value.storeDependencies(peer); | |
120 } | |
121 } | |
122 | |
123 Session session = ImporterSession.getInstance().getDatabaseSession(); | |
124 session.flush(); | |
125 } | |
126 | |
127 /** | |
128 * Asserts all dependent entities (ElevationModel, TimeInterval, Range, | |
129 * BedHeighEpoch) are in db and returns bound (either newly created or | |
130 * freshly fetched) BedHeightEpoch. | |
131 */ | |
132 @Override | |
133 public BedHeightEpoch getPeer(River river) { | |
134 if (peer == null) { | |
135 ElevationModel theCurModel = null; | |
136 if (curElevationModel != null) { | |
137 curElevationModel.storeDependencies(); | |
138 theCurModel = curElevationModel.getPeer(); | |
139 } | |
140 | |
141 if (theCurModel == null) { | |
142 log.warn("BHE: Skip file - invalid current elevation model."); | |
143 return null; | |
144 } | |
145 | |
146 TimeInterval theTime = null; | |
147 if (timeInterval != null) { | |
148 theTime = timeInterval.getPeer(); | |
149 } | |
150 | |
151 if (theTime == null) { | |
152 log.warn("BHE: Skip file - invalid time range."); | |
153 return null; | |
154 } | |
155 | |
156 Range theRange = range != null ? range.getPeer(river) : null; | |
157 | |
158 if (theRange == null) { | |
159 log.warn("BHE: invalid km range."); | |
160 } | |
161 | |
162 Session session = ImporterSession.getInstance() | |
163 .getDatabaseSession(); | |
164 | |
165 Query query = session.createQuery("from BedHeightEpoch where " | |
166 + " river=:river and " + " timeInterval=:timeInterval and " | |
167 + " curElevationModel=:curElevationModel and " | |
168 + " range=:range and " + " evaluationBy=:evaluationBy and " | |
169 + " description=:description"); | |
170 | |
171 query.setParameter("river", river); | |
172 query.setParameter("timeInterval", theTime); | |
173 query.setParameter("curElevationModel", theCurModel); | |
174 query.setParameter("range", theRange); | |
175 query.setParameter("evaluationBy", evaluationBy); | |
176 query.setParameter("description", description); | |
177 | |
178 List<BedHeightEpoch> bedHeights = query.list(); | |
179 | |
180 if (bedHeights.isEmpty()) { | |
181 log.info("Create new BedHeightEpoch DB instance."); | |
182 | |
183 peer = new BedHeightEpoch(river, theTime, theRange, | |
184 theCurModel, | |
185 oldElevationModel != null ? oldElevationModel.getPeer() | |
186 : null, evaluationBy, description); | |
187 | |
188 session.save(peer); | |
189 } | |
190 else { | |
191 peer = bedHeights.get(0); | |
192 } | |
193 } | |
194 | |
195 return peer; | |
196 } | |
197 } | |
198 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |