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