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