001 /* 002 * ---------------------------------------------------------------------- 003 * Copyright (C) 2009 Enrique Lara (k957@68k.org) 004 * 005 * TinLizard is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public License 007 * as published by the Free Software Foundation; either version 3.0 008 * of the License, or (at your option) any later version. 009 * 010 * TinLizard is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public License 016 * along with TinLizard. If not, see http://www.gnu.org/licenses/. 017 * ---------------------------------------------------------------------- 018 */ 019 package tinlizard.model; 020 021 import tinlizard.dao.jpa.JpaDao; 022 import tinlizard.dao.jpa.Persistable; 023 024 import java.util.ArrayList; 025 import java.util.Collection; 026 import java.util.Date; 027 import java.util.Iterator; 028 029 import javax.persistence.Column; 030 import javax.persistence.Entity; 031 import javax.persistence.EntityManager; 032 import javax.persistence.EntityTransaction; 033 import javax.persistence.GeneratedValue; 034 import javax.persistence.GenerationType; 035 import javax.persistence.Id; 036 import javax.persistence.JoinColumn; 037 import javax.persistence.ManyToOne; 038 import javax.persistence.NamedQueries; 039 import javax.persistence.NamedQuery; 040 import javax.persistence.Table; 041 import javax.persistence.Version; 042 043 import org.hibernate.search.annotations.DateBridge; 044 import org.hibernate.search.annotations.Field; 045 import org.hibernate.search.annotations.Index; 046 import org.hibernate.search.annotations.Indexed; 047 import org.hibernate.search.annotations.Resolution; 048 import org.hibernate.search.annotations.Store; 049 050 /** 051 * A Dependency declared as such in the pom.xml of any Codeline in TinLizard. 052 */ 053 @Entity(name = "Dependency") 054 @Table(name = "TL_DEPENDENCY") 055 @NamedQueries({@NamedQuery(name = QueryNames.DEPENDENCIES_BY_CODELINE,query = "select o from Dependency o left join o.codeline pb where pb = ?" + Dependency.ORDER_BY) 056 }) 057 @Indexed 058 public final class Dependency implements Persistable { 059 static final String ORDER_BY = " order by o.groupId, o.artifactId"; 060 private static final Class<Dependency> CLASS = Dependency.class; 061 @Id 062 @GeneratedValue(strategy = GenerationType.AUTO) 063 @Column(name = "ID") 064 private Integer id; 065 @Column(name = "NAME", nullable = false) 066 @Field(index = Index.TOKENIZED, store = Store.NO) 067 private String name; 068 @Column(name = "CREATED", nullable = false) 069 @Field(index = Index.UN_TOKENIZED, store = Store.YES) 070 @DateBridge(resolution = Resolution.SECOND) 071 private Date created; 072 @Column(name = "CREATED_BY", nullable = false) 073 @Field(index = Index.TOKENIZED, store = Store.NO) 074 private String createdBy; 075 @Version 076 @Column(name = "LAST_MODIFIED") 077 @Field(index = Index.UN_TOKENIZED, store = Store.YES) 078 @DateBridge(resolution = Resolution.SECOND) 079 private Date lastModified; 080 @Column(name = "LAST_MODIFIED_BY") 081 @Field(index = Index.TOKENIZED, store = Store.NO) 082 private String lastModifiedBy; 083 @Column(name = "GROUP_ID", nullable = false) 084 @Field(index = Index.TOKENIZED, store = Store.NO) 085 private String groupId; 086 @Column(name = "ARTIFACT_ID", nullable = false) 087 @Field(index = Index.TOKENIZED, store = Store.NO) 088 private String artifactId; 089 @Column(name = "CLASSIFIER") 090 @Field(index = Index.TOKENIZED, store = Store.NO) 091 private String classifier; 092 @Column(name = "VERSION") 093 @Field(index = Index.TOKENIZED, store = Store.NO) 094 private String version; 095 @ManyToOne 096 @JoinColumn(name = "CODELINE_ID") //XXX, nullable = false) 097 098 private Codeline codeline; 099 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 }