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 tinlizard.util.Messages;
025    
026    import java.util.Collection;
027    import java.util.Date;
028    
029    import javax.persistence.Column;
030    import javax.persistence.Entity;
031    import javax.persistence.GeneratedValue;
032    import javax.persistence.GenerationType;
033    import javax.persistence.Id;
034    import javax.persistence.NamedQueries;
035    import javax.persistence.NamedQuery;
036    import javax.persistence.Table;
037    import javax.persistence.UniqueConstraint;
038    import javax.persistence.Version;
039    
040    import org.apache.log4j.Logger;
041    
042    import org.codehaus.plexus.util.StringUtils;
043    
044    import org.hibernate.search.annotations.DateBridge;
045    import org.hibernate.search.annotations.Field;
046    import org.hibernate.search.annotations.Index;
047    import org.hibernate.search.annotations.Indexed;
048    import org.hibernate.search.annotations.Resolution;
049    import org.hibernate.search.annotations.Store;
050    
051    /**
052     * A User that can log in and/or own a Codeline.
053     */
054    @Entity(name = "User")
055    @Table(name = "TL_USER", uniqueConstraints =  {
056        @UniqueConstraint(columnNames =  {
057            "NAME"}
058        )
059    }
060    )
061    @NamedQueries({@NamedQuery(name = QueryNames.USER_BY_NAME,query = "select o from User o where o.name = ?" + User.ORDER_BY)
062    })
063    @Indexed
064    public final class User implements Persistable {
065        static final String ORDER_BY = " order by o.name";
066        private static final Class<User> CLASS = User.class;
067        private static final Logger LOG = Logger.getLogger(CLASS);
068        @Id
069        @GeneratedValue(strategy = GenerationType.AUTO)
070        @Column(name = "ID")
071        private Integer id;
072        @Column(name = "NAME", nullable = false)
073        @Field(index = Index.TOKENIZED, store = Store.NO)
074        private String name;
075        @Column(name = "CREATED", nullable = false)
076        @Field(index = Index.UN_TOKENIZED, store = Store.YES)
077        @DateBridge(resolution = Resolution.SECOND)
078        private Date created;
079        @Column(name = "CREATED_BY", nullable = false)
080        @Field(index = Index.TOKENIZED, store = Store.NO)
081        private String createdBy;
082        @Version
083        @Column(name = "LAST_MODIFIED")
084        @Field(index = Index.UN_TOKENIZED, store = Store.YES)
085        @DateBridge(resolution = Resolution.SECOND)
086        private Date lastModified;
087        @Column(name = "LAST_MODIFIED_BY")
088        @Field(index = Index.TOKENIZED, store = Store.NO)
089        private String lastModifiedBy;
090        @Column(name = "EMAIL")
091        @Field(index = Index.TOKENIZED, store = Store.NO)
092        private String email;
093    
094        public Integer getId() {
095            return this.id;
096        }
097    
098        public String getName() {
099            return this.name;
100        }
101    
102        public Date getCreated() {
103            return this.created;
104        }
105    
106        public String getCreatedBy() {
107            return this.createdBy;
108        }
109    
110        public Date getLastModified() {
111            return this.lastModified;
112        }
113    
114        public String getLastModifiedBy() {
115            return this.lastModifiedBy;
116        }
117    
118        public void setId(final Integer id) {
119            this.id = id;
120        }
121    
122        public void setName(final String name) {
123            this.name = name;
124        }
125    
126        public void setCreated(final Date created) {
127            this.created = created;
128        }
129    
130        public void setCreatedBy(final String createdBy) {
131            this.createdBy = createdBy;
132        }
133    
134        public void setLastModified(final Date lastModified) {
135            this.lastModified = lastModified;
136        }
137    
138        public void setLastModifiedBy(final String lastModifiedBy) {
139            this.lastModifiedBy = lastModifiedBy;
140        }
141    
142        public String getEmail() {
143            return this.email;
144        }
145    
146        public void setEmail(final String email) {
147            this.email = email;
148        }
149    
150        public Collection<Codeline> getCodelines() {
151            Object[] params = {
152                                  this
153            };
154    
155            return JpaDao.getInstance().findByNamedQuery(Codeline.class, QueryNames.CODELINES_BY_USER, params);
156        }
157    
158        public void add() {
159            JpaDao.getInstance().add(this);
160        }
161    
162        public void update() {
163            JpaDao.getInstance().update(this);
164        }
165    
166        public void delete() {
167            JpaDao.getInstance().delete(CLASS, getId());
168        }
169    
170        public void index() {
171            JpaDao.getInstance().index(this);
172        }
173    
174        public static Collection<User> findAll() {
175            return JpaDao.getInstance().findAll(CLASS);
176        }
177    
178        public static User findByName(final String name) {
179            Object[] params = {
180                                  name
181            };
182    
183            return JpaDao.getInstance().findSingleByNamedQuery(CLASS, QueryNames.USER_BY_NAME, params);
184        }
185    
186        public static User getUserByName(final String name, final boolean createIfNotExist) {
187            User user = User.findByName(name);
188    
189            if ((user == null) && createIfNotExist) {
190                user = add(name);
191            } else {
192                if (LOG.isDebugEnabled()) {
193                    LOG.debug(Messages.debug_5000(name));
194                }
195            }
196    
197            return user;
198        }
199    
200        public static User getSystemUser() {
201            return User.getUserByName("SYSTEM", true);
202        }
203    
204        public static User add(final String name) {
205            if (StringUtils.isBlank(name)) {
206                throw new IllegalArgumentException(Messages.error_0105());
207            }
208    
209            User user = new User();
210            user.setName(name);
211            user.add();
212    
213            return user;
214        }
215    
216        public static Collection<User> search(final String query) {
217            String[] fields = {
218                                  "name",
219                                  "created",
220                                  "createdBy",
221                                  "lastModified",
222                                  "lastModifiedBy",
223                                  "email"
224            };
225    
226            return JpaDao.getInstance().findByTextSearch(CLASS, query, fields);
227        }
228    
229        public static void indexAll() {
230            JpaDao.getInstance().indexAll(CLASS);
231        }
232    }