123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- /*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2009 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * Sonar is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
- package org.sonar.api.qualitymodel;
-
- import org.apache.commons.lang.StringUtils;
- import org.apache.commons.lang.builder.ToStringBuilder;
- import org.apache.commons.lang.builder.ToStringStyle;
- import org.hibernate.annotations.Sort;
- import org.hibernate.annotations.SortType;
- import org.sonar.api.rules.Rule;
-
- import javax.persistence.*;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
-
- /**
- * @since 2.3
- */
- @Entity
- @Table(name = "characteristics")
- public final class Characteristic implements Comparable<Characteristic> {
-
- public static final int ROOT_DEPTH = 1;
-
- @Id
- @Column(name = "id")
- @GeneratedValue
- private Integer id;
-
- @Column(name = "kee", nullable = true, length = 100)
- private String key;
-
- @Column(name = "name", nullable = true, length = 100)
- private String name;
-
- @Column(name = "depth")
- private int depth = ROOT_DEPTH;
-
- @Column(name = "characteristic_order")
- private int order = 0;
-
- @ManyToOne(fetch = FetchType.EAGER)
- @JoinColumn(name = "quality_model_id")
- private Model model;
-
- @ManyToOne(fetch = FetchType.EAGER)
- @JoinColumn(name = "rule_id")
- private Rule rule;
-
- @Column(name = "description", nullable = true, length = 4000)
- private String description;
-
- @Column(name = "enabled", updatable = true, nullable = true)
- private Boolean enabled = Boolean.TRUE;
-
- @ManyToMany
- @JoinTable(
- name = "characteristic_edges",
- joinColumns = @JoinColumn(name = "child_id", referencedColumnName = "id"),
- inverseJoinColumns = @JoinColumn(name = "parent_id",
- referencedColumnName = "id")
- )
- private List<Characteristic> parents = new ArrayList<Characteristic>();
-
- @Sort(type = SortType.NATURAL)
- @ManyToMany(mappedBy = "parents", cascade = CascadeType.ALL)
- private List<Characteristic> children = new ArrayList<Characteristic>();
-
- @OneToMany(mappedBy = "characteristic", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE})
- private List<CharacteristicProperty> properties = new ArrayList<CharacteristicProperty>();
-
- Characteristic() {
- }
-
- public Integer getId() {
- return id;
- }
-
- Characteristic setId(Integer id) {
- this.id = id;
- return this;
- }
-
- public String getKey() {
- return key;
- }
-
- Characteristic setKey(String s) {
- this.key = StringUtils.trimToEmpty(s);
- return this;
- }
-
- public String getName() {
- return name;
- }
-
- Characteristic setName(String s, boolean asKey) {
- this.name = StringUtils.trimToEmpty(s);
- if (asKey) {
- this.key = StringUtils.upperCase(this.name);
- this.key = StringUtils.replaceChars(this.key, ' ', '_');
- }
- return this;
- }
-
- public Model getModel() {
- return model;
- }
-
- Characteristic setModel(Model model) {
- this.model = model;
- return this;
- }
-
- public Rule getRule() {
- return rule;
- }
-
- public Characteristic setRule(Rule r) {
- this.rule = r;
- return this;
- }
-
- public Boolean getEnabled() {
- return enabled;
- }
-
- public Characteristic setEnabled(Boolean b) {
- this.enabled = b;
- return this;
- }
-
- public Characteristic addChildren(Characteristic... list) {
- if (list != null) {
- for (Characteristic c : list) {
- addChild(c);
- }
- }
- return this;
- }
-
- public Characteristic addChild(Characteristic child) {
- propagateDepth(child, depth + 1);
- child.addParents(this);
- child.setModel(model);
- children.add(child);
- return this;
- }
-
- private static void propagateDepth(Characteristic characteristic, int depth) {
- characteristic.setDepth(depth);
- for (Characteristic child : characteristic.getChildren()) {
- propagateDepth(child, depth + 1);
- }
- }
-
- Characteristic addParents(Characteristic... pc) {
- if (pc != null) {
- Collections.addAll(this.parents, pc);
- }
- return this;
- }
-
- public List<Characteristic> getParents() {
- return parents;
- }
-
- public Characteristic getParent(String name) {
- for (Characteristic parent : parents) {
- if (StringUtils.equals(parent.getName(), name)) {
- return parent;
- }
- }
- return null;
- }
-
- /**
- * Children sorted by insertion order
- */
- public List<Characteristic> getChildren() {
- return children;
- }
-
- public Characteristic getChild(String name) {
- for (Characteristic child : children) {
- if (StringUtils.equals(child.getName(), name)) {
- return child;
- }
- }
- return null;
- }
-
- public int getDepth() {
- return depth;
- }
-
- public boolean isRoot() {
- return depth == ROOT_DEPTH;
- }
-
- Characteristic setDepth(int i) {
- this.depth = i;
- return this;
- }
-
- public int getOrder() {
- return order;
- }
-
- Characteristic setOrder(int i) {
- this.order = i;
- return this;
- }
-
- public String getDescription() {
- return description;
- }
-
- public Characteristic setDescription(String s) {
- this.description = s;
- return this;
- }
-
- public CharacteristicProperty setProperty(String key, String value) {
- return addProperty(CharacteristicProperty.create(key).setValue(value));
- }
-
- public CharacteristicProperty setProperty(String key, double value) {
- return addProperty(CharacteristicProperty.create(key).setValue(value));
- }
-
- public CharacteristicProperty addProperty(CharacteristicProperty property) {
- property.setCharacteristic(this);
- properties.add(property);
- return property;
- }
-
- public CharacteristicProperty getProperty(String key) {
- for (CharacteristicProperty property : properties) {
- if (StringUtils.equals(key, property.getKey())) {
- return property;
- }
- }
- return null;
- }
-
- public String getPropertyValueAsString(String key) {
- CharacteristicProperty property = getProperty(key);
- return property != null ? property.getValue() : null;
- }
-
- public Double getPropertyValueAsDouble(String key) {
- CharacteristicProperty property = getProperty(key);
- return property != null ? property.getValueAsDouble() : null;
- }
-
- @Override
- public boolean equals(Object o) {
- if (this == o) {
- return true;
- }
- if (o == null || getClass() != o.getClass()) {
- return false;
- }
-
- Characteristic that = (Characteristic) o;
- if (key != null ? !key.equals(that.key) : that.key != null) {
- return false;
- }
- if (rule != null ? !rule.equals(that.rule) : that.rule != null) {
- return false;
- }
- return true;
- }
-
- @Override
- public int hashCode() {
- int result = key != null ? key.hashCode() : 0;
- result = 31 * result + (rule != null ? rule.hashCode() : 0);
- return result;
- }
-
- @Override
- public String toString() {
- return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
- .append("key", key)
- .append("name", name)
- .append("rule", rule)
- .append("description", description)
- .toString();
- }
-
- public int compareTo(Characteristic o) {
- if (equals(o)) {
- return 0;
- }
- return order - o.order;
- }
- }
|