Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/ImportWst.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 | 5358a5497b2b |
children | 5e38e2924c07 |
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 org.dive4elements.river.model.Wst; | |
12 import org.dive4elements.river.model.River; | |
13 | |
14 import org.apache.log4j.Logger; | |
15 | |
16 import org.hibernate.Session; | |
17 import org.hibernate.Query; | |
18 | |
19 import java.util.ArrayList; | |
20 import java.util.List; | |
21 | |
22 /** Not (yet) db-mapped WST object. */ | |
23 public class ImportWst | |
24 { | |
25 private static Logger log = Logger.getLogger(ImportWst.class); | |
26 | |
27 public interface ImportWstColumnFactory { | |
28 ImportWstColumn create(ImportWst iw, int position); | |
29 } | |
30 | |
31 public static final ImportWstColumnFactory COLUMN_FACTORY = | |
32 new ImportWstColumnFactory() { | |
33 @Override | |
34 public ImportWstColumn create(ImportWst importWst, int position) { | |
35 return new ImportWstColumn(importWst, null, null, position); | |
36 } | |
37 }; | |
38 | |
39 protected String description; | |
40 | |
41 protected Integer kind; | |
42 | |
43 protected List<ImportWstColumn> columns; | |
44 | |
45 protected ImportUnit unit; | |
46 | |
47 protected ImportWstColumnFactory columnFactory; | |
48 | |
49 protected boolean kmUp; | |
50 | |
51 /** Wst as in db. */ | |
52 protected Wst peer; | |
53 | |
54 public ImportWst() { | |
55 this(COLUMN_FACTORY); | |
56 } | |
57 | |
58 public ImportWst(ImportWstColumnFactory columnFactory) { | |
59 this.columnFactory = columnFactory; | |
60 kind = 0; | |
61 columns = new ArrayList<ImportWstColumn>(); | |
62 } | |
63 | |
64 public ImportWst(String description) { | |
65 this(description, COLUMN_FACTORY); | |
66 } | |
67 | |
68 public ImportWst(String description, ImportWstColumnFactory columnFactory) { | |
69 this(columnFactory); | |
70 this.description = description; | |
71 } | |
72 | |
73 public String getDescription() { | |
74 return description; | |
75 } | |
76 | |
77 public Integer getKind() { | |
78 return kind; | |
79 } | |
80 | |
81 public void setKind(Integer kind) { | |
82 this.kind = kind; | |
83 } | |
84 | |
85 public boolean getKmUp() { | |
86 return kmUp; | |
87 } | |
88 | |
89 public void setKmUp(boolean kmUp) { | |
90 this.kmUp = kmUp; | |
91 } | |
92 | |
93 public void setDescription(String description) { | |
94 this.description = description; | |
95 } | |
96 | |
97 /** Create columns that can be accessed with getColumn. */ | |
98 public void setNumberColumns(int numColumns) { | |
99 for (int i = 0; i < numColumns; ++i) { | |
100 columns.add(columnFactory.create(this, i)); | |
101 } | |
102 } | |
103 | |
104 public int getNumberColumns() { | |
105 return columns.size(); | |
106 } | |
107 | |
108 public ImportWstColumn getColumn(int index) { | |
109 return columns.get(index); | |
110 } | |
111 | |
112 public List<ImportWstColumn> getColumns() { | |
113 return columns; | |
114 } | |
115 | |
116 /** Adds a column. Assumes that columns wst is this instance. */ | |
117 public void addColumn(ImportWstColumn column) { | |
118 columns.add(column); | |
119 } | |
120 | |
121 public ImportUnit getUnit() { | |
122 return unit; | |
123 } | |
124 | |
125 public void setUnit(ImportUnit unit) { | |
126 this.unit = unit; | |
127 } | |
128 | |
129 public void storeDependencies(River river) { | |
130 | |
131 log.info("store '" + description + "'"); | |
132 getPeer(river); | |
133 | |
134 for (ImportWstColumn column: columns) { | |
135 column.storeDependencies(river); | |
136 } | |
137 | |
138 Session session = ImporterSession.getInstance().getDatabaseSession(); | |
139 session.flush(); | |
140 } | |
141 | |
142 public boolean guessWaterLevelIncreasing() { | |
143 int up = 0; | |
144 for (ImportWstColumn column: columns) { | |
145 if (column.guessWaterLevelIncreasing()) ++up; | |
146 } | |
147 return up > columns.size() - up; | |
148 } | |
149 | |
150 /** Get corresponding mapped wst (from database). */ | |
151 public Wst getPeer(River river) { | |
152 if (peer == null) { | |
153 Session session = ImporterSession.getInstance().getDatabaseSession(); | |
154 Query query = session.createQuery( | |
155 "from Wst where " + | |
156 "river=:river and description=:description and kind=:kind"); | |
157 query.setParameter("river", river); | |
158 query.setParameter("description", description); | |
159 query.setParameter("kind", kind); | |
160 List<Wst> wsts = query.list(); | |
161 if (wsts.isEmpty()) { | |
162 peer = new Wst(river, description, kind); | |
163 session.save(peer); | |
164 } | |
165 else { | |
166 peer = wsts.get(0); | |
167 } | |
168 | |
169 } | |
170 return peer; | |
171 } | |
172 } | |
173 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |