2 * Copyright (C) 2016 - 2020 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>
50 * @todo Remove AllUserNames after cleanup of deprecations in EJB/JSF
52 @Entity (name = "users")
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"),}
65 @SuppressWarnings ("PersistenceUnitPresent")
66 public class LoginUser implements User {
72 private static final long serialVersionUID = 4_328_454_581_751L;
77 @Basic (optional = false)
78 @Column (name = "user_account_status", nullable = false)
79 @Enumerated (value = EnumType.STRING)
80 private UserAccountStatus userAccountStatus;
85 @Column (name = "user_confirm_key", unique = true)
86 private String userConfirmKey;
89 * Id number from "contacts" table
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;
98 @Basic (optional = false)
99 @Temporal (TemporalType.TIMESTAMP)
100 @Column (name = "user_created", nullable = false, updatable = false)
101 private Date userCreated;
106 @Basic (optional = false)
107 @Column (name = "user_encrypted_password", nullable = false)
108 private String userEncryptedPassword;
114 @Column (name = "user_id", nullable = false, updatable = false)
115 @GeneratedValue (strategy = GenerationType.IDENTITY)
119 * Last "locked" timestamp
121 @Temporal (TemporalType.TIMESTAMP)
122 @Column (name = "user_last_locked_timestamp")
123 private Date userLastLocked;
129 @Column (name = "user_last_locked_reason")
130 private String userLastLockedReason;
135 @Column (name = "user_locale")
136 private Locale userLocale;
139 * Whether the user must change password after login
141 @Basic (optional = false)
142 @Column (name = "user_must_change_password", nullable = false)
143 private Boolean userMustChangePassword;
148 @Basic (optional = false)
149 @Column (name = "user_name", nullable = false, length = 30, unique = true)
150 private String userName;
153 * Profile mode of this user
155 @Basic (optional = false)
156 @Enumerated (EnumType.STRING)
157 @Column (name = "user_profile_mode", nullable = false)
158 private ProfileMode userProfileMode;
161 * When this user has been updated
163 @Temporal (TemporalType.TIMESTAMP)
164 @Column (name = "user_updated", insertable = false)
165 private Date userUpdated;
168 * Default constructor
170 public LoginUser () {
174 * Constructor with all required fields
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
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
187 // Validate all parameter
188 if (null == userName) {
190 throw new NullPointerException("userName is null"); //NOI18N
191 } else if (userName.isEmpty()) {
193 throw new IllegalArgumentException("userName is empty"); //NOI18N
194 } else if (null == userProfileMode) {
196 throw new NullPointerException("userProfileMode is null"); //NOI18N
197 } else if (null == userMustChangePassword) {
199 throw new NullPointerException("userMustChangePassword is null"); //NOI18N
200 } else if (null == userEncryptedPassword) {
202 throw new NullPointerException("userEncryptedPassword is null"); //NOI18N
203 } else if (userEncryptedPassword.isEmpty()) {
205 throw new IllegalArgumentException("userEncryptedPassword is empty"); //NOI18N
206 } else if (null == userAccountStatus) {
208 throw new NullPointerException("userAccountStatus is null"); //NOI18N
209 } else if (null == userContact) {
211 throw new NullPointerException("userContact is null"); //NOI18N
215 this.userAccountStatus = userAccountStatus;
216 this.userEncryptedPassword = userEncryptedPassword;
217 this.userMustChangePassword = userMustChangePassword;
218 this.userName = userName;
219 this.userProfileMode = userProfileMode;
220 this.userContact = userContact;
224 public int compareTo (final User user) {
225 // Checkparameter and return 0 if equal
228 throw new NullPointerException("user is null"); //NOI18N
229 } else if (user.equals(this)) {
235 final int comparitors[] = {
237 this.getUserContact().compareTo(user.getUserContact()),
238 // ... then user name
239 this.getUserName().compareTo(user.getUserName())
243 final int comparison = Comparables.checkAll(comparitors);
250 public boolean equals (final Object object) {
251 if (null == object) {
253 } else if (this.getClass() != object.getClass()) {
257 final User other = (User) object;
259 if (!Objects.equals(this.getUserName(), other.getUserName())) {
261 } else if (!Objects.equals(this.getUserConfirmKey(), other.getUserConfirmKey())) {
263 } else if (!Objects.equals(this.getUserAccountStatus(), other.getUserAccountStatus())) {
265 } else if (!Objects.equals(this.getUserContact(), other.getUserContact())) {
267 } else if (!Objects.equals(this.getUserProfileMode(), other.getUserProfileMode())) {
275 public UserAccountStatus getUserAccountStatus () {
276 return this.userAccountStatus;
280 public void setUserAccountStatus (final UserAccountStatus userAccountStatus) {
281 this.userAccountStatus = userAccountStatus;
285 public String getUserConfirmKey () {
286 return this.userConfirmKey;
290 public void setUserConfirmKey (final String userConfirmKey) {
291 this.userConfirmKey = userConfirmKey;
295 public Contact getUserContact () {
296 return this.userContact;
300 public void setUserContact (final Contact userContact) {
301 this.userContact = userContact;
305 @SuppressWarnings ("ReturnOfDateField")
306 public Date getUserCreated () {
307 return this.userCreated;
311 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
312 public void setUserCreated (final Date userCreated) {
313 this.userCreated = userCreated;
317 public String getUserEncryptedPassword () {
318 return this.userEncryptedPassword;
322 public void setUserEncryptedPassword (final String userEncryptedPassword) {
323 this.userEncryptedPassword = userEncryptedPassword;
327 public Long getUserId () {
332 public void setUserId (final Long userId) {
333 this.userId = userId;
337 @SuppressWarnings ("ReturnOfDateField")
338 public Date getUserLastLocked () {
339 return this.userLastLocked;
343 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
344 public void setUserLastLocked (final Date userLastLocked) {
345 this.userLastLocked = userLastLocked;
349 public String getUserLastLockedReason () {
350 return this.userLastLockedReason;
354 public void setUserLastLockedReason (final String userLastLockedReason) {
355 this.userLastLockedReason = userLastLockedReason;
359 public Locale getUserLocale () {
360 return this.userLocale;
364 public void setUserLocale (final Locale userLocale) {
365 this.userLocale = userLocale;
369 public Boolean getUserMustChangePassword () {
370 return this.userMustChangePassword;
374 public void setUserMustChangePassword (final Boolean userMustChangePassword) {
375 this.userMustChangePassword = userMustChangePassword;
379 public String getUserName () {
380 return this.userName;
384 public void setUserName (final String userName) {
385 this.userName = userName;
389 public ProfileMode getUserProfileMode () {
390 return this.userProfileMode;
394 public void setUserProfileMode (final ProfileMode userProfileMode) {
395 this.userProfileMode = userProfileMode;
399 @SuppressWarnings ("ReturnOfDateField")
400 public Date getUserUpdated () {
401 return this.userUpdated;
405 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
406 public void setUserUpdated (final Date userUpdated) {
407 this.userUpdated = userUpdated;
411 public int hashCode () {
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());