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.

Characteristic.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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.qualitymodel;
  21. import org.apache.commons.lang.StringUtils;
  22. import org.apache.commons.lang.builder.ToStringBuilder;
  23. import org.apache.commons.lang.builder.ToStringStyle;
  24. import org.hibernate.annotations.Sort;
  25. import org.hibernate.annotations.SortType;
  26. import org.sonar.api.rules.Rule;
  27. import javax.persistence.*;
  28. import java.util.ArrayList;
  29. import java.util.Collections;
  30. import java.util.List;
  31. /**
  32. * @since 2.3
  33. */
  34. @Entity
  35. @Table(name = "characteristics")
  36. public final class Characteristic implements Comparable<Characteristic> {
  37. public static final int ROOT_DEPTH = 1;
  38. @Id
  39. @Column(name = "id")
  40. @GeneratedValue
  41. private Integer id;
  42. @Column(name = "kee", nullable = true, length = 100)
  43. private String key;
  44. @Column(name = "name", nullable = true, length = 100)
  45. private String name;
  46. @Column(name = "depth")
  47. private int depth = ROOT_DEPTH;
  48. @Column(name = "characteristic_order")
  49. private int order = 0;
  50. @ManyToOne(fetch = FetchType.EAGER)
  51. @JoinColumn(name = "quality_model_id")
  52. private Model model;
  53. @ManyToOne(fetch = FetchType.EAGER)
  54. @JoinColumn(name = "rule_id")
  55. private Rule rule;
  56. @Column(name = "description", nullable = true, length = 4000)
  57. private String description;
  58. @Column(name = "enabled", updatable = true, nullable = true)
  59. private Boolean enabled = Boolean.TRUE;
  60. @ManyToMany
  61. @JoinTable(
  62. name = "characteristic_edges",
  63. joinColumns = @JoinColumn(name = "child_id", referencedColumnName = "id"),
  64. inverseJoinColumns = @JoinColumn(name = "parent_id",
  65. referencedColumnName = "id")
  66. )
  67. private List<Characteristic> parents = new ArrayList<Characteristic>();
  68. @Sort(type = SortType.NATURAL)
  69. @ManyToMany(mappedBy = "parents", cascade = CascadeType.ALL)
  70. private List<Characteristic> children = new ArrayList<Characteristic>();
  71. @OneToMany(mappedBy = "characteristic", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE})
  72. private List<CharacteristicProperty> properties = new ArrayList<CharacteristicProperty>();
  73. Characteristic() {
  74. }
  75. public Integer getId() {
  76. return id;
  77. }
  78. Characteristic setId(Integer id) {
  79. this.id = id;
  80. return this;
  81. }
  82. public String getKey() {
  83. return key;
  84. }
  85. Characteristic setKey(String s) {
  86. this.key = StringUtils.trimToEmpty(s);
  87. return this;
  88. }
  89. public String getName() {
  90. return name;
  91. }
  92. Characteristic setName(String s, boolean asKey) {
  93. this.name = StringUtils.trimToEmpty(s);
  94. if (asKey) {
  95. this.key = StringUtils.upperCase(this.name);
  96. this.key = StringUtils.replaceChars(this.key, ' ', '_');
  97. }
  98. return this;
  99. }
  100. public Model getModel() {
  101. return model;
  102. }
  103. Characteristic setModel(Model model) {
  104. this.model = model;
  105. return this;
  106. }
  107. public Rule getRule() {
  108. return rule;
  109. }
  110. public Characteristic setRule(Rule r) {
  111. this.rule = r;
  112. return this;
  113. }
  114. public Boolean getEnabled() {
  115. return enabled;
  116. }
  117. public Characteristic setEnabled(Boolean b) {
  118. this.enabled = b;
  119. return this;
  120. }
  121. public Characteristic addChildren(Characteristic... list) {
  122. if (list != null) {
  123. for (Characteristic c : list) {
  124. addChild(c);
  125. }
  126. }
  127. return this;
  128. }
  129. public Characteristic addChild(Characteristic child) {
  130. propagateDepth(child, depth + 1);
  131. child.addParents(this);
  132. child.setModel(model);
  133. children.add(child);
  134. return this;
  135. }
  136. private static void propagateDepth(Characteristic characteristic, int depth) {
  137. characteristic.setDepth(depth);
  138. for (Characteristic child : characteristic.getChildren()) {
  139. propagateDepth(child, depth + 1);
  140. }
  141. }
  142. Characteristic addParents(Characteristic... pc) {
  143. if (pc != null) {
  144. Collections.addAll(this.parents, pc);
  145. }
  146. return this;
  147. }
  148. public List<Characteristic> getParents() {
  149. return parents;
  150. }
  151. public Characteristic getParent(String name) {
  152. for (Characteristic parent : parents) {
  153. if (StringUtils.equals(parent.getName(), name)) {
  154. return parent;
  155. }
  156. }
  157. return null;
  158. }
  159. /**
  160. * Children sorted by insertion order
  161. */
  162. public List<Characteristic> getChildren() {
  163. return children;
  164. }
  165. public Characteristic getChild(String name) {
  166. for (Characteristic child : children) {
  167. if (StringUtils.equals(child.getName(), name)) {
  168. return child;
  169. }
  170. }
  171. return null;
  172. }
  173. public int getDepth() {
  174. return depth;
  175. }
  176. public boolean isRoot() {
  177. return depth == ROOT_DEPTH;
  178. }
  179. Characteristic setDepth(int i) {
  180. this.depth = i;
  181. return this;
  182. }
  183. public int getOrder() {
  184. return order;
  185. }
  186. Characteristic setOrder(int i) {
  187. this.order = i;
  188. return this;
  189. }
  190. public String getDescription() {
  191. return description;
  192. }
  193. public Characteristic setDescription(String s) {
  194. this.description = s;
  195. return this;
  196. }
  197. public CharacteristicProperty setProperty(String key, String value) {
  198. return createProperty(key).setValue(value);
  199. }
  200. public CharacteristicProperty setProperty(String key, double value) {
  201. return createProperty(key).setValue(value);
  202. }
  203. public CharacteristicProperty createProperty(String key) {
  204. CharacteristicProperty property = new CharacteristicProperty(this, key);
  205. properties.add(property);
  206. return property;
  207. }
  208. @Override
  209. public boolean equals(Object o) {
  210. if (this == o) {
  211. return true;
  212. }
  213. if (o == null || getClass() != o.getClass()) {
  214. return false;
  215. }
  216. Characteristic that = (Characteristic) o;
  217. if (key != null ? !key.equals(that.key) : that.key != null) {
  218. return false;
  219. }
  220. if (rule != null ? !rule.equals(that.rule) : that.rule != null) {
  221. return false;
  222. }
  223. return true;
  224. }
  225. @Override
  226. public int hashCode() {
  227. int result = key != null ? key.hashCode() : 0;
  228. result = 31 * result + (rule != null ? rule.hashCode() : 0);
  229. return result;
  230. }
  231. @Override
  232. public String toString() {
  233. return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
  234. .append("key", key)
  235. .append("name", name)
  236. .append("rule", rule)
  237. .append("description", description)
  238. .toString();
  239. }
  240. public int compareTo(Characteristic o) {
  241. if (equals(o)) {
  242. return 0;
  243. }
  244. return order - o.order;
  245. }
  246. }