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     * General collection of Codelines.
047     */
048    @Entity(name = "View")
049    @Table(name = "TL_VIEW", uniqueConstraints =  {
050        @UniqueConstraint(columnNames =  {
051            "NAME"}
052        )
053    }
054    )
055    @NamedQueries({@NamedQuery(name = QueryNames.VIEW_BY_NAME,query = "select o from View o where o.name = ?" + View.ORDER_BY)
056    })
057    @Indexed
058    public final class View implements Persistable {
059        static final String ORDER_BY = " order by o.name";
060        private static final Class<View> CLASS = View.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    
087        public Integer getId() {
088            return this.id;
089        }
090    
091        public String getName() {
092            return this.name;
093        }
094    
095        public Date getCreated() {
096            return this.created;
097        }
098    
099        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    }