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