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.profiles;
021    
022    import org.apache.commons.collections.CollectionUtils;
023    import org.apache.commons.collections.Transformer;
024    import org.apache.commons.lang.builder.EqualsBuilder;
025    import org.apache.commons.lang.builder.HashCodeBuilder;
026    import org.sonar.api.database.BaseIdentifiable;
027    import org.sonar.api.database.model.ResourceModel;
028    import org.sonar.api.rules.ActiveRule;
029    import org.sonar.api.rules.Rule;
030    import org.sonar.api.rules.RulePriority;
031    
032    import java.util.ArrayList;
033    import java.util.List;
034    import javax.persistence.*;
035    
036    @Entity
037    @Table(name = "rules_profiles")
038    public class RulesProfile extends BaseIdentifiable implements Cloneable {
039    
040      public static final String SONAR_WAY_NAME = "Sonar way";
041      public static final String SONAR_WAY_FINDBUGS_NAME = "Sonar way with Findbugs";
042      public static final String SUN_CONVENTIONS_NAME = "Sun checks";
043    
044      @Column(name = "name", updatable = true, nullable = false)
045      private String name;
046    
047      @Column(name = "default_profile", updatable = true, nullable = false)
048      private Boolean defaultProfile = Boolean.FALSE;
049    
050      @Column(name = "provided", updatable = true, nullable = false)
051      private Boolean provided = Boolean.FALSE;
052    
053      @Column(name = "language", updatable = true, nullable = false)
054      private String language;
055    
056      @OneToMany(mappedBy = "rulesProfile", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE})
057      private List<ActiveRule> activeRules;
058    
059      @OneToMany(mappedBy = "rulesProfile", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE})
060      private List<Alert> alerts;
061    
062      @OneToMany(mappedBy = "rulesProfile", fetch = FetchType.LAZY)
063      private List<ResourceModel> projects;
064    
065      public RulesProfile() {
066      }
067    
068      public RulesProfile(String name, String language) {
069        this.name = name;
070        this.language = language;
071        this.activeRules = new ArrayList<ActiveRule>();
072        this.alerts = new ArrayList<Alert>();
073        this.projects = new ArrayList<ResourceModel>();
074      }
075    
076      public RulesProfile(String name, String language, boolean defaultProfile, boolean provided) {
077        this(name, language);
078        this.defaultProfile = defaultProfile;
079        this.provided = provided;
080      }
081    
082      public String getName() {
083        return name;
084      }
085    
086      public void setName(String name) {
087        this.name = name;
088      }
089    
090      public List<ActiveRule> getActiveRules() {
091        return activeRules;
092      }
093    
094      public void setActiveRules(List<ActiveRule> activeRules) {
095        this.activeRules = activeRules;
096      }
097    
098      public Boolean getDefaultProfile() {
099        return defaultProfile;
100      }
101    
102      public void setDefaultProfile(Boolean defaultProfile) {
103        this.defaultProfile = defaultProfile;
104      }
105    
106      public Boolean getProvided() {
107        return provided;
108      }
109    
110      public void setProvided(Boolean provided) {
111        this.provided = provided;
112      }
113    
114      public String getLanguage() {
115        return language;
116      }
117    
118      public void setLanguage(String language) {
119        this.language = language;
120      }
121    
122      public List<Alert> getAlerts() {
123        return alerts;
124      }
125    
126      public void setAlerts(List<Alert> alerts) {
127        this.alerts = alerts;
128      }
129    
130      public List<ResourceModel> getProjects() {
131        return projects;
132      }
133    
134      public void setProjects(List<ResourceModel> projects) {
135        this.projects = projects;
136      }
137    
138      public List<ActiveRule> getActiveRules(RulePriority priority) {
139        List<ActiveRule> result = new ArrayList<ActiveRule>();
140        for (ActiveRule activeRule : getActiveRules()) {
141          if (activeRule.getPriority().equals(priority)) {
142            result.add(activeRule);
143          }
144        }
145        return result;
146      }
147    
148      public List<ActiveRule> getActiveRulesByPlugin(String pluginKey) {
149        List<ActiveRule> result = new ArrayList<ActiveRule>();
150        for (ActiveRule activeRule : getActiveRules()) {
151          if (pluginKey.equals(activeRule.getPluginName())) {
152            result.add(activeRule);
153          }
154        }
155        return result;
156      }
157    
158      public ActiveRule getActiveRule(String pluginKey, String ruleKey) {
159        for (ActiveRule activeRule : getActiveRules()) {
160          if (activeRule != null && activeRule.getRuleKey().equals(ruleKey) && activeRule.getPluginName().equals(pluginKey)) {
161            return activeRule;
162          }
163        }
164        return null;
165      }
166    
167      public ActiveRule getActiveRule(Rule rule) {
168        return getActiveRule(rule.getPluginName(), rule.getKey());
169      }
170    
171      @Override
172      public boolean equals(Object obj) {
173        if (!(obj instanceof RulesProfile)) {
174          return false;
175        }
176        if (this == obj) {
177          return true;
178        }
179        RulesProfile other = (RulesProfile) obj;
180        return new EqualsBuilder().append(language, other.getLanguage()).append(name, other.getName()).isEquals();
181      }
182    
183      @Override
184      public int hashCode() {
185        return new HashCodeBuilder(17, 37).append(language).append(name).toHashCode();
186      }
187    
188      @Override
189      public Object clone() {
190        RulesProfile clone = new RulesProfile(getName(), getLanguage(), getDefaultProfile(), getProvided());
191        if (CollectionUtils.isNotEmpty(getActiveRules())) {
192          clone.setActiveRules(new ArrayList<ActiveRule>(CollectionUtils.collect(getActiveRules(), new Transformer() {
193            public Object transform(Object input) {
194              return ((ActiveRule) input).clone();
195            }
196          })));
197        }
198        if (CollectionUtils.isNotEmpty(getAlerts())) {
199          clone.setAlerts(new ArrayList<Alert>(CollectionUtils.collect(getAlerts(), new Transformer() {
200            public Object transform(Object input) {
201              return ((Alert) input).clone();
202            }
203          })));
204        }
205        if (CollectionUtils.isNotEmpty(getProjects())) {
206          clone.setProjects(new ArrayList<ResourceModel>(CollectionUtils.collect(getProjects(), new Transformer() {
207            public Object transform(Object input) {
208              return ((ResourceModel) input).clone();
209            }
210          })));
211        }
212        return clone;
213      }
214    
215      @Override
216      public String toString() {
217        return new StringBuilder().append(name).append(", language=").append(language).toString();
218      }
219    }