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.Collection;
25  import java.util.Date;
26  
27  import javax.persistence.Column;
28  import javax.persistence.Entity;
29  import javax.persistence.GeneratedValue;
30  import javax.persistence.GenerationType;
31  import javax.persistence.Id;
32  import javax.persistence.NamedQueries;
33  import javax.persistence.NamedQuery;
34  import javax.persistence.Table;
35  import javax.persistence.UniqueConstraint;
36  import javax.persistence.Version;
37  
38  import org.hibernate.search.annotations.DateBridge;
39  import org.hibernate.search.annotations.Field;
40  import org.hibernate.search.annotations.Index;
41  import org.hibernate.search.annotations.Indexed;
42  import org.hibernate.search.annotations.Resolution;
43  import org.hibernate.search.annotations.Store;
44  
45  /***
46   * General collection of Codelines.
47   */
48  @Entity(name = "View")
49  @Table(name = "TL_VIEW", uniqueConstraints =  {
50      @UniqueConstraint(columnNames =  {
51          "NAME"}
52      )
53  }
54  )
55  @NamedQueries({@NamedQuery(name = QueryNames.VIEW_BY_NAME,query = "select o from View o where o.name = ?" + View.ORDER_BY)
56  })
57  @Indexed
58  public final class View implements Persistable {
59      static final String ORDER_BY = " order by o.name";
60      private static final Class<View> CLASS = View.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 = "DESCRIPTION")
84      @Field(index = Index.TOKENIZED, store = Store.NO)
85      private String description;
86  
87      public Integer getId() {
88          return this.id;
89      }
90  
91      public String getName() {
92          return this.name;
93      }
94  
95      public Date getCreated() {
96          return this.created;
97      }
98  
99      public String getCreatedBy() {
100         return this.createdBy;
101     }
102 
103     public Date getLastModified() {
104         return this.lastModified;
105     }
106 
107     public String getLastModifiedBy() {
108         return this.lastModifiedBy;
109     }
110 
111     public void setId(final Integer id) {
112         this.id = id;
113     }
114 
115     public void setName(final String name) {
116         this.name = name;
117     }
118 
119     public void setCreated(final Date created) {
120         this.created = created;
121     }
122 
123     public void setCreatedBy(final String createdBy) {
124         this.createdBy = createdBy;
125     }
126 
127     public void setLastModified(final Date lastModified) {
128         this.lastModified = lastModified;
129     }
130 
131     public void setLastModifiedBy(final String lastModifiedBy) {
132         this.lastModifiedBy = lastModifiedBy;
133     }
134 
135     public View() {
136     }
137 
138     public View(final String name, final String description) {
139         setName(name);
140         this.description = description;
141     }
142 
143     public String getDescription() {
144         return this.description;
145     }
146 
147     public void setDescription(final String description) {
148         this.description = description;
149     }
150 
151     public Collection<Codeline> getCodelines() {
152         Object[] params = {
153                               this
154         };
155 
156         return JpaDao.getInstance().findByNamedQuery(Codeline.class, QueryNames.CODELINES_ALL_FOR_VIEW, params);
157     }
158 
159     public Collection<Codeline> getActiveCodelines() {
160         Object[] params = {
161                               this
162         };
163 
164         return JpaDao.getInstance().findByNamedQuery(Codeline.class, QueryNames.CODELINES_ACTIVE_FOR_VIEW, params);
165     }
166 
167     public void add() {
168         JpaDao.getInstance().add(this);
169     }
170 
171     public void update() {
172         JpaDao.getInstance().update(this);
173     }
174 
175     public void index() {
176         JpaDao.getInstance().index(this);
177     }
178 
179     public void delete() {
180         TinLizard.getInstance().getScmDao().releaseFiles(getCodelines());
181         JpaDao.getInstance().delete(CLASS, getId());
182     }
183 
184     public static Collection<View> findAll() {
185         return JpaDao.getInstance().findAll(CLASS);
186     }
187 
188     public static View findByName(final String name) {
189         Object[] params = {
190                               name
191         };
192 
193         return JpaDao.getInstance().findSingleByNamedQuery(CLASS, QueryNames.VIEW_BY_NAME, params);
194     }
195 
196     public static View getDefaultView() {
197         View v = View.findByName("Default");
198 
199         if (v == null) {
200             v = new View("Default", "Default View.");
201             v.add();
202         }
203 
204         return v;
205     }
206 
207     public static Collection<View> search(final String query) {
208         String[] fields = {
209                               "name",
210                               "created",
211                               "createdBy",
212                               "lastModified",
213                               "lastModifiedBy",
214                               "description"
215         };
216 
217         return JpaDao.getInstance().findByTextSearch(CLASS, query, fields);
218     }
219 
220     public static void indexAll() {
221         JpaDao.getInstance().indexAll(CLASS);
222     }
223 }