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    
021    package org.sonar.api.profiles;
022    
023    import org.hibernate.annotations.Cache;
024    import org.hibernate.annotations.CacheConcurrencyStrategy;
025    import org.sonar.api.database.BaseIdentifiable;
026    import org.sonar.api.measures.Metric;
027    
028    import javax.persistence.*;
029    
030    @Entity
031    @Table(name = "alerts")
032    public class Alert extends BaseIdentifiable implements Cloneable {
033    
034      public static final String OPERATOR_GREATER = ">";
035      public static final String OPERATOR_SMALLER = "<";
036      public static final String OPERATOR_EQUALS = "=";
037      public static final String OPERATOR_NOT_EQUALS = "!=";
038    
039      @ManyToOne(fetch = FetchType.LAZY)
040      @JoinColumn(name = "profile_id")
041      @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
042      private RulesProfile rulesProfile;
043    
044      @ManyToOne(fetch = FetchType.EAGER)
045      @JoinColumn(name = "metric_id", nullable = true)
046      @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
047      private Metric metric;
048    
049      @Column(name = "operator", updatable = false, nullable = true, length = 3)
050      private String operator;
051    
052      @Column(name = "value_error", updatable = false, nullable = true, length = 64)
053      private String valueError;
054    
055      @Column(name = "value_warning", updatable = false, nullable = true, length = 64)
056      private String valueWarning;
057    
058      public Alert() {
059      }
060    
061      public Alert(RulesProfile rulesProfile, Metric metric, String operator, String valueError, String valueWarning) {
062        super();
063        this.rulesProfile = rulesProfile;
064        this.metric = metric;
065        this.operator = operator;
066        this.valueError = valueError;
067        this.valueWarning = valueWarning;
068      }
069    
070      public RulesProfile getRulesProfile() {
071        return rulesProfile;
072      }
073    
074      public void setRulesProfile(RulesProfile rulesProfile) {
075        this.rulesProfile = rulesProfile;
076      }
077    
078      public Metric getMetric() {
079        return metric;
080      }
081    
082      public void setMetric(Metric metric) {
083        this.metric = metric;
084      }
085    
086      public String getOperator() {
087        return operator;
088      }
089    
090      public void setOperator(String operator) {
091        this.operator = operator;
092      }
093    
094      public String getValueError() {
095        return valueError;
096      }
097    
098      public void setValueError(String valueError) {
099        this.valueError = valueError;
100      }
101    
102      public String getValueWarning() {
103        return valueWarning;
104      }
105    
106      public void setValueWarning(String valueWarning) {
107        this.valueWarning = valueWarning;
108      }
109    
110      public boolean isGreaterOperator() {
111        return operator.equals(OPERATOR_GREATER);
112      }
113    
114      public boolean isSmallerOperator() {
115        return operator.equals(OPERATOR_SMALLER);
116      }
117    
118      public boolean isEqualsOperator() {
119        return operator.equals(OPERATOR_EQUALS);
120      }
121    
122      public boolean isNotEqualsOperator() {
123        return operator.equals(OPERATOR_NOT_EQUALS);
124      }
125    
126    
127      public String getAlertLabel(Metric.Level level) {
128        return new StringBuilder()
129            .append(getMetric().getName())
130            .append(" ").append(getOperator())
131            .append(" ")
132            .append(level.equals(Metric.Level.ERROR) ? getValueError() : getValueWarning()).toString();
133      }
134    
135      @Override
136      public Object clone() {
137        return new Alert(getRulesProfile(), getMetric(), getOperator(), getValueError(), getValueWarning());
138      }
139    
140    }