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.lang.StringUtils;
023 import org.apache.commons.lang.builder.EqualsBuilder;
024 import org.apache.commons.lang.builder.HashCodeBuilder;
025 import org.apache.commons.lang.builder.ToStringBuilder;
026 import org.hibernate.annotations.Cache;
027 import org.hibernate.annotations.CacheConcurrencyStrategy;
028 import org.sonar.api.database.BaseIdentifiable;
029 import org.sonar.api.database.DatabaseProperties;
030
031 import java.util.ArrayList;
032 import java.util.List;
033 import javax.persistence.*;
034
035 @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
036 @Entity
037 @Table(name = "rules")
038 public class Rule extends BaseIdentifiable {
039
040 public static final RulePriority DEFAULT_PRIORITY = RulePriority.MAJOR;
041
042
043 @Column(name = "name", updatable = true, nullable = false)
044 private String name;
045
046 @Column(name = "plugin_rule_key", updatable = false, nullable = true)
047 private String key;
048
049 @Column(name = "plugin_config_key", updatable = true, nullable = true)
050 private String configKey;
051
052 @ManyToOne(fetch = FetchType.EAGER)
053 @JoinColumn(name = "rules_category_id", updatable = true, nullable = true)
054 private RulesCategory rulesCategory;
055
056 @Column(name = "priority", updatable = true, nullable = true)
057 @Enumerated(EnumType.ORDINAL)
058 private RulePriority priority = DEFAULT_PRIORITY;
059
060 @Column(name = "description", updatable = true, nullable = true, length = DatabaseProperties.MAX_TEXT_SIZE)
061 private String description;
062
063 @Column(name = "plugin_name", updatable = true, nullable = true)
064 private String pluginName;
065
066 @OneToMany(mappedBy = "rule", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE})
067 private List<RuleParam> params = new ArrayList<RuleParam>();
068
069 public Rule() {
070 }
071
072
073 public Rule(String pluginName, String key) {
074 this.pluginName = pluginName;
075 this.key = key;
076 this.configKey = key;
077 }
078
079 public Rule(String pluginKey, String key, String name, RulesCategory rulesCategory, RulePriority priority) {
080 setName(name);
081 this.key = key;
082 this.configKey = key;
083 this.rulesCategory = rulesCategory;
084 this.priority = priority;
085 this.pluginName = pluginKey;
086 }
087
088 @Deprecated
089 public Rule(String name, String key, RulesCategory rulesCategory, String pluginName, String description) {
090 this();
091 setName(name);
092 this.key = key;
093 this.configKey = key;
094 this.rulesCategory = rulesCategory;
095 this.pluginName = pluginName;
096 this.description = description;
097 }
098
099 @Deprecated
100 public Rule(String name, String key, String configKey, RulesCategory rulesCategory, String pluginName, String description) {
101 this();
102 setName(name);
103 this.key = key;
104 this.configKey = configKey;
105 this.rulesCategory = rulesCategory;
106 this.pluginName = pluginName;
107 this.description = description;
108 }
109
110
111 public String getName() {
112 return name;
113 }
114
115 public final Rule setName(String name) {
116 this.name = removeNewLineCharacters(name);
117 return this;
118 }
119
120 public String getKey() {
121 return key;
122 }
123
124 public Rule setKey(String key) {
125 this.key = key;
126 return this;
127 }
128
129 public RulesCategory getRulesCategory() {
130 return rulesCategory;
131 }
132
133 public Rule setRulesCategory(RulesCategory rulesCategory) {
134 this.rulesCategory = rulesCategory;
135 return this;
136 }
137
138 public String getPluginName() {
139 return pluginName;
140 }
141
142 public Rule setPluginName(String pluginName) {
143 this.pluginName = pluginName;
144 return this;
145 }
146
147 public String getConfigKey() {
148 return configKey;
149 }
150
151 public Rule setConfigKey(String configKey) {
152 this.configKey = configKey;
153 return this;
154 }
155
156 public String getDescription() {
157 return description;
158 }
159
160 public Rule setDescription(String description) {
161 this.description = StringUtils.strip(description);
162 return this;
163 }
164
165 public List<RuleParam> getParams() {
166 return params;
167 }
168
169 public Rule setParams(List<RuleParam> params) {
170 this.params = params;
171 return this;
172 }
173
174 public Integer getCategoryId() {
175 if (rulesCategory != null) {
176 return rulesCategory.getId();
177 }
178 return null;
179 }
180
181 public RulePriority getPriority() {
182 return priority;
183 }
184
185 public Rule setPriority(RulePriority priority) {
186 if (priority == null) {
187 this.priority = DEFAULT_PRIORITY;
188 } else {
189 this.priority = priority;
190 }
191
192 return this;
193 }
194
195 @Override
196 public boolean equals(Object obj) {
197 if (!(obj instanceof Rule)) {
198 return false;
199 }
200 if (this == obj) {
201 return true;
202 }
203 Rule other = (Rule) obj;
204 return new EqualsBuilder()
205 .append(pluginName, other.getPluginName())
206 .append(key, other.getKey())
207 .isEquals();
208 }
209
210 @Override
211 public int hashCode() {
212 return new HashCodeBuilder(17, 37)
213 .append(pluginName)
214 .append(key)
215 .toHashCode();
216 }
217
218 @Override
219 public String toString() {
220 return new ToStringBuilder(this)
221 .append("id", getId())
222 .append("name", name)
223 .append("key", key)
224 .append("configKey", configKey)
225 .append("categ", rulesCategory)
226 .append("plugin", pluginName)
227 .append("params", params)
228 .toString();
229 }
230
231 private String removeNewLineCharacters(String text) {
232 String removedCRLF = StringUtils.remove(text, "\n");
233 removedCRLF = StringUtils.remove(removedCRLF, "\r");
234 removedCRLF = StringUtils.remove(removedCRLF, "\n\r");
235 removedCRLF = StringUtils.remove(removedCRLF, "\r\n");
236 return removedCRLF;
237 }
238 }