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.StringUtils;
025    import org.apache.commons.lang.builder.ToStringBuilder;
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 implements Cloneable {
038    
039      @Id
040      @Column(name = "id")
041      @GeneratedValue
042      private Integer id;
043    
044      @ManyToOne(fetch = FetchType.EAGER)
045      @JoinColumn(name = "rule_id", updatable = true, nullable = false)
046      private Rule rule;
047    
048      @Column(name = "failure_level", updatable = true, nullable = false)
049      @Enumerated(EnumType.ORDINAL)
050      private RulePriority priority;
051    
052      @ManyToOne(fetch = FetchType.EAGER)
053      @JoinColumn(name = "profile_id", updatable = true, nullable = false)
054      private RulesProfile rulesProfile;
055    
056      @OneToMany(mappedBy = "activeRule", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE})
057      private List<ActiveRuleParam> activeRuleParams = new ArrayList<ActiveRuleParam>();
058    
059      /**
060       * @deprecated visibility should be reduced to protected or package
061       */
062      @Deprecated
063      public ActiveRule() {
064      }
065    
066      /**
067       * @deprecated visibility should be reduced to protected or package
068       */
069      @Deprecated
070      public ActiveRule(RulesProfile profile, Rule rule, RulePriority priority) {
071        this.rule = rule;
072        if (priority == null && rule != null) {
073          this.priority = rule.getPriority();
074        } else {
075          this.priority = priority;
076        }
077    
078        this.rulesProfile = profile;
079      }
080    
081      public Integer getId() {
082        return id;
083      }
084    
085      /**
086       * @deprecated visibility should be decreased to protected or package
087       */
088      @Deprecated
089      public void setId(Integer id) {
090        this.id = id;
091      }
092    
093      public Rule getRule() {
094        return rule;
095      }
096    
097      /**
098        * @deprecated visibility should be reduced to protected or package
099       */
100      @Deprecated
101      public void setRule(Rule rule) {
102        this.rule = rule;
103      }
104    
105      public RulePriority getPriority() {
106        return priority;
107      }
108    
109      public void setPriority(RulePriority priority) {
110        this.priority = priority;
111      }
112    
113      public RulesProfile getRulesProfile() {
114        return rulesProfile;
115      }
116    
117      /**
118        * @deprecated visibility should be reduced to protected or package
119       */
120      @Deprecated
121      public void setRulesProfile(RulesProfile rulesProfile) {
122        this.rulesProfile = rulesProfile;
123      }
124    
125      public List<ActiveRuleParam> getActiveRuleParams() {
126        return activeRuleParams;
127      }
128    
129      /**
130       * @deprecated use setParameter()
131       */
132      @Deprecated
133      public void setActiveRuleParams(List<ActiveRuleParam> params) {
134        this.activeRuleParams = params;
135      }
136    
137      public ActiveRule setParameter(String key, String value) {
138        RuleParam ruleParameter = rule.getParam(key);
139        if (ruleParameter != null) {
140          activeRuleParams.add(new ActiveRuleParam(this, ruleParameter, value));
141        }
142        return this;
143      }
144    
145      public String getParameter(String key) {
146        if (activeRuleParams != null) {
147          for (ActiveRuleParam param : activeRuleParams) {
148            if (StringUtils.equals(key, param.getKey())) {
149              return param.getValue();
150            }
151          }
152        }
153        return null;
154      }
155    
156      /**
157       * @deprecated use getRepositoryKey()
158       */
159      @Deprecated
160      public String getPluginName() {
161        return rule.getPluginName();
162      }
163    
164      public String getRepositoryKey() {
165        return rule.getRepositoryKey();
166      }
167    
168      /**
169       * @return the config key the active rule belongs to
170       */
171      public String getConfigKey() {
172        return rule.getConfigKey();
173      }
174    
175      /**
176       * @return the key of the active rule
177       */
178      public String getRuleKey() {
179        return rule.getKey();
180      }
181    
182      @Override
183      public boolean equals(Object o) {
184        if (this == o) {
185          return true;
186        }
187        if (o == null || getClass() != o.getClass()) {
188          return false;
189        }
190    
191        ActiveRule that = (ActiveRule) o;
192    
193        if (!rule.equals(that.rule)) {
194          return false;
195        }
196        if (rulesProfile != null ? !rulesProfile.equals(that.rulesProfile) : that.rulesProfile != null) {
197          return false;
198        }
199    
200        return true;
201      }
202    
203      @Override
204      public int hashCode() {
205        int result = rule.hashCode();
206        result = 31 * result + (rulesProfile != null ? rulesProfile.hashCode() : 0);
207        return result;
208      }
209    
210      @Override
211      public String toString() {
212        return new ToStringBuilder(this).append("id", getId()).append("rule", rule).append("priority", priority).append("params", activeRuleParams).toString();
213      }
214    
215      @Override
216      public Object clone() {
217        ActiveRule clone = new ActiveRule(getRulesProfile(), getRule(), getPriority());
218        if (CollectionUtils.isNotEmpty(getActiveRuleParams())) {
219          clone.setActiveRuleParams(new ArrayList<ActiveRuleParam>(CollectionUtils.collect(getActiveRuleParams(), new Transformer() {
220            public Object transform(Object input) {
221              return ((ActiveRuleParam) input).clone();
222            }
223          })));
224        }
225        return clone;
226      }
227    
228    }