+++ /dev/null
-/*
- * Copyright (C) 2016, 2017 Roland Häder
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero 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 Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
-package org.mxchange.jusercore.model.user;
-
-import java.text.MessageFormat;
-import java.util.GregorianCalendar;
-import javax.ejb.EJB;
-import javax.ejb.Stateless;
-import org.mxchange.addressbook.database.BaseAddressbookDatabaseBean;
-import org.mxchange.jcontacts.contact.Contact;
-import org.mxchange.jusercore.exceptions.EmailAddressAlreadyRegisteredException;
-import org.mxchange.jusercore.exceptions.UserNameAlreadyRegisteredException;
-import org.mxchange.jusercore.exceptions.UserNotFoundException;
-import org.mxchange.jusercore.exceptions.UserStatusConfirmedException;
-import org.mxchange.jusercore.exceptions.UserStatusLockedException;
-import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
-import org.mxchange.jusercore.model.user.register.UserRegistrationSessionBeanRemote;
-import org.mxchange.jusercore.model.user.status.UserAccountStatus;
-
-/**
- * An administrative user EJB
- * <p>
- * @author Roland Häder<roland@mxchange.org>
- */
-@Stateless (name = "adminUser", description = "A bean handling the user data")
-public class AddressbookAdminUserSessionBean extends BaseAddressbookDatabaseBean implements AdminUserSessionBeanRemote {
-
- /**
- * Serial number
- */
- private static final long serialVersionUID = 542_145_347_916L;
-
- /**
- * Registration EJB
- */
- @EJB
- private UserRegistrationSessionBeanRemote registerBean;
-
- /**
- * Regular user bean
- */
- @EJB
- private UserSessionBeanRemote userBean;
-
- /**
- * Default constructor
- */
- public AddressbookAdminUserSessionBean () {
- // Call super constructor
- super("jms/addressbook-queue-factory", "jms/addressbook-email-queue"); //NOI18N
- }
-
- @Override
- public User addUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
-
- // user should not be null
- if (null == user) {
- // Abort here
- throw new NullPointerException("user is null"); //NOI18N
- } else if (user.getUserId() instanceof Long) {
- // Not allowed here
- throw new IllegalStateException(MessageFormat.format("user.userId must be null, is: {0}", user.getUserId())); //NOI18N
- }
-
- // Check if user is registered
- if (this.registerBean.isUserNameRegistered(user)) {
- // Abort here
- throw new UserNameAlreadyRegisteredException(user);
- } else if (this.registerBean.isEmailAddressRegistered(user)) {
- // Abort here
- throw new EmailAddressAlreadyRegisteredException(user);
- }
-
- // Set created timestamp
- user.setUserCreated(new GregorianCalendar());
- user.getUserContact().setContactCreated(new GregorianCalendar());
-
- // Update cellphone, land-line and fax instance
- this.setAllContactPhoneEntriesCreated(user.getUserContact());
-
- // Persist it
- this.getEntityManager().persist(user);
-
- // Flush to get id back
- this.getEntityManager().flush();
-
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1},user.userId={2} - EXIT!", this.getClass().getSimpleName(), user, user.getUserId())); //NOI18N
-
- // Return it
- return user;
- }
-
- @Override
- public void deleteUser (final User user, final String userDeleteReason) throws UserNotFoundException {
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
-
- // user should not be null
- if (null == user) {
- // Abort here
- throw new NullPointerException("user is null"); //NOI18N
- } else if (user.getUserId() == null) {
- // Id is set
- throw new NullPointerException("user.userId is null"); //NOI18N
- } else if (user.getUserId() < 1) {
- // Not valid id number
- throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid", user.getUserId())); //NOI18N
- } else if (user.getUserContact() == null) {
- // Throw NPE again
- throw new NullPointerException("user.userContact is null"); //NOI18N
- } else if (user.getUserContact().getContactId() == null) {
- // Throw NPE again
- throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
- } else if (user.getUserContact().getContactId() < 1) {
- // Not valid id number
- throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
- } else if (user.getUserAccountStatus() == null) {
- // Throw NPE again
- throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
- } else if (!this.userBean.ifUserExists(user)) {
- // Name already found
- throw new UserNotFoundException(user);
- }
-
- // Get a managed instance
- User managedUser = this.getManagedUser(user);
-
- // Should be found!
- assert (managedUser instanceof User) : MessageFormat.format("User with id {0} not found, but should be.", user.getUserId()); //NOI18N
-
- // Delete it
- this.getEntityManager().remove(managedUser);
-
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: EXIT!", this.getClass().getSimpleName())); //NOI18N
- }
-
- @Override
- public User linkUser (final User user) throws UserNameAlreadyRegisteredException, EmailAddressAlreadyRegisteredException {
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={0} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
-
- // user should not be null
- if (null == user) {
- // Abort here
- throw new NullPointerException("user is null"); //NOI18N
- } else if (user.getUserId() instanceof Long) {
- // Id is set
- throw new IllegalArgumentException("user.userId is not null"); //NOI18N
- } else if (user.getUserContact() == null) {
- // Throw NPE again
- throw new NullPointerException("user.userContact is null"); //NOI18N
- } else if (user.getUserContact().getContactId() == null) {
- // Throw NPE again
- throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
- } else if (user.getUserContact().getContactId() < 1) {
- // Not valid id number
- throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
- } else if (user.getUserAccountStatus() == null) {
- // Throw NPE again
- throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
- } else if (this.userBean.ifUserNameExists(user.getUserName())) {
- // Name already found
- throw new UserNameAlreadyRegisteredException(user.getUserName());
- }
-
- // Try to find the contact
- Contact managedContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
-
- // Should be found!
- assert (managedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
-
- // Set detached object in rexcruiter instance
- user.setUserContact(managedContact);
-
- // Set timestamp
- user.setUserCreated(new GregorianCalendar());
-
- // Perist it
- this.getEntityManager().persist(user);
-
- // Flush it to get updated instance back
- this.getEntityManager().flush();
-
- // Log trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.linkUser: user={1} - EXIT!", this.getClass().getSimpleName(), user)); //NOI18N
-
- // Return updated instanc
- return user;
- }
-
- @Override
- public User lockUserAccount (final User user, final String userLockReason, final String baseUrl) throws UserStatusLockedException, UserStatusUnconfirmedException, UserNotFoundException {
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},userLockReason={2},baseUrl={3} - CALLED!", this.getClass().getSimpleName(), user, userLockReason, baseUrl)); //NOI18N
-
- // user should not be null
- if (null == user) {
- // Abort here
- throw new NullPointerException("user is null"); //NOI18N
- } else if (user.getUserId() == null) {
- // Id is set
- throw new NullPointerException("user.userId is null"); //NOI18N
- } else if (user.getUserId() < 1) {
- // Id is set
- throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
- } else if (user.getUserContact() == null) {
- // Throw NPE again
- throw new NullPointerException("user.userContact is null"); //NOI18N
- } else if (user.getUserContact().getContactId() == null) {
- // Throw NPE again
- throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
- } else if (user.getUserContact().getContactId() < 1) {
- // Not valid id number
- throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
- } else if (user.getUserAccountStatus() == null) {
- // Throw NPE again
- throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
- } else if (!this.userBean.ifUserExists(user)) {
- // Name already found
- throw new UserNotFoundException(user);
- } else if (user.getUserAccountStatus() == UserAccountStatus.LOCKED) {
- // Account is locked
- throw new UserStatusLockedException(user);
- } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
- // Account is unconfirmed
- throw new UserStatusUnconfirmedException(user);
- } else if (null == userLockReason) {
- // Throw NPE again
- throw new NullPointerException("userLockReason is null"); //NOI18N
- } else if (userLockReason.isEmpty()) {
- // Is empty
- throw new IllegalArgumentException("userLockReason is empty"); //NOI18N
- }
-
- // Remove contact instance as this is not updated
- user.setUserContact(null);
-
- // Set as locked, set timestamp and lock reason
- user.setUserAccountStatus(UserAccountStatus.LOCKED);
- user.setUserLastLocked(new GregorianCalendar());
- user.setUserLastLockedReason(userLockReason);
-
- // Update user
- User managedUser = this.userBean.updateUserData(user);
-
- // @TODO Create user lock history entry
-
- // Send out email
- // @TODO externalize subject line
- this.sendEmail("User account locked", "user_account_locked", managedUser, baseUrl, null); //NOI18N
-
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
-
- // Return detached (and updated) user
- return managedUser;
- }
-
- @Override
- public User unlockUserAccount (final User user, final String baseUrl) throws UserStatusConfirmedException, UserStatusUnconfirmedException, UserNotFoundException {
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: user={1},baseUrl={2} - CALLED!", this.getClass().getSimpleName(), user, baseUrl)); //NOI18N
-
- // user should not be null
- if (null == user) {
- // Abort here
- throw new NullPointerException("user is null"); //NOI18N
- } else if (user.getUserId() == null) {
- // Id is set
- throw new NullPointerException("user.userId is null"); //NOI18N
- } else if (user.getUserId() < 1) {
- // Id is set
- throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is null", user.getUserId())); //NOI18N
- } else if (user.getUserContact() == null) {
- // Throw NPE again
- throw new NullPointerException("user.userContact is null"); //NOI18N
- } else if (user.getUserContact().getContactId() == null) {
- // Throw NPE again
- throw new NullPointerException("user.userContact.contactId is null"); //NOI18N
- } else if (user.getUserContact().getContactId() < 1) {
- // Not valid id number
- throw new IllegalArgumentException(MessageFormat.format("user.userContact.contactId={0} is not valid", user.getUserContact().getContactId())); //NOI18N
- } else if (user.getUserAccountStatus() == null) {
- // Throw NPE again
- throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
- } else if (!this.userBean.ifUserExists(user)) {
- // Name already found
- throw new UserNotFoundException(user);
- } else if (user.getUserAccountStatus() == UserAccountStatus.CONFIRMED) {
- // Account is confirmed
- throw new UserStatusConfirmedException(user);
- } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
- // Account is unconfirmed
- throw new UserStatusUnconfirmedException(user);
- }
-
- // Remove contact instance as this is not updated
- user.setUserContact(null);
-
- // Unlock account
- user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
-
- // Update user
- User managedUser = this.userBean.updateUserData(user);
-
- // @TODO Create user lock history entry
-
- // Send out email
- // @TODO externalize subject line
- this.sendEmail("User account unlocked", "user_account_unlocked", managedUser, baseUrl, null); //NOI18N
-
- // Trace message
- this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.lockUserAccount: managedUser={1} - EXIT!", this.getClass().getSimpleName(), managedUser)); //NOI18N
-
- // Return changed account
- return managedUser;
- }
-
-}