2 * Copyright (C) 2016 - 2022 Free Software Foundation
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.
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.
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/>.
17 package org.mxchange.jusercore.model.user;
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.Contacts;
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;
48 * A generic user entity class
50 * @author Roland Häder<roland@mxchange.org>
52 @Entity (name = "users")
58 @NamedQuery (name = "AllUsers", query = "SELECT u FROM users AS u ORDER BY u.userId ASC")
61 @SuppressWarnings ("PersistenceUnitPresent")
62 public class LoginUser implements User {
68 private static final long serialVersionUID = 4_328_454_581_751L;
73 @Basic (optional = false)
74 @Column (name = "user_account_status", nullable = false)
75 @Enumerated (value = EnumType.STRING)
76 private UserAccountStatus userAccountStatus;
81 @Column (name = "user_confirm_key", unique = true)
82 private String userConfirmKey;
85 * Id number from "contacts" table
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;
94 @Basic (optional = false)
95 @Column (name = "user_encrypted_password", nullable = false)
96 private String userEncryptedPassword;
99 * When this user entry has been created
101 @Basic (optional = false)
102 @Temporal (TemporalType.TIMESTAMP)
103 @Column (name = "user_entry_created", nullable = false, updatable = false)
104 private Date userEntryCreated;
107 * When this user entry has been updated
109 @Temporal (TemporalType.TIMESTAMP)
110 @Column (name = "user_entry_updated", insertable = false)
111 private Date userEntryUpdated;
117 @Column (name = "user_id", nullable = false, updatable = false)
118 @GeneratedValue (strategy = GenerationType.IDENTITY)
122 * Last "locked" timestamp
124 @Temporal (TemporalType.TIMESTAMP)
125 @Column (name = "user_last_locked_timestamp")
126 private Date userLastLocked;
132 @Column (name = "user_last_locked_reason")
133 private String userLastLockedReason;
138 @Column (name = "user_locale")
139 private Locale userLocale;
142 * Whether the user must change password after login
144 @Basic (optional = false)
145 @Column (name = "user_must_change_password", nullable = false)
146 private Boolean userMustChangePassword;
151 @Basic (optional = false)
152 @Column (name = "user_name", nullable = false, length = 30, unique = true)
153 private String userName;
156 * Profile mode of this user
158 @Basic (optional = false)
159 @Enumerated (EnumType.STRING)
160 @Column (name = "user_profile_mode", nullable = false)
161 private ProfileMode userProfileMode;
164 * Default constructor
166 public LoginUser () {
170 * Constructor with all required fields
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
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
183 // Validate all parameter
184 if (null == userName) {
186 throw new NullPointerException("userName is null"); //NOI18N
187 } else if (userName.isEmpty()) {
189 throw new IllegalArgumentException("userName is empty"); //NOI18N
190 } else if (null == userProfileMode) {
192 throw new NullPointerException("userProfileMode is null"); //NOI18N
193 } else if (null == userMustChangePassword) {
195 throw new NullPointerException("userMustChangePassword is null"); //NOI18N
196 } else if (null == userEncryptedPassword) {
198 throw new NullPointerException("userEncryptedPassword is null"); //NOI18N
199 } else if (userEncryptedPassword.isEmpty()) {
201 throw new IllegalArgumentException("userEncryptedPassword is empty"); //NOI18N
202 } else if (null == userAccountStatus) {
204 throw new NullPointerException("userAccountStatus is null"); //NOI18N
205 } else if (null == userContact) {
207 throw new NullPointerException("userContact is null"); //NOI18N
211 this.userAccountStatus = userAccountStatus;
212 this.userEncryptedPassword = userEncryptedPassword;
213 this.userMustChangePassword = userMustChangePassword;
214 this.userName = userName;
215 this.userProfileMode = userProfileMode;
216 this.userContact = userContact;
220 public int compareTo (final User user) {
221 // Checkparameter and return 0 if equal
224 throw new NullPointerException("user is null"); //NOI18N
225 } else if (user.equals(this)) {
231 final int comparitors[] = {
233 Contacts.compare(this.getUserContact(), user.getUserContact()),
234 // ... then user name
235 this.getUserName().compareTo(user.getUserName())
239 final int comparison = Comparables.checkAll(comparitors);
246 public boolean equals (final Object object) {
247 if (null == object) {
249 } else if (this.getClass() != object.getClass()) {
253 final User user = (User) object;
255 if (!Objects.equals(this.getUserName(), user.getUserName())) {
257 } else if (!Objects.equals(this.getUserAccountStatus(), user.getUserAccountStatus())) {
259 } else if (!Objects.equals(this.getUserConfirmKey(), user.getUserConfirmKey())) {
261 } else if (!Objects.equals(this.getUserId(), user.getUserId())) {
263 } else if (!Objects.equals(this.getUserMustChangePassword(), user.getUserMustChangePassword())) {
265 } else if (!Objects.equals(this.getUserName(), user.getUserName())) {
267 } else if (!Objects.equals(this.getUserProfileMode(), user.getUserProfileMode())) {
269 } else if (!Objects.equals(this.getUserContact(), user.getUserContact())) {
277 public UserAccountStatus getUserAccountStatus () {
278 return this.userAccountStatus;
282 public void setUserAccountStatus (final UserAccountStatus userAccountStatus) {
283 this.userAccountStatus = userAccountStatus;
287 public String getUserConfirmKey () {
288 return this.userConfirmKey;
292 public void setUserConfirmKey (final String userConfirmKey) {
293 this.userConfirmKey = userConfirmKey;
297 public Contact getUserContact () {
298 return this.userContact;
302 public void setUserContact (final Contact userContact) {
303 this.userContact = userContact;
307 public String getUserEncryptedPassword () {
308 return this.userEncryptedPassword;
312 public void setUserEncryptedPassword (final String userEncryptedPassword) {
313 this.userEncryptedPassword = userEncryptedPassword;
317 @SuppressWarnings ("ReturnOfDateField")
318 public Date getUserEntryCreated () {
319 return this.userEntryCreated;
323 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
324 public void setUserEntryCreated (final Date userEntryCreated) {
325 this.userEntryCreated = userEntryCreated;
329 @SuppressWarnings ("ReturnOfDateField")
330 public Date getUserEntryUpdated () {
331 return this.userEntryUpdated;
335 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
336 public void setUserEntryUpdated (final Date userEntryUpdated) {
337 this.userEntryUpdated = userEntryUpdated;
341 public Long getUserId () {
346 public void setUserId (final Long userId) {
347 this.userId = userId;
351 @SuppressWarnings ("ReturnOfDateField")
352 public Date getUserLastLocked () {
353 return this.userLastLocked;
357 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
358 public void setUserLastLocked (final Date userLastLocked) {
359 this.userLastLocked = userLastLocked;
363 public String getUserLastLockedReason () {
364 return this.userLastLockedReason;
368 public void setUserLastLockedReason (final String userLastLockedReason) {
369 this.userLastLockedReason = userLastLockedReason;
373 public Locale getUserLocale () {
374 return this.userLocale;
378 public void setUserLocale (final Locale userLocale) {
379 this.userLocale = userLocale;
383 public Boolean getUserMustChangePassword () {
384 return this.userMustChangePassword;
388 public void setUserMustChangePassword (final Boolean userMustChangePassword) {
389 this.userMustChangePassword = userMustChangePassword;
393 public String getUserName () {
394 return this.userName;
398 public void setUserName (final String userName) {
399 this.userName = userName;
403 public ProfileMode getUserProfileMode () {
404 return this.userProfileMode;
408 public void setUserProfileMode (final ProfileMode userProfileMode) {
409 this.userProfileMode = userProfileMode;
413 public int hashCode () {
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());