]> 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 - 2020 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.jcoreutils.Comparables;
43 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
44 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
45
46 /**
47  * A generic user entity class
48  * <p>
49  * @author Roland Häder<roland@mxchange.org>
50  * @todo Remove AllUserNames after cleanup of deprecations in EJB/JSF
51  */
52 @Entity (name = "users")
53 @Table (
54                 name = "users"
55 )
56 @NamedQueries (
57                 {
58                         @NamedQuery (name = "AllUserNames", query = "SELECT DISTINCT u.userName FROM users AS u ORDER BY u.userId ASC"),
59                         @NamedQuery (name = "SearchUserByName", query = "SELECT u FROM users AS u WHERE LOWER(u.userName) LIKE LOWER(:userName)"),
60                         @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)"),
61                         @NamedQuery (name = "SearchUserByConfirmKey", query = "SELECT u FROM users AS u WHERE u.userConfirmKey = :confirmKey"),
62                         @NamedQuery (name = "SearchAllUsersExcept", query = "SELECT u FROM users AS u WHERE u != :user ORDER BY u.userId ASC"),
63                         @NamedQuery (name = "AllUsers", query = "SELECT u FROM users AS u ORDER BY u.userId ASC"),}
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 (user.equals(this)) {
230                         // Same object
231                         return 0;
232                 }
233
234                 // Init comparitors
235                 final int comparitors[] = {
236                         // First contact
237                         this.getUserContact().compareTo(user.getUserContact()),
238                         // ... then user name
239                         this.getUserName().compareTo(user.getUserName())
240                 };
241
242                 // Check all values
243                 final int comparison = Comparables.checkAll(comparitors);
244
245                 // Return value
246                 return comparison;
247         }
248
249         @Override
250         public boolean equals (final Object object) {
251                 if (null == object) {
252                         return false;
253                 } else if (this.getClass() != object.getClass()) {
254                         return false;
255                 }
256
257                 final User other = (User) object;
258
259                 if (!Objects.equals(this.getUserName(), other.getUserName())) {
260                         return false;
261                 } else if (!Objects.equals(this.getUserConfirmKey(), other.getUserConfirmKey())) {
262                         return false;
263                 } else if (!Objects.equals(this.getUserAccountStatus(), other.getUserAccountStatus())) {
264                         return false;
265                 } else if (!Objects.equals(this.getUserContact(), other.getUserContact())) {
266                         return false;
267                 } else if (!Objects.equals(this.getUserProfileMode(), other.getUserProfileMode())) {
268                         return false;
269                 }
270
271                 return true;
272         }
273
274         @Override
275         public UserAccountStatus getUserAccountStatus () {
276                 return this.userAccountStatus;
277         }
278
279         @Override
280         public void setUserAccountStatus (final UserAccountStatus userAccountStatus) {
281                 this.userAccountStatus = userAccountStatus;
282         }
283
284         @Override
285         public String getUserConfirmKey () {
286                 return this.userConfirmKey;
287         }
288
289         @Override
290         public void setUserConfirmKey (final String userConfirmKey) {
291                 this.userConfirmKey = userConfirmKey;
292         }
293
294         @Override
295         public Contact getUserContact () {
296                 return this.userContact;
297         }
298
299         @Override
300         public void setUserContact (final Contact userContact) {
301                 this.userContact = userContact;
302         }
303
304         @Override
305         @SuppressWarnings ("ReturnOfDateField")
306         public Date getUserCreated () {
307                 return this.userCreated;
308         }
309
310         @Override
311         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
312         public void setUserCreated (final Date userCreated) {
313                 this.userCreated = userCreated;
314         }
315
316         @Override
317         public String getUserEncryptedPassword () {
318                 return this.userEncryptedPassword;
319         }
320
321         @Override
322         public void setUserEncryptedPassword (final String userEncryptedPassword) {
323                 this.userEncryptedPassword = userEncryptedPassword;
324         }
325
326         @Override
327         public Long getUserId () {
328                 return this.userId;
329         }
330
331         @Override
332         public void setUserId (final Long userId) {
333                 this.userId = userId;
334         }
335
336         @Override
337         @SuppressWarnings ("ReturnOfDateField")
338         public Date getUserLastLocked () {
339                 return this.userLastLocked;
340         }
341
342         @Override
343         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
344         public void setUserLastLocked (final Date userLastLocked) {
345                 this.userLastLocked = userLastLocked;
346         }
347
348         @Override
349         public String getUserLastLockedReason () {
350                 return this.userLastLockedReason;
351         }
352
353         @Override
354         public void setUserLastLockedReason (final String userLastLockedReason) {
355                 this.userLastLockedReason = userLastLockedReason;
356         }
357
358         @Override
359         public Locale getUserLocale () {
360                 return this.userLocale;
361         }
362
363         @Override
364         public void setUserLocale (final Locale userLocale) {
365                 this.userLocale = userLocale;
366         }
367
368         @Override
369         public Boolean getUserMustChangePassword () {
370                 return this.userMustChangePassword;
371         }
372
373         @Override
374         public void setUserMustChangePassword (final Boolean userMustChangePassword) {
375                 this.userMustChangePassword = userMustChangePassword;
376         }
377
378         @Override
379         public String getUserName () {
380                 return this.userName;
381         }
382
383         @Override
384         public void setUserName (final String userName) {
385                 this.userName = userName;
386         }
387
388         @Override
389         public ProfileMode getUserProfileMode () {
390                 return this.userProfileMode;
391         }
392
393         @Override
394         public void setUserProfileMode (final ProfileMode userProfileMode) {
395                 this.userProfileMode = userProfileMode;
396         }
397
398         @Override
399         @SuppressWarnings ("ReturnOfDateField")
400         public Date getUserUpdated () {
401                 return this.userUpdated;
402         }
403
404         @Override
405         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
406         public void setUserUpdated (final Date userUpdated) {
407                 this.userUpdated = userUpdated;
408         }
409
410         @Override
411         public int hashCode () {
412                 int hash = 5;
413
414                 hash = 83 * hash + Objects.hashCode(this.getUserName());
415                 hash = 83 * hash + Objects.hashCode(this.getUserConfirmKey());
416                 hash = 83 * hash + Objects.hashCode(this.getUserAccountStatus());
417                 hash = 83 * hash + Objects.hashCode(this.getUserContact());
418                 hash = 83 * hash + Objects.hashCode(this.getUserProfileMode());
419
420                 return hash;
421         }
422
423 }