View Javadoc

1   /*
2    * Sonar, entreprise quality control tool.
3    * Copyright (C) 2007-2008 Hortis-GRC SA
4    * mailto:be_agile HAT hortis DOT ch
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 {library}; if not, write to the Free Software
18   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
19   */
20  package ch.hortis.sonar.model;
21  
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.commons.lang.builder.EqualsBuilder;
24  import org.apache.commons.lang.builder.HashCodeBuilder;
25  import org.apache.commons.lang.builder.ToStringBuilder;
26  import org.hibernate.annotations.BatchSize;
27  import org.hibernate.annotations.Cache;
28  import org.hibernate.annotations.CacheConcurrencyStrategy;
29  
30  import javax.persistence.*;
31  import java.io.Serializable;
32  import java.util.ArrayList;
33  import java.util.List;
34  
35  @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
36  @Entity
37  @Table(name = "projects")
38  @NamedQueries(
39      {
40      @NamedQuery(name = MavenProject.SQL_SELECT_BY_ID, query = "SELECT p FROM MavenProject p WHERE p.id = :id"),
41      @NamedQuery(name = MavenProject.SQL_SELECT_DISABLED_PROJECTS, query = "SELECT p FROM MavenProject p WHERE p.enabled=false AND " +
42      		"SCOPE='" + MavenProject.SCOPE_PROJECT + "' AND " +
43      		"p.rootId IS NULL")
44  }
45  )
46  public class MavenProject implements Serializable {
47    
48    public static final String SCOPE_PROJECT = "PRJ";
49    public static final String SCOPE_PACKAGE = "PAC";
50    public static final String SCOPE_CLASS = "CLA";
51  
52    public final static String SQL_SELECT_BY_ID = "MavenProject.selectById";
53    public static final String SQL_SELECT_DISABLED_PROJECTS = "MavenProject.selectDisabledProjects";
54  
55    private static final long serialVersionUID = -7747333646654989328L;
56    public static final int DESCRIPTION_COLUMN_SIZE = 2000;
57    public static final int NAME_COLUMN_SIZE = 256;
58    public static final int KEY_SIZE = 230;
59  
60    @Id
61    @Column(name = "id")
62    @SequenceGenerator(name = "PROJECTS_SEQ", sequenceName = "PROJECTS_SEQ")
63    @GeneratedValue(strategy = GenerationType.AUTO, generator = "PROJECTS_SEQ")
64    private Integer id;
65  
66    @Column(name = "name", updatable = true, nullable = true, length = NAME_COLUMN_SIZE)
67    private String name;
68  
69    @Column(name = "description", updatable = true, nullable = true, length = DESCRIPTION_COLUMN_SIZE)
70    private String description;
71  
72    @Column(name = "enabled", updatable = true, nullable = false)
73    private Boolean enabled = Boolean.TRUE;
74  
75    @Column(name = "scope", updatable = false, nullable = false, length = 3)
76    private String scope;
77  
78    @Column(name = "qualifier", updatable = false, nullable = true, length = 3)
79    private String qualifier;
80  
81  
82    @Column(name = "kee", updatable = false, nullable = false, length = KEY_SIZE)
83    private String key;
84  
85    @Column(name = "root_id", updatable = true, nullable = true)
86    private Integer rootId;
87  
88    @OneToMany(mappedBy = "mavenProject", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE})
89    @BatchSize(size = 8)
90    private List<ProjectLink> projectLinks = new ArrayList<ProjectLink>();
91  
92  
93    public MavenProject() {
94    }
95  
96    public MavenProject(String scope, String key, String qualifier, Integer rootId, String name) {
97      this.scope = scope;
98      this.key = key;
99      this.rootId = rootId;
100     this.name = name;
101     this.qualifier = qualifier;
102   }
103 
104   public static MavenProject newMavenProject(String groupId, String artifactId, String branch, String name) {
105     MavenProject project = new MavenProject();
106     project.setScope(SCOPE_PROJECT);
107     project.setMavenId(groupId, artifactId, branch);
108     project.setName(name);
109     return project;
110   }
111 
112   public static String toMavenKey(String groupId, String artifactId, String branch) {
113     StringBuilder sb = new StringBuilder().append(groupId).append(":").append(artifactId);
114     if (branch != null) {
115       sb.append(":").append(branch);
116     }
117     return sb.toString();
118   }
119 
120   public String[] getMavenId() {
121     if (SCOPE_PROJECT.equals(scope)) {
122       return StringUtils.split(key, ":");
123     }
124     return null;
125   }
126 
127   public void setMavenId(String groupId, String artifactId, String branch) {
128     key = toMavenKey(groupId, artifactId, branch);
129     if (key.length() > KEY_SIZE) {
130       throw new IllegalArgumentException("key is too long, max is " + KEY_SIZE + " characters : " + key);
131     }
132 
133   }
134 
135   public String getMavenGroupId() {
136     String[] mavenId = getMavenId();
137     if (mavenId != null) {
138       return mavenId[0];
139     }
140     return null;
141   }
142 
143   public String getMavenArtifactId() {
144     String[] mavenId = getMavenId();
145     if (mavenId != null) {
146       return mavenId[1];
147     }
148     return null;
149   }
150 
151   public String getMavenBranch() {
152     String[] mavenId = getMavenId();
153     if (mavenId != null && mavenId.length > 2) {
154       return mavenId[2];
155     }
156     return null;
157   }
158 
159   public List<ProjectLink> getProjectLinks() {
160     return projectLinks;
161   }
162 
163   public void setProjectLinks(List<ProjectLink> projectLinks) {
164     this.projectLinks = projectLinks;
165   }
166 
167   public String getDescription() {
168     return description;
169   }
170 
171   public void setDescription(String description) {
172     this.description = StringUtils.abbreviate(description, DESCRIPTION_COLUMN_SIZE);
173   }
174 
175   public String getName() {
176     return name;
177   }
178 
179   public void setName(String name) {
180     this.name = StringUtils.abbreviate(name, NAME_COLUMN_SIZE);
181   }
182 
183   public Integer getId() {
184     return id;
185   }
186 
187   public void setId(Integer id) {
188     this.id = id;
189   }
190 
191   public Boolean getEnabled() {
192     return enabled;
193   }
194 
195   public void setEnabled(Boolean enabled) {
196     this.enabled = enabled;
197   }
198 
199   public String getScope() {
200     return scope;
201   }
202 
203   public void setScope(String scope) {
204     this.scope = scope;
205   }
206 
207   public String getKey() {
208     return key;
209   }
210 
211   public void setKey(String key) {
212     this.key = key;
213   }
214 
215   public Integer getRootId() {
216     return rootId;
217   }
218 
219   public void setRootId(Integer rootId) {
220     this.rootId = rootId;
221   }
222 
223   public String getQualifier() {
224     return qualifier;
225   }
226 
227   public void setQualifier(String qualifier) {
228     this.qualifier = qualifier;
229   }
230 
231   public ProjectLink getProjectLinkByType(String type) {
232     if (type == null) {
233       return null;
234     }
235     for (ProjectLink projectLink : projectLinks) {
236       if (type.equals(projectLink.getType())) {
237         return projectLink;
238       }
239     }
240     return null;
241   }
242 
243   public boolean equals(Object obj) {
244     if (!(obj instanceof MavenProject)) {
245       return false;
246     }
247     if (this == obj) {
248       return true;
249     }
250     MavenProject other = (MavenProject) obj;
251     return new EqualsBuilder()
252         .append(scope, other.scope)
253         .append(key, other.key)
254         .append(enabled, other.enabled)
255         .append(rootId, other.rootId)
256         .append(qualifier, other.qualifier)
257         .isEquals();
258   }
259 
260   public int hashCode() {
261     return new HashCodeBuilder(17, 37)
262         .append(scope)
263         .append(key)
264         .append(enabled)
265         .append(qualifier)
266         .append(rootId)
267         .toHashCode();
268   }
269 
270   public String toString() {
271     return new ToStringBuilder(this)
272         .append("id", id)
273         .append("scope", scope)
274         .append("key", key)
275         .append("qualifier", qualifier)
276         .append("name", name)
277         .append("enabled", enabled)
278         .append("rootId", rootId)
279         .toString();
280   }
281 }