View Javadoc

1   /*
2    * Sonar, entreprise quality control tool.
3    * Copyright (C) 2007-2008 Hortis-GRC SA
4    * mailto:be_agile HAT hortis DOT ch
5    *
6    * Sonar is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 3 of the License, or (at your option) any later version.
10   *
11   * Sonar is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with {library}; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
19   */
20  package ch.hortis.sonar.model;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.commons.lang.builder.EqualsBuilder;
24  import org.apache.commons.lang.builder.HashCodeBuilder;
25  import org.apache.commons.lang.builder.ToStringBuilder;
26  import org.hibernate.annotations.Cache;
27  import org.hibernate.annotations.CacheConcurrencyStrategy;
28  import org.hibernate.annotations.Immutable;
29  import org.sonar.commons.database.DatabaseProperties;
30  import org.sonar.commons.rules.RuleParam;
31  
32  import javax.persistence.*;
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  @Immutable
37  @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
38  @Entity
39  @Table(name = "rules")
40  @NamedQueries(
41      {@NamedQuery(name = Rule.SQL_SELECT_ALL, query = "SELECT r FROM Rule r")}
42  )
43  public class Rule {
44    public final static String SQL_SELECT_ALL = "Rule.selectAll";
45  
46    @Id
47    @Column(name = "id")
48    @SequenceGenerator(name = "RULES_SEQ", sequenceName = "RULES_SEQ")
49    @GeneratedValue(strategy = GenerationType.AUTO, generator = "RULES_SEQ")
50    private Integer id;
51  
52    @Column(name = "name", updatable = false, nullable = false)
53    private String name;
54  
55    @Column(name = "plugin_rule_key", updatable = false, nullable = true)
56    private String key;
57  
58    @Column(name = "plugin_config_key", updatable = false, nullable = true)
59    private String configKey;
60  
61    @ManyToOne(fetch = FetchType.EAGER)
62    @JoinColumn(name = "rules_category_id", updatable = true, nullable = true)
63    private RulesCategory rulesCategory;
64  
65    @Column(name = "description", updatable = true, nullable = true, length = DatabaseProperties.MAX_TEXT_SIZE)
66    private String description;
67  
68    @Column(name = "plugin_name", updatable = true, nullable = true)
69    private String pluginName;
70  
71    @OneToMany(mappedBy = "rule", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE})
72    private List<RuleParam> params = new ArrayList<RuleParam>();
73  
74    public Rule() {
75    }
76  
77    public Rule(String name, String key, RulesCategory rulesCategory, String pluginName, String description) {
78      this();
79      this.name = name;
80      this.key = key;
81      this.rulesCategory = rulesCategory;
82      this.pluginName = pluginName;
83      this.description = description;
84    }
85  
86    public Rule(String name, String key, String configKey, RulesCategory rulesCategory, String pluginName, String description) {
87      this();
88      this.name = name;
89      this.key = key;
90      this.configKey = configKey;
91      this.rulesCategory = rulesCategory;
92      this.pluginName = pluginName;
93      this.description = description;
94    }
95  
96    public Integer getId() {
97      return id;
98    }
99  
100   public void setId(Integer id) {
101     this.id = id;
102   }
103 
104   public String getName() {
105     return name;
106   }
107 
108   public void setName(String name) {
109     this.name = name;
110   }
111 
112   public String getKey() {
113     return key;
114   }
115 
116   public void setKey(String key) {
117     this.key = key;
118   }
119 
120   public RulesCategory getRulesCategory() {
121     return rulesCategory;
122   }
123 
124   public void setRulesCategory(RulesCategory rulesCategory) {
125     this.rulesCategory = rulesCategory;
126   }
127 
128   public String getPluginName() {
129     return pluginName;
130   }
131 
132   public void setPluginName(String pluginName) {
133     this.pluginName = pluginName;
134   }
135 
136   public String getConfigKey() {
137     return configKey;
138   }
139 
140   public void setConfigKey(String configKey) {
141     this.configKey = configKey;
142   }
143 
144   public String getDescription() {
145     return description;
146   }
147 
148   public void setDescription(String description) {
149     this.description = StringUtils.strip(description);
150   }
151 
152   public List<RuleParam> getParams() {
153     return params;
154   }
155 
156   public void setParams(List<RuleParam> params) {
157     this.params = params;
158   }
159 
160   public boolean equals(Object obj) {
161     if (!(obj instanceof Rule)) {
162       return false;
163     }
164     if (this == obj) {
165       return true;
166     }
167     Rule other = (Rule) obj;
168     return new EqualsBuilder()
169         .append(pluginName, other.getPluginName())
170         .append(key, other.getKey())
171         .isEquals();
172   }
173 
174   public int hashCode() {
175     return new HashCodeBuilder(17, 37).
176         append(pluginName).append(key).toHashCode();
177   }
178 
179   public String toString() {
180     return new ToStringBuilder(this)
181         .append("id", id)
182         .append("name", name)
183         .append("key", key)
184         .append("configKey", configKey)
185         .append("categ", rulesCategory)
186         .append("plugin", pluginName)
187         .append("params", params)
188         .toString();
189   }
190 
191 }