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.Collection;
025    import java.util.Date;
026    
027    import javax.persistence.Column;
028    import javax.persistence.Entity;
029    import javax.persistence.GeneratedValue;
030    import javax.persistence.GenerationType;
031    import javax.persistence.Id;
032    import javax.persistence.NamedQueries;
033    import javax.persistence.NamedQuery;
034    import javax.persistence.Table;
035    import javax.persistence.UniqueConstraint;
036    import javax.persistence.Version;
037    
038    import org.hibernate.search.annotations.DateBridge;
039    import org.hibernate.search.annotations.Field;
040    import org.hibernate.search.annotations.Index;
041    import org.hibernate.search.annotations.Indexed;
042    import org.hibernate.search.annotations.Resolution;
043    import org.hibernate.search.annotations.Store;
044    
045    /**
046     * Describes what kinds of changes are expected on a Codeline.
047     */
048    @Entity(name = "Policy")
049    @Table(name = "TL_POLICY", uniqueConstraints =  {
050        @UniqueConstraint(columnNames =  {
051            "NAME"}
052        )
053    }
054    )
055    @NamedQueries({@NamedQuery(name = QueryNames.POLICY_BY_NAME,query = "select o from Policy o where o.name = ?" + Policy.ORDER_BY)
056    })
057    @Indexed
058    public final class Policy implements Persistable {
059        static final String ORDER_BY = " order by o.name";
060        private static final Class<Policy> CLASS = Policy.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 = "DESCRIPTION")
084        @Field(index = Index.TOKENIZED, store = Store.NO)
085        private String description;
086        @Column(name = "ACTIVE_FLAG", nullable = false)
087        private Boolean active = false;
088    
089        public Integer getId() {
090            return this.id;
091        }
092    
093        public String getName() {
094            return this.name;
095        }
096    
097        public Date getCreated() {
098            return this.created;
099        }
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    }