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.qualitymodel;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.apache.commons.lang.builder.ToStringBuilder;
024    import org.sonar.api.rules.Rule;
025    
026    import java.util.ArrayList;
027    import java.util.List;
028    import javax.persistence.*;
029    
030    /**
031     * @since 2.3
032     */
033    @Entity
034    @Table(name = "quality_models")
035    public final class Model implements Comparable<Model> {
036    
037      @Id
038      @Column(name = "id")
039      @GeneratedValue
040      private Integer id;
041    
042      @Column(name = "name", nullable = false, unique = true, length = 100)
043      private String name;
044    
045      @OneToMany(mappedBy = "model", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
046      private List<Characteristic> characteristics = new ArrayList<Characteristic>();
047    
048      Model() {
049      }
050    
051      public static Model create() {
052        return new Model();
053      }
054    
055      public static Model createByName(String s) {
056        return new Model().setName(s);
057      }
058    
059      public Characteristic createCharacteristicByName(String name) {
060        Characteristic c = new Characteristic().setName(name, true);
061        return addCharacteristic(c);
062      }
063    
064      public Characteristic createCharacteristicByKey(String key, String name) {
065        Characteristic c = new Characteristic().setKey(key).setName(name, false);
066        return addCharacteristic(c);
067      }
068    
069      public Characteristic createCharacteristicByRule(Rule rule) {
070        Characteristic c = new Characteristic().setRule(rule);
071        return addCharacteristic(c);
072      }
073    
074      public Integer getId() {
075        return id;
076      }
077    
078      Model setId(Integer id) {
079        this.id = id;
080        return this;
081      }
082    
083      public String getName() {
084        return name;
085      }
086    
087      public List<Characteristic> getRootCharacteristics() {
088        return getCharacteristicsByDepth(Characteristic.ROOT_DEPTH);
089      }
090    
091      public Model setName(String name) {
092        this.name = StringUtils.trim(name);
093        return this;
094      }
095    
096      private Characteristic addCharacteristic(Characteristic c) {
097        c.setModel(this);
098        c.setOrder(characteristics.size()+1);
099        characteristics.add(c);
100        return c;
101      }
102    
103      public List<Characteristic> getCharacteristics() {
104        return characteristics;
105      }
106    
107      public Characteristic getCharacteristicByKey(String key) {
108        for (Characteristic characteristic : getCharacteristics()) {
109          if (StringUtils.equals(key, characteristic.getKey())) {
110            return characteristic;
111          }
112        }
113        return null;
114      }
115    
116      public List<Characteristic> getCharacteristicsByDepth(int depth) {
117        List<Characteristic> result = new ArrayList<Characteristic>();
118        for (Characteristic c : characteristics) {
119          if (c.getDepth()==depth) {
120            result.add(c);
121          }
122        }
123        return result;
124      }
125    
126      public Characteristic getCharacteristicByName(String name) {
127        for (Characteristic characteristic : getCharacteristics()) {
128          if (StringUtils.equals(name, characteristic.getName())) {
129            return characteristic;
130          }
131        }
132        return null;
133      }
134    
135      @Override
136      public boolean equals(Object o) {
137        if (this == o) {
138          return true;
139        }
140        if (o == null || getClass() != o.getClass()) {
141          return false;
142        }
143    
144        Model model = (Model) o;
145        if (name != null ? !name.equals(model.name) : model.name != null) {
146          return false;
147        }
148        return true;
149      }
150    
151      @Override
152      public int hashCode() {
153        return name != null ? name.hashCode() : 0;
154      }
155    
156      @Override
157      public String toString() {
158        return new ToStringBuilder(this)
159            .append("id", id)
160            .append("name", name)
161            .toString();
162      }
163    
164      public int compareTo(Model o) {
165        return getName().compareTo(o.getName());
166      }
167    
168    }