001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar is distributed in the hope that it will be useful,
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.api.measures;
021    
022    import org.sonar.api.BatchExtension;
023    import org.sonar.api.ServerExtension;
024    import org.sonar.api.database.BaseIdentifiable;
025    
026    import javax.persistence.*;
027    
028    /**
029     * @since 1.10
030     */
031    @Table(name = "metrics")
032    @Entity(name = "Metric")
033    public class Metric extends BaseIdentifiable implements ServerExtension, BatchExtension {
034    
035      /**
036       * A metric bigger value means a degradation
037       */
038      public final static int DIRECTION_WORST = -1;
039      /**
040       * A metric bigger value means an improvement
041       */
042      public final static int DIRECTION_BETTER = 1;
043      /**
044       * The metric direction has no meaning
045       */
046      public final static int DIRECTION_NONE = 0;
047    
048      public enum ValueType {
049        INT, FLOAT, PERCENT, BOOL, STRING, MILLISEC, DATA, LEVEL, DISTRIB
050      }
051    
052      public enum Level {
053        OK("Green"), WARN("Orange"), ERROR("Red");
054    
055        private String colorName;
056    
057        Level(String colorName) {
058          this.colorName = colorName;
059        }
060    
061        public String getColorName() {
062          return colorName;
063        }
064      }
065    
066      public enum Origin {
067        JAV, GUI, WS
068      }
069    
070      @Transient
071      private Formula formula;
072    
073      @Column(name = "name", updatable = false, nullable = false, length = 64)
074      private String key;
075    
076      @Column(name = "description", updatable = true, nullable = true, length = 255)
077      private String description;
078    
079      @Column(name = "val_type", updatable = true, nullable = true)
080      @Enumerated(EnumType.STRING)
081      private ValueType type;
082    
083      @Column(name = "direction", updatable = true, nullable = true)
084      private Integer direction;
085    
086      @Column(name = "domain", updatable = true, nullable = true, length = 60)
087      private String domain;
088    
089      @Column(name = "short_name", updatable = true, nullable = true, length = 64)
090      private String name;
091    
092      @Column(name = "qualitative", updatable = true, nullable = true)
093      private Boolean qualitative = Boolean.FALSE;
094    
095      @Column(name = "user_managed", updatable = true, nullable = true)
096      private Boolean userManaged = Boolean.FALSE;
097    
098      @Column(name = "enabled", updatable = true, nullable = true)
099      private Boolean enabled = Boolean.TRUE;
100    
101      @Column(name = "origin", updatable = true, nullable = true, length = 3)
102      @Enumerated(EnumType.STRING)
103      private Origin origin;
104    
105      public Metric() {
106      }
107    
108      public Metric(String key) {
109        this(key, ValueType.INT);
110      }
111    
112      public Metric(String key, ValueType type) {
113        this(key, key, key, type, -1, Boolean.FALSE, null, false);
114      }
115    
116      public Metric(String key, String name, String description, ValueType type, Integer direction, Boolean qualitative, String domain) {
117        this(key, name, description, type, direction, qualitative, domain, false);
118      }
119    
120      @Deprecated
121      public Metric(String key, String name, String description, ValueType type, Integer direction, Boolean qualitative, String domain, boolean userManaged) {
122        this.key = key;
123        this.description = description;
124        this.type = type;
125        this.direction = direction;
126        this.domain = domain;
127        this.name = name;
128        this.qualitative = qualitative;
129        this.userManaged = userManaged;
130        this.origin = Origin.JAV;
131      }
132    
133      public Metric(String key, String name, ValueType type, Integer direction, Boolean qualitative, String domain, Formula formula) {
134        this.key = key;
135        this.name = name;
136        this.type = type;
137        this.direction = direction;
138        this.domain = domain;
139        this.qualitative = qualitative;
140        this.origin = Origin.JAV;
141        this.enabled = true;
142        this.userManaged = false;
143        this.formula = formula;
144      }
145    
146      public Formula getFormula() {
147        return formula;
148      }
149    
150      public Metric setFormula(Formula formula) {
151        this.formula = formula;
152        return this;
153      }
154    
155      public Boolean getQualitative() {
156        return qualitative;
157      }
158    
159      public void setQualitative(Boolean qualitative) {
160        this.qualitative = qualitative;
161      }
162    
163      public String getKey() {
164        return key;
165      }
166    
167      public void setKey(String key) {
168        this.key = key;
169      }
170    
171      public ValueType getType() {
172        return type;
173      }
174    
175      public void setType(ValueType type) {
176        this.type = type;
177      }
178    
179      public String getDescription() {
180        return description;
181      }
182    
183      public void setDescription(String description) {
184        this.description = description;
185      }
186    
187      public Boolean getUserManaged() {
188        return userManaged;
189      }
190    
191      public void setUserManaged(Boolean userManaged) {
192        this.userManaged = userManaged;
193      }
194    
195      public Boolean getEnabled() {
196        return enabled;
197      }
198    
199      public void setEnabled(Boolean enabled) {
200        this.enabled = enabled;
201      }
202    
203      public Integer getDirection() {
204        return direction;
205      }
206    
207      public void setDirection(Integer direction) {
208        this.direction = direction;
209      }
210    
211      public String getDomain() {
212        return domain;
213      }
214    
215      public void setDomain(String domain) {
216        this.domain = domain;
217      }
218    
219      public String getName() {
220        return name;
221      }
222    
223      public void setName(String name) {
224        this.name = name;
225      }
226    
227      public Origin getOrigin() {
228        return origin;
229      }
230    
231      public void setOrigin(Origin origin) {
232        this.origin = origin;
233      }
234    
235      public boolean isNumericType() {
236        return ValueType.INT.equals(type)
237            || ValueType.FLOAT.equals(type)
238            || ValueType.PERCENT.equals(type)
239            || ValueType.BOOL.equals(type)
240            || ValueType.MILLISEC.equals(type);
241      }
242    
243      public boolean isDataType() {
244        return ValueType.DATA.equals(type) || ValueType.DISTRIB.equals(type);
245      }
246    
247      public boolean isPercentageType() {
248        return ValueType.PERCENT.equals(type);
249      }
250    
251    
252      @Override
253      public int hashCode() {
254        return key.hashCode();
255      }
256    
257      @Override
258      public boolean equals(Object obj) {
259        if (!(obj instanceof Metric)) {
260          return false;
261        }
262        if (this == obj) {
263          return true;
264        }
265        Metric other = (Metric) obj;
266        return key.equals(other.getKey());
267      }
268    
269      @Override
270      public String toString() {
271        return key;
272      }
273    
274    }