001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2008-2011 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    package org.sonar.api.rules;
021    
022    import org.apache.commons.lang.builder.EqualsBuilder;
023    import org.apache.commons.lang.builder.HashCodeBuilder;
024    import org.apache.commons.lang.builder.ToStringBuilder;
025    import org.sonar.api.database.BaseIdentifiable;
026    import org.sonar.api.profiles.RulesProfile;
027    
028    import java.util.ArrayList;
029    import java.util.Calendar;
030    import java.util.Date;
031    import java.util.List;
032    
033    import javax.persistence.*;
034    
035    /**
036     * A class to map a RuleChange to the hibernate model
037     * 
038     * @since 2.9
039     */
040    @Entity
041    @Table(name = "active_rule_changes")
042    public class ActiveRuleChange extends BaseIdentifiable {
043    
044      @Column(name = "user_name", updatable = false, nullable = false)
045      private String userName;
046    
047      @ManyToOne(fetch = FetchType.EAGER)
048      @JoinColumn(name = "profile_id", updatable = false, nullable = false)
049      private RulesProfile rulesProfile;
050    
051      @Column(name = "profile_version", updatable = false, nullable = false)
052      private int profileVersion;
053    
054      @ManyToOne(fetch = FetchType.EAGER)
055      @JoinColumn(name = "rule_id", updatable = false, nullable = false)
056      private Rule rule;
057    
058      @Column(name = "change_date", updatable = false, nullable = false)
059      private Date date;
060    
061      /**
062       * true means rule was enabled
063       * false means rule was disabled
064       * null means rule stay enabled (another param was changed)
065       */
066      @Column(name = "enabled")
067      private Boolean enabled;
068    
069      @Column(name = "old_severity", updatable = false, nullable = true)
070      @Enumerated(EnumType.ORDINAL)
071      private RulePriority oldSeverity;
072    
073      @Column(name = "new_severity", updatable = false, nullable = true)
074      @Enumerated(EnumType.ORDINAL)
075      private RulePriority newSeverity;
076    
077      @OneToMany(mappedBy = "activeRuleChange", fetch = FetchType.LAZY, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE })
078      private List<ActiveRuleParamChange> activeRuleParamChanges = new ArrayList<ActiveRuleParamChange>();
079    
080      public ActiveRuleChange(String userName, RulesProfile profile, Rule rule) {
081        this.userName = userName;
082        this.rulesProfile = profile;
083        this.profileVersion = profile.getVersion();
084        this.rule = rule;
085        this.date = Calendar.getInstance().getTime();
086      }
087    
088      public Rule getRule() {
089        return rule;
090      }
091    
092      public RulePriority getOldSeverity() {
093        return oldSeverity;
094      }
095    
096      public void setOldSeverity(RulePriority oldSeverity) {
097        this.oldSeverity = oldSeverity;
098      }
099    
100      public RulePriority getNewSeverity() {
101        return newSeverity;
102      }
103    
104      public void setNewSeverity(RulePriority newSeverity) {
105        this.newSeverity = newSeverity;
106      }
107    
108      public RulesProfile getRulesProfile() {
109        return rulesProfile;
110      }
111    
112      public int getProfileVersion() {
113        return profileVersion;
114      }
115    
116      public String getRepositoryKey() {
117        return rule.getRepositoryKey();
118      }
119    
120      /**
121       * @return the config key the changed rule belongs to
122       */
123      public String getConfigKey() {
124        return rule.getConfigKey();
125      }
126    
127      /**
128       * @return the key of the changed rule
129       */
130      public String getRuleKey() {
131        return rule.getKey();
132      }
133    
134      public Boolean isEnabled() {
135        return enabled;
136      }
137    
138      public void setEnabled(Boolean enabled) {
139        this.enabled = enabled;
140      }
141    
142      public List<ActiveRuleParamChange> getActiveRuleParamChanges() {
143        return activeRuleParamChanges;
144      }
145    
146      public String getUserName() {
147        return userName;
148      }
149    
150      public ActiveRuleChange setParameterChange(String key, String oldValue, String newValue) {
151        RuleParam ruleParameter = rule.getParam(key);
152        if (ruleParameter != null) {
153          activeRuleParamChanges.add(new ActiveRuleParamChange(this, ruleParameter, oldValue, newValue));
154        }
155        return this;
156      }
157    
158      @Override
159      public boolean equals(Object obj) {
160        if (obj == null) {
161          return false;
162        }
163        if (obj == this) {
164          return true;
165        }
166        if (obj.getClass() != getClass()) {
167          return false;
168        }
169        ActiveRuleChange rhs = (ActiveRuleChange) obj;
170        return new EqualsBuilder()
171            .appendSuper(super.equals(obj))
172            .append(userName, rhs.userName)
173            .append(rulesProfile, rhs.rulesProfile)
174            .append(rule, rhs.rule)
175            .append(date, rhs.date)
176            .append(enabled, rhs.enabled)
177            .append(newSeverity, rhs.newSeverity)
178            .isEquals();
179      }
180    
181      @Override
182      public int hashCode() {
183        return new HashCodeBuilder(41, 33)
184            .append(userName)
185            .append(rulesProfile)
186            .append(rule)
187            .append(date)
188            .append(enabled)
189            .append(newSeverity)
190            .toHashCode();
191      }
192    
193      @Override
194      public String toString() {
195        return new ToStringBuilder(this)
196            .append("id", getId())
197            .append("profile", rulesProfile)
198            .append("rule", rule)
199            .append("modifier", userName)
200            .append("changed at", date)
201            .append("enabled", enabled)
202            .append("new severity", newSeverity)
203            .toString();
204      }
205    
206    }