001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
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
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.*;
029
030/**
031 * Class to map alerts with hibernate model
032 */
033@Entity
034@Table(name = "alerts")
035public class Alert extends BaseIdentifiable implements Cloneable {
036  /**
037   * Operator strictly greater than
038   */
039  public static final String OPERATOR_GREATER = ">";
040
041  /**
042   * Operator strictly lesser than
043   */
044  public static final String OPERATOR_SMALLER = "<";
045
046  /**
047   * Operator equals
048   */
049  public static final String OPERATOR_EQUALS = "=";
050
051  /**
052   * Operator not equals
053   */
054  public static final String OPERATOR_NOT_EQUALS = "!=";
055
056  @ManyToOne(fetch = FetchType.LAZY)
057  @JoinColumn(name = "profile_id")
058  @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
059  private RulesProfile rulesProfile;
060
061  @ManyToOne(fetch = FetchType.EAGER)
062  @JoinColumn(name = "metric_id", nullable = true)
063  @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
064  private Metric metric;
065
066  @Column(name = "operator", updatable = false, nullable = true, length = 3)
067  private String operator;
068
069  @Column(name = "value_error", updatable = false, nullable = true, length = 64)
070  private String valueError;
071
072  @Column(name = "value_warning", updatable = false, nullable = true, length = 64)
073  private String valueWarning;
074
075  /**
076   * Default constructor
077   */
078  public Alert() {
079  }
080
081  /**
082   * Creates an alert
083   *
084   * @param rulesProfile the profile used to trigger the alert
085   * @param metric the metric tested for the alert
086   * @param operator the operator defined
087   * @param valueError the error value
088   * @param valueWarning the warning value
089   */
090  public Alert(RulesProfile rulesProfile, Metric metric, String operator, String valueError, String valueWarning) {
091    super();
092    this.rulesProfile = rulesProfile;
093    this.metric = metric;
094    this.operator = operator;
095    this.valueError = valueError;
096    this.valueWarning = valueWarning;
097  }
098
099  /**
100   * @return the alert profile
101   */
102  public RulesProfile getRulesProfile() {
103    return rulesProfile;
104  }
105
106  /**
107   * Sets the alert profile
108   */
109  public void setRulesProfile(RulesProfile rulesProfile) {
110    this.rulesProfile = rulesProfile;
111  }
112
113  /**
114   * @return the alert metric
115   */
116  public Metric getMetric() {
117    return metric;
118  }
119
120  /**
121   * Sets the alert metric
122   */
123  public void setMetric(Metric metric) {
124    this.metric = metric;
125  }
126
127  /**
128   * @return the alert operator
129   */
130  public String getOperator() {
131    return operator;
132  }
133
134  /**
135   * Sets the alert operator
136   */
137  public void setOperator(String operator) {
138    this.operator = operator;
139  }
140
141  /**
142   * @return the error value
143   */
144  public String getValueError() {
145    return valueError;
146  }
147
148  /**
149   * Sets the error value if any
150   */
151  public void setValueError(String valueError) {
152    this.valueError = valueError;
153  }
154
155  /**
156   * @return the warning value
157   */
158  public String getValueWarning() {
159    return valueWarning;
160  }
161
162  /**
163   * Sets the warning value if any
164   */
165  public void setValueWarning(String valueWarning) {
166    this.valueWarning = valueWarning;
167  }
168
169  /**
170   * @return whether the operator is greater than
171   */
172  public boolean isGreaterOperator() {
173    return operator.equals(OPERATOR_GREATER);
174  }
175
176  /**
177   * @return whether the operator is lesser than
178   */
179  public boolean isSmallerOperator() {
180    return operator.equals(OPERATOR_SMALLER);
181  }
182
183  /**
184   * @return whether the operator is equals
185   */
186  public boolean isEqualsOperator() {
187    return operator.equals(OPERATOR_EQUALS);
188  }
189
190  /**
191   * @return whether the operator is not equals
192   */
193  public boolean isNotEqualsOperator() {
194    return operator.equals(OPERATOR_NOT_EQUALS);
195  }
196
197
198  public String getAlertLabel(Metric.Level level) {
199    return new StringBuilder()
200        .append(getMetric().getName())
201        .append(" ").append(getOperator())
202        .append(" ")
203        .append(level.equals(Metric.Level.ERROR) ? getValueError() : getValueWarning()).toString();
204  }
205
206  @Override
207  public Object clone() {
208    return new Alert(getRulesProfile(), getMetric(), getOperator(), getValueError(), getValueWarning());
209  }
210
211}