]> git.mxchange.org Git - juser-core.git/blob - src/org/mxchange/jusercore/model/user/LoginUser.java
Continued:
[juser-core.git] / src / org / mxchange / jusercore / model / user / LoginUser.java
1 /*
2  * Copyright (C) 2016 - 2018 Free Software Foundation
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jusercore.model.user;
18
19 import java.util.Date;
20 import java.util.Locale;
21 import java.util.Objects;
22 import javax.persistence.Basic;
23 import javax.persistence.CascadeType;
24 import javax.persistence.Column;
25 import javax.persistence.Entity;
26 import javax.persistence.EnumType;
27 import javax.persistence.Enumerated;
28 import javax.persistence.GeneratedValue;
29 import javax.persistence.GenerationType;
30 import javax.persistence.Id;
31 import javax.persistence.JoinColumn;
32 import javax.persistence.Lob;
33 import javax.persistence.NamedQueries;
34 import javax.persistence.NamedQuery;
35 import javax.persistence.OneToOne;
36 import javax.persistence.Table;
37 import javax.persistence.Temporal;
38 import javax.persistence.TemporalType;
39 import javax.persistence.Transient;
40 import org.mxchange.jcontacts.model.contact.Contact;
41 import org.mxchange.jcontacts.model.contact.UserContact;
42 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
43 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
44
45 /**
46  * A generic user entity class
47  * <p>
48  * @author Roland Häder<roland@mxchange.org>
49  * @todo Remove AllUserNames after cleanup of deprecations in EJB/JSF
50  */
51 @Entity (name = "users")
52 @Table (
53                 name = "users"
54 )
55 @NamedQueries (
56                 {
57                         @NamedQuery (name = "AllUserNames", query = "SELECT DISTINCT u.userName FROM users AS u ORDER BY u.userId ASC"),
58                         @NamedQuery (name = "SearchUserByName", query = "SELECT u FROM users AS u WHERE LOWER(u.userName) LIKE LOWER(:userName)"),
59                         @NamedQuery (name = "SearchUserByEmailAddress", query = "SELECT u FROM users AS u INNER JOIN contacts AS c ON u.userContact = c WHERE LOWER(c.contactEmailAddress) LIKE LOWER(:emailAddress)"),
60                         @NamedQuery (name = "SearchUserByConfirmKey", query = "SELECT u FROM users AS u WHERE u.userConfirmKey = :confirmKey"),
61                         @NamedQuery (name = "SearchAllUsersExcept", query = "SELECT u FROM users AS u WHERE u != :user ORDER BY u.userId ASC"),
62                         @NamedQuery (name = "AllUsers", query = "SELECT u FROM users AS u ORDER BY u.userId ASC"),
63                 }
64 )
65 @SuppressWarnings ("PersistenceUnitPresent")
66 public class LoginUser implements User {
67
68         /**
69          * Serial number
70          */
71         @Transient
72         private static final long serialVersionUID = 4_328_454_581_751L;
73
74         /**
75          * Account status
76          */
77         @Basic (optional = false)
78         @Column (name = "user_account_status", nullable = false)
79         @Enumerated (value = EnumType.STRING)
80         private UserAccountStatus userAccountStatus;
81
82         /**
83          * Confirmation key
84          */
85         @Column (name = "user_confirm_key", unique = true)
86         private String userConfirmKey;
87
88         /**
89          * Id number from "contacts" table
90          */
91         @JoinColumn (name = "user_contact_id", referencedColumnName = "contact_id", nullable = false, updatable = false, unique = true)
92         @OneToOne (targetEntity = UserContact.class, cascade = CascadeType.ALL, optional = false)
93         private Contact userContact;
94
95         /**
96          * "created" timestamp
97          */
98         @Basic (optional = false)
99         @Temporal (TemporalType.TIMESTAMP)
100         @Column (name = "user_created", nullable = false, updatable = false)
101         private Date userCreated;
102
103         /**
104          * Encrypted password
105          */
106         @Basic (optional = false)
107         @Column (name = "user_encrypted_password", nullable = false)
108         private String userEncryptedPassword;
109
110         /**
111          * User id
112          */
113         @Id
114         @Column (name = "user_id", nullable = false, updatable = false)
115         @GeneratedValue (strategy = GenerationType.IDENTITY)
116         private Long userId;
117
118         /**
119          * Last "locked" timestamp
120          */
121         @Temporal (TemporalType.TIMESTAMP)
122         @Column (name = "user_last_locked_timestamp")
123         private Date userLastLocked;
124
125         /**
126          * Last locked reason
127          */
128         @Lob
129         @Column (name = "user_last_locked_reason")
130         private String userLastLockedReason;
131
132         /**
133          * User locale
134          */
135         @Column (name = "user_locale")
136         private Locale userLocale;
137
138         /**
139          * Whether the user must change password after login
140          */
141         @Basic (optional = false)
142         @Column (name = "user_must_change_password", nullable = false)
143         private Boolean userMustChangePassword;
144
145         /**
146          * User name
147          */
148         @Basic (optional = false)
149         @Column (name = "user_name", nullable = false, length = 30, unique = true)
150         private String userName;
151
152         /**
153          * Profile mode of this user
154          */
155         @Basic (optional = false)
156         @Enumerated (EnumType.STRING)
157         @Column (name = "user_profile_mode", nullable = false)
158         private ProfileMode userProfileMode;
159
160         /**
161          * When this user has been updated
162          */
163         @Temporal (TemporalType.TIMESTAMP)
164         @Column (name = "user_updated", insertable = false)
165         private Date userUpdated;
166
167         /**
168          * Default constructor
169          */
170         public LoginUser () {
171         }
172
173         /**
174          * Constructor with all required fields
175          * <p>
176          * @param userName               Username
177          * @param userProfileMode        Profile mode
178          * @param userMustChangePassword Whether user must change password
179          * @param userEncryptedPassword  Encrypted password
180          * @param userAccountStatus      Account status
181          * @param userContact User's contact data
182          */
183         public LoginUser (final String userName, final ProfileMode userProfileMode, final Boolean userMustChangePassword, final String userEncryptedPassword, final UserAccountStatus userAccountStatus, final Contact userContact) {
184                 // Call other constructor first
185                 this();
186
187                 // Validate all parameter
188                 if (null == userName) {
189                         // Throw NPE
190                         throw new NullPointerException("userName is null"); //NOI18N
191                 } else if (userName.isEmpty()) {
192                         // Throw IAE
193                         throw new IllegalArgumentException("userName is empty"); //NOI18N
194                 } else if (null == userProfileMode) {
195                         // Throw NPE
196                         throw new NullPointerException("userProfileMode is null"); //NOI18N
197                 } else if (null == userMustChangePassword) {
198                         // Throw it again
199                         throw new NullPointerException("userMustChangePassword is null"); //NOI18N
200                 } else if (null == userEncryptedPassword) {
201                         // Throw it again
202                         throw new NullPointerException("userEncryptedPassword is null"); //NOI18N
203                 } else if (userEncryptedPassword.isEmpty()) {
204                         // Throw IAE
205                         throw new IllegalArgumentException("userEncryptedPassword is empty"); //NOI18N
206                 } else if (null == userAccountStatus) {
207                         // Throw NPE
208                         throw new NullPointerException("userAccountStatus is null"); //NOI18N
209                 } else if (null == userContact) {
210                         // Throw it again
211                         throw new NullPointerException("userContact is null"); //NOI18N
212                 }
213
214                 // Set all fields
215                 this.userAccountStatus = userAccountStatus;
216                 this.userEncryptedPassword = userEncryptedPassword;
217                 this.userMustChangePassword = userMustChangePassword;
218                 this.userName = userName;
219                 this.userProfileMode = userProfileMode;
220                 this.userContact = userContact;
221         }
222
223         @Override
224         public int compareTo (final User user) {
225                 // Checkparameter and return 0 if equal
226                 if (null == user) {
227                         // Should not happen
228                         throw new NullPointerException("user is null"); //NOI18N
229                 } else if (Objects.equals(this, user)) {
230                         // Same object
231                         return 0;
232                 }
233
234                 // There can only be one user per user name
235                 return this.getUserName().compareTo(user.getUserName());
236         }
237
238         @Override
239         public boolean equals (final Object object) {
240                 if (null == object) {
241                         return false;
242                 } else if (this.getClass() != object.getClass()) {
243                         return false;
244                 }
245
246                 final User other = (User) object;
247
248                 if (!Objects.equals(this.getUserName(), other.getUserName())) {
249                         return false;
250                 } else if (!Objects.equals(this.getUserId(), other.getUserId())) {
251                         return false;
252                 }
253
254                 return true;
255         }
256
257         @Override
258         public UserAccountStatus getUserAccountStatus () {
259                 return this.userAccountStatus;
260         }
261
262         @Override
263         public void setUserAccountStatus (final UserAccountStatus userAccountStatus) {
264                 this.userAccountStatus = userAccountStatus;
265         }
266
267         @Override
268         public String getUserConfirmKey () {
269                 return this.userConfirmKey;
270         }
271
272         @Override
273         public void setUserConfirmKey (final String userConfirmKey) {
274                 this.userConfirmKey = userConfirmKey;
275         }
276
277         @Override
278         public Contact getUserContact () {
279                 return this.userContact;
280         }
281
282         @Override
283         public void setUserContact (final Contact userContact) {
284                 this.userContact = userContact;
285         }
286
287         @Override
288         @SuppressWarnings ("ReturnOfDateField")
289         public Date getUserCreated () {
290                 return this.userCreated;
291         }
292
293         @Override
294         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
295         public void setUserCreated (final Date userCreated) {
296                 this.userCreated = userCreated;
297         }
298
299         @Override
300         public String getUserEncryptedPassword () {
301                 return this.userEncryptedPassword;
302         }
303
304         @Override
305         public void setUserEncryptedPassword (final String userEncryptedPassword) {
306                 this.userEncryptedPassword = userEncryptedPassword;
307         }
308
309         @Override
310         public Long getUserId () {
311                 return this.userId;
312         }
313
314         @Override
315         public void setUserId (final Long userId) {
316                 this.userId = userId;
317         }
318
319         @Override
320         @SuppressWarnings ("ReturnOfDateField")
321         public Date getUserLastLocked () {
322                 return this.userLastLocked;
323         }
324
325         @Override
326         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
327         public void setUserLastLocked (final Date userLastLocked) {
328                 this.userLastLocked = userLastLocked;
329         }
330
331         @Override
332         public String getUserLastLockedReason () {
333                 return this.userLastLockedReason;
334         }
335
336         @Override
337         public void setUserLastLockedReason (final String userLastLockedReason) {
338                 this.userLastLockedReason = userLastLockedReason;
339         }
340
341         @Override
342         public Locale getUserLocale () {
343                 return this.userLocale;
344         }
345
346         @Override
347         public void setUserLocale (final Locale userLocale) {
348                 this.userLocale = userLocale;
349         }
350
351         @Override
352         public Boolean getUserMustChangePassword () {
353                 return this.userMustChangePassword;
354         }
355
356         @Override
357         public void setUserMustChangePassword (final Boolean userMustChangePassword) {
358                 this.userMustChangePassword = userMustChangePassword;
359         }
360
361         @Override
362         public String getUserName () {
363                 return this.userName;
364         }
365
366         @Override
367         public void setUserName (final String userName) {
368                 this.userName = userName;
369         }
370
371         @Override
372         public ProfileMode getUserProfileMode () {
373                 return this.userProfileMode;
374         }
375
376         @Override
377         public void setUserProfileMode (final ProfileMode userProfileMode) {
378                 this.userProfileMode = userProfileMode;
379         }
380
381         @Override
382         @SuppressWarnings ("ReturnOfDateField")
383         public Date getUserUpdated () {
384                 return this.userUpdated;
385         }
386
387         @Override
388         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
389         public void setUserUpdated (final Date userUpdated) {
390                 this.userUpdated = userUpdated;
391         }
392
393         @Override
394         public int hashCode () {
395                 int hash = 5;
396                 hash = 83 * hash + Objects.hashCode(this.getUserName());
397                 hash = 83 * hash + Objects.hashCode(this.getUserId());
398                 return hash;
399         }
400
401 }