View Javadoc

1   /*
2    * Sonar, entreprise quality control tool.
3    * Copyright (C) 2007-2008 Hortis-GRC SA
4    * mailto:be_agile HAT hortis DOT ch
5    *
6    * Sonar is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 3 of the License, or (at your option) any later version.
10   *
11   * Sonar is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with Sonar; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
19   */
20  package ch.hortis.sonar.model;
21  
22  import ch.hortis.sonar.service.MeasureKey;
23  import org.apache.commons.lang.builder.ToStringBuilder;
24  import org.hibernate.annotations.Cache;
25  import org.hibernate.annotations.CacheConcurrencyStrategy;
26  
27  import javax.persistence.*;
28  
29  @Entity
30  @Table(name = "project_measures")
31  public class ProjectMeasure {
32  
33    @Id
34    @Column(name = "id")
35    @SequenceGenerator(name = "PROJECT_MEASURES_SEQ", sequenceName = "PROJECT_MEASURES_SEQ")
36    @GeneratedValue(strategy = GenerationType.AUTO, generator = "PROJECT_MEASURES_SEQ")
37    private Integer id;
38  
39    @Column(name = "value", updatable = false, nullable = false, precision = 30, scale = 20)
40    private Double value = 0.0;
41  
42    @Column(name = "text_value", updatable = false, nullable = true, length = 96)
43    private String textValue;
44  
45    @Column(name = "tendency", updatable = true, nullable = true)
46    private Integer tendency;
47  
48    @ManyToOne(fetch = FetchType.EAGER)
49    @JoinColumn(name = "metric_id")
50    @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
51    private Metric metric;
52  
53    @Column(name = "snapshot_id")
54    private Integer snapshotId;
55  
56    @ManyToOne(fetch = FetchType.LAZY)
57    @JoinColumn(name = "rule_id")
58    @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
59    private Rule rule;
60  
61    @Column(name = "rules_category_id")
62    private Integer rulesCategoryId;
63  
64  
65    public ProjectMeasure(Snapshot snapshot, MeasureKey key, Double value) {
66      this(snapshot, key.getMetric(), value);
67      rule = key.getRule();
68      rulesCategoryId = key.getRulesCategoryId();
69    }
70  
71    public ProjectMeasure(Snapshot snapshot, Metric metric, Double value) throws IllegalArgumentException {
72      this.snapshot = snapshot;
73      if (snapshot != null) {
74        this.snapshotId = snapshot.getId();
75      }
76      this.metric = metric;
77      if (value != null && (value.isNaN() || value.isInfinite())){
78        throw new IllegalArgumentException();
79      }
80      this.value = value;
81    }
82  
83    public ProjectMeasure(Snapshot snapshot, Metric metric, String value) {
84      this(snapshot, metric, (Double) null);
85      this.value = 0.0;
86      this.textValue = value;
87    }
88  
89    public Double getValue() {
90      return value;
91    }
92  
93    public void setValue(Double value) throws IllegalArgumentException {
94      if (value != null && (value.isNaN() || value.isInfinite())){
95        throw new IllegalArgumentException();
96      }
97      this.value = value;
98    }
99  
100   public String getTextValue() {
101     return textValue;
102   }
103 
104   public void setTextValue(String textValue) {
105     this.textValue = textValue;
106   }
107 
108   public Integer getTendency() {
109     return tendency;
110   }
111 
112   public void setTendency(Integer tendency) {
113     this.tendency = tendency;
114   }
115 
116   public Metric getMetric() {
117     return metric;
118   }
119 
120   public void setMetric(Metric metric) {
121     this.metric = metric;
122   }
123 
124   public Integer getSnapshotId() {
125     return snapshotId;
126   }
127 
128   public void setSnapshotId(Integer snapshotId) {
129     this.snapshotId = snapshotId;
130   }
131 
132   @Transient
133   private Snapshot snapshot = null;
134   public Snapshot ephemereSnapshot() {
135     return snapshot;
136   }
137 
138   public Rule getRule() {
139     return rule;
140   }
141 
142   public void setRule(Rule rule) {
143     this.rule = rule;
144   }
145 
146   public Integer getRulesCategoryId() {
147     return rulesCategoryId;
148   }
149 
150   public void setRulesCategoryId(Integer id) {
151     this.rulesCategoryId = id;
152   }
153 
154   public Integer getId() {
155     return id;
156   }
157 
158   public void setId(Integer id) {
159     this.id = id;
160   }
161 
162   public ProjectMeasure() {
163   }
164 
165   @Override
166   public String toString() {
167     return new ToStringBuilder(this).
168         append("value", value).
169         append("metric", metric).
170         toString();
171   }
172 
173   public Integer getRuleId() {
174     if (getRule()!=null) {
175       return getRule().getId();
176     }
177     return null;
178   }
179 }