org.sonar.api.profiles.RulesProfile Maven / Gradle / Ivy
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2009 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* Sonar is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* Sonar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.api.profiles;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.sonar.api.database.BaseIdentifiable;
import org.sonar.api.database.model.ResourceModel;
import org.sonar.api.rules.ActiveRule;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RulePriority;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.*;
/**
* Class to map rules profile with hibernate model
*/
@Entity
@Table(name = "rules_profiles")
public class RulesProfile extends BaseIdentifiable implements Cloneable {
/**
* The profile key for the embedded profile Sonar Way
*/
public static final String SONAR_WAY_NAME = "Sonar way";
/**
* The profile key for the embedded profile Sonar Way with Findbugs
*/
public static final String SONAR_WAY_FINDBUGS_NAME = "Sonar way with Findbugs";
/**
* The profile key for the embedded profile Sun checks
*/
public static final String SUN_CONVENTIONS_NAME = "Sun checks";
@Column(name = "name", updatable = true, nullable = false)
private String name;
@Column(name = "default_profile", updatable = true, nullable = false)
private Boolean defaultProfile = Boolean.FALSE;
@Column(name = "provided", updatable = true, nullable = false)
private Boolean provided = Boolean.FALSE;
@Column(name = "language", updatable = true, nullable = false)
private String language;
@OneToMany(mappedBy = "rulesProfile", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE})
private List activeRules;
@OneToMany(mappedBy = "rulesProfile", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE})
private List alerts;
@OneToMany(mappedBy = "rulesProfile", fetch = FetchType.LAZY)
private List projects;
/**
* Default constructor
*/
public RulesProfile() {
}
/**
* Creates a profile of rules with empty active rules, empty alerts and empty project lists.
*
* @param name the name to be used to access the profile, will be used as a key and display name
* @param language the language to which this profile applies
*/
public RulesProfile(String name, String language) {
this.name = name;
this.language = language;
this.activeRules = new ArrayList();
this.alerts = new ArrayList();
this.projects = new ArrayList();
}
/**
* Creates a profile of rules with empty active rules, empty alerts and empty project lists.
*
* @param name the name to be used to access the profile, will be used as a key and display name
* @param language the language to which this profile applies
* @param defaultProfile whether this is the default profile for the language
* @param provided whether the profile is embarked in core Sonar
*/
public RulesProfile(String name, String language, boolean defaultProfile, boolean provided) {
this(name, language);
this.defaultProfile = defaultProfile;
this.provided = provided;
}
/**
* @return the name of the profile
*/
public String getName() {
return name;
}
/**
* Sets the name of the profile
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the list of active rules
*/
public List getActiveRules() {
return activeRules;
}
/**
* Sets the list of active rules
*/
public void setActiveRules(List activeRules) {
this.activeRules = activeRules;
}
/**
* @return whether this is the default profile for the language
*/
public Boolean getDefaultProfile() {
return defaultProfile;
}
/**
* Sets whether this is the default profile for the language
*/
public void setDefaultProfile(Boolean defaultProfile) {
this.defaultProfile = defaultProfile;
}
/**
* @return whether the profile ships with Sonar core
*/
public Boolean getProvided() {
return provided;
}
/**
* Sets wether the profile ships with Sonar core
*/
public void setProvided(Boolean provided) {
this.provided = provided;
}
/**
* @return the language of the profile
*/
public String getLanguage() {
return language;
}
/**
* Sets the language for the profile
*/
public void setLanguage(String language) {
this.language = language;
}
/**
* @return the list of alerts defined in the profile
*/
public List getAlerts() {
return alerts;
}
/**
* Sets the list of alerts for the profile
*/
public void setAlerts(List alerts) {
this.alerts = alerts;
}
/**
* @return the list of projects attached to the profile
*/
public List getProjects() {
return projects;
}
/**
* Sets the list of projects attached to the profile
*/
public void setProjects(List projects) {
this.projects = projects;
}
/**
* @return the list of active rules for a given priority
*/
public List getActiveRules(RulePriority priority) {
List result = new ArrayList();
for (ActiveRule activeRule : getActiveRules()) {
if (activeRule.getPriority().equals(priority)) {
result.add(activeRule);
}
}
return result;
}
/**
* @return the list of active rules for a given plugin
*/
public List getActiveRulesByPlugin(String pluginKey) {
List result = new ArrayList();
for (ActiveRule activeRule : getActiveRules()) {
if (pluginKey.equals(activeRule.getPluginName())) {
result.add(activeRule);
}
}
return result;
}
/**
* @return an active rule from a plugin key and a rule key if the rule is activated, null otherwise
*/
public ActiveRule getActiveRule(String pluginKey, String ruleKey) {
for (ActiveRule activeRule : getActiveRules()) {
if (activeRule != null && activeRule.getRuleKey().equals(ruleKey) && activeRule.getPluginName().equals(pluginKey)) {
return activeRule;
}
}
return null;
}
/**
* THIS METHOD SHOULD NOT BE USED AS CURRENTLY THE PLUGIN KEY CAN NOT BE DETERMINED
*
* @return an active rule from a rule key if the rule is activated, null otherwise
*/
public ActiveRule getActiveRule(Rule rule) {
return getActiveRule(rule.getPluginName(), rule.getKey());
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof RulesProfile)) {
return false;
}
if (this == obj) {
return true;
}
RulesProfile other = (RulesProfile) obj;
return new EqualsBuilder().append(language, other.getLanguage()).append(name, other.getName()).isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37).append(language).append(name).toHashCode();
}
@Override
public Object clone() {
RulesProfile clone = new RulesProfile(getName(), getLanguage(), getDefaultProfile(), getProvided());
if (CollectionUtils.isNotEmpty(getActiveRules())) {
clone.setActiveRules(new ArrayList(CollectionUtils.collect(getActiveRules(), new Transformer() {
public Object transform(Object input) {
return ((ActiveRule) input).clone();
}
})));
}
if (CollectionUtils.isNotEmpty(getAlerts())) {
clone.setAlerts(new ArrayList(CollectionUtils.collect(getAlerts(), new Transformer() {
public Object transform(Object input) {
return ((Alert) input).clone();
}
})));
}
if (CollectionUtils.isNotEmpty(getProjects())) {
clone.setProjects(new ArrayList(CollectionUtils.collect(getProjects(), new Transformer() {
public Object transform(Object input) {
return ((ResourceModel) input).clone();
}
})));
}
return clone;
}
@Override
public String toString() {
return new StringBuilder().append(name).append(", language=").append(language).toString();
}
}