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   * Describes what kinds of changes are expected on a Codeline.
47   */
48  @Entity(name = "Policy")
49  @Table(name = "TL_POLICY", uniqueConstraints =  {
50      @UniqueConstraint(columnNames =  {
51          "NAME"}
52      )
53  }
54  )
55  @NamedQueries({@NamedQuery(name = QueryNames.POLICY_BY_NAME,query = "select o from Policy o where o.name = ?" + Policy.ORDER_BY)
56  })
57  @Indexed
58  public final class Policy implements Persistable {
59      static final String ORDER_BY = " order by o.name";
60      private static final Class<Policy> CLASS = Policy.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      @Column(name = "ACTIVE_FLAG", nullable = false)
87      private Boolean active = false;
88  
89      public Integer getId() {
90          return this.id;
91      }
92  
93      public String getName() {
94          return this.name;
95      }
96  
97      public Date getCreated() {
98          return this.created;
99      }
100 
101     public String getCreatedBy() {
102         return this.createdBy;
103     }
104 
105     public Date getLastModified() {
106         return this.lastModified;
107     }
108 
109     public String getLastModifiedBy() {
110         return this.lastModifiedBy;
111     }
112 
113     public void setId(final Integer id) {
114         this.id = id;
115     }
116 
117     public void setName(final String name) {
118         this.name = name;
119     }
120 
121     public void setCreated(final Date created) {
122         this.created = created;
123     }
124 
125     public void setCreatedBy(final String createdBy) {
126         this.createdBy = createdBy;
127     }
128 
129     public void setLastModified(final Date lastModified) {
130         this.lastModified = lastModified;
131     }
132 
133     public void setLastModifiedBy(final String lastModifiedBy) {
134         this.lastModifiedBy = lastModifiedBy;
135     }
136 
137     public Policy() {
138     }
139 
140     public Policy(final String name, final String description) {
141         setName(name);
142         this.description = description;
143     }
144 
145     public String getDescription() {
146         return this.description;
147     }
148 
149     /***
150      * An active policy expects/permits SCM activity.
151      */
152     public boolean getActive() {
153         return this.active;
154     }
155 
156     public boolean isActive() {
157         return getActive();
158     }
159 
160     public void setDescription(final String description) {
161         this.description = description;
162     }
163 
164     public void setActive(final boolean active) {
165         this.active = active;
166     }
167 
168     public Collection<Codeline> getCodelines() {
169         Object[] params = {
170                               this
171         };
172 
173         return JpaDao.getInstance().findByNamedQuery(Codeline.class, QueryNames.CODELINES_ALL_FOR_POLICY, params);
174     }
175 
176     public void add() {
177         JpaDao.getInstance().add(this);
178     }
179 
180     public void update() {
181         JpaDao.getInstance().update(this);
182     }
183 
184     public void delete() {
185         JpaDao.getInstance().delete(getClass(), getId());
186     }
187 
188     public void index() {
189         JpaDao.getInstance().index(this);
190     }
191 
192     public static Collection<Policy> findAll() {
193         return JpaDao.getInstance().findAll(CLASS);
194     }
195 
196     public static Policy findByName(final String name) {
197         Object[] params = {
198                               name
199         };
200 
201         return JpaDao.getInstance().findSingleByNamedQuery(CLASS, QueryNames.POLICY_BY_NAME, params);
202     }
203 
204     public static Policy getDefaultPolicy() {
205         Policy p = Policy.findByName("DEV"); //XXX configurable?
206 
207         if (p == null) {
208             p = new Policy("DEV", "DEV");
209             p.setActive(true);
210             p.add();
211         }
212 
213         return p;
214     }
215 
216     public static Collection<Policy> search(final String query) {
217         String[] fields = {
218                               "name",
219                               "created",
220                               "createdBy",
221                               "lastModified",
222                               "lastModifiedBy",
223                               "description"
224         };
225 
226         return JpaDao.getInstance().findByTextSearch(CLASS, query, fields);
227     }
228 
229     public static void indexAll() {
230         JpaDao.getInstance().indexAll(CLASS);
231     }
232 }