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