001/*
002 * SonarQube, open source software quality management tool.
003 * Copyright (C) 2008-2013 SonarSource
004 * mailto:contact AT sonarsource DOT com
005 *
006 * SonarQube 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 * SonarQube 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 License
017 * along with this program; if not, write to the Free Software Foundation,
018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019 */
020
021package org.sonar.api.profiles;
022
023import org.hibernate.annotations.Cache;
024import org.hibernate.annotations.CacheConcurrencyStrategy;
025import org.sonar.api.database.BaseIdentifiable;
026import org.sonar.api.measures.Metric;
027
028import javax.persistence.Column;
029import javax.persistence.Entity;
030import javax.persistence.FetchType;
031import javax.persistence.JoinColumn;
032import javax.persistence.ManyToOne;
033import javax.persistence.Table;
034
035/**
036 * Class to map alerts with hibernate model
037 */
038@Entity
039@Table(name = "alerts")
040public class Alert extends BaseIdentifiable implements Cloneable {
041  /**
042   * Operator strictly greater than
043   */
044  public static final String OPERATOR_GREATER = ">";
045
046  /**
047   * Operator strictly lesser than
048   */
049  public static final String OPERATOR_SMALLER = "<";
050
051  /**
052   * Operator equals
053   */
054  public static final String OPERATOR_EQUALS = "=";
055
056  /**
057   * Operator not equals
058   */
059  public static final String OPERATOR_NOT_EQUALS = "!=";
060
061  @ManyToOne(fetch = FetchType.LAZY)
062  @JoinColumn(name = "profile_id")
063  @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
064  private RulesProfile rulesProfile;
065
066  @ManyToOne(fetch = FetchType.EAGER)
067  @JoinColumn(name = "metric_id", nullable = true)
068  @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
069  private Metric metric;
070
071  @Column(name = "operator", updatable = false, nullable = true, length = 3)
072  private String operator;
073
074  @Column(name = "value_error", updatable = false, nullable = true, length = 64)
075  private String valueError;
076
077  @Column(name = "value_warning", updatable = false, nullable = true, length = 64)
078  private String valueWarning;
079
080  @Column(name = "period", updatable = false, nullable = true)
081  private Integer period;
082
083  /**
084   * Default constructor
085   */
086  public Alert() {
087  }
088
089  /**
090   * Creates an alert
091   *
092   * @param rulesProfile the profile used to trigger the alert
093   * @param metric the metric tested for the alert
094   * @param operator the operator defined
095   * @param valueError the error value
096   * @param valueWarning the warning value
097   */
098  public Alert(RulesProfile rulesProfile, Metric metric, String operator, String valueError, String valueWarning) {
099    super();
100    this.rulesProfile = rulesProfile;
101    this.metric = metric;
102    this.operator = operator;
103    this.valueError = valueError;
104    this.valueWarning = valueWarning;
105  }
106
107  /**
108   * Creates an alert
109   *
110   * @param rulesProfile the profile used to trigger the alert
111   * @param metric the metric tested for the alert
112   * @param operator the operator defined
113   * @param valueError the error value
114   * @param valueWarning the warning value
115   */
116  public Alert(RulesProfile rulesProfile, Metric metric, String operator, String valueError, String valueWarning, Integer period) {
117    this(rulesProfile, metric, operator, valueError, valueWarning);
118    this.period = period;
119  }
120
121  /**
122   * @return the alert profile
123   */
124  public RulesProfile getRulesProfile() {
125    return rulesProfile;
126  }
127
128  /**
129   * Sets the alert profile
130   */
131  public void setRulesProfile(RulesProfile rulesProfile) {
132    this.rulesProfile = rulesProfile;
133  }
134
135  /**
136   * @return the alert metric
137   */
138  public Metric getMetric() {
139    return metric;
140  }
141
142  /**
143   * Sets the alert metric
144   */
145  public void setMetric(Metric metric) {
146    this.metric = metric;
147  }
148
149  /**
150   * @return the alert operator
151   */
152  public String getOperator() {
153    return operator;
154  }
155
156  /**
157   * Sets the alert operator
158   */
159  public void setOperator(String operator) {
160    this.operator = operator;
161  }
162
163  /**
164   * @return the error value
165   */
166  public String getValueError() {
167    return valueError;
168  }
169
170  /**
171   * Sets the error value if any
172   */
173  public void setValueError(String valueError) {
174    this.valueError = valueError;
175  }
176
177  /**
178   * @return the warning value
179   */
180  public String getValueWarning() {
181    return valueWarning;
182  }
183
184  /**
185   * Sets the warning value if any
186   */
187  public void setValueWarning(String valueWarning) {
188    this.valueWarning = valueWarning;
189  }
190
191  /**
192   * @return the period
193   */
194  public Integer getPeriod() {
195    return period;
196  }
197
198  /**
199   * Sets the period if any
200   */
201  public void setPeriod(Integer period) {
202    this.period = period;
203  }
204
205  /**
206   * @return whether the operator is greater than
207   */
208  public boolean isGreaterOperator() {
209    return operator.equals(OPERATOR_GREATER);
210  }
211
212  /**
213   * @return whether the operator is lesser than
214   */
215  public boolean isSmallerOperator() {
216    return operator.equals(OPERATOR_SMALLER);
217  }
218
219  /**
220   * @return whether the operator is equals
221   */
222  public boolean isEqualsOperator() {
223    return operator.equals(OPERATOR_EQUALS);
224  }
225
226  /**
227   * @return whether the operator is not equals
228   */
229  public boolean isNotEqualsOperator() {
230    return operator.equals(OPERATOR_NOT_EQUALS);
231  }
232
233  /**
234   * @deprecated since 3.4 because it does not manage alerts with variation
235   */
236  @Deprecated
237  public String getAlertLabel(Metric.Level level) {
238    return new StringBuilder()
239        .append(getMetric().getName())
240        .append(" ").append(getOperator())
241        .append(" ")
242        .append(level.equals(Metric.Level.ERROR) ? getValueError() : getValueWarning()).toString();
243  }
244
245  @Override
246  public Object clone() {
247    return new Alert(getRulesProfile(), getMetric(), getOperator(), getValueError(), getValueWarning(), getPeriod());
248  }
249
250}