comparison backend/src/main/java/org/dive4elements/river/model/DischargeTable.java @ 9159:64e56a51db3f

Added methods to select a gauge's discharge table and main value list; added a main value type key enum
author mschaefer
date Tue, 19 Jun 2018 14:23:15 +0200
parents ff27548d078c
children 491e1a434457
comparison
equal deleted inserted replaced
9158:4a41b7fba4da 9159:64e56a51db3f
21 import javax.persistence.OneToMany; 21 import javax.persistence.OneToMany;
22 import javax.persistence.OneToOne; 22 import javax.persistence.OneToOne;
23 import javax.persistence.OrderBy; 23 import javax.persistence.OrderBy;
24 import javax.persistence.SequenceGenerator; 24 import javax.persistence.SequenceGenerator;
25 import javax.persistence.Table; 25 import javax.persistence.Table;
26 26 import javax.persistence.Transient;
27
28 import org.dive4elements.river.backend.SessionHolder;
29 import org.hibernate.Query;
27 import org.hibernate.Session; 30 import org.hibernate.Session;
28 import org.hibernate.Query;
29 import org.dive4elements.river.backend.SessionHolder;
30 31
31 @Entity 32 @Entity
32 @Table(name = "discharge_tables") 33 @Table(name = "discharge_tables")
33 public class DischargeTable 34 public class DischargeTable
34 implements Serializable, Comparable<DischargeTable> 35 implements Serializable, Comparable<DischargeTable>
41 private TimeInterval timeInterval; 42 private TimeInterval timeInterval;
42 43
43 private List<DischargeTableValue> dischargeTableValues; 44 private List<DischargeTableValue> dischargeTableValues;
44 45
45 public DischargeTable() { 46 public DischargeTable() {
46 kind = 0; 47 this.kind = 0;
47 } 48 }
48 49
49 public DischargeTable(Gauge gauge) { 50 public DischargeTable(final Gauge gauge) {
50 this(gauge, null, null, 0, null); 51 this(gauge, null, null, 0, null);
51 } 52 }
52 53
53 public DischargeTable( 54 public DischargeTable(
54 Gauge gauge, 55 final Gauge gauge,
55 String description, 56 final String description,
56 String bfgId, 57 final String bfgId,
57 Integer kind, 58 final Integer kind,
58 TimeInterval timeInterval 59 final TimeInterval timeInterval
59 ) { 60 ) {
60 this.gauge = gauge; 61 this.gauge = gauge;
61 this.description = description; 62 this.description = description;
62 this.bfgId = bfgId; 63 this.bfgId = bfgId;
63 this.kind = kind; 64 this.kind = kind;
64 this.timeInterval = timeInterval; 65 this.timeInterval = timeInterval;
65 } 66 }
66 67
67 @Id 68 @Id
68 @SequenceGenerator( 69 @SequenceGenerator(
69 name = "SEQUENCE_DISCHARGE_TABLES_ID_SEQ", 70 name = "SEQUENCE_DISCHARGE_TABLES_ID_SEQ",
70 sequenceName = "DISCHARGE_TABLES_ID_SEQ", 71 sequenceName = "DISCHARGE_TABLES_ID_SEQ",
71 allocationSize = 1) 72 allocationSize = 1)
72 @GeneratedValue( 73 @GeneratedValue(
73 strategy = GenerationType.SEQUENCE, 74 strategy = GenerationType.SEQUENCE,
74 generator = "SEQUENCE_DISCHARGE_TABLES_ID_SEQ") 75 generator = "SEQUENCE_DISCHARGE_TABLES_ID_SEQ")
75 @Column(name = "id") 76 @Column(name = "id")
76 public Integer getId() { 77 public Integer getId() {
77 return id; 78 return this.id;
78 } 79 }
79 80
80 public void setId(Integer id) { 81 public void setId(final Integer id) {
81 this.id = id; 82 this.id = id;
82 } 83 }
83 84
84 @OneToOne 85 @OneToOne
85 @JoinColumn(name = "gauge_id") 86 @JoinColumn(name = "gauge_id")
86 public Gauge getGauge() { 87 public Gauge getGauge() {
87 return gauge; 88 return this.gauge;
88 } 89 }
89 90
90 public void setGauge(Gauge gauge) { 91 public void setGauge(final Gauge gauge) {
91 this.gauge = gauge; 92 this.gauge = gauge;
92 } 93 }
93 94
94 @Column(name = "description") 95 @Column(name = "description")
95 public String getDescription() { 96 public String getDescription() {
96 return description; 97 return this.description;
97 } 98 }
98 99
99 public void setDescription(String description) { 100 public void setDescription(final String description) {
100 this.description = description; 101 this.description = description;
101 } 102 }
102 103
103 @Column(name = "bfg_id") 104 @Column(name = "bfg_id")
104 public String getBfgId() { 105 public String getBfgId() {
105 return bfgId; 106 return this.bfgId;
106 } 107 }
107 108
108 public void setBfgId(String bfgId) { 109 public void setBfgId(final String bfgId) {
109 this.bfgId = bfgId; 110 this.bfgId = bfgId;
110 } 111 }
111 112
112 @Column(name = "kind") 113 @Column(name = "kind")
113 public Integer getKind() { 114 public Integer getKind() {
114 return kind; 115 return this.kind;
115 } 116 }
116 117
117 public void setKind(Integer kind) { 118 public void setKind(final Integer kind) {
118 this.kind = kind; 119 this.kind = kind;
119 } 120 }
120 121
121 @OneToOne 122 @OneToOne
122 @JoinColumn(name = "time_interval_id") 123 @JoinColumn(name = "time_interval_id")
123 public TimeInterval getTimeInterval() { 124 public TimeInterval getTimeInterval() {
124 return timeInterval; 125 return this.timeInterval;
125 } 126 }
126 127
127 public void setTimeInterval(TimeInterval timeInterval) { 128 public void setTimeInterval(final TimeInterval timeInterval) {
128 this.timeInterval = timeInterval; 129 this.timeInterval = timeInterval;
129 } 130 }
130 131
131 @OneToMany 132 @OneToMany
132 @JoinColumn(name = "table_id") 133 @JoinColumn(name = "table_id")
133 @OrderBy("q") 134 @OrderBy("q")
134 public List<DischargeTableValue> getDischargeTableValues() { 135 public List<DischargeTableValue> getDischargeTableValues() {
135 return dischargeTableValues; 136 return this.dischargeTableValues;
136 } 137 }
137 138
138 public void setDischargeTableValues( 139 public void setDischargeTableValues(
139 List<DischargeTableValue> dischargeTableValues 140 final List<DischargeTableValue> dischargeTableValues
140 ) { 141 ) {
141 this.dischargeTableValues = dischargeTableValues; 142 this.dischargeTableValues = dischargeTableValues;
142 } 143 }
143 144
145 @Transient
146 public double[] getWs() {
147 final double[] ws = new double[this.dischargeTableValues.size()];
148 for (int i=0; i<=this.dischargeTableValues.size()-1; i++)
149 ws[i] = this.dischargeTableValues.get(i).getW().doubleValue();
150
151 return ws;
152 }
153
144 @Override 154 @Override
145 public int compareTo(DischargeTable o) { 155 public int compareTo(final DischargeTable o) {
146 if (getKind() == 0 && o.getKind() != 0) { 156 if (getKind() == 0 && o.getKind() != 0) {
147 return 1; 157 return 1;
148 } 158 }
149 159
150 TimeInterval other = o.getTimeInterval(); 160 final TimeInterval other = o.getTimeInterval();
151 if (other == null && timeInterval == null) { 161 if (other == null && this.timeInterval == null) {
152 return 1; 162 return 1;
153 } 163 }
154 else if (other == null) { 164 else if (other == null) {
155 return -1; 165 return -1;
156 } 166 }
157 else if (timeInterval == null) { 167 else if (this.timeInterval == null) {
158 return 1; 168 return 1;
159 } 169 }
160 170
161 Date otherStartTime = other.getStartTime(); 171 final Date otherStartTime = other.getStartTime();
162 Date thisStartTime = timeInterval.getStartTime(); 172 final Date thisStartTime = this.timeInterval.getStartTime();
163 173
164 if (otherStartTime == null) { 174 if (otherStartTime == null) {
165 return -1; 175 return -1;
166 } 176 }
167 else if (thisStartTime == null) { 177 else if (thisStartTime == null) {
168 return 1; 178 return 1;
169 } 179 }
170 180
171 long otherStart = otherStartTime.getTime(); 181 final long otherStart = otherStartTime.getTime();
172 long thisStart = thisStartTime.getTime(); 182 final long thisStart = thisStartTime.getTime();
173 183
174 if (otherStart < thisStart) { 184 if (otherStart < thisStart) {
175 return 1; 185 return 1;
176 } 186 }
177 else if (otherStart > thisStart) { 187 else if (otherStart > thisStart) {
178 return -1; 188 return -1;
179 } 189 }
180 190
181 Date otherStopTime = other.getStopTime(); 191 final Date otherStopTime = other.getStopTime();
182 Date thisStopTime = timeInterval.getStopTime(); 192 final Date thisStopTime = this.timeInterval.getStopTime();
183 193
184 if (otherStopTime == null) { 194 if (otherStopTime == null) {
185 return -1; 195 return -1;
186 } 196 }
187 else if (thisStopTime == null) { 197 else if (thisStopTime == null) {
188 return 1; 198 return 1;
189 } 199 }
190 200
191 long otherEnd = otherStopTime.getTime(); 201 final long otherEnd = otherStopTime.getTime();
192 long thisEnd = thisStopTime.getTime(); 202 final long thisEnd = thisStopTime.getTime();
193 203
194 if (otherEnd < thisEnd) { 204 if (otherEnd < thisEnd) {
195 return 1; 205 return 1;
196 } 206 }
197 else if (otherEnd > thisEnd) { 207 else if (otherEnd > thisEnd) {
200 else { 210 else {
201 return 0; 211 return 0;
202 } 212 }
203 } 213 }
204 214
205 public static DischargeTable getDischargeTableById(int dtId) 215 public static DischargeTable getDischargeTableById(final int dtId)
206 { 216 {
207 Session session = SessionHolder.HOLDER.get(); 217 final Session session = SessionHolder.HOLDER.get();
208 Query query = session.createQuery( 218 final Query query = session.createQuery("from DischargeTable where id =:dtId");
209 "from DischargeTable where id =:dtId");
210 query.setParameter("dtId", dtId); 219 query.setParameter("dtId", dtId);
211 220
212 List<DischargeTable> list = query.list(); 221 final List<DischargeTable> list = query.list();
213 return list.isEmpty() ? null : list.get(0); 222 return list.isEmpty() ? null : list.get(0);
223 }
224
225 /**
226 * Selects from the database the main discharge table of a gauge
227 */
228 public static DischargeTable getGaugeMainDischargeTable(final Gauge gauge) {
229 final Session session = SessionHolder.HOLDER.get();
230 final Query query = session.createQuery("FROM DischargeTable WHERE gauge_id=:gaugeid AND kind=0");
231 query.setParameter("gaugeid", gauge.getId());
232 final List<DischargeTable> list = query.list();
233 return list.isEmpty() ? null : list.get(0);
234 }
235
236 /**
237 * Selects from the database the values of a discharge table sorted by W
238 */
239 public static List<DischargeTableValue> getValuesSortedByW(final DischargeTable dischargeTable) {
240 final Session session = SessionHolder.HOLDER.get();
241 final Query query = session.createQuery("FROM DischargeTableValue WHERE table_id=:parentid ORDER BY w");
242 query.setParameter("parentid", dischargeTable.getId());
243 return query.list();
214 } 244 }
215 } 245 }
216 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 246 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org