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 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 public RulePriority getPriority() { 156 return severity; 157 } 158 159 /** 160 * @deprecated since 2.5 use {@link #setSeverity(RulePriority)} instead. See http://jira.codehaus.org/browse/SONAR-1829 161 */ 162 public void setPriority(RulePriority priority) { 163 this.severity = priority; 164 } 165 166 public RulesProfile getRulesProfile() { 167 return rulesProfile; 168 } 169 170 /** 171 * @deprecated visibility should be reduced to protected or package 172 */ 173 @Deprecated 174 public void setRulesProfile(RulesProfile rulesProfile) { 175 this.rulesProfile = rulesProfile; 176 } 177 178 public List<ActiveRuleParam> getActiveRuleParams() { 179 return activeRuleParams; 180 } 181 182 /** 183 * @deprecated use setParameter() 184 */ 185 @Deprecated 186 public void setActiveRuleParams(List<ActiveRuleParam> params) { 187 this.activeRuleParams = params; 188 } 189 190 public ActiveRule setParameter(String key, String value) { 191 RuleParam ruleParameter = rule.getParam(key); 192 if (ruleParameter != null) { 193 activeRuleParams.add(new ActiveRuleParam(this, ruleParameter, value)); 194 } 195 return this; 196 } 197 198 public String getParameter(String key) { 199 if (activeRuleParams != null) { 200 for (ActiveRuleParam param : activeRuleParams) { 201 if (StringUtils.equals(key, param.getKey())) { 202 return param.getValue(); 203 } 204 } 205 } 206 return null; 207 } 208 209 /** 210 * @deprecated since 2.3 use {@link #getRepositoryKey()} instead 211 */ 212 @Deprecated 213 public String getPluginName() { 214 return rule.getRepositoryKey(); 215 } 216 217 public String getRepositoryKey() { 218 return rule.getRepositoryKey(); 219 } 220 221 /** 222 * @return the config key the active rule belongs to 223 */ 224 public String getConfigKey() { 225 return rule.getConfigKey(); 226 } 227 228 /** 229 * @return the key of the active rule 230 */ 231 public String getRuleKey() { 232 return rule.getKey(); 233 } 234 235 @Override 236 public boolean equals(Object o) { 237 if (this == o) { 238 return true; 239 } 240 if (o == null || getClass() != o.getClass()) { 241 return false; 242 } 243 244 ActiveRule that = (ActiveRule) o; 245 246 if (!rule.equals(that.rule)) { 247 return false; 248 } 249 if (rulesProfile != null ? !rulesProfile.equals(that.rulesProfile) : that.rulesProfile != null) { 250 return false; 251 } 252 253 return true; 254 } 255 256 @Override 257 public int hashCode() { 258 int result = rule.hashCode(); 259 result = 31 * result + (rulesProfile != null ? rulesProfile.hashCode() : 0); 260 return result; 261 } 262 263 @Override 264 public String toString() { 265 return new ToStringBuilder(this).append("id", getId()).append("rule", rule).append("priority", severity) 266 .append("params", activeRuleParams).toString(); 267 } 268 269 @Override 270 public Object clone() { 271 final ActiveRule clone = new ActiveRule(getRulesProfile(), getRule(), getSeverity()); 272 clone.setInheritance(getInheritance()); 273 if (CollectionUtils.isNotEmpty(getActiveRuleParams())) { 274 clone.setActiveRuleParams(new ArrayList<ActiveRuleParam>(CollectionUtils.collect(getActiveRuleParams(), new Transformer() { 275 public Object transform(Object input) { 276 ActiveRuleParam activeRuleParamClone = (ActiveRuleParam) ((ActiveRuleParam) input).clone(); 277 activeRuleParamClone.setActiveRule(clone); 278 return activeRuleParamClone; 279 } 280 }))); 281 } 282 return clone; 283 } 284 285 }