You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Rule.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2009 SonarSource SA
  4. * mailto:contact AT sonarsource DOT com
  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 Sonar; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.api.rules;
  21. import org.apache.commons.lang.StringUtils;
  22. import org.apache.commons.lang.builder.EqualsBuilder;
  23. import org.apache.commons.lang.builder.HashCodeBuilder;
  24. import org.apache.commons.lang.builder.ToStringBuilder;
  25. import org.sonar.api.database.DatabaseProperties;
  26. import javax.persistence.*;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. @Entity
  30. @Table(name = "rules")
  31. public final class Rule {
  32. public static enum Cardinality {
  33. SINGLE, MULTIPLE
  34. }
  35. @Id
  36. @Column(name = "id")
  37. @GeneratedValue
  38. private Integer id;
  39. /**
  40. * The default priority given to a rule if not explicitely set
  41. */
  42. public static final RulePriority DEFAULT_PRIORITY = RulePriority.MAJOR;
  43. @Column(name = "name", updatable = true, nullable = false)
  44. private String name;
  45. @Column(name = "plugin_rule_key", updatable = false, nullable = true)
  46. private String key;
  47. @Column(name = "enabled", updatable = true, nullable = true)
  48. private Boolean enabled = Boolean.TRUE;
  49. @Column(name = "plugin_config_key", updatable = true, nullable = true)
  50. private String configKey;
  51. @ManyToOne(fetch = FetchType.EAGER)
  52. @JoinColumn(name = "rules_category_id", updatable = true, nullable = true)
  53. private RulesCategory rulesCategory;
  54. @Column(name = "priority", updatable = true, nullable = true)
  55. @Enumerated(EnumType.ORDINAL)
  56. private RulePriority priority = DEFAULT_PRIORITY;
  57. @Column(name = "description", updatable = true, nullable = true, length = DatabaseProperties.MAX_TEXT_SIZE)
  58. private String description;
  59. @Column(name = "plugin_name", updatable = true, nullable = false)
  60. private String pluginName;
  61. @Enumerated(EnumType.STRING)
  62. @Column(name = "cardinality", updatable = true, nullable = false)
  63. private Cardinality cardinality = Cardinality.SINGLE;
  64. @ManyToOne(fetch = FetchType.EAGER)
  65. @JoinColumn(name = "parent_id", updatable = true, nullable = true)
  66. private Rule parent = null;
  67. @org.hibernate.annotations.Cascade(
  68. {org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN}
  69. )
  70. @OneToMany(mappedBy = "rule")
  71. private List<RuleParam> params = new ArrayList<RuleParam>();
  72. /**
  73. * @deprecated since 2.3. Use the factory method create()
  74. */
  75. @Deprecated
  76. public Rule() {
  77. // TODO reduce visibility to package
  78. }
  79. /**
  80. * Creates rule with minimum set of info
  81. *
  82. * @param pluginName the plugin name indicates which plugin the rule belongs to
  83. * @param key the key should be unique within a plugin, but it is even more careful for the time being that it is unique
  84. * across the application
  85. * @deprecated since 2.3. Use the factory method create()
  86. */
  87. @Deprecated
  88. public Rule(String pluginName, String key) {
  89. this.pluginName = pluginName;
  90. this.key = key;
  91. this.configKey = key;
  92. }
  93. /**
  94. * Creates a fully qualified rule
  95. *
  96. * @param pluginKey the plugin the rule belongs to
  97. * @param key the key should be unique within a plugin, but it is even more careful for the time being that it is unique
  98. * across the application
  99. * @param name the name displayed in the UI
  100. * @param rulesCategory the ISO category the rule belongs to
  101. * @param priority this is the priority associated to the rule
  102. * @deprecated since 2.3. Use the factory method create()
  103. */
  104. @Deprecated
  105. public Rule(String pluginKey, String key, String name, RulesCategory rulesCategory, RulePriority priority) {
  106. setName(name);
  107. this.key = key;
  108. this.configKey = key;
  109. this.rulesCategory = rulesCategory;
  110. this.priority = priority;
  111. this.pluginName = pluginKey;
  112. }
  113. /**
  114. * @deprecated Use the factory method create()
  115. */
  116. @Deprecated
  117. public Rule(String name, String key, RulesCategory rulesCategory, String pluginName, String description) {
  118. this();
  119. setName(name);
  120. this.key = key;
  121. this.configKey = key;
  122. this.rulesCategory = rulesCategory;
  123. this.pluginName = pluginName;
  124. this.description = description;
  125. }
  126. /**
  127. * @deprecated since 2.3. Use the factory method create()
  128. */
  129. @Deprecated
  130. public Rule(String name, String key, String configKey, RulesCategory rulesCategory, String pluginName, String description) {
  131. this();
  132. setName(name);
  133. this.key = key;
  134. this.configKey = configKey;
  135. this.rulesCategory = rulesCategory;
  136. this.pluginName = pluginName;
  137. this.description = description;
  138. }
  139. public Integer getId() {
  140. return id;
  141. }
  142. /**
  143. * @deprecated visibility should be decreased to protected or package
  144. */
  145. @Deprecated
  146. public void setId(Integer id) {
  147. this.id = id;
  148. }
  149. public String getName() {
  150. return name;
  151. }
  152. /**
  153. * Sets the rule name
  154. */
  155. public Rule setName(String name) {
  156. this.name = removeNewLineCharacters(name);
  157. return this;
  158. }
  159. public String getKey() {
  160. return key;
  161. }
  162. /**
  163. * Sets the rule key
  164. */
  165. public Rule setKey(String key) {
  166. this.key = key;
  167. return this;
  168. }
  169. /**
  170. * @return the rule category
  171. */
  172. public RulesCategory getRulesCategory() {
  173. return rulesCategory;
  174. }
  175. /**
  176. * Sets the rule category
  177. */
  178. public Rule setRulesCategory(RulesCategory rulesCategory) {
  179. this.rulesCategory = rulesCategory;
  180. return this;
  181. }
  182. public String getPluginName() {
  183. return pluginName;
  184. }
  185. /**
  186. * Sets the plugin name the rule belongs to
  187. */
  188. public Rule setPluginName(String pluginName) {
  189. this.pluginName = pluginName;
  190. return this;
  191. }
  192. public String getConfigKey() {
  193. return configKey;
  194. }
  195. /**
  196. * Sets the configuration key
  197. */
  198. public Rule setConfigKey(String configKey) {
  199. this.configKey = configKey;
  200. return this;
  201. }
  202. public String getDescription() {
  203. return description;
  204. }
  205. public Boolean isEnabled() {
  206. return enabled;
  207. }
  208. /**
  209. * Do not call. Used only by sonar.
  210. */
  211. public Rule setEnabled(Boolean b) {
  212. this.enabled = b;
  213. return this;
  214. }
  215. /**
  216. * Sets the rule description
  217. */
  218. public Rule setDescription(String description) {
  219. this.description = StringUtils.strip(description);
  220. return this;
  221. }
  222. public List<RuleParam> getParams() {
  223. return params;
  224. }
  225. public RuleParam getParam(String key) {
  226. for (RuleParam param : params) {
  227. if (StringUtils.equals(key, param.getKey())) {
  228. return param;
  229. }
  230. }
  231. return null;
  232. }
  233. /**
  234. * Sets the rule parameters
  235. */
  236. public Rule setParams(List<RuleParam> params) {
  237. this.params.clear();
  238. for (RuleParam param : params) {
  239. param.setRule(this);
  240. this.params.add(param);
  241. }
  242. return this;
  243. }
  244. public RuleParam createParameter() {
  245. RuleParam parameter = new RuleParam();
  246. parameter.setRule(this);
  247. params.add(parameter);
  248. return parameter;
  249. }
  250. public RuleParam createParameter(String key) {
  251. RuleParam parameter = new RuleParam()
  252. .setKey(key)
  253. .setRule(this);
  254. params.add(parameter);
  255. return parameter;
  256. }
  257. public Integer getCategoryId() {
  258. if (rulesCategory != null) {
  259. return rulesCategory.getId();
  260. }
  261. return null;
  262. }
  263. public RulePriority getPriority() {
  264. return priority;
  265. }
  266. /**
  267. * Sets the rule priority. If null, uses the default priority
  268. */
  269. public Rule setPriority(RulePriority priority) {
  270. if (priority == null) {
  271. this.priority = DEFAULT_PRIORITY;
  272. } else {
  273. this.priority = priority;
  274. }
  275. return this;
  276. }
  277. public String getRepositoryKey() {
  278. return pluginName;
  279. }
  280. public Rule setRepositoryKey(String s) {
  281. this.pluginName = s;
  282. return this;
  283. }
  284. public Rule setUniqueKey(String repositoryKey, String key) {
  285. return setRepositoryKey(repositoryKey).setKey(key).setConfigKey(key);
  286. }
  287. public Cardinality getCardinality() {
  288. return cardinality;
  289. }
  290. public Rule setCardinality(Cardinality c) {
  291. this.cardinality = c;
  292. return this;
  293. }
  294. public Rule getParent() {
  295. return parent;
  296. }
  297. public Rule setParent(Rule parent) {
  298. this.parent = parent;
  299. return this;
  300. }
  301. @Override
  302. public boolean equals(Object obj) {
  303. if (!(obj instanceof Rule)) {
  304. return false;
  305. }
  306. if (this == obj) {
  307. return true;
  308. }
  309. Rule other = (Rule) obj;
  310. return new EqualsBuilder()
  311. .append(pluginName, other.getPluginName())
  312. .append(key, other.getKey())
  313. .isEquals();
  314. }
  315. @Override
  316. public int hashCode() {
  317. return new HashCodeBuilder(17, 37)
  318. .append(pluginName)
  319. .append(key)
  320. .toHashCode();
  321. }
  322. @Override
  323. public String toString() {
  324. return new ToStringBuilder(this)
  325. .append("id", getId())
  326. .append("name", name)
  327. .append("key", key)
  328. .append("configKey", configKey)
  329. .append("categ", rulesCategory)
  330. .append("plugin", pluginName)
  331. .toString();
  332. }
  333. private String removeNewLineCharacters(String text) {
  334. String removedCRLF = StringUtils.remove(text, "\n");
  335. removedCRLF = StringUtils.remove(removedCRLF, "\r");
  336. removedCRLF = StringUtils.remove(removedCRLF, "\n\r");
  337. removedCRLF = StringUtils.remove(removedCRLF, "\r\n");
  338. return removedCRLF;
  339. }
  340. public static Rule create() {
  341. return new Rule();
  342. }
  343. /**
  344. * Create with all required fields
  345. */
  346. public static Rule create(String repositoryKey, String key, String name) {
  347. return new Rule().setUniqueKey(repositoryKey, key).setName(name);
  348. }
  349. }