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