+++ /dev/null
-/*
- * Copyright (C) 2016 - 2018 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 <http://www.gnu.org/licenses/>.
- */
-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.Properties;
-import java.util.Random;
-import org.mxchange.jcontacts.model.contact.Contact;
-import org.mxchange.jcontacts.model.contact.Contacts;
-
-/**
- * An utilities class for users
- * <p>
- * @author Roland Häder<roland@mxchange.org>
- */
-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];
- }
-
- /**
- * Copies all attributes from other user object to target
- * <p>
- * @param sourceUser Source instance
- * @param targetUser Target instance
- */
- public static void copyAll (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
- }
-
- // Is contact set?
- if (sourceUser.getUserContact() instanceof Contact) {
- // Copy also contact data
- Contacts.copyAll(sourceUser.getUserContact(), targetUser.getUserContact());
- }
-
- // Copy other data
- targetUser.setUserConfirmKey(sourceUser.getUserConfirmKey());
- targetUser.setUserName(sourceUser.getUserName());
- targetUser.setUserEncryptedPassword(sourceUser.getUserEncryptedPassword());
- targetUser.setUserAccountStatus(sourceUser.getUserAccountStatus());
- targetUser.setUserCreated(sourceUser.getUserCreated());
- targetUser.setUserLastLocked(sourceUser.getUserLastLocked());
- targetUser.setUserLastLockedReason(sourceUser.getUserLastLockedReason());
- targetUser.setUserUpdated(sourceUser.getUserUpdated());
- targetUser.setUserProfileMode(sourceUser.getUserProfileMode());
- targetUser.setUserLocale(sourceUser.getUserLocale());
- targetUser.setUserMustChangePassword(sourceUser.getUserMustChangePassword());
- }
-
- /**
- * Generates a pseudo-random user name
- * <p>
- * @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.
- * <p>
- * @param user User instance to get all fields from
- * <p>
- * @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
- 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.getUserCreated())); //NOI18N
- if (user.getUserUpdated() != null) {
- properties.setProperty("userUpdated", getFormattedTimestampFromUser(user, user.getUserUpdated())); //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().getContactCreated().toString()); //NOI18N
- if (user.getUserContact().getContactUpdated() != null) {
- properties.setProperty("contactUpdated", user.getUserContact().getContactUpdated().toString()); //NOI18N
- }
-
- // Return it
- return properties;
- }
-
- /**
- * Returns a formatted string from given user's locale and Date instance
- * <p>
- * @param user User instance
- * @param date Date instance
- * <p>
- * @return A formatted string from Date instance
- */
- public static String getFormattedTimestampFromUser (final User user, final Date date) {
- // Get formatter
- DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, user.getUserLocale());
-
- // Now simply format the calendar's Time (not just time) field
- String dateTime = format.format(date.getTime());
-
- // Return it
- return dateTime;
- }
-
- /**
- * No instance from this class
- */
- private UserUtils () {
- }
-
-}