2 * Copyright (C) 2016 - 2018 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.jusercore.model.user.profilemodes.ProfileMode;
43 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
46 * A generic user entity class
48 * @author Roland Häder<roland@mxchange.org>
49 * @todo Remove AllUserNames after cleanup of deprecations in EJB/JSF
51 @Entity (name = "users")
57 @NamedQuery (name = "AllUserNames", query = "SELECT DISTINCT u.userName FROM users AS u ORDER BY u.userId ASC"),
58 @NamedQuery (name = "SearchUserByName", query = "SELECT u FROM users AS u WHERE LOWER(u.userName) LIKE LOWER(:userName)"),
59 @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)"),
60 @NamedQuery (name = "SearchUserByConfirmKey", query = "SELECT u FROM users AS u WHERE u.userConfirmKey = :confirmKey"),
61 @NamedQuery (name = "SearchAllUsersExcept", query = "SELECT u FROM users AS u WHERE u != :user ORDER BY u.userId ASC"),
62 @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 (Objects.equals(this, user)) {
234 // There can only be one user per user name
235 return this.getUserName().compareTo(user.getUserName());
239 public boolean equals (final Object object) {
240 if (null == object) {
242 } else if (this.getClass() != object.getClass()) {
246 final User other = (User) object;
248 if (!Objects.equals(this.getUserName(), other.getUserName())) {
250 } else if (!Objects.equals(this.getUserId(), other.getUserId())) {
258 public UserAccountStatus getUserAccountStatus () {
259 return this.userAccountStatus;
263 public void setUserAccountStatus (final UserAccountStatus userAccountStatus) {
264 this.userAccountStatus = userAccountStatus;
268 public String getUserConfirmKey () {
269 return this.userConfirmKey;
273 public void setUserConfirmKey (final String userConfirmKey) {
274 this.userConfirmKey = userConfirmKey;
278 public Contact getUserContact () {
279 return this.userContact;
283 public void setUserContact (final Contact userContact) {
284 this.userContact = userContact;
288 @SuppressWarnings ("ReturnOfDateField")
289 public Date getUserCreated () {
290 return this.userCreated;
294 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
295 public void setUserCreated (final Date userCreated) {
296 this.userCreated = userCreated;
300 public String getUserEncryptedPassword () {
301 return this.userEncryptedPassword;
305 public void setUserEncryptedPassword (final String userEncryptedPassword) {
306 this.userEncryptedPassword = userEncryptedPassword;
310 public Long getUserId () {
315 public void setUserId (final Long userId) {
316 this.userId = userId;
320 @SuppressWarnings ("ReturnOfDateField")
321 public Date getUserLastLocked () {
322 return this.userLastLocked;
326 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
327 public void setUserLastLocked (final Date userLastLocked) {
328 this.userLastLocked = userLastLocked;
332 public String getUserLastLockedReason () {
333 return this.userLastLockedReason;
337 public void setUserLastLockedReason (final String userLastLockedReason) {
338 this.userLastLockedReason = userLastLockedReason;
342 public Locale getUserLocale () {
343 return this.userLocale;
347 public void setUserLocale (final Locale userLocale) {
348 this.userLocale = userLocale;
352 public Boolean getUserMustChangePassword () {
353 return this.userMustChangePassword;
357 public void setUserMustChangePassword (final Boolean userMustChangePassword) {
358 this.userMustChangePassword = userMustChangePassword;
362 public String getUserName () {
363 return this.userName;
367 public void setUserName (final String userName) {
368 this.userName = userName;
372 public ProfileMode getUserProfileMode () {
373 return this.userProfileMode;
377 public void setUserProfileMode (final ProfileMode userProfileMode) {
378 this.userProfileMode = userProfileMode;
382 @SuppressWarnings ("ReturnOfDateField")
383 public Date getUserUpdated () {
384 return this.userUpdated;
388 @SuppressWarnings ("AssignmentToDateFieldFromParameter")
389 public void setUserUpdated (final Date userUpdated) {
390 this.userUpdated = userUpdated;
394 public int hashCode () {
396 hash = 83 * hash + Objects.hashCode(this.getUserName());
397 hash = 83 * hash + Objects.hashCode(this.getUserId());