comparison backend/src/main/java/org/dive4elements/river/model/Attribute.java @ 9176:1614cb14308f

Work on calculations for S-Info flood duration workflow
author mschaefer
date Mon, 25 Jun 2018 19:21:11 +0200
parents 4c3ccf2b0304
children
comparison
equal deleted inserted replaced
9175:34dc0163ad2d 9176:1614cb14308f
7 */ 7 */
8 8
9 package org.dive4elements.river.model; 9 package org.dive4elements.river.model;
10 10
11 import java.io.Serializable; 11 import java.io.Serializable;
12 import java.util.List;
12 13
14 import javax.persistence.Column;
13 import javax.persistence.Entity; 15 import javax.persistence.Entity;
16 import javax.persistence.GeneratedValue;
17 import javax.persistence.GenerationType;
14 import javax.persistence.Id; 18 import javax.persistence.Id;
19 import javax.persistence.SequenceGenerator;
15 import javax.persistence.Table; 20 import javax.persistence.Table;
16 import javax.persistence.GeneratedValue; 21 import javax.persistence.Transient;
17 import javax.persistence.Column; 22
18 import javax.persistence.SequenceGenerator; 23 import org.dive4elements.river.backend.SessionHolder;
19 import javax.persistence.GenerationType; 24 import org.hibernate.Query;
25 import org.hibernate.Session;
20 26
21 @Entity 27 @Entity
22 @Table(name = "attributes") 28 @Table(name = "attributes")
23 public class Attribute 29 public class Attribute
24 implements Serializable 30 implements Serializable
25 { 31 {
32
33 /***** TYPES *****/
34
35 /**
36 * River attribute (river side or range type)
37 *
38 */
39 public enum AttributeKey {
40 NONE(""), STRECKE(">>>>>>>>>>>>>>>"), LEFT("links"), RIGHT("rechts"), UNKNOWN("?");
41
42 private final String name;
43 private int id;
44 private boolean ready;
45
46 AttributeKey(final String name) {
47 this.name = name;
48 this.id = 0;
49 this.ready = false;
50 }
51
52 /**
53 * Type name in the database
54 */
55 public String getName() {
56 return this.name;
57 }
58
59 /**
60 * Type id in the database
61 */
62 public int getId() {
63 initFromDatabase();
64 return this.id;
65 }
66
67 /**
68 * Set the type id
69 */
70 public void setId(final int id) {
71 this.id = id;
72 }
73
74 protected boolean getReady() {
75 return this.ready;
76 }
77
78 protected void setReady(final boolean ready) {
79 this.ready = ready;
80 }
81
82 /**
83 * Main value type key for a database name value
84 */
85 public static AttributeKey forDbName(final String dbname) {
86 initFromDatabase();
87 for (final AttributeKey k : AttributeKey.values()) {
88 if (k.getName().equalsIgnoreCase(dbname))
89 return k;
90 }
91 return UNKNOWN;
92 }
93
94 /**
95 * Main value type key for a database id value
96 */
97 public static AttributeKey forDbId(final int dbid) {
98 initFromDatabase();
99 for (final AttributeKey k : AttributeKey.values()) {
100 if (k.getId() == dbid)
101 return k;
102 }
103 return UNKNOWN;
104 }
105
106 /**
107 * Initially queries the database ids
108 */
109 private static void initFromDatabase() {
110 if (STRECKE.getReady())
111 return;
112 // Avoid recursion
113 for (final AttributeKey k : AttributeKey.values())
114 k.setReady(true);
115 // Select database ids
116 final Session session = SessionHolder.HOLDER.get();
117 final Query query = session.createQuery("FROM Attribute");
118 final List<Attribute> rows = query.list();
119 if (!rows.isEmpty()) {
120 for (int i = 0; i <= rows.size() - 1; i++) {
121 if (forDbName(rows.get(i).getValue()) != UNKNOWN)
122 forDbName(rows.get(i).getValue()).setId(rows.get(i).getId());
123 }
124 }
125 }
126 }
127
128 /***** FIELDS *****/
129
26 private Integer id; 130 private Integer id;
27 131
28 private String value; 132 private String value;
29 133
30 public Attribute() { 134 public Attribute() {
31 } 135 }
32 136
33 public Attribute(String value) { 137 /***** CONSTRUCTORS *****/
138
139 public Attribute(final String value) {
34 this.value = value; 140 this.value = value;
35 } 141 }
36 142
143 /***** METHODS *****/
144
37 @Id 145 @Id
38 @SequenceGenerator( 146 @SequenceGenerator(
39 name = "SEQUENCE_ATTRIBUTES_ID_SEQ", 147 name = "SEQUENCE_ATTRIBUTES_ID_SEQ",
40 sequenceName = "ATTRIBUTES_ID_SEQ", 148 sequenceName = "ATTRIBUTES_ID_SEQ",
41 allocationSize = 1) 149 allocationSize = 1)
42 @GeneratedValue( 150 @GeneratedValue(
43 strategy = GenerationType.SEQUENCE, 151 strategy = GenerationType.SEQUENCE,
44 generator = "SEQUENCE_ATTRIBUTES_ID_SEQ") 152 generator = "SEQUENCE_ATTRIBUTES_ID_SEQ")
45 @Column(name = "id") 153 @Column(name = "id")
46 public Integer getId() { 154 public Integer getId() {
47 return id; 155 return this.id;
48 } 156 }
49 157
50 public void setId(Integer id) { 158 public void setId(final Integer id) {
51 this.id = id; 159 this.id = id;
52 } 160 }
53 161
54 @Column(name = "value") 162 @Column(name = "value")
55 public String getValue() { 163 public String getValue() {
56 return value; 164 return this.value;
57 } 165 }
58 166
59 public void setValue(String value) { 167 public void setValue(final String value) {
60 this.value = value; 168 this.value = value;
169 }
170
171 @Transient
172 public AttributeKey getKey() {
173 return AttributeKey.forDbId(this.getId());
61 } 174 }
62 } 175 }
63 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 176 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org