1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package ch.hortis.sonar.model;
21
22 import org.hibernate.annotations.Cache;
23 import org.hibernate.annotations.CacheConcurrencyStrategy;
24 import org.hibernate.annotations.Immutable;
25
26 import javax.persistence.*;
27
28 @Immutable
29 @Table(name = "metrics")
30 @Entity(name = "Metric")
31 @NamedQueries({@NamedQuery(name = Metric.SQL_SELECT_BY_NAME, query = "SELECT m FROM Metric m WHERE m.name = :name"),
32 @NamedQuery(name = Metric.SQL_SELECT_ALL, query = "SELECT m FROM Metric m")
33 })
34 public class Metric {
35
36 public final static String SQL_SELECT_BY_NAME = "Metrics.selectByName";
37 public static final String SQL_SELECT_ALL = "Metrics.selectAll";
38
39 @Id
40 @Column(name = "id")
41 @SequenceGenerator(name = "METRICS_SEQ", sequenceName = "METRICS_SEQ")
42 @GeneratedValue(strategy = GenerationType.AUTO, generator = "METRICS_SEQ")
43 private Integer id;
44
45 @Column(name = "name", updatable = false, nullable = false, length = 64)
46 private String name;
47
48 @Column(name = "description", updatable = false, nullable = true, length = 255)
49 private String description;
50
51 @Column(name = "value_type", updatable = false, nullable = false)
52 @Enumerated(EnumType.ORDINAL)
53 private MetricType type;
54
55 @Column(name = "direction", updatable = false, nullable = true)
56 private Integer direction;
57
58 @Column(name = "domain", updatable = false, nullable = true, length = 60)
59 private String domain;
60
61 @Column(name = "short_name", updatable = false, nullable = true, length = 64)
62 private String shortName;
63
64 @Column(name = "long_name", updatable = false, nullable = true, length = 64)
65 private String longName;
66
67 @Column(name = "comparable", nullable = false)
68 private Boolean comparable;
69
70 public Metric() {
71 }
72
73 public Metric(String name) {
74 this(name, MetricType.DOUBLE);
75 }
76
77 public Metric(String name, MetricType type) {
78 this(name, "", type, -1, null, name, name, Boolean.FALSE, Boolean.FALSE);
79 }
80
81 public Metric(String name, String description, MetricType type, Integer direction) {
82 this.name = name;
83 this.description = description;
84 this.type = type;
85 this.direction = direction;
86 this.shortName = name;
87 this.longName = name;
88 this.comparable = Boolean.FALSE;
89 }
90
91 public Metric(String name, String description, MetricType type, Integer direction, String domain, String shortName, String longName, Boolean qualitative, Boolean comparable) {
92 this.name = name;
93 this.description = description;
94 this.type = type;
95 this.direction = direction;
96 this.domain = domain;
97 this.shortName = shortName;
98 this.longName = longName;
99 this.comparable = comparable;
100 }
101
102 public Integer getId() {
103 return id;
104 }
105
106 public void setId(Integer id) {
107 this.id = id;
108 }
109
110 public String getName() {
111 return name;
112 }
113
114 public void setName(String name) {
115 this.name = name;
116 }
117
118 public MetricType getType() {
119 return type;
120 }
121
122 public void setType(MetricType type) {
123 this.type = type;
124 }
125
126 public static Number getParsedValue(MetricType type, Double value) {
127 if (value == null) {
128 return null;
129 }
130 switch (type) {
131 case DOUBLE:
132 return value;
133 case FLOAT:
134 case PERCENTAGE:
135 return value.floatValue();
136 case INT:
137 return value.intValue();
138 case LONG:
139 return value.longValue();
140 case SHORT:
141 return value.shortValue();
142 case BYTE:
143 return value.byteValue();
144 case DURATION_MS:
145 return value.longValue();
146 }
147 return null;
148 }
149
150 public String getDescription() {
151 return description;
152 }
153
154 public void setDescription(String description) {
155 this.description = description;
156 }
157
158 public Integer getDirection() {
159 return direction;
160 }
161
162 public void setDirection(Integer direction) {
163 this.direction = direction;
164 }
165
166 public String getDomain() {
167 return domain;
168 }
169
170 public void setDomain(String domain) {
171 this.domain = domain;
172 }
173
174 public String getShortName() {
175 return shortName;
176 }
177
178 public void setShortName(String shortName) {
179 this.shortName = shortName;
180 }
181
182 public String getLongName() {
183 return longName;
184 }
185
186 public void setLongName(String longName) {
187 this.longName = longName;
188 }
189
190 public Boolean isComparable() {
191 return comparable;
192 }
193
194 public void setComparable(Boolean comparable) {
195 this.comparable = comparable;
196 }
197
198 public int hashCode() {
199 return name.hashCode();
200 }
201
202 public boolean equals(Object obj) {
203 if (!(obj instanceof Metric)) {
204 return false;
205 }
206 if (this == obj) {
207 return true;
208 }
209 Metric other = (Metric) obj;
210 return name.equals(other.getName());
211 }
212
213 public String toString() {
214 return name;
215 }
216 }