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