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