]> git.mxchange.org Git - juser-core.git/blobdiff - src/org/mxchange/jusercore/model/user/LoginUser.java
Continued:
[juser-core.git] / src / org / mxchange / jusercore / model / user / LoginUser.java
index b5bc4daf5ca0fd0e13a25a1cba165bfce9105998..d6de5bf9a24a69c44f20abb5ddfc61aa31665f14 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Roland Haeder
+ * Copyright (C) 2016 - 2022 Free Software Foundation
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -16,7 +16,8 @@
  */
 package org.mxchange.jusercore.model.user;
 
-import java.util.Calendar;
+import java.util.Date;
+import java.util.Locale;
 import java.util.Objects;
 import javax.persistence.Basic;
 import javax.persistence.CascadeType;
@@ -27,7 +28,6 @@ import javax.persistence.Enumerated;
 import javax.persistence.GeneratedValue;
 import javax.persistence.GenerationType;
 import javax.persistence.Id;
-import javax.persistence.Index;
 import javax.persistence.JoinColumn;
 import javax.persistence.Lob;
 import javax.persistence.NamedQueries;
@@ -36,43 +36,26 @@ import javax.persistence.OneToOne;
 import javax.persistence.Table;
 import javax.persistence.Temporal;
 import javax.persistence.TemporalType;
-import org.mxchange.jcontacts.contact.Contact;
-import org.mxchange.jcontacts.contact.UserContact;
+import javax.persistence.Transient;
+import org.mxchange.jcontacts.model.contact.Contact;
+import org.mxchange.jcontacts.model.contact.UserContact;
+import org.mxchange.jcontacts.model.utils.ContactUtils;
+import org.mxchange.jcoreutils.Comparables;
 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
 
 /**
  * A generic user entity class
  * <p>
- * @author Roland Haeder<roland@mxchange.org>
+ * @author Roland Hรคder<roland@mxchange.org>
  */
 @Entity (name = "users")
 @Table (
-               name = "users",
-               indexes = {
-                       @Index (
-                                       name = "confirmation_key",
-                                       unique = true,
-                                       columnList = "user_confirm_key"
-                       ),
-                       @Index (
-                                       name = "user_name",
-                                       unique = true,
-                                       columnList = "user_name"
-                       )
-               }
+               name = "users"
 )
 @NamedQueries (
                {
-                       @NamedQuery (name = "AllUserNames", query = "SELECT DISTINCT u.userName FROM users AS u ORDER BY u.userId ASC"),
-                       @NamedQuery (name = "AllEmailAddresses", query = "SELECT DISTINCT c.contactEmailAddress FROM contacts AS c INNER JOIN users AS u ON u.userContact = c ORDER BY c.contactId ASC"),
-                       @NamedQuery (name = "SearchUserName", query = "SELECT u FROM users AS u WHERE LOWER(u.userName) LIKE LOWER(:param)"),
-                       @NamedQuery (name = "SearchUserId", query = "SELECT u FROM users AS u WHERE u.userId = :id"),
-                       @NamedQuery (name = "SearchEmailAddress", query = "SELECT u FROM users AS u INNER JOIN contacts AS c ON u.userContact = c WHERE LOWER(c.contactEmailAddress) LIKE LOWER(:param)"),
-                       @NamedQuery (name = "SearchAllUsersExcept", query = "SELECT u FROM users AS u WHERE u != :user ORDER BY u.userId ASC"),
-                       @NamedQuery (name = "AllUsers", query = "SELECT u FROM users AS u ORDER BY u.userId ASC"),
-                       @NamedQuery (name = "AllPublicUsers", query = "SELECT u FROM users AS u WHERE u.userAccountStatus = :status AND u.userProfileMode = :mode ORDER BY u.userId ASC"),
-                       @NamedQuery (name = "AllMemberPublicUsers", query = "SELECT u FROM users AS u WHERE u.userAccountStatus = :status AND u.userProfileMode IN (:public, :members) ORDER BY u.userId ASC")
+                       @NamedQuery (name = "AllUsers", query = "SELECT u FROM users AS u ORDER BY u.userId ASC")
                }
 )
 @SuppressWarnings ("PersistenceUnitPresent")
@@ -81,6 +64,7 @@ public class LoginUser implements User {
        /**
         * Serial number
         */
+       @Transient
        private static final long serialVersionUID = 4_328_454_581_751L;
 
        /**
@@ -94,7 +78,7 @@ public class LoginUser implements User {
        /**
         * Confirmation key
         */
-       @Column (name = "user_confirm_key", length = 50)
+       @Column (name = "user_confirm_key", unique = true)
        private String userConfirmKey;
 
        /**
@@ -105,25 +89,32 @@ public class LoginUser implements User {
        private Contact userContact;
 
        /**
-        * "created" timestamp
+        * Encrypted password
         */
        @Basic (optional = false)
-       @Temporal (TemporalType.TIMESTAMP)
-       @Column (name = "user_created", nullable = false)
-       private Calendar userCreated;
+       @Column (name = "user_encrypted_password", nullable = false)
+       private String userEncryptedPassword;
 
        /**
-        * Encrypted password
+        * When this user entry has been created
         */
        @Basic (optional = false)
-       @Column (name = "user_encrypted_password", nullable = false)
-       private String userEncryptedPassword;
+       @Temporal (TemporalType.TIMESTAMP)
+       @Column (name = "user_entry_created", nullable = false, updatable = false)
+       private Date userEntryCreated;
+
+       /**
+        * When this user entry has been updated
+        */
+       @Temporal (TemporalType.TIMESTAMP)
+       @Column (name = "user_entry_updated", insertable = false)
+       private Date userEntryUpdated;
 
        /**
         * User id
         */
        @Id
-       @Column (name = "user_id", nullable = false, length = 20, updatable = false)
+       @Column (name = "user_id", nullable = false, updatable = false)
        @GeneratedValue (strategy = GenerationType.IDENTITY)
        private Long userId;
 
@@ -132,7 +123,7 @@ public class LoginUser implements User {
         */
        @Temporal (TemporalType.TIMESTAMP)
        @Column (name = "user_last_locked_timestamp")
-       private Calendar userLastLocked;
+       private Date userLastLocked;
 
        /**
         * Last locked reason
@@ -141,11 +132,24 @@ public class LoginUser implements User {
        @Column (name = "user_last_locked_reason")
        private String userLastLockedReason;
 
+       /**
+        * User locale
+        */
+       @Column (name = "user_locale")
+       private Locale userLocale;
+
+       /**
+        * Whether the user must change password after login
+        */
+       @Basic (optional = false)
+       @Column (name = "user_must_change_password", nullable = false)
+       private Boolean userMustChangePassword;
+
        /**
         * User name
         */
        @Basic (optional = false)
-       @Column (name = "user_name", nullable = false, length = 20)
+       @Column (name = "user_name", nullable = false, length = 30, unique = true)
        private String userName;
 
        /**
@@ -157,50 +161,116 @@ public class LoginUser implements User {
        private ProfileMode userProfileMode;
 
        /**
-        * When this user has been updated
+        * Default constructor
         */
-       @Temporal (TemporalType.TIMESTAMP)
-       @Column (name = "user_updated")
-       private Calendar userUpdated;
+       public LoginUser () {
+       }
 
        /**
-        * Default constructor
+        * Constructor with all required fields
+        * <p>
+        * @param userName               Username
+        * @param userProfileMode        Profile mode
+        * @param userMustChangePassword Whether user must change password
+        * @param userEncryptedPassword  Encrypted password
+        * @param userAccountStatus      Account status
+        * @param userContact            User's contact data
         */
-       public LoginUser () {
-               // Default is invisible
-               this.userProfileMode = ProfileMode.INVISIBLE;
+       public LoginUser (final String userName, final ProfileMode userProfileMode, final Boolean userMustChangePassword, final String userEncryptedPassword, final UserAccountStatus userAccountStatus, final Contact userContact) {
+               // Call other constructor first
+               this();
+
+               // Validate all parameter
+               if (null == userName) {
+                       // Throw NPE
+                       throw new NullPointerException("userName is null"); //NOI18N
+               } else if (userName.isEmpty()) {
+                       // Throw IAE
+                       throw new IllegalArgumentException("userName is empty"); //NOI18N
+               } else if (null == userProfileMode) {
+                       // Throw NPE
+                       throw new NullPointerException("userProfileMode is null"); //NOI18N
+               } else if (null == userMustChangePassword) {
+                       // Throw it again
+                       throw new NullPointerException("userMustChangePassword is null"); //NOI18N
+               } else if (null == userEncryptedPassword) {
+                       // Throw it again
+                       throw new NullPointerException("userEncryptedPassword is null"); //NOI18N
+               } else if (userEncryptedPassword.isEmpty()) {
+                       // Throw IAE
+                       throw new IllegalArgumentException("userEncryptedPassword is empty"); //NOI18N
+               } else if (null == userAccountStatus) {
+                       // Throw NPE
+                       throw new NullPointerException("userAccountStatus is null"); //NOI18N
+               } else if (null == userContact) {
+                       // Throw it again
+                       throw new NullPointerException("userContact is null"); //NOI18N
+               }
+
+               // Set all fields
+               this.userAccountStatus = userAccountStatus;
+               this.userEncryptedPassword = userEncryptedPassword;
+               this.userMustChangePassword = userMustChangePassword;
+               this.userName = userName;
+               this.userProfileMode = userProfileMode;
+               this.userContact = userContact;
        }
 
        @Override
-       public void copyAll (final User user) {
-               // Is contact set?
-               if (user.getUserContact() instanceof Contact) {
-                       // Copy also contact data
-                       this.getUserContact().copyAll(user.getUserContact());
+       public int compareTo (final User user) {
+               // Checkparameter and return 0 if equal
+               if (null == user) {
+                       // Should not happen
+                       throw new NullPointerException("Parameter 'user' is null"); //NOI18N
+               } else if (Objects.equals(this, user)) {
+                       // Same object
+                       return 0;
                }
 
-               // Copy other data
-               this.setUserConfirmKey(user.getUserConfirmKey());
-               this.setUserName(user.getUserName());
-               this.setUserEncryptedPassword(user.getUserEncryptedPassword());
-               this.setUserAccountStatus(user.getUserAccountStatus());
-               this.setUserCreated(user.getUserCreated());
-               this.setUserLastLocked(user.getUserLastLocked());
+               // Init comparitors
+               final int comparitors[] = {
+                       // First contact
+                       ContactUtils.compare(this.getUserContact(), user.getUserContact()),
+                       // ... then user name
+                       this.getUserName().compareTo(user.getUserName())
+               };
+
+               // Check all values
+               final int comparison = Comparables.checkAll(comparitors);
+
+               // Return value
+               return comparison;
        }
 
        @Override
        public boolean equals (final Object object) {
                if (null == object) {
                        return false;
-               }
-               if (this.getClass() != object.getClass()) {
+               } else if (this.getClass() != object.getClass()) {
                        return false;
                }
 
-               final User other = (User) object;
+               final User user = (User) object;
+
+               if (!Objects.equals(this.getUserName(), user.getUserName())) {
+                       return false;
+               } else if (!Objects.equals(this.getUserAccountStatus(), user.getUserAccountStatus())) {
+                       return false;
+               } else if (!Objects.equals(this.getUserConfirmKey(), user.getUserConfirmKey())) {
+                       return false;
+               } else if (!Objects.equals(this.getUserId(), user.getUserId())) {
+                       return false;
+               } else if (!Objects.equals(this.getUserMustChangePassword(), user.getUserMustChangePassword())) {
+                       return false;
+               } else if (!Objects.equals(this.getUserName(), user.getUserName())) {
+                       return false;
+               } else if (!Objects.equals(this.getUserProfileMode(), user.getUserProfileMode())) {
+                       return false;
+               } else if (!Objects.equals(this.getUserContact(), user.getUserContact())) {
+                       return false;
+               }
 
-               return ((Objects.equals(this.getUserName(), other.getUserName())) &&
-                               (Objects.equals(this.getUserId(), other.getUserId())));
+               return true;
        }
 
        @Override
@@ -234,23 +304,37 @@ public class LoginUser implements User {
        }
 
        @Override
-       public Calendar getUserCreated () {
-               return this.userCreated;
+       public String getUserEncryptedPassword () {
+               return this.userEncryptedPassword;
+       }
+
+       @Override
+       public void setUserEncryptedPassword (final String userEncryptedPassword) {
+               this.userEncryptedPassword = userEncryptedPassword;
        }
 
        @Override
-       public void setUserCreated (final Calendar userCreated) {
-               this.userCreated = userCreated;
+       @SuppressWarnings ("ReturnOfDateField")
+       public Date getUserEntryCreated () {
+               return this.userEntryCreated;
        }
 
        @Override
-       public String getUserEncryptedPassword () {
-               return this.userEncryptedPassword;
+       @SuppressWarnings ("AssignmentToDateFieldFromParameter")
+       public void setUserEntryCreated (final Date userEntryCreated) {
+               this.userEntryCreated = userEntryCreated;
        }
 
        @Override
-       public void setUserEncryptedPassword (final String userEncryptedPassword) {
-               this.userEncryptedPassword = userEncryptedPassword;
+       @SuppressWarnings ("ReturnOfDateField")
+       public Date getUserEntryUpdated () {
+               return this.userEntryUpdated;
+       }
+
+       @Override
+       @SuppressWarnings ("AssignmentToDateFieldFromParameter")
+       public void setUserEntryUpdated (final Date userEntryUpdated) {
+               this.userEntryUpdated = userEntryUpdated;
        }
 
        @Override
@@ -264,12 +348,14 @@ public class LoginUser implements User {
        }
 
        @Override
-       public Calendar getUserLastLocked () {
+       @SuppressWarnings ("ReturnOfDateField")
+       public Date getUserLastLocked () {
                return this.userLastLocked;
        }
 
        @Override
-       public void setUserLastLocked (final Calendar userLastLocked) {
+       @SuppressWarnings ("AssignmentToDateFieldFromParameter")
+       public void setUserLastLocked (final Date userLastLocked) {
                this.userLastLocked = userLastLocked;
        }
 
@@ -283,6 +369,26 @@ public class LoginUser implements User {
                this.userLastLockedReason = userLastLockedReason;
        }
 
+       @Override
+       public Locale getUserLocale () {
+               return this.userLocale;
+       }
+
+       @Override
+       public void setUserLocale (final Locale userLocale) {
+               this.userLocale = userLocale;
+       }
+
+       @Override
+       public Boolean getUserMustChangePassword () {
+               return this.userMustChangePassword;
+       }
+
+       @Override
+       public void setUserMustChangePassword (final Boolean userMustChangePassword) {
+               this.userMustChangePassword = userMustChangePassword;
+       }
+
        @Override
        public String getUserName () {
                return this.userName;
@@ -303,21 +409,18 @@ public class LoginUser implements User {
                this.userProfileMode = userProfileMode;
        }
 
-       @Override
-       public Calendar getUserUpdated () {
-               return this.userUpdated;
-       }
-
-       @Override
-       public void setUserUpdated (final Calendar userUpdated) {
-               this.userUpdated = userUpdated;
-       }
-
        @Override
        public int hashCode () {
                int hash = 5;
+
                hash = 83 * hash + Objects.hashCode(this.getUserName());
+               hash = 83 * hash + Objects.hashCode(this.getUserAccountStatus());
+               hash = 83 * hash + Objects.hashCode(this.getUserConfirmKey());
                hash = 83 * hash + Objects.hashCode(this.getUserId());
+               hash = 83 * hash + Objects.hashCode(this.getUserMustChangePassword());
+               hash = 83 * hash + Objects.hashCode(this.getUserProfileMode());
+               hash = 83 * hash + Objects.hashCode(this.getUserContact());
+
                return hash;
        }