View Javadoc

1   /*
2    * ----------------------------------------------------------------------
3    * Copyright (C) 2009 Enrique Lara (k957@68k.org)
4    *
5    * TinLizard is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public License
7    * as published by the Free Software Foundation; either version 3.0
8    * of the License, or (at your option) any later version.
9    *
10   * TinLizard is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public License
16   * along with TinLizard. If not, see http://www.gnu.org/licenses/.
17   * ----------------------------------------------------------------------
18   */
19  package tinlizard.model;
20  
21  import tinlizard.dao.jpa.JpaDao;
22  import tinlizard.dao.jpa.Persistable;
23  
24  import java.util.ArrayList;
25  import java.util.Collection;
26  import java.util.Date;
27  import java.util.Iterator;
28  
29  import javax.persistence.Column;
30  import javax.persistence.Entity;
31  import javax.persistence.EntityManager;
32  import javax.persistence.EntityTransaction;
33  import javax.persistence.GeneratedValue;
34  import javax.persistence.GenerationType;
35  import javax.persistence.Id;
36  import javax.persistence.JoinColumn;
37  import javax.persistence.ManyToOne;
38  import javax.persistence.NamedQueries;
39  import javax.persistence.NamedQuery;
40  import javax.persistence.Table;
41  import javax.persistence.Version;
42  
43  import org.hibernate.search.annotations.DateBridge;
44  import org.hibernate.search.annotations.Field;
45  import org.hibernate.search.annotations.Index;
46  import org.hibernate.search.annotations.Indexed;
47  import org.hibernate.search.annotations.Resolution;
48  import org.hibernate.search.annotations.Store;
49  
50  /***
51   * A Dependency declared as such in the pom.xml of any Codeline in TinLizard.
52   */
53  @Entity(name = "Dependency")
54  @Table(name = "TL_DEPENDENCY")
55  @NamedQueries({@NamedQuery(name = QueryNames.DEPENDENCIES_BY_CODELINE,query = "select o from Dependency o left join o.codeline pb where pb = ?" + Dependency.ORDER_BY)
56  })
57  @Indexed
58  public final class Dependency implements Persistable {
59      static final String ORDER_BY = " order by o.groupId, o.artifactId";
60      private static final Class<Dependency> CLASS = Dependency.class;
61      @Id
62      @GeneratedValue(strategy = GenerationType.AUTO)
63      @Column(name = "ID")
64      private Integer id;
65      @Column(name = "NAME", nullable = false)
66      @Field(index = Index.TOKENIZED, store = Store.NO)
67      private String name;
68      @Column(name = "CREATED", nullable = false)
69      @Field(index = Index.UN_TOKENIZED, store = Store.YES)
70      @DateBridge(resolution = Resolution.SECOND)
71      private Date created;
72      @Column(name = "CREATED_BY", nullable = false)
73      @Field(index = Index.TOKENIZED, store = Store.NO)
74      private String createdBy;
75      @Version
76      @Column(name = "LAST_MODIFIED")
77      @Field(index = Index.UN_TOKENIZED, store = Store.YES)
78      @DateBridge(resolution = Resolution.SECOND)
79      private Date lastModified;
80      @Column(name = "LAST_MODIFIED_BY")
81      @Field(index = Index.TOKENIZED, store = Store.NO)
82      private String lastModifiedBy;
83      @Column(name = "GROUP_ID", nullable = false)
84      @Field(index = Index.TOKENIZED, store = Store.NO)
85      private String groupId;
86      @Column(name = "ARTIFACT_ID", nullable = false)
87      @Field(index = Index.TOKENIZED, store = Store.NO)
88      private String artifactId;
89      @Column(name = "CLASSIFIER")
90      @Field(index = Index.TOKENIZED, store = Store.NO)
91      private String classifier;
92      @Column(name = "VERSION")
93      @Field(index = Index.TOKENIZED, store = Store.NO)
94      private String version;
95      @ManyToOne
96      @JoinColumn(name = "CODELINE_ID") //XXX, nullable = false)
97  
98      private Codeline codeline;
99  
100     public Integer getId() {
101         return this.id;
102     }
103 
104     public String getName() {
105         return this.name;
106     }
107 
108     public Date getCreated() {
109         return this.created;
110     }
111 
112     public String getCreatedBy() {
113         return this.createdBy;
114     }
115 
116     public Date getLastModified() {
117         return this.lastModified;
118     }
119 
120     public String getLastModifiedBy() {
121         return this.lastModifiedBy;
122     }
123 
124     public void setId(final Integer id) {
125         this.id = id;
126     }
127 
128     public void setName(final String name) {
129         this.name = name;
130     }
131 
132     public void setCreated(final Date created) {
133         this.created = created;
134     }
135 
136     public void setCreatedBy(final String createdBy) {
137         this.createdBy = createdBy;
138     }
139 
140     public void setLastModified(final Date lastModified) {
141         this.lastModified = lastModified;
142     }
143 
144     public void setLastModifiedBy(final String lastModifiedBy) {
145         this.lastModifiedBy = lastModifiedBy;
146     }
147 
148     public String getGroupId() {
149         return this.groupId;
150     }
151 
152     public String getArtifactId() {
153         return artifactId;
154     }
155 
156     public String getClassifier() {
157         return this.classifier;
158     }
159 
160     public String getVersion() {
161         return this.version;
162     }
163 
164     public Codeline getCodeline() {
165         return this.codeline;
166     }
167 
168     void setName() {
169         setName(groupId + "." + artifactId);
170     }
171 
172     public void setGroupId(final String groupId) {
173         this.groupId = groupId;
174         setName();
175     }
176 
177     public void setArtifactId(final String artifactId) {
178         this.artifactId = artifactId;
179         setName();
180     }
181 
182     public void setClassifier(final String classifier) {
183         this.classifier = classifier;
184         setName();
185     }
186 
187     public void setVersion(final String version) {
188         this.version = version;
189     }
190 
191     public void setCodeline(final Codeline codeline) {
192         this.codeline = codeline;
193         setName();
194     }
195 
196     public Collection<Codeline> findAllConsumers(final boolean useVersion) {
197         if (useVersion) {
198             Object[] params = {
199                                   getGroupId(),
200                                   getArtifactId(),
201                                   getVersion()
202             };
203 
204             return JpaDao.getInstance().findByNamedQuery(Codeline.class, QueryNames.CODELINES_DEPENDENT_ON_ARTIFACT, params);
205         } else {
206             Object[] params = {
207                                   getGroupId(),
208                                   getArtifactId()
209             };
210 
211             return JpaDao.getInstance().findByNamedQuery(Codeline.class, QueryNames.CODELINES_DEPENDENT_ON_ARTIFACT_ANY_VERSION, params);
212         }
213     }
214 
215     public Collection<Codeline> findAllDefiningDependency(final boolean useVersion) {
216         if (useVersion) {
217             Object[] params = {
218                                   getGroupId(),
219                                   getArtifactId(),
220                                   getVersion()
221             };
222 
223             return JpaDao.getInstance().findByNamedQuery(Codeline.class, QueryNames.CODELINES_DEFINING_ARTIFACT, params);
224         } else {
225             Object[] params = {
226                                   getGroupId(),
227                                   getArtifactId()
228             };
229 
230             return JpaDao.getInstance().findByNamedQuery(Codeline.class, QueryNames.CODELINES_DEFINING_ARTIFACT_ANY_VERSION, params);
231         }
232     }
233 
234     public void add() {
235         JpaDao.getInstance().add(this);
236     }
237 
238     public void update() {
239         JpaDao.getInstance().update(this);
240     }
241 
242     public void delete() {
243         JpaDao.getInstance().delete(CLASS, getId());
244     }
245 
246     public void index() {
247         JpaDao.getInstance().index(this);
248     }
249 
250     @SuppressWarnings("unchecked")
251     public static Collection<String> findAllGroupIds() {
252         EntityManager em = JpaDao.getInstance().getEm();
253         EntityTransaction tx = em.getTransaction();
254         tx.begin();
255 
256         Collection<String> groupIds = null;
257 
258         try {
259             groupIds = new ArrayList<String>();
260 
261             Collection results = em.createQuery("select distinct o.groupId from Dependency o").getResultList();
262 
263             for (Iterator it = results.iterator(); it.hasNext();) {
264                 Object obj = it.next();
265                 groupIds.add(obj.toString());
266             }
267 
268             tx.commit();
269         } catch (Exception e) {
270             JpaDao.getInstance().handleError(tx, "Delete Error", e);
271         }
272 
273         return groupIds;
274     }
275 
276     @SuppressWarnings("unchecked")
277     public static Collection<String> findAllArtifactIds(final String groupId) {
278         EntityManager em = JpaDao.getInstance().getEm();
279         EntityTransaction tx = em.getTransaction();
280         tx.begin();
281 
282         Collection<String> artifactIds = null;
283 
284         try {
285             artifactIds = new ArrayList<String>();
286 
287             Collection results = em.createQuery("select distinct o.artifactId from Dependency o where groupId = ?").setParameter(1, groupId).getResultList();
288 
289             for (Iterator it = results.iterator(); it.hasNext();) {
290                 Object obj = it.next();
291                 artifactIds.add(obj.toString());
292             }
293 
294             tx.commit();
295         } catch (Exception e) {
296             JpaDao.getInstance().handleError(tx, "Delete Error", e);
297         }
298 
299         return artifactIds;
300     }
301 
302     public static Collection<Dependency> search(final String query) {
303         String[] fields = {
304                               "name",
305                               "created",
306                               "createdBy",
307                               "lastModified",
308                               "lastModifiedBy",
309                               "groupId",
310                               "artifactId",
311                               "classifier",
312                               "version"
313         };
314 
315         return JpaDao.getInstance().findByTextSearch(CLASS, query, fields);
316     }
317 
318     public static void indexAll() {
319         JpaDao.getInstance().indexAll(CLASS);
320     }
321 }