All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.api.profiles.RulesProfile Maven / Gradle / Ivy

There is a newer version: 5.1
Show newest version
/*
 * 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.*;

@Entity
@Table(name = "rules_profiles")
public class RulesProfile extends BaseIdentifiable implements Cloneable {

  public static final String SONAR_WAY_NAME = "Sonar way";
  public static final String SONAR_WAY_FINDBUGS_NAME = "Sonar way with Findbugs";
  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;

  public RulesProfile() {
  }

  public RulesProfile(String name, String language) {
    this.name = name;
    this.language = language;
    this.activeRules = new ArrayList();
    this.alerts = new ArrayList();
    this.projects = new ArrayList();
  }

  public RulesProfile(String name, String language, boolean defaultProfile, boolean provided) {
    this(name, language);
    this.defaultProfile = defaultProfile;
    this.provided = provided;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public List getActiveRules() {
    return activeRules;
  }

  public void setActiveRules(List activeRules) {
    this.activeRules = activeRules;
  }

  public Boolean getDefaultProfile() {
    return defaultProfile;
  }

  public void setDefaultProfile(Boolean defaultProfile) {
    this.defaultProfile = defaultProfile;
  }

  public Boolean getProvided() {
    return provided;
  }

  public void setProvided(Boolean provided) {
    this.provided = provided;
  }

  public String getLanguage() {
    return language;
  }

  public void setLanguage(String language) {
    this.language = language;
  }

  public List getAlerts() {
    return alerts;
  }

  public void setAlerts(List alerts) {
    this.alerts = alerts;
  }

  public List getProjects() {
    return projects;
  }

  public void setProjects(List projects) {
    this.projects = projects;
  }

  public List getActiveRules(RulePriority priority) {
    List result = new ArrayList();
    for (ActiveRule activeRule : getActiveRules()) {
      if (activeRule.getPriority().equals(priority)) {
        result.add(activeRule);
      }
    }
    return result;
  }

  public List getActiveRulesByPlugin(String pluginKey) {
    List result = new ArrayList();
    for (ActiveRule activeRule : getActiveRules()) {
      if (pluginKey.equals(activeRule.getPluginName())) {
        result.add(activeRule);
      }
    }
    return result;
  }

  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;
  }

  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();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy