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 /**
033 * A class to map an ActiveRule to the hibernate model
034 */
035 @Entity
036 @Table(name = "active_rules")
037 public class ActiveRule extends BaseIdentifiable implements Cloneable {
038
039 @ManyToOne(fetch = FetchType.EAGER)
040 @JoinColumn(name = "rule_id", updatable = true, nullable = false)
041 private Rule rule;
042
043 @Column(name = "failure_level", updatable = true, nullable = false)
044 @Enumerated(EnumType.ORDINAL)
045 private RulePriority priority;
046
047 @ManyToOne(fetch = FetchType.LAZY)
048 @JoinColumn(name = "profile_id", updatable = true, nullable = false)
049 private RulesProfile rulesProfile;
050
051 @OneToMany(mappedBy = "activeRule", cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE})
052 private List<ActiveRuleParam> activeRuleParams = new ArrayList<ActiveRuleParam>();
053
054 public ActiveRule() {
055 }
056
057 /**
058 * Creates an ActiveRule
059 *
060 * @param profile the profile the rule is activated in
061 * @param rule the rule that is active
062 * @param priority the priority of the rule within the profile
063 */
064 public ActiveRule(RulesProfile profile, Rule rule, RulePriority priority) {
065 this.rule = rule;
066 if (priority == null && rule != null) {
067 this.priority = rule.getPriority();
068 } else {
069 this.priority = priority;
070 }
071
072 this.rulesProfile = profile;
073 }
074
075 public Rule getRule() {
076 return rule;
077 }
078
079 public void setRule(Rule rule) {
080 this.rule = rule;
081 }
082
083 public RulePriority getPriority() {
084 return priority;
085 }
086
087 public void setPriority(RulePriority priority) {
088 this.priority = priority;
089 }
090
091 public RulesProfile getRulesProfile() {
092 return rulesProfile;
093 }
094
095 public void setRulesProfile(RulesProfile rulesProfile) {
096 this.rulesProfile = rulesProfile;
097 }
098
099 public List<ActiveRuleParam> getActiveRuleParams() {
100 return activeRuleParams;
101 }
102
103 /**
104 * Sets the list of parameters for the active rule
105 */
106 public void setActiveRuleParams(List<ActiveRuleParam> params) {
107 this.activeRuleParams = params;
108 }
109
110 /**
111 * @return the name of the plugin the active rule belongs to
112 */
113 public String getPluginName() {
114 return rule.getPluginName();
115 }
116
117 /**
118 * @return the config key the active rule belongs to
119 */
120 public String getConfigKey() {
121 return rule.getConfigKey();
122 }
123
124 /**
125 * @return the key of the active rule
126 */
127 public String getRuleKey() {
128 return rule.getKey();
129 }
130
131 @Override
132 public boolean equals(Object o) {
133 if (this == o) {
134 return true;
135 }
136 if (o == null || getClass() != o.getClass()) {
137 return false;
138 }
139
140 ActiveRule that = (ActiveRule) o;
141
142 if (!rule.equals(that.rule)) {
143 return false;
144 }
145 if (rulesProfile != null ? !rulesProfile.equals(that.rulesProfile) : that.rulesProfile != null) {
146 return false;
147 }
148
149 return true;
150 }
151
152 @Override
153 public int hashCode() {
154 int result = rule.hashCode();
155 result = 31 * result + (rulesProfile != null ? rulesProfile.hashCode() : 0);
156 return result;
157 }
158
159 @Override
160 public String toString() {
161 return new ToStringBuilder(this).append("id", getId()).append("rule", rule).append("priority", priority).append("params", activeRuleParams).toString();
162 }
163
164 @Override
165 public Object clone() {
166 ActiveRule clone = new ActiveRule(getRulesProfile(), getRule(), getPriority());
167 if (CollectionUtils.isNotEmpty(getActiveRuleParams())) {
168 clone.setActiveRuleParams(new ArrayList<ActiveRuleParam>(CollectionUtils.collect(getActiveRuleParams(), new Transformer() {
169 public Object transform(Object input) {
170 return ((ActiveRuleParam) input).clone();
171 }
172 })));
173 }
174 return clone;
175 }
176
177 }