]> git.mxchange.org Git - jjobs-ejb.git/blobdiff - src/java/org/mxchange/jusercore/model/user/JobsAdminUserSessionBean.java
Updated copyright year
[jjobs-ejb.git] / src / java / org / mxchange / jusercore / model / user / JobsAdminUserSessionBean.java
index 5b7eb69104a99660d5d8f41c6be71d0389c9f591..f1c95218c98713a9448ec422b25cdb6aa2be0d1d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2016 Roland Haeder
+ * Copyright (C) 2016 - 2024 Free Software Foundation
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as
 package org.mxchange.jusercore.model.user;
 
 import java.text.MessageFormat;
-import java.util.GregorianCalendar;
+import java.util.Date;
 import javax.ejb.EJB;
-import javax.ejb.EJBException;
 import javax.ejb.Stateless;
-import javax.mail.Address;
-import javax.mail.internet.AddressException;
-import javax.mail.internet.InternetAddress;
-import org.mxchange.jcontacts.contact.Contact;
-import org.mxchange.jjobs.database.BaseJobsDatabaseBean;
+import org.mxchange.jcontacts.model.contact.Contact;
+import org.mxchange.jjobs.enterprise.BaseJobsEnterpriseBean;
 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.register.UserRegistrationSessionBeanRemote;
 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
 
 /**
  * An administrative user EJB
  * <p>
- * @author Roland Haeder<roland@mxchange.org>
+ * @author Roland Hรคder<roland@mxchange.org>
  */
 @Stateless (name = "adminUser", description = "A bean handling the user data")
-public class JobsAdminUserSessionBean extends BaseJobsDatabaseBean implements AdminUserSessionBeanRemote {
+public class JobsAdminUserSessionBean extends BaseJobsEnterpriseBean implements AdminUserSessionBeanRemote {
 
        /**
         * Serial number
         */
-       private static final long serialVersionUID = 542_145_347_916L;
-
-       /**
-        * Registration EJB
-        */
-       @EJB
-       private UserRegistrationSessionBeanRemote registerBean;
+       private static final long serialVersionUID = 542_145_349_001L;
 
        /**
         * Regular user bean
         */
-       @EJB
+       @EJB (lookup = "java:global/jjobs-ejb/user!org.mxchange.jusercore.model.user.UserSessionBeanRemote")
        private UserSessionBeanRemote userBean;
 
        /**
         * Default constructor
         */
        public JobsAdminUserSessionBean () {
+               // Call super constructor
+               super("jms/jjobs-queue-factory", "jms/jjobs-email-queue"); //NOI18N
        }
 
        @Override
@@ -71,37 +62,40 @@ public class JobsAdminUserSessionBean extends BaseJobsDatabaseBean implements Ad
                // Trace message
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.addUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
 
-               // user should not be null
+               // Validate parameter
                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
+               } else if (null == user.getUserContact()) {
+                       // Abort here
+                       throw new NullPointerException("user.contact is null"); //NOI18N
+               } else if (user.getUserContact().getContactId() != null) {
+                       // Not allowed here
+                       throw new IllegalStateException(MessageFormat.format("user.userContact.contactId must be null, is: {0}", user.getUserId())); //NOI18N
                }
 
                // Check if user is registered
-               if (this.registerBean.isUserNameRegistered(user)) {
+               if (this.userBean.isUserNameRegistered(user)) {
                        // Abort here
                        throw new UserNameAlreadyRegisteredException(user);
-               } else if (this.registerBean.isEmailAddressRegistered(user)) {
+               } else if (this.userBean.isEmailAddressRegistered(user)) {
                        // Abort here
                        throw new EmailAddressAlreadyRegisteredException(user);
                }
 
                // Set created timestamp
-               user.setUserCreated(new GregorianCalendar());
-               user.getUserContact().setContactCreated(new GregorianCalendar());
+               user.setUserEntryCreated(new Date());
+               user.getUserContact().setContactEntryCreated(new Date());
 
-               // Update cellphone, land-line and fax instance
-               this.setAllContactPhoneEntriesCreated(user.getUserContact());
+               // Update mobile, land-line and fax instance
+               this.setAllPhoneEntriesCreated(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
 
@@ -114,13 +108,16 @@ public class JobsAdminUserSessionBean extends BaseJobsDatabaseBean implements Ad
                // Trace message
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: user={1} - CALLED!", this.getClass().getSimpleName(), user)); //NOI18N
 
-               // user should not be null
+               // Validate parameters
                if (null == user) {
                        // Abort here
                        throw new NullPointerException("user is null"); //NOI18N
-               } else if (user.getUserId() instanceof Long) {
+               } else if (user.getUserId() == null) {
                        // Id is set
-                       throw new IllegalArgumentException("user.userId is not null"); //NOI18N
+                       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
@@ -139,13 +136,13 @@ public class JobsAdminUserSessionBean extends BaseJobsDatabaseBean implements Ad
                }
 
                // Get a managed instance
-               User managedUser = this.getManagedUser(user);
+               final User managedUser = this.createManaged(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().refresh(managedUser);
+               this.getEntityManager().remove(managedUser);
 
                // Trace message
                this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.deleteUser: EXIT!", this.getClass().getSimpleName())); //NOI18N
@@ -181,23 +178,17 @@ public class JobsAdminUserSessionBean extends BaseJobsDatabaseBean implements Ad
                }
 
                // 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
+               final Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
 
                // Set detached object in rexcruiter instance
-               user.setUserContact(managedContact);
+               user.setUserContact(foundContact);
 
                // Set timestamp
-               user.setUserCreated(new GregorianCalendar());
+               user.setUserEntryCreated(new Date());
 
                // 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
 
@@ -247,6 +238,12 @@ public class JobsAdminUserSessionBean extends BaseJobsDatabaseBean implements Ad
                } else if (userLockReason.isEmpty()) {
                        // Is empty
                        throw new IllegalArgumentException("userLockReason is empty"); //NOI18N
+               } else if (null == baseUrl) {
+                       // Throw NPE again
+                       throw new NullPointerException("baseUrl is null"); //NOI18N
+               } else if (baseUrl.isEmpty()) {
+                       // Throw IAE
+                       throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
                }
 
                // Remove contact instance as this is not updated
@@ -254,27 +251,16 @@ public class JobsAdminUserSessionBean extends BaseJobsDatabaseBean implements Ad
 
                // Set as locked, set timestamp and lock reason
                user.setUserAccountStatus(UserAccountStatus.LOCKED);
-               user.setUserLastLocked(new GregorianCalendar());
+               user.setUserLastLocked(new Date());
                user.setUserLastLockedReason(userLockReason);
 
                // Update user
-               User managedUser = this.userBean.updateUserData(user);
+               final User managedUser = this.userBean.updateUserData(user);
 
                // @TODO Create user lock history entry
-               // Init variable
-               Address emailAddress;
-
-               try {
-                       // Create email address and set
-                       emailAddress = new InternetAddress(managedUser.getUserContact().getContactEmailAddress());
-               } catch (final AddressException ex) {
-                       // Throw again
-                       throw new EJBException(ex);
-               }
-
                // Send out email
                // @TODO externalize subject line
-               this.sendEmail("Account locked", "account_locked", emailAddress, managedUser, baseUrl); //NOI18N
+               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
@@ -319,6 +305,12 @@ public class JobsAdminUserSessionBean extends BaseJobsDatabaseBean implements Ad
                } else if (user.getUserAccountStatus() == UserAccountStatus.UNCONFIRMED) {
                        // Account is unconfirmed
                        throw new UserStatusUnconfirmedException(user);
+               } else if (null == baseUrl) {
+                       // Throw NPE again
+                       throw new NullPointerException("baseUrl is null"); //NOI18N
+               } else if (baseUrl.isEmpty()) {
+                       // Throw IAE
+                       throw new IllegalArgumentException("baseUrl is empty"); //NOI18N
                }
 
                // Remove contact instance as this is not updated
@@ -328,23 +320,12 @@ public class JobsAdminUserSessionBean extends BaseJobsDatabaseBean implements Ad
                user.setUserAccountStatus(UserAccountStatus.CONFIRMED);
 
                // Update user
-               User managedUser = this.userBean.updateUserData(user);
+               final User managedUser = this.userBean.updateUserData(user);
 
                // @TODO Create user lock history entry
-               // Init variable
-               Address emailAddress;
-
-               try {
-                       // Create email address and set
-                       emailAddress = new InternetAddress(managedUser.getUserContact().getContactEmailAddress());
-               } catch (final AddressException ex) {
-                       // Throw again
-                       throw new EJBException(ex);
-               }
-
                // Send out email
                // @TODO externalize subject line
-               this.sendEmail("Account unlocked", "account_unlocked", emailAddress, managedUser, baseUrl); //NOI18N
+               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