comparison backend/src/main/java/org/dive4elements/river/importer/ImportBedHeight.java @ 8559:6d8d7425a6b5

Bed heights are just bed heights since a while ('single' is obsolete).
author "Tom Gottfried <tom@intevation.de>"
date Mon, 16 Feb 2015 11:08:33 +0100
parents
children 6fcf4717605f
comparison
equal deleted inserted replaced
8558:d0ea092a32f5 8559:6d8d7425a6b5
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
16 import org.hibernate.Session;
17 import org.hibernate.Query;
18
19 import org.dive4elements.river.model.BedHeight;
20 import org.dive4elements.river.model.BedHeightType;
21 import org.dive4elements.river.model.ElevationModel;
22 import org.dive4elements.river.model.Range;
23 import org.dive4elements.river.model.River;
24
25
26 public class ImportBedHeight
27 {
28 private static Logger log = Logger.getLogger(ImportBedHeight.class);
29
30 protected Integer year;
31 protected int soundingWidth;
32
33 protected String evaluationBy;
34 protected String description;
35
36 protected ImportRange range;
37 protected ImportBedHeightType type;
38 protected ImportLocationSystem locationSystem;
39 protected ImportElevationModel curElevationModel;
40 protected ImportElevationModel oldElevationModel;
41
42 protected List<ImportBedHeightValue> values;
43
44 protected BedHeight peer;
45
46
47 public ImportBedHeight(String description) {
48 this.description = description;
49 this.values = new ArrayList<ImportBedHeightValue>();
50 }
51
52
53 public String getDescription() {
54 return description;
55 }
56
57 public int getValueCount() {
58 return values.size();
59 }
60
61
62 public void setYear(int year) {
63 this.year = year;
64 }
65
66 public void setTimeInterval(ImportTimeInterval timeInterval) {
67 // do nothing
68 }
69
70 public void setSoundingWidth(int soundingWidth) {
71 this.soundingWidth = soundingWidth;
72 }
73
74 public void setEvaluationBy(String evaluationBy) {
75 this.evaluationBy = evaluationBy;
76 }
77
78 public void setDescription(String description) {
79 this.description = description;
80 }
81
82 public void setRange(ImportRange range) {
83 this.range = range;
84 }
85
86 public void setType(ImportBedHeightType type) {
87 this.type = type;
88 }
89
90 public void setLocationSystem(ImportLocationSystem locationSystem) {
91 this.locationSystem = locationSystem;
92 }
93
94 public void setCurElevationModel(ImportElevationModel curElevationModel) {
95 this.curElevationModel = curElevationModel;
96 }
97
98 public void setOldElevationModel(ImportElevationModel oldElevationModel) {
99 this.oldElevationModel = oldElevationModel;
100 }
101
102 public void addValue(ImportBedHeightValue value) {
103 values.add((ImportBedHeightValue) value);
104 }
105
106 public void storeDependencies(River river) {
107 log.info("Store dependencies for single: '" + getDescription() + "'");
108
109 if (type != null) {
110 type.storeDependencies();
111 }
112
113 if (locationSystem != null) {
114 locationSystem.storeDependencies();
115 }
116
117 if (curElevationModel != null) {
118 curElevationModel.storeDependencies();
119 }
120
121 if (oldElevationModel != null) {
122 oldElevationModel.storeDependencies();
123 }
124
125 BedHeight peer = getPeer(river);
126
127 if (peer != null) {
128 for (ImportBedHeightValue value: values) {
129 value.storeDependencies(peer);
130 }
131 }
132
133 Session session = ImporterSession.getInstance().getDatabaseSession();
134 session.flush();
135 }
136
137 public BedHeight getPeer(River river) {
138 if (peer == null) {
139 BedHeightType theType = type != null ? type.getPeer() : null;
140 ElevationModel theCurModel = curElevationModel.getPeer();
141 Range theRange = range != null ? range.getPeer(river) : null;
142
143 if (theType == null) {
144 log.warn("BHS: No bed height type given. Skip file '" +
145 description + "'");
146 return null;
147 }
148
149 if (theCurModel == null) {
150 log.warn("BHS: No elevation model given. Skip file '" +
151 description + "'");
152 return null;
153 }
154
155 if (theRange == null) {
156 log.warn("BHS: No km-range given: '" +
157 description + "'");
158 }
159
160 Session session = ImporterSession.getInstance().getDatabaseSession();
161
162 Query query = session.createQuery(
163 "from BedHeight where " +
164 "river=:river and year=:year and soundingWidth=:soundingWidth " +
165 "and type=:type and locationSystem=:locationSystem and " +
166 "curElevationModel=:curElevationModel and range=:range");
167
168 query.setParameter("river", river);
169 query.setParameter("year", year);
170 query.setParameter("soundingWidth", soundingWidth);
171 query.setParameter("type", theType);
172 query.setParameter("locationSystem", locationSystem.getPeer());
173 query.setParameter("curElevationModel", theCurModel);
174 query.setParameter("range", range.getPeer(river));
175
176 List<BedHeight> bedHeights = query.list();
177 if (bedHeights.isEmpty()) {
178 log.info("Create new BedHeight DB instance.");
179
180 peer = new BedHeight(
181 river,
182 year,
183 soundingWidth,
184 theType,
185 locationSystem.getPeer(),
186 theCurModel,
187 oldElevationModel != null ? oldElevationModel.getPeer() : null,
188 range.getPeer(river),
189 evaluationBy,
190 description
191 );
192
193 session.save(peer);
194 }
195 else {
196 peer = bedHeights.get(0);
197 }
198 }
199
200 return peer;
201 }
202 }
203 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org