]> 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 - 2022 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.utils.ContactUtils;
42 import org.mxchange.jcontacts.model.contact.UserContact;
43 import org.mxchange.jcoreutils.Comparables;
44 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
45 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
46
47 /**
48  * A generic user entity class
49  * <p>
50  * @author Roland Häder<roland@mxchange.org>
51  */
52 @Entity (name = "users")
53 @Table (
54                 name = "users"
55 )
56 @NamedQueries (
57                 {
58                         @NamedQuery (name = "AllUsers", query = "SELECT u FROM users AS u ORDER BY u.userId ASC")
59                 }
60 )
61 @SuppressWarnings ("PersistenceUnitPresent")
62 public class LoginUser implements User {
63
64         /**
65          * Serial number
66          */
67         @Transient
68         private static final long serialVersionUID = 4_328_454_581_751L;
69
70         /**
71          * Account status
72          */
73         @Basic (optional = false)
74         @Column (name = "user_account_status", nullable = false)
75         @Enumerated (value = EnumType.STRING)
76         private UserAccountStatus userAccountStatus;
77
78         /**
79          * Confirmation key
80          */
81         @Column (name = "user_confirm_key", unique = true)
82         private String userConfirmKey;
83
84         /**
85          * Id number from "contacts" table
86          */
87         @JoinColumn (name = "user_contact_id", referencedColumnName = "contact_id", nullable = false, updatable = false, unique = true)
88         @OneToOne (targetEntity = UserContact.class, cascade = CascadeType.ALL, optional = false)
89         private Contact userContact;
90
91         /**
92          * Encrypted password
93          */
94         @Basic (optional = false)
95         @Column (name = "user_encrypted_password", nullable = false)
96         private String userEncryptedPassword;
97
98         /**
99          * When this user entry has been created
100          */
101         @Basic (optional = false)
102         @Temporal (TemporalType.TIMESTAMP)
103         @Column (name = "user_entry_created", nullable = false, updatable = false)
104         private Date userEntryCreated;
105
106         /**
107          * When this user entry has been updated
108          */
109         @Temporal (TemporalType.TIMESTAMP)
110         @Column (name = "user_entry_updated", insertable = false)
111         private Date userEntryUpdated;
112
113         /**
114          * User id
115          */
116         @Id
117         @Column (name = "user_id", nullable = false, updatable = false)
118         @GeneratedValue (strategy = GenerationType.IDENTITY)
119         private Long userId;
120
121         /**
122          * Last "locked" timestamp
123          */
124         @Temporal (TemporalType.TIMESTAMP)
125         @Column (name = "user_last_locked_timestamp")
126         private Date userLastLocked;
127
128         /**
129          * Last locked reason
130          */
131         @Lob
132         @Column (name = "user_last_locked_reason")
133         private String userLastLockedReason;
134
135         /**
136          * User locale
137          */
138         @Column (name = "user_locale")
139         private Locale userLocale;
140
141         /**
142          * Whether the user must change password after login
143          */
144         @Basic (optional = false)
145         @Column (name = "user_must_change_password", nullable = false)
146         private Boolean userMustChangePassword;
147
148         /**
149          * User name
150          */
151         @Basic (optional = false)
152         @Column (name = "user_name", nullable = false, length = 30, unique = true)
153         private String userName;
154
155         /**
156          * Profile mode of this user
157          */
158         @Basic (optional = false)
159         @Enumerated (EnumType.STRING)
160         @Column (name = "user_profile_mode", nullable = false)
161         private ProfileMode userProfileMode;
162
163         /**
164          * Default constructor
165          */
166         public LoginUser () {
167         }
168
169         /**
170          * Constructor with all required fields
171          * <p>
172          * @param userName               Username
173          * @param userProfileMode        Profile mode
174          * @param userMustChangePassword Whether user must change password
175          * @param userEncryptedPassword  Encrypted password
176          * @param userAccountStatus      Account status
177          * @param userContact            User's contact data
178          */
179         public LoginUser (final String userName, final ProfileMode userProfileMode, final Boolean userMustChangePassword, final String userEncryptedPassword, final UserAccountStatus userAccountStatus, final Contact userContact) {
180                 // Call other constructor first
181                 this();
182
183                 // Validate all parameter
184                 if (null == userName) {
185                         // Throw NPE
186                         throw new NullPointerException("userName is null"); //NOI18N
187                 } else if (userName.isEmpty()) {
188                         // Throw IAE
189                         throw new IllegalArgumentException("userName is empty"); //NOI18N
190                 } else if (null == userProfileMode) {
191                         // Throw NPE
192                         throw new NullPointerException("userProfileMode is null"); //NOI18N
193                 } else if (null == userMustChangePassword) {
194                         // Throw it again
195                         throw new NullPointerException("userMustChangePassword is null"); //NOI18N
196                 } else if (null == userEncryptedPassword) {
197                         // Throw it again
198                         throw new NullPointerException("userEncryptedPassword is null"); //NOI18N
199                 } else if (userEncryptedPassword.isEmpty()) {
200                         // Throw IAE
201                         throw new IllegalArgumentException("userEncryptedPassword is empty"); //NOI18N
202                 } else if (null == userAccountStatus) {
203                         // Throw NPE
204                         throw new NullPointerException("userAccountStatus is null"); //NOI18N
205                 } else if (null == userContact) {
206                         // Throw it again
207                         throw new NullPointerException("userContact is null"); //NOI18N
208                 }
209
210                 // Set all fields
211                 this.userAccountStatus = userAccountStatus;
212                 this.userEncryptedPassword = userEncryptedPassword;
213                 this.userMustChangePassword = userMustChangePassword;
214                 this.userName = userName;
215                 this.userProfileMode = userProfileMode;
216                 this.userContact = userContact;
217         }
218
219         @Override
220         public int compareTo (final User user) {
221                 // Checkparameter and return 0 if equal
222                 if (null == user) {
223                         // Should not happen
224                         throw new NullPointerException("user is null"); //NOI18N
225                 } else if (user.equals(this)) {
226                         // Same object
227                         return 0;
228                 }
229
230                 // Init comparitors
231                 final int comparitors[] = {
232                         // First contact
233                         ContactUtils.compare(this.getUserContact(), user.getUserContact()),
234                         // ... then user name
235                         this.getUserName().compareTo(user.getUserName())
236                 };
237
238                 // Check all values
239                 final int comparison = Comparables.checkAll(comparitors);
240
241                 // Return value
242                 return comparison;
243         }
244
245         @Override
246         public boolean equals (final Object object) {
247                 if (null == object) {
248                         return false;
249                 } else if (this.getClass() != object.getClass()) {
250                         return false;
251                 }
252
253                 final User user = (User) object;
254
255                 if (!Objects.equals(this.getUserName(), user.getUserName())) {
256                         return false;
257                 } else if (!Objects.equals(this.getUserAccountStatus(), user.getUserAccountStatus())) {
258                         return false;
259                 } else if (!Objects.equals(this.getUserConfirmKey(), user.getUserConfirmKey())) {
260                         return false;
261                 } else if (!Objects.equals(this.getUserId(), user.getUserId())) {
262                         return false;
263                 } else if (!Objects.equals(this.getUserMustChangePassword(), user.getUserMustChangePassword())) {
264                         return false;
265                 } else if (!Objects.equals(this.getUserName(), user.getUserName())) {
266                         return false;
267                 } else if (!Objects.equals(this.getUserProfileMode(), user.getUserProfileMode())) {
268                         return false;
269                 } else if (!Objects.equals(this.getUserContact(), user.getUserContact())) {
270                         return false;
271                 }
272
273                 return true;
274         }
275
276         @Override
277         public UserAccountStatus getUserAccountStatus () {
278                 return this.userAccountStatus;
279         }
280
281         @Override
282         public void setUserAccountStatus (final UserAccountStatus userAccountStatus) {
283                 this.userAccountStatus = userAccountStatus;
284         }
285
286         @Override
287         public String getUserConfirmKey () {
288                 return this.userConfirmKey;
289         }
290
291         @Override
292         public void setUserConfirmKey (final String userConfirmKey) {
293                 this.userConfirmKey = userConfirmKey;
294         }
295
296         @Override
297         public Contact getUserContact () {
298                 return this.userContact;
299         }
300
301         @Override
302         public void setUserContact (final Contact userContact) {
303                 this.userContact = userContact;
304         }
305
306         @Override
307         public String getUserEncryptedPassword () {
308                 return this.userEncryptedPassword;
309         }
310
311         @Override
312         public void setUserEncryptedPassword (final String userEncryptedPassword) {
313                 this.userEncryptedPassword = userEncryptedPassword;
314         }
315
316         @Override
317         @SuppressWarnings ("ReturnOfDateField")
318         public Date getUserEntryCreated () {
319                 return this.userEntryCreated;
320         }
321
322         @Override
323         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
324         public void setUserEntryCreated (final Date userEntryCreated) {
325                 this.userEntryCreated = userEntryCreated;
326         }
327
328         @Override
329         @SuppressWarnings ("ReturnOfDateField")
330         public Date getUserEntryUpdated () {
331                 return this.userEntryUpdated;
332         }
333
334         @Override
335         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
336         public void setUserEntryUpdated (final Date userEntryUpdated) {
337                 this.userEntryUpdated = userEntryUpdated;
338         }
339
340         @Override
341         public Long getUserId () {
342                 return this.userId;
343         }
344
345         @Override
346         public void setUserId (final Long userId) {
347                 this.userId = userId;
348         }
349
350         @Override
351         @SuppressWarnings ("ReturnOfDateField")
352         public Date getUserLastLocked () {
353                 return this.userLastLocked;
354         }
355
356         @Override
357         @SuppressWarnings ("AssignmentToDateFieldFromParameter")
358         public void setUserLastLocked (final Date userLastLocked) {
359                 this.userLastLocked = userLastLocked;
360         }
361
362         @Override
363         public String getUserLastLockedReason () {
364                 return this.userLastLockedReason;
365         }
366
367         @Override
368         public void setUserLastLockedReason (final String userLastLockedReason) {
369                 this.userLastLockedReason = userLastLockedReason;
370         }
371
372         @Override
373         public Locale getUserLocale () {
374                 return this.userLocale;
375         }
376
377         @Override
378         public void setUserLocale (final Locale userLocale) {
379                 this.userLocale = userLocale;
380         }
381
382         @Override
383         public Boolean getUserMustChangePassword () {
384                 return this.userMustChangePassword;
385         }
386
387         @Override
388         public void setUserMustChangePassword (final Boolean userMustChangePassword) {
389                 this.userMustChangePassword = userMustChangePassword;
390         }
391
392         @Override
393         public String getUserName () {
394                 return this.userName;
395         }
396
397         @Override
398         public void setUserName (final String userName) {
399                 this.userName = userName;
400         }
401
402         @Override
403         public ProfileMode getUserProfileMode () {
404                 return this.userProfileMode;
405         }
406
407         @Override
408         public void setUserProfileMode (final ProfileMode userProfileMode) {
409                 this.userProfileMode = userProfileMode;
410         }
411
412         @Override
413         public int hashCode () {
414                 int hash = 5;
415
416                 hash = 83 * hash + Objects.hashCode(this.getUserName());
417                 hash = 83 * hash + Objects.hashCode(this.getUserAccountStatus());
418                 hash = 83 * hash + Objects.hashCode(this.getUserConfirmKey());
419                 hash = 83 * hash + Objects.hashCode(this.getUserId());
420                 hash = 83 * hash + Objects.hashCode(this.getUserMustChangePassword());
421                 hash = 83 * hash + Objects.hashCode(this.getUserProfileMode());
422                 hash = 83 * hash + Objects.hashCode(this.getUserContact());
423
424                 return hash;
425         }
426
427 }