From: Roland Häder Date: Thu, 6 Oct 2022 14:06:44 +0000 (+0200) Subject: Continued: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=35f0b23609f1aafe94155ff8b58c2f34701e420a;p=juser-core.git Continued: - renamed utilities class Users to UserUtils - moved it to own package --- diff --git a/src/org/mxchange/jusercore/model/user/LoginUser.java b/src/org/mxchange/jusercore/model/user/LoginUser.java index cf17e5f..d6de5bf 100644 --- a/src/org/mxchange/jusercore/model/user/LoginUser.java +++ b/src/org/mxchange/jusercore/model/user/LoginUser.java @@ -38,8 +38,8 @@ import javax.persistence.Temporal; import javax.persistence.TemporalType; import javax.persistence.Transient; import org.mxchange.jcontacts.model.contact.Contact; -import org.mxchange.jcontacts.model.utils.ContactUtils; 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; @@ -221,8 +221,8 @@ public class LoginUser implements User { // Checkparameter and return 0 if equal if (null == user) { // Should not happen - throw new NullPointerException("user is null"); //NOI18N - } else if (user.equals(this)) { + throw new NullPointerException("Parameter 'user' is null"); //NOI18N + } else if (Objects.equals(this, user)) { // Same object return 0; } diff --git a/src/org/mxchange/jusercore/model/user/Users.java b/src/org/mxchange/jusercore/model/user/Users.java deleted file mode 100644 index 79bcb77..0000000 --- a/src/org/mxchange/jusercore/model/user/Users.java +++ /dev/null @@ -1,311 +0,0 @@ -/* - * 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 - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.mxchange.jusercore.model.user; - -import java.io.Serializable; -import java.security.SecureRandom; -import java.text.DateFormat; -import java.text.MessageFormat; -import java.util.Date; -import java.util.Objects; -import java.util.Properties; -import java.util.Random; -import org.mxchange.jcontacts.model.contact.Contact; -import org.mxchange.jcontacts.model.utils.ContactUtils; - -/** - * An utilities class for users - *

- * @author Roland Häder - */ -public class Users implements Serializable { - - /** - * Password alphabet - */ - private static final String PASSWORD_ALPHABET; - - /** - * Password alphabet parts - */ - private static final String[] PASSWORD_ALPHABET_PARTS = { - // lower-case - "abcdefghijklmnopqrstuvwxyz", //NOI18N - - // upper-case - "ABCDEFGHIJKLMNOPQRSTUVWXYZ", //NOI18N - - // numbers - "0123456789", //NOI18N - - // characters - "~^!$%&/()=?{[]}@+*#-_,.;:<|>" //NOI18N - }; - - /** - * Hard-coded minimum password length - */ - private static final Integer PASSWORD_MINIMUM_LENGTH = 5; - - /** - * Random number generator - */ - private static final Random RANDOM_NUMBER_GENERATOR; - - /** - * Serial number - */ - private static final long serialVersionUID = 18_356_847_120_972L; - - /** - * Static initializer - */ - static { - // Init RNG - RANDOM_NUMBER_GENERATOR = new SecureRandom(); - - // Init alphabet - PASSWORD_ALPHABET = Users.PASSWORD_ALPHABET_PARTS[0] + - Users.PASSWORD_ALPHABET_PARTS[1] + - Users.PASSWORD_ALPHABET_PARTS[2] + - Users.PASSWORD_ALPHABET_PARTS[3]; - } - - /** - * Compares both user instances. This method returns -1 if second instance - * is null. - *

- * @param user1 User instance 1 - * @param user2 User instance 2 - *

- * @return Comparison value - */ - public static int compare (final User user1, final User user2) { - // Check equality, then at least first must be given - if (Objects.equals(user1, user2)) { - // Both are same - return 0; - } else if (null == user1) { - // First is null - return -1; - } else if (null == user2) { - // Second is null - return 1; - } - - // Invoke compare() method - return user1.compareTo(user2); - } - - /** - * Copies all attributes from other user object to target - *

- * @param sourceUser Source instance - * @param targetUser Target instance - */ - public static void copyUserData (final User sourceUser, final User targetUser) { - // Check all parameter - if (null == sourceUser) { - // Throw NPE - throw new NullPointerException("sourceUser is null"); //NOI18N - } else if (null == targetUser) { - // Throw NPE - throw new NullPointerException("targetUser is null"); //NOI18N - } else if (Objects.equals(sourceUser, targetUser)) { - // Is exactly the same! - throw new IllegalArgumentException("sourcerUser and targetUser are the same."); //NOI18N - } - - // Is contact set? - if (sourceUser.getUserContact() instanceof Contact) { - // Copy also contact data - ContactUtils.copyContactData(sourceUser.getUserContact(), targetUser.getUserContact()); - } - - // Copy other data - targetUser.setUserConfirmKey(sourceUser.getUserConfirmKey()); - targetUser.setUserName(sourceUser.getUserName()); - targetUser.setUserEncryptedPassword(sourceUser.getUserEncryptedPassword()); - targetUser.setUserAccountStatus(sourceUser.getUserAccountStatus()); - targetUser.setUserLastLocked(sourceUser.getUserLastLocked()); - targetUser.setUserLastLockedReason(sourceUser.getUserLastLockedReason()); - targetUser.setUserEntryUpdated(sourceUser.getUserEntryUpdated()); - targetUser.setUserProfileMode(sourceUser.getUserProfileMode()); - targetUser.setUserLocale(sourceUser.getUserLocale()); - targetUser.setUserMustChangePassword(sourceUser.getUserMustChangePassword()); - } - - /** - * Generates a pseudo-random user name - *

- * @return User name - */ - public static String generateRandomUserName () { - // User name's format is normally "user" + random number (10 digits) - String userName = String.format("user%d", RANDOM_NUMBER_GENERATOR.nextInt(10000000)); //NOI18N - - // Return it - return userName; - } - - /** - * Returns a Properties object from given user instance. - *

- * @param user User instance to get all fields from - *

- * @return All properties from given user - */ - public static Properties getAllUserFields (final User user) { - // Parameter should be valid - if (null == user) { - // Throw NPE - throw new NullPointerException("user is null"); //NOI18N - } else if (user.getUserId() == null) { - // Throw NPE again - throw new NullPointerException("user.userId is null"); //NOI18N - } else if (user.getUserId() < 1) { - // Not valid number - throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N - } else if (user.getUserName() == null) { - // Throw NPE again - throw new NullPointerException("user.userName is null"); //NOI18N - } else if (user.getUserName().isEmpty()) { - // Empty string - throw new IllegalArgumentException("user.userName is empty"); //NOI18N - } else if (user.getUserAccountStatus() == null) { - // Throw NPE - throw new NullPointerException("user.userAccountStatus is null"); //NOI18N - } else if (user.getUserContact() == null) { - // Throw it again - throw new NullPointerException("user.userContact is null"); //NOI18N - } else if (user.getUserContact().getContactId() == null) { - // .. and again - throw new NullPointerException("user.userContact.contactId is null"); //NOI18N - } else if (user.getUserContact().getContactId() < 1) { - // Invalid id - throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is invalid", user.getUserContact().getContactId())); //NOI18N - } else if (user.getUserContact().getContactPersonalTitle() == null) { - // Throw NPE again - throw new NullPointerException("user.userContact.contactPersonalTitle is null"); //NOI18N - } - - // Init properties list - final Properties properties = new Properties(); - - // Init some vbalues with empty strings - properties.setProperty("userConfirmKey", ""); //NOI18N - properties.setProperty("userUpdated", ""); //NOI18N - properties.setProperty("userLastLockedReason", ""); //NOI18N - properties.setProperty("contactTitle", ""); //NOI18N - properties.setProperty("contactStreet", ""); //NOI18N - properties.setProperty("contactHouseNumber", ""); //NOI18N - properties.setProperty("contactCity", ""); //NOI18N - // @TODO Unfinished: - properties.setProperty("contactCountry", ""); //NOI18N - properties.setProperty("contactZipCode", ""); //NOI18N - properties.setProperty("contactBirthday", ""); //NOI18N - properties.setProperty("contactEmailAddress", ""); //NOI18N - properties.setProperty("contactUpdated", ""); //NOI18N - - // Set all: - // - User data - properties.setProperty("userId", Long.toString(user.getUserId())); //NOI18N - properties.setProperty("userName", user.getUserName()); //NOI18N - properties.setProperty("userCreated", getFormattedTimestampFromUser(user, user.getUserEntryCreated())); //NOI18N - if (user.getUserEntryUpdated() != null) { - properties.setProperty("userUpdated", getFormattedTimestampFromUser(user, user.getUserEntryUpdated())); //NOI18N - } - if (user.getUserConfirmKey() != null) { - properties.setProperty("userConfirmKey", user.getUserConfirmKey()); //NOI18N - } - if (user.getUserLastLockedReason() != null) { - properties.setProperty("userLastLockedReason", user.getUserLastLockedReason()); //NOI18N - } - if (user.getUserLastLocked() != null) { - properties.setProperty("userLastLocked", getFormattedTimestampFromUser(user, user.getUserLastLocked())); //NOI18N - } - properties.setProperty("userAccountStatus", user.getUserAccountStatus().toString()); //NOI18N - - // - Contact data - properties.setProperty("contactPersonalTitle", user.getUserContact().getContactPersonalTitle().toString()); //NOI18N - if (user.getUserContact().getContactTitle() != null) { - properties.setProperty("contactTitle", user.getUserContact().getContactTitle()); //NOI18N - } - properties.setProperty("contactFirstName", user.getUserContact().getContactFirstName()); //NOI18N - properties.setProperty("contactFamilyName", user.getUserContact().getContactFamilyName()); //NOI18N - if (user.getUserContact().getContactStreet() != null) { - properties.setProperty("contactStreet", user.getUserContact().getContactStreet()); //NOI18N - } - if (user.getUserContact().getContactHouseNumber() != null) { - properties.setProperty("contactHouseNumber", Short.toString(user.getUserContact().getContactHouseNumber())); //NOI18N - } - if (user.getUserContact().getContactCity() != null) { - properties.setProperty("contactCity", user.getUserContact().getContactCity()); //NOI18N - } - if (user.getUserContact().getContactZipCode() != null) { - properties.setProperty("contactZipCode", Integer.toString(user.getUserContact().getContactZipCode())); //NOI18N - } - if (user.getUserContact().getContactBirthday() != null) { - properties.setProperty("contactBirthday", user.getUserContact().getContactBirthday().toString()); //NOI18N - } - if (user.getUserContact().getContactEmailAddress() != null) { - properties.setProperty("contactEmailAddress", user.getUserContact().getContactEmailAddress()); //NOI18N - } - properties.setProperty("contactCreated", user.getUserContact().getContactEntryCreated().toString()); //NOI18N - if (user.getUserContact().getContactEntryUpdated() != null) { - properties.setProperty("contactUpdated", user.getUserContact().getContactEntryUpdated().toString()); //NOI18N - } - - // Return it - return properties; - } - - /** - * Returns a formatted string from given user's locale and Date instance - *

- * @param user User instance - * @param date Date instance - *

- * @return A formatted string from Date instance - */ - public static String getFormattedTimestampFromUser (final User user, final Date date) { - // Validate parameter - if (null == user) { - // Throw NPE - throw new NullPointerException("Parameter 'user' is null"); //NOI18N - } else if (null == date) { - // Throw NPE - throw new NullPointerException("Parameter 'date' is null"); //NOI18N - } - - // Get formatter - final DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, user.getUserLocale()); - - // Now simply format the calendar's Time (not just time) field - final String dateTime = format.format(date.getTime()); - - // Return it - return dateTime; - } - - /** - * No instance from this class - */ - private Users () { - } - -} diff --git a/src/org/mxchange/jusercore/model/utils/UserUtils.java b/src/org/mxchange/jusercore/model/utils/UserUtils.java new file mode 100644 index 0000000..6a21724 --- /dev/null +++ b/src/org/mxchange/jusercore/model/utils/UserUtils.java @@ -0,0 +1,312 @@ +/* + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.mxchange.jusercore.model.utils; + +import java.io.Serializable; +import java.security.SecureRandom; +import java.text.DateFormat; +import java.text.MessageFormat; +import java.util.Date; +import java.util.Objects; +import java.util.Properties; +import java.util.Random; +import org.mxchange.jcontacts.model.contact.Contact; +import org.mxchange.jcontacts.model.utils.ContactUtils; +import org.mxchange.jusercore.model.user.User; + +/** + * An utilities class for users + *

+ * @author Roland Häder + */ +public class UserUtils implements Serializable { + + /** + * Password alphabet + */ + private static final String PASSWORD_ALPHABET; + + /** + * Password alphabet parts + */ + private static final String[] PASSWORD_ALPHABET_PARTS = { + // lower-case + "abcdefghijklmnopqrstuvwxyz", //NOI18N + + // upper-case + "ABCDEFGHIJKLMNOPQRSTUVWXYZ", //NOI18N + + // numbers + "0123456789", //NOI18N + + // characters + "~^!$%&/()=?{[]}@+*#-_,.;:<|>" //NOI18N + }; + + /** + * Hard-coded minimum password length + */ + private static final Integer PASSWORD_MINIMUM_LENGTH = 5; + + /** + * Random number generator + */ + private static final Random RANDOM_NUMBER_GENERATOR; + + /** + * Serial number + */ + private static final long serialVersionUID = 18_356_847_120_972L; + + /** + * Static initializer + */ + static { + // Init RNG + RANDOM_NUMBER_GENERATOR = new SecureRandom(); + + // Init alphabet + PASSWORD_ALPHABET = UserUtils.PASSWORD_ALPHABET_PARTS[0] + + UserUtils.PASSWORD_ALPHABET_PARTS[1] + + UserUtils.PASSWORD_ALPHABET_PARTS[2] + + UserUtils.PASSWORD_ALPHABET_PARTS[3]; + } + + /** + * Compares both user instances. This method returns -1 if second instance + * is null. + *

+ * @param user1 User instance 1 + * @param user2 User instance 2 + *

+ * @return Comparison value + */ + public static int compare (final User user1, final User user2) { + // Check equality, then at least first must be given + if (Objects.equals(user1, user2)) { + // Both are same + return 0; + } else if (null == user1) { + // First is null + return -1; + } else if (null == user2) { + // Second is null + return 1; + } + + // Invoke compare() method + return user1.compareTo(user2); + } + + /** + * Copies all attributes from other user object to target + *

+ * @param sourceUser Source instance + * @param targetUser Target instance + */ + public static void copyUserData (final User sourceUser, final User targetUser) { + // Check all parameter + if (null == sourceUser) { + // Throw NPE + throw new NullPointerException("sourceUser is null"); //NOI18N + } else if (null == targetUser) { + // Throw NPE + throw new NullPointerException("targetUser is null"); //NOI18N + } else if (Objects.equals(sourceUser, targetUser)) { + // Is exactly the same! + throw new IllegalArgumentException("sourcerUser and targetUser are the same."); //NOI18N + } + + // Is contact set? + if (sourceUser.getUserContact() instanceof Contact) { + // Copy also contact data + ContactUtils.copyContactData(sourceUser.getUserContact(), targetUser.getUserContact()); + } + + // Copy other data + targetUser.setUserConfirmKey(sourceUser.getUserConfirmKey()); + targetUser.setUserName(sourceUser.getUserName()); + targetUser.setUserEncryptedPassword(sourceUser.getUserEncryptedPassword()); + targetUser.setUserAccountStatus(sourceUser.getUserAccountStatus()); + targetUser.setUserLastLocked(sourceUser.getUserLastLocked()); + targetUser.setUserLastLockedReason(sourceUser.getUserLastLockedReason()); + targetUser.setUserEntryUpdated(sourceUser.getUserEntryUpdated()); + targetUser.setUserProfileMode(sourceUser.getUserProfileMode()); + targetUser.setUserLocale(sourceUser.getUserLocale()); + targetUser.setUserMustChangePassword(sourceUser.getUserMustChangePassword()); + } + + /** + * Generates a pseudo-random user name + *

+ * @return User name + */ + public static String generateRandomUserName () { + // User name's format is normally "user" + random number (10 digits) + String userName = String.format("user%d", RANDOM_NUMBER_GENERATOR.nextInt(10000000)); //NOI18N + + // Return it + return userName; + } + + /** + * Returns a Properties object from given user instance. + *

+ * @param user User instance to get all fields from + *

+ * @return All properties from given user + */ + public static Properties getAllUserFields (final User user) { + // Parameter should be valid + if (null == user) { + // Throw NPE + throw new NullPointerException("user is null"); //NOI18N + } else if (user.getUserId() == null) { + // Throw NPE again + throw new NullPointerException("user.userId is null"); //NOI18N + } else if (user.getUserId() < 1) { + // Not valid number + throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N + } else if (user.getUserName() == null) { + // Throw NPE again + throw new NullPointerException("user.userName is null"); //NOI18N + } else if (user.getUserName().isEmpty()) { + // Empty string + throw new IllegalArgumentException("user.userName is empty"); //NOI18N + } else if (user.getUserAccountStatus() == null) { + // Throw NPE + throw new NullPointerException("user.userAccountStatus is null"); //NOI18N + } else if (user.getUserContact() == null) { + // Throw it again + throw new NullPointerException("user.userContact is null"); //NOI18N + } else if (user.getUserContact().getContactId() == null) { + // .. and again + throw new NullPointerException("user.userContact.contactId is null"); //NOI18N + } else if (user.getUserContact().getContactId() < 1) { + // Invalid id + throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is invalid", user.getUserContact().getContactId())); //NOI18N + } else if (user.getUserContact().getContactPersonalTitle() == null) { + // Throw NPE again + throw new NullPointerException("user.userContact.contactPersonalTitle is null"); //NOI18N + } + + // Init properties list + final Properties properties = new Properties(); + + // Init some vbalues with empty strings + properties.setProperty("userConfirmKey", ""); //NOI18N + properties.setProperty("userUpdated", ""); //NOI18N + properties.setProperty("userLastLockedReason", ""); //NOI18N + properties.setProperty("contactTitle", ""); //NOI18N + properties.setProperty("contactStreet", ""); //NOI18N + properties.setProperty("contactHouseNumber", ""); //NOI18N + properties.setProperty("contactCity", ""); //NOI18N + // @TODO Unfinished: + properties.setProperty("contactCountry", ""); //NOI18N + properties.setProperty("contactZipCode", ""); //NOI18N + properties.setProperty("contactBirthday", ""); //NOI18N + properties.setProperty("contactEmailAddress", ""); //NOI18N + properties.setProperty("contactUpdated", ""); //NOI18N + + // Set all: + // - User data + properties.setProperty("userId", Long.toString(user.getUserId())); //NOI18N + properties.setProperty("userName", user.getUserName()); //NOI18N + properties.setProperty("userCreated", getFormattedTimestampFromUser(user, user.getUserEntryCreated())); //NOI18N + if (user.getUserEntryUpdated() != null) { + properties.setProperty("userUpdated", getFormattedTimestampFromUser(user, user.getUserEntryUpdated())); //NOI18N + } + if (user.getUserConfirmKey() != null) { + properties.setProperty("userConfirmKey", user.getUserConfirmKey()); //NOI18N + } + if (user.getUserLastLockedReason() != null) { + properties.setProperty("userLastLockedReason", user.getUserLastLockedReason()); //NOI18N + } + if (user.getUserLastLocked() != null) { + properties.setProperty("userLastLocked", getFormattedTimestampFromUser(user, user.getUserLastLocked())); //NOI18N + } + properties.setProperty("userAccountStatus", user.getUserAccountStatus().toString()); //NOI18N + + // - Contact data + properties.setProperty("contactPersonalTitle", user.getUserContact().getContactPersonalTitle().toString()); //NOI18N + if (user.getUserContact().getContactTitle() != null) { + properties.setProperty("contactTitle", user.getUserContact().getContactTitle()); //NOI18N + } + properties.setProperty("contactFirstName", user.getUserContact().getContactFirstName()); //NOI18N + properties.setProperty("contactFamilyName", user.getUserContact().getContactFamilyName()); //NOI18N + if (user.getUserContact().getContactStreet() != null) { + properties.setProperty("contactStreet", user.getUserContact().getContactStreet()); //NOI18N + } + if (user.getUserContact().getContactHouseNumber() != null) { + properties.setProperty("contactHouseNumber", Short.toString(user.getUserContact().getContactHouseNumber())); //NOI18N + } + if (user.getUserContact().getContactCity() != null) { + properties.setProperty("contactCity", user.getUserContact().getContactCity()); //NOI18N + } + if (user.getUserContact().getContactZipCode() != null) { + properties.setProperty("contactZipCode", Integer.toString(user.getUserContact().getContactZipCode())); //NOI18N + } + if (user.getUserContact().getContactBirthday() != null) { + properties.setProperty("contactBirthday", user.getUserContact().getContactBirthday().toString()); //NOI18N + } + if (user.getUserContact().getContactEmailAddress() != null) { + properties.setProperty("contactEmailAddress", user.getUserContact().getContactEmailAddress()); //NOI18N + } + properties.setProperty("contactCreated", user.getUserContact().getContactEntryCreated().toString()); //NOI18N + if (user.getUserContact().getContactEntryUpdated() != null) { + properties.setProperty("contactUpdated", user.getUserContact().getContactEntryUpdated().toString()); //NOI18N + } + + // Return it + return properties; + } + + /** + * Returns a formatted string from given user's locale and Date instance + *

+ * @param user User instance + * @param date Date instance + *

+ * @return A formatted string from Date instance + */ + public static String getFormattedTimestampFromUser (final User user, final Date date) { + // Validate parameter + if (null == user) { + // Throw NPE + throw new NullPointerException("Parameter 'user' is null"); //NOI18N + } else if (null == date) { + // Throw NPE + throw new NullPointerException("Parameter 'date' is null"); //NOI18N + } + + // Get formatter + final DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, user.getUserLocale()); + + // Now simply format the calendar's Time (not just time) field + final String dateTime = format.format(date.getTime()); + + // Return it + return dateTime; + } + + /** + * No instance from this class + */ + private UserUtils () { + } + +}