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.builder.ToStringBuilder;
025 import org.sonar.api.database.BaseIdentifiable;
026 import org.sonar.api.profiles.RulesProfile;
027
028 import java.util.ArrayList;
029 import java.util.List;
030 import javax.persistence.*;
031
032 @Entity
033 @Table(name = "active_rules")
034 public class ActiveRule extends BaseIdentifiable implements Cloneable {
035
036 @ManyToOne(fetch = FetchType.EAGER)
037 @JoinColumn(name = "rule_id", updatable = true, nullable = false)
038 private Rule rule;
039
040 @Column(name = "failure_level", updatable = true, nullable = false)
041 @Enumerated(EnumType.ORDINAL)
042 private RulePriority priority;
043
044 @ManyToOne(fetch = FetchType.LAZY)
045 @JoinColumn(name = "profile_id", updatable = true, nullable = false)
046 private RulesProfile rulesProfile;
047
048 @OneToMany(mappedBy = "activeRule", cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE})
049 private List<ActiveRuleParam> activeRuleParams = new ArrayList<ActiveRuleParam>();
050
051 public ActiveRule() {
052 }
053
054 public ActiveRule(RulesProfile profile, Rule rule, RulePriority priority) {
055 this.rule = rule;
056 if (priority == null && rule != null) {
057 this.priority = rule.getPriority();
058 } else {
059 this.priority = priority;
060 }
061
062 this.rulesProfile = profile;
063 }
064
065 public Rule getRule() {
066 return rule;
067 }
068
069 public void setRule(Rule rule) {
070 this.rule = rule;
071 }
072
073 public RulePriority getPriority() {
074 return priority;
075 }
076
077 public void setPriority(RulePriority priority) {
078 this.priority = priority;
079 }
080
081 public RulesProfile getRulesProfile() {
082 return rulesProfile;
083 }
084
085 public void setRulesProfile(RulesProfile rulesProfile) {
086 this.rulesProfile = rulesProfile;
087 }
088
089 public List<ActiveRuleParam> getActiveRuleParams() {
090 return activeRuleParams;
091 }
092
093 public void setActiveRuleParams(List<ActiveRuleParam> params) {
094 this.activeRuleParams = params;
095 }
096
097 public String getPluginName() {
098 return rule.getPluginName();
099 }
100
101 public String getConfigKey() {
102 return rule.getConfigKey();
103 }
104
105 public String getRuleKey() {
106 return rule.getKey();
107 }
108
109 @Override
110 public boolean equals(Object o) {
111 if (this == o) {
112 return true;
113 }
114 if (o == null || getClass() != o.getClass()) {
115 return false;
116 }
117
118 ActiveRule that = (ActiveRule) o;
119
120 if (!rule.equals(that.rule)) {
121 return false;
122 }
123 if (rulesProfile != null ? !rulesProfile.equals(that.rulesProfile) : that.rulesProfile != null) {
124 return false;
125 }
126
127 return true;
128 }
129
130 @Override
131 public int hashCode() {
132 int result = rule.hashCode();
133 result = 31 * result + (rulesProfile != null ? rulesProfile.hashCode() : 0);
134 return result;
135 }
136
137 @Override
138 public String toString() {
139 return new ToStringBuilder(this).append("id", getId()).append("rule", rule).append("priority", priority).append("params", activeRuleParams).toString();
140 }
141
142 @Override
143 public Object clone() {
144 ActiveRule clone = new ActiveRule(getRulesProfile(), getRule(), getPriority());
145 if (CollectionUtils.isNotEmpty(getActiveRuleParams())) {
146 clone.setActiveRuleParams(new ArrayList<ActiveRuleParam>(CollectionUtils.collect(getActiveRuleParams(), new Transformer() {
147 public Object transform(Object input) {
148 return ((ActiveRuleParam) input).clone();
149 }
150 })));
151 }
152 return clone;
153 }
154
155 }