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.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;
47 * A generic user entity class
49 * @author Roland Häder<roland@mxchange.org>
51 @Entity (name = "users")
57 @NamedQuery (name = "AllUsers", query = "SELECT u FROM users AS u ORDER BY u.userId ASC")
60 @SuppressWarnings ("PersistenceUnitPresent")
61 public class LoginUser implements User {
67 private static final long serialVersionUID = 4_328_454_581_751L;
72 @Basic (optional = false)
73 @Column (name = "user_account_status", nullable = false)
74 @Enumerated (value = EnumType.STRING)
75 private UserAccountStatus userAccountStatus;
80 @Column (name = "user_confirm_key", unique = true)
81 private String userConfirmKey;
84 * Id number from "contacts" table
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;
93 @Basic (optional = false)
94 @Column (name = "user_encrypted_password", nullable = false)
95 private String userEncryptedPassword;
98 * When this user entry has been created
100 @Basic (optional = false)
101 @Temporal (TemporalType.TIMESTAMP)
102 @Column (name = "user_entry_created", nullable = false, updatable = false)
103 private Date userEntryCreated;
106 * When this user entry has been updated
108 @Temporal (TemporalType.TIMESTAMP)
109 @Column (name = "user_entry_updated", insertable = false)
110 private Date userEntryUpdated;
116 @Column (name = "user_id", nullable = false, updatable = false)
117 @GeneratedValue (strategy = GenerationType.IDENTITY)
121 * Last "locked" timestamp
123 @Temporal (TemporalType.TIMESTAMP)
124 @Column (name = "user_last_locked_timestamp")
125 private Date userLastLocked;
131 @Column (name = "user_last_locked_reason")
132 private String userLastLockedReason;
137 @Column (name = "user_locale")
138 private Locale userLocale;
141 * Whether the user must change password after login
143 @Basic (optional = false)
144 @Column (name = "user_must_change_password", nullable = false)
145 private Boolean userMustChangePassword;
150 @Basic (optional = false)
151 @Column (name = "user_name", nullable = false, length = 30, unique = true)
152 private String userName;
155 * Profile mode of this user
157 @Basic (optional = false)
158 @Enumerated (EnumType.STRING)
159 @Column (name = "user_profile_mode", nullable = false)
160 private ProfileMode userProfileMode;
163 * Default constructor
165 public LoginUser () {
169 * Constructor with all required fields
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
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
182 // Validate all parameter
183 if (null == userName) {
185 throw new NullPointerException("userName is null"); //NOI18N
186 } else if (userName.isEmpty()) {
188 throw new IllegalArgumentException("userName is empty"); //NOI18N
189 } else if (null == userProfileMode) {
191 throw new NullPointerException("userProfileMode is null"); //NOI18N
192 } else if (null == userMustChangePassword) {
194 throw new NullPointerException("userMustChangePassword is null"); //NOI18N
195 } else if (null == userEncryptedPassword) {
197 throw new NullPointerException("userEncryptedPassword is null"); //NOI18N
198 } else if (userEncryptedPassword.isEmpty()) {
200 throw new IllegalArgumentException("userEncryptedPassword is empty"); //NOI18N
201 } else if (null == userAccountStatus) {
203 throw new NullPointerException("userAccountStatus is null"); //NOI18N
204 } else if (null == userContact) {
206 throw new NullPointerException("userContact is null"); //NOI18N
210 this.userAccountStatus = userAccountStatus;
211 this.userEncryptedPassword = userEncryptedPassword;
212 this.userMustChangePassword = userMustChangePassword;
213 this.userName = userName;
214 this.userProfileMode = userProfileMode;
215 this.userContact = userContact;
219 public int compareTo (final User user) {
220 // Checkparameter and return 0 if equal
223 throw new NullPointerException("user is null"); //NOI18N
224 } else if (user.equals(this)) {
230 final int comparitors[] = {
232 this.getUserContact().compareTo(user.getUserContact()),
233 // ... then user name
234 this.getUserName().compareTo(user.getUserName())
238 final int comparison = Comparables.checkAll(comparitors);
245 public boolean equals (final Object object) {
246 if (null == object) {
248 } else if (this.getClass() != object.getClass()) {
252 final User user = (User) object;
254 if (!Objects.equals(this.getUserName(), user.getUserName())) {
256 } else if (!Objects.equals(this.getUserConfirmKey(), user.getUserConfirmKey())) {
258 } else if (!Objects.equals(this.getUserAccountStatus(), user.getUserAccountStatus())) {
260 } else if (!Objects.equals(this.getUserContact(), user.getUserContact())) {
262 } else if (!Objects.equals(this.getUserProfileMode(), user.getUserProfileMode())) {
270 public UserAccountStatus getUserAccountStatus () {
271 return this.userAccountStatus;
275 public void setUserAccountStatus (final UserAccountStatus userAccountStatus) {
276 this.userAccountStatus = userAccountStatus;
280 public String getUserConfirmKey () {
281 return this.userConfirmKey;
285 public void setUserConfirmKey (final String userConfirmKey) {
286 this.userConfirmKey = userConfirmKey;
290 public Contact getUserContact () {
291 return this.userContact;
295 public void setUserContact (final Contact userContact) {
296 this.userContact = userContact;
300 public String getUserEncryptedPassword () {
301 return this.userEncryptedPassword;
305 public void setUserEncryptedPassword (final String userEncryptedPassword) {
306 this.userEncryptedPassword = userEncryptedPassword;
310 @SuppressWarnings ("ReturnOfDateField")
311 public Date getUserEntryCreated () {
312 return this.userEntryCreated;
316 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
317 public void setUserEntryCreated (final Date userEntryCreated) {
318 this.userEntryCreated = userEntryCreated;
322 @SuppressWarnings ("ReturnOfDateField")
323 public Date getUserEntryUpdated () {
324 return this.userEntryUpdated;
328 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
329 public void setUserEntryUpdated (final Date userEntryUpdated) {
330 this.userEntryUpdated = userEntryUpdated;
334 public Long getUserId () {
339 public void setUserId (final Long userId) {
340 this.userId = userId;
344 @SuppressWarnings ("ReturnOfDateField")
345 public Date getUserLastLocked () {
346 return this.userLastLocked;
350 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
351 public void setUserLastLocked (final Date userLastLocked) {
352 this.userLastLocked = userLastLocked;
356 public String getUserLastLockedReason () {
357 return this.userLastLockedReason;
361 public void setUserLastLockedReason (final String userLastLockedReason) {
362 this.userLastLockedReason = userLastLockedReason;
366 public Locale getUserLocale () {
367 return this.userLocale;
371 public void setUserLocale (final Locale userLocale) {
372 this.userLocale = userLocale;
376 public Boolean getUserMustChangePassword () {
377 return this.userMustChangePassword;
381 public void setUserMustChangePassword (final Boolean userMustChangePassword) {
382 this.userMustChangePassword = userMustChangePassword;
386 public String getUserName () {
387 return this.userName;
391 public void setUserName (final String userName) {
392 this.userName = userName;
396 public ProfileMode getUserProfileMode () {
397 return this.userProfileMode;
401 public void setUserProfileMode (final ProfileMode userProfileMode) {
402 this.userProfileMode = userProfileMode;
406 public int hashCode () {
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());