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