]> git.mxchange.org Git - addressbook-ejb.git/blobdiff - src/java/org/mxchange/jcontactsbusiness/model/branchoffice/AddressbookAdminBranchOfficeSessionBean.java
Don't cherry-pick:
[addressbook-ejb.git] / src / java / org / mxchange / jcontactsbusiness / model / branchoffice / AddressbookAdminBranchOfficeSessionBean.java
index 05a7fd1371d73a83526f633167b0dbd8e7dd627d..e158137edbd9c344a72b74e48ae8e72bf04a6707 100644 (file)
@@ -23,9 +23,12 @@ import java.util.Objects;
 import javax.ejb.EJB;
 import javax.ejb.Stateless;
 import org.mxchange.addressbook.enterprise.BaseAddressbookEnterpriseBean;
+import org.mxchange.jcontacts.model.contact.Contact;
 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeAlreadyAddedException;
+import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeNotFoundException;
 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
 import org.mxchange.jcontactsbusiness.model.opening_time.OpeningTime;
+import org.mxchange.jcontactsbusiness.model.utils.BranchOfficeUtils;
 import org.mxchange.jcountry.model.data.Country;
 import org.mxchange.jusercore.model.user.User;
 
@@ -45,7 +48,7 @@ public class AddressbookAdminBranchOfficeSessionBean extends BaseAddressbookEnte
        /**
         * General branch office bean
         */
-       @EJB (lookup = "java:global/addressbook-ejb/branchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote")
+       @EJB (lookup = "java:global/jfinancials-ejb/branchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote")
        private BranchOfficeSessionBeanRemote branchOfficeBean;
 
        /**
@@ -76,6 +79,24 @@ public class AddressbookAdminBranchOfficeSessionBean extends BaseAddressbookEnte
                // Add created timestamp
                branchOffice.setBranchEntryCreated(new Date());
 
+               // Is contact employee set?
+               if (branchOffice.getBranchContactEmployee() instanceof Contact) {
+                       // Get managed lead contact
+                       final Contact managedContact = this.createManaged(branchOffice.getBranchContactEmployee());
+
+                       // Set it back
+                       branchOffice.setBranchContactEmployee(managedContact);
+               }
+
+               // Is owner employee set?
+               if (branchOffice.getBranchOwnerEmployee() instanceof Contact) {
+                       // Get managed lead contact
+                       final Contact managedContact = this.createManaged(branchOffice.getBranchOwnerEmployee());
+
+                       // Set it back
+                       branchOffice.setBranchOwnerEmployee(managedContact);
+               }
+
                // Is user instance set?
                if (branchOffice.getBranchCompany() instanceof BasicData) {
                        // Get managed instance back
@@ -110,7 +131,7 @@ public class AddressbookAdminBranchOfficeSessionBean extends BaseAddressbookEnte
                final List<OpeningTime> openingTimes = branchOffice.getBranchOpeningTimes();
 
                // Debugging:
-               this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.addBranchOffice(): branchOffice.branchOfficeOpeningTimes={1}", this.getClass().getSimpleName(), openingTimes));
+               this.getLoggerBeanLocal().logDebug(MessageFormat.format("{0}.addBranchOffice(): branchOffice.branchOfficeOpeningTimes={1}", this.getClass().getSimpleName(), openingTimes)); //NOI18N
 
                // Is opening times set and not empty?
                if ((openingTimes instanceof List) && (!openingTimes.isEmpty())) {
@@ -131,6 +152,36 @@ public class AddressbookAdminBranchOfficeSessionBean extends BaseAddressbookEnte
                return branchOffice;
        }
 
+       @Override
+       public BranchOffice updateBranchOffice (final BranchOffice branchOffice) throws BranchOfficeNotFoundException {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateBranchOffice(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
+
+               // Is parameter valid?
+               if (null == branchOffice) {
+                       // Throw NPE
+                       throw new NullPointerException("branchOffice is null"); //NOI18N
+               } else if (branchOffice.getBranchId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("branchOffice.branchId is null"); //NOI18N
+               } else if (branchOffice.getBranchId() < 1) {
+                       // Throw IAE again
+                       throw new IllegalArgumentException(MessageFormat.format("branchOffice.branchId={0} is invalid", branchOffice.getBranchId())); //NOI18N
+               } else if (!this.isBranchOfficeFound(branchOffice)) {
+                       // Not found
+                       throw new BranchOfficeNotFoundException(branchOffice.getBranchId());
+               }
+
+               // Merge data
+               final BranchOffice updatedBranchOffice = this.mergeBranchOfficeData(branchOffice);
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateBranchOffice(): updatedBranchOffice={1} - EXIT!", this.getClass().getSimpleName(), updatedBranchOffice));
+
+               // Return updated instance
+               return updatedBranchOffice;
+       }
+
        /**
         * Checks if given branch office's address is already persisted. The whole
         * (persisted) list is being loaded and each address is being matched
@@ -141,6 +192,9 @@ public class AddressbookAdminBranchOfficeSessionBean extends BaseAddressbookEnte
         * @return Whether it has been found
         */
        private boolean isBranchOfficeFound (final BranchOffice branchOffice) {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isBranchOfficeFound(): branchOffice={1} - CALLED!", this.getClass().getSimpleName(), branchOffice)); //NOI18N
+
                // Get whole list
                final List<BranchOffice> branchOffices = this.branchOfficeBean.fetchAllBranchOffices();
 
@@ -150,13 +204,16 @@ public class AddressbookAdminBranchOfficeSessionBean extends BaseAddressbookEnte
                // Check all single addresses
                for (final BranchOffice bo : branchOffices) {
                        // Is the same address found?
-                       if (Objects.equals(bo.getBranchId(), branchOffice.getBranchId()) || BranchOffices.isSameAddress(bo, branchOffice)) {
+                       if (Objects.equals(bo.getBranchId(), branchOffice.getBranchId()) || BranchOfficeUtils.isSameAddress(bo, branchOffice)) {
                                // Found one
                                isFound = true;
                                break;
                        }
                }
 
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.isBranchOfficeFound(): isFound={1} - EXIT!", this.getClass().getSimpleName(), isFound)); //NOI18N
+
                // Return flag
                return isFound;
        }