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 package org.sonar.api.rules;
021
022 import org.apache.commons.collections.CollectionUtils;
023 import org.apache.commons.collections.Transformer;
024 import org.apache.commons.lang.StringUtils;
025 import org.apache.commons.lang.builder.ToStringBuilder;
026 import org.sonar.api.profiles.RulesProfile;
027
028 import java.util.ArrayList;
029 import java.util.List;
030
031 import javax.persistence.*;
032
033 /**
034 * A class to map an ActiveRule to the hibernate model
035 */
036 @Entity
037 @Table(name = "active_rules")
038 public class ActiveRule implements Cloneable {
039
040 public static final String INHERITED = "INHERITED";
041 public static final String OVERRIDES = "OVERRIDES";
042
043 @Id
044 @Column(name = "id")
045 @GeneratedValue
046 private Integer id;
047
048 @ManyToOne(fetch = FetchType.EAGER)
049 @JoinColumn(name = "rule_id", updatable = true, nullable = false)
050 private Rule rule;
051
052 @Column(name = "failure_level", updatable = true, nullable = false)
053 @Enumerated(EnumType.ORDINAL)
054 private RulePriority severity;
055
056 @ManyToOne(fetch = FetchType.EAGER)
057 @JoinColumn(name = "profile_id", updatable = true, nullable = false)
058 private RulesProfile rulesProfile;
059
060 @OneToMany(mappedBy = "activeRule", fetch = FetchType.LAZY, cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE })
061 private List<ActiveRuleParam> activeRuleParams = new ArrayList<ActiveRuleParam>();
062
063 @Column(name = "inheritance", updatable = true, nullable = true)
064 private String inheritance;
065
066 /**
067 * @deprecated visibility should be reduced to protected or package
068 */
069 @Deprecated
070 public ActiveRule() {
071 }
072
073 /**
074 * @deprecated visibility should be reduced to protected or package
075 */
076 @Deprecated
077 public ActiveRule(RulesProfile profile, Rule rule, RulePriority severity) {
078 this.rule = rule;
079 if (severity == null && rule != null) {
080 this.severity = rule.getSeverity();
081 } else {
082 this.severity = severity;
083 }
084
085 this.rulesProfile = profile;
086 }
087
088 public Integer getId() {
089 return id;
090 }
091
092 /**
093 * For internal use only.
094 *
095 * @since 2.5
096 */
097 public String getInheritance() {
098 return inheritance;
099 }
100
101 /**
102 * For internal use only.
103 *
104 * @since 2.5
105 */
106 public void setInheritance(String s) {
107 this.inheritance = s;
108 }
109
110 public boolean isInherited() {
111 return StringUtils.equals(INHERITED, inheritance);
112 }
113
114 public boolean doesOverride() {
115 return StringUtils.equals(OVERRIDES, inheritance);
116 }
117
118 /**
119 * @deprecated visibility should be decreased to protected or package
120 */
121 @Deprecated
122 public void setId(Integer id) {
123 this.id = id;
124 }
125
126 public Rule getRule() {
127 return rule;
128 }
129
130 /**
131 * @deprecated visibility should be reduced to protected or package
132 */
133 @Deprecated
134 public void setRule(Rule rule) {
135 this.rule = rule;
136 }
137
138 /**
139 * @since 2.5
140 */
141 public RulePriority getSeverity() {
142 return severity;
143 }
144
145 /**
146 * @since 2.5
147 */
148 public void setSeverity(RulePriority severity) {
149 this.severity = severity;
150 }
151
152 /**
153 * @deprecated since 2.5 use {@link #getSeverity()} instead. See http://jira.codehaus.org/browse/SONAR-1829
154 */
155 @Deprecated
156 public RulePriority getPriority() {
157 return severity;
158 }
159
160 /**
161 * @deprecated since 2.5 use {@link #setSeverity(RulePriority)} instead. See http://jira.codehaus.org/browse/SONAR-1829
162 */
163 @Deprecated
164 public void setPriority(RulePriority priority) {
165 this.severity = priority;
166 }
167
168 public RulesProfile getRulesProfile() {
169 return rulesProfile;
170 }
171
172 /**
173 * @deprecated visibility should be reduced to protected or package
174 */
175 @Deprecated
176 public void setRulesProfile(RulesProfile rulesProfile) {
177 this.rulesProfile = rulesProfile;
178 }
179
180 public List<ActiveRuleParam> getActiveRuleParams() {
181 return activeRuleParams;
182 }
183
184 /**
185 * @deprecated use setParameter()
186 */
187 @Deprecated
188 public void setActiveRuleParams(List<ActiveRuleParam> params) {
189 this.activeRuleParams = params;
190 }
191
192 public ActiveRule setParameter(String key, String value) {
193 RuleParam ruleParameter = rule.getParam(key);
194 if (ruleParameter != null) {
195 activeRuleParams.add(new ActiveRuleParam(this, ruleParameter, value));
196 }
197 return this;
198 }
199
200 public String getParameter(String key) {
201 if (activeRuleParams != null) {
202 for (ActiveRuleParam param : activeRuleParams) {
203 if (StringUtils.equals(key, param.getKey())) {
204 return param.getValue();
205 }
206 }
207 }
208 return null;
209 }
210
211 /**
212 * @deprecated since 2.3 use {@link #getRepositoryKey()} instead
213 */
214 @Deprecated
215 public String getPluginName() {
216 return rule.getRepositoryKey();
217 }
218
219 public String getRepositoryKey() {
220 return rule.getRepositoryKey();
221 }
222
223 /**
224 * @return the config key the active rule belongs to
225 */
226 public String getConfigKey() {
227 return rule.getConfigKey();
228 }
229
230 /**
231 * @return the key of the active rule
232 */
233 public String getRuleKey() {
234 return rule.getKey();
235 }
236
237 @Override
238 public boolean equals(Object o) {
239 if (this == o) {
240 return true;
241 }
242 if (o == null || getClass() != o.getClass()) {
243 return false;
244 }
245
246 ActiveRule that = (ActiveRule) o;
247
248 if (!rule.equals(that.rule)) {
249 return false;
250 }
251 if (rulesProfile != null ? !rulesProfile.equals(that.rulesProfile) : that.rulesProfile != null) {
252 return false;
253 }
254
255 return true;
256 }
257
258 @Override
259 public int hashCode() {
260 int result = rule.hashCode();
261 result = 31 * result + (rulesProfile != null ? rulesProfile.hashCode() : 0);
262 return result;
263 }
264
265 @Override
266 public String toString() {
267 return new ToStringBuilder(this).append("id", getId()).append("rule", rule).append("priority", severity)
268 .append("params", activeRuleParams).toString();
269 }
270
271 @Override
272 public Object clone() {
273 final ActiveRule clone = new ActiveRule(getRulesProfile(), getRule(), getSeverity());
274 clone.setInheritance(getInheritance());
275 if (CollectionUtils.isNotEmpty(getActiveRuleParams())) {
276 clone.setActiveRuleParams(new ArrayList<ActiveRuleParam>(CollectionUtils.collect(getActiveRuleParams(), new Transformer() {
277 public Object transform(Object input) {
278 ActiveRuleParam activeRuleParamClone = (ActiveRuleParam) ((ActiveRuleParam) input).clone();
279 activeRuleParamClone.setActiveRule(clone);
280 return activeRuleParamClone;
281 }
282 })));
283 }
284 return clone;
285 }
286
287 /**
288 * @since 2.6
289 */
290 public boolean isEnabled() {
291 return getRule()!=null && getRule().isEnabled();
292 }
293 }