]> git.mxchange.org Git - jfinancials-mailer-ejb.git/blobdiff - src/java/org/mxchange/jusercore/model/user/UserSessionBean.java
better throw EJBException here
[jfinancials-mailer-ejb.git] / src / java / org / mxchange / jusercore / model / user / UserSessionBean.java
index af59b8ebc26d132ec32fecf904c78cb46ed6ea49..2f0cb500b9900248c0a12e8b709353996369ca7d 100644 (file)
@@ -19,11 +19,16 @@ package org.mxchange.jusercore.model.user;
 import java.text.MessageFormat;
 import java.util.GregorianCalendar;
 import java.util.List;
+import javax.ejb.EJBException;
 import javax.ejb.Stateless;
 import javax.persistence.NoResultException;
 import javax.persistence.PersistenceException;
 import javax.persistence.Query;
+import org.mxchange.jcontacts.contact.Contact;
 import org.mxchange.jcoreee.database.BaseDatabaseBean;
+import org.mxchange.jphone.phonenumbers.cellphone.DialableCellphoneNumber;
+import org.mxchange.jphone.phonenumbers.fax.DialableFaxNumber;
+import org.mxchange.jphone.phonenumbers.landline.DialableLandLineNumber;
 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
 
@@ -155,6 +160,56 @@ public class UserSessionBean extends BaseDatabaseBean implements UserSessionBean
                return userNameList;
        }
 
+       @Override
+       public boolean ifUserExists (final User user) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: user={0} - CALLED!", user)); //NOI18N
+
+               // userId should not be null
+               if (null == user) {
+                       // Abort here
+                       throw new NullPointerException("user is null"); //NOI18N
+               } else if (user.getUserId() == null) {
+                       // Abort here
+                       throw new NullPointerException("user.userId is null"); //NOI18N
+               } else if (user.getUserId() < 1) {
+                       // Invalid number
+                       throw new IllegalArgumentException(MessageFormat.format("userId is not valid: {0}", user.getUserId())); //NOI18N
+               }
+
+               // Generate query
+               Query query = this.getEntityManager().createNamedQuery("SearchUserId", LoginUser.class); //NOI18N
+
+               // Set parameter
+               query.setParameter("id", user.getUserId()); //NOI18N
+
+               // Try this
+               try {
+                       User dummy = (User) query.getSingleResult();
+
+                       // Debug message
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: dummy.id={0} found.", dummy.getUserId())); //NOI18N
+               } catch (final NoResultException ex) {
+                       // Log it
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("ifUserExists: getSingleResult() returned no result: {0}", ex)); //NOI18N
+
+                       // User name does not exist
+                       return false;
+               } catch (final PersistenceException ex) {
+                       // Something bad happened
+                       this.getLoggerBeanLocal().logWarning(MessageFormat.format("More than one user {0} found.", user, ex)); //NOI18N
+
+                       // Throw again
+                       throw ex;
+               }
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("ifUserExists: Found user {0} - EXIT!", user)); //NOI18N
+
+               // Found it
+               return true;
+       }
+
        @Override
        public boolean ifUserIdExists (final Long userId) {
                // Trace message
@@ -286,19 +341,25 @@ public class UserSessionBean extends BaseDatabaseBean implements UserSessionBean
 
        @Override
        public void updateUserPersonalData (final User user) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("updateUserPersonalData: user={0} - CALLED!", user));
+
                // user should not be null
                if (null == user) {
                        // Abort here
                        throw new NullPointerException("user is null"); //NOI18N
                } else if (user.getUserId() == null) {
                        // Throw NPE again
-                       throw new NullPointerException("user.userId is null");
+                       throw new NullPointerException("user.userId is null"); //NOI18N
                } else if (user.getUserId() < 1) {
                        // Not valid
-                       throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId()));
-               } else if (!this.ifUserIdExists(user.getUserId())) {
+                       throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId())); //NOI18N
+               } else if (user.getUserAccountStatus() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("user.userAccountStatus is null"); //NOI18N
+               } else if (!this.ifUserExists(user)) {
                        // User does not exist
-                       throw new PersistenceException(MessageFormat.format("User with id {0} does not exist.", user.getUserId()));
+                       throw new EJBException(MessageFormat.format("User with id {0} does not exist.", user.getUserId())); //NOI18N
                }
 
                // Find the instance
@@ -313,14 +374,114 @@ public class UserSessionBean extends BaseDatabaseBean implements UserSessionBean
                // Should be found!
                assert (detachedUser instanceof User) : MessageFormat.format("User with id {0} not merged, but should be.", user.getUserId()); //NOI18N
 
-               // Strange things needs to be logged
-               this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: user.userAccountStatus={0},foundUser.userAccountStatus={1},user.userContact.phoneId={2},detachedUser.userContact.phoneId={3}", user.getUserAccountStatus(), detachedUser.getUserAccountStatus(), user.getUserContact().getContactLandLineNumber().getPhoneId(), detachedUser.getUserContact().getContactLandLineNumber().getPhoneId())); //NOI18N
-
                // Copy all data
                detachedUser.copyAll(user);
 
                // Set as updated
                detachedUser.setUserUpdated(new GregorianCalendar());
                detachedUser.getUserContact().setContactUpdated(new GregorianCalendar());
+
+               // Get contact from it and find it
+               Contact foundContact = this.getEntityManager().find(user.getUserContact().getClass(), user.getUserContact().getContactId());
+
+               // Should be found
+               assert (foundContact instanceof Contact) : MessageFormat.format("Contact with id {0} not found, but should be.", user.getUserContact().getContactId()); //NOI18N
+
+               // Debug message
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: contact.contactId={0}", foundContact.getContactId()));
+
+               // Merge contact instance
+               Contact detachedContact = this.getEntityManager().merge(foundContact);
+
+               // Copy all
+               detachedContact.copyAll(user.getUserContact());
+
+               // Set it back in user
+               user.setUserContact(detachedContact);
+
+               // Should be found!
+               assert (detachedContact instanceof Contact) : MessageFormat.format("Contact with id {0} not merged, but should be.", user.getUserContact().getContactId()); //NOI18N
+
+               // Get cellphone instance
+               DialableCellphoneNumber cellphone = detachedContact.getContactCellphoneNumber();
+
+               // Is there a  cellphone instance set?
+               if (cellphone instanceof DialableCellphoneNumber) {
+                       // Debug message
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: cellphone.phoneId={0} is being updated ...", cellphone.getPhoneId()));
+
+                       // Then find it, too
+                       DialableCellphoneNumber foundCellphone = this.getEntityManager().find(cellphone.getClass(), cellphone.getPhoneId());
+
+                       // Should be there
+                       assert (foundCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", foundCellphone.getPhoneId());
+
+                       // Then merge it, too
+                       DialableCellphoneNumber detachedCellphone = this.getEntityManager().merge(foundCellphone);
+
+                       // Should be there
+                       assert (detachedCellphone instanceof DialableCellphoneNumber) : MessageFormat.format("Cellphone number with id {0} not found but should be.", detachedCellphone.getPhoneId());
+
+                       // Copy all
+                       detachedCellphone.copyAll(user.getUserContact().getContactCellphoneNumber());
+
+                       // Set it back
+                       detachedContact.setContactCellphoneNumber(detachedCellphone);
+               }
+
+               // Get cellphone instance
+               DialableFaxNumber fax = detachedContact.getContactFaxNumber();
+
+               // Is there a  fax instance set?
+               if (fax instanceof DialableFaxNumber) {
+                       // Debug message
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: fax.phoneId={0} is being updated ...", fax.getPhoneId()));
+
+                       // Then find it, too
+                       DialableFaxNumber foundFax = this.getEntityManager().find(fax.getClass(), fax.getPhoneId());
+
+                       // Should be there
+                       assert (foundFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", foundFax.getPhoneId());
+
+                       // Then merge it, too
+                       DialableFaxNumber detachedFax = this.getEntityManager().merge(foundFax);
+
+                       // Should be there
+                       assert (detachedFax instanceof DialableFaxNumber) : MessageFormat.format("Fax number with id {0} not found but should be.", detachedFax.getPhoneId());
+
+                       // Copy all
+                       detachedFax.copyAll(user.getUserContact().getContactFaxNumber());
+
+                       // Set it back
+                       detachedContact.setContactFaxNumber(detachedFax);
+               }
+
+               // Get cellphone instance
+               DialableLandLineNumber landLine = detachedContact.getContactLandLineNumber();
+
+               // Is there a  fax instance set?
+               if (landLine instanceof DialableLandLineNumber) {
+                       // Debug message
+                       this.getLoggerBeanLocal().logDebug(MessageFormat.format("updateUserPersonalData: landLine.phoneId={0} is being updated ...", landLine.getPhoneId()));
+
+                       // Then find it, too
+                       DialableLandLineNumber foundLandLine = this.getEntityManager().find(landLine.getClass(), landLine.getPhoneId());
+
+                       // Should be there
+                       assert (foundLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", foundLandLine.getPhoneId());
+
+                       // Then merge it, too
+                       DialableLandLineNumber detachedLandLine = this.getEntityManager().merge(foundLandLine);
+
+                       // Should be there
+                       assert (detachedLandLine instanceof DialableLandLineNumber) : MessageFormat.format("Land line number with id {0} not found but should be.", detachedLandLine.getPhoneId());
+
+                       // Copy all
+                       detachedLandLine.copyAll(user.getUserContact().getContactLandLineNumber());
+
+                       // Set it back
+                       detachedContact.setContactLandLineNumber(detachedLandLine);
+               }
        }
+
 }