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 tinlizard.util.Messages;
25  
26  import java.util.Collection;
27  import java.util.Date;
28  
29  import javax.persistence.Column;
30  import javax.persistence.Entity;
31  import javax.persistence.GeneratedValue;
32  import javax.persistence.GenerationType;
33  import javax.persistence.Id;
34  import javax.persistence.NamedQueries;
35  import javax.persistence.NamedQuery;
36  import javax.persistence.Table;
37  import javax.persistence.UniqueConstraint;
38  import javax.persistence.Version;
39  
40  import org.apache.log4j.Logger;
41  
42  import org.codehaus.plexus.util.StringUtils;
43  
44  import org.hibernate.search.annotations.DateBridge;
45  import org.hibernate.search.annotations.Field;
46  import org.hibernate.search.annotations.Index;
47  import org.hibernate.search.annotations.Indexed;
48  import org.hibernate.search.annotations.Resolution;
49  import org.hibernate.search.annotations.Store;
50  
51  /***
52   * A User that can log in and/or own a Codeline.
53   */
54  @Entity(name = "User")
55  @Table(name = "TL_USER", uniqueConstraints =  {
56      @UniqueConstraint(columnNames =  {
57          "NAME"}
58      )
59  }
60  )
61  @NamedQueries({@NamedQuery(name = QueryNames.USER_BY_NAME,query = "select o from User o where o.name = ?" + User.ORDER_BY)
62  })
63  @Indexed
64  public final class User implements Persistable {
65      static final String ORDER_BY = " order by o.name";
66      private static final Class<User> CLASS = User.class;
67      private static final Logger LOG = Logger.getLogger(CLASS);
68      @Id
69      @GeneratedValue(strategy = GenerationType.AUTO)
70      @Column(name = "ID")
71      private Integer id;
72      @Column(name = "NAME", nullable = false)
73      @Field(index = Index.TOKENIZED, store = Store.NO)
74      private String name;
75      @Column(name = "CREATED", nullable = false)
76      @Field(index = Index.UN_TOKENIZED, store = Store.YES)
77      @DateBridge(resolution = Resolution.SECOND)
78      private Date created;
79      @Column(name = "CREATED_BY", nullable = false)
80      @Field(index = Index.TOKENIZED, store = Store.NO)
81      private String createdBy;
82      @Version
83      @Column(name = "LAST_MODIFIED")
84      @Field(index = Index.UN_TOKENIZED, store = Store.YES)
85      @DateBridge(resolution = Resolution.SECOND)
86      private Date lastModified;
87      @Column(name = "LAST_MODIFIED_BY")
88      @Field(index = Index.TOKENIZED, store = Store.NO)
89      private String lastModifiedBy;
90      @Column(name = "EMAIL")
91      @Field(index = Index.TOKENIZED, store = Store.NO)
92      private String email;
93  
94      public Integer getId() {
95          return this.id;
96      }
97  
98      public String getName() {
99          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 }