]> git.mxchange.org Git - addressbook-ejb.git/blobdiff - src/java/org/mxchange/jcontactsbusiness/model/department/AddressbookAdminDepartmentSessionBean.java
Don't cherry-pick:
[addressbook-ejb.git] / src / java / org / mxchange / jcontactsbusiness / model / department / AddressbookAdminDepartmentSessionBean.java
index 18d5bfac08fed94e95b6e99f70ef0b9383e04a96..fcb71e0f19ff6d81a387f359aee86e78b2c8b8ae 100644 (file)
@@ -21,12 +21,14 @@ import java.util.Date;
 import java.util.List;
 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.department.DepartmentAlreadyAddedException;
+import org.mxchange.jcontactsbusiness.exceptions.department.DepartmentNotFoundException;
 import org.mxchange.jcontactsbusiness.model.basicdata.BasicData;
 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
-import org.mxchange.addressbook.enterprise.BaseAddressbookEnterpriseBean;
-import org.mxchange.jcontactsbusiness.model.employee.Employable;
 import org.mxchange.jcontactsbusiness.model.headquarter.Headquarter;
+import org.mxchange.jcontactsbusiness.model.utils.DepartmentUtils;
 import org.mxchange.jusercore.model.user.User;
 
 /**
@@ -89,7 +91,7 @@ public class AddressbookAdminDepartmentSessionBean extends BaseAddressbookEnterp
                }
 
                // Set created timestamp
-               department.setDepartmentCreated(new Date());
+               department.setDepartmentEntryCreated(new Date());
 
                // Get managed basic data instance
                final BasicData managedBasicData = this.createManaged(department.getDepartmentCompany());
@@ -116,16 +118,16 @@ public class AddressbookAdminDepartmentSessionBean extends BaseAddressbookEnterp
                }
 
                // Is lead employee set?
-               if (department.getDepartmentLead()instanceof Employable) {
-                       // Get managed lead employee
-                       final Employable managedEmployee = this.createManaged(department.getDepartmentLead());
+               if (department.getDepartmentLead() instanceof Contact) {
+                       // Get managed lead contact
+                       final Contact managedContact = this.createManaged(department.getDepartmentLead());
 
                        // Set it back
-                       department.setDepartmentLead(managedEmployee);
+                       department.setDepartmentLead(managedContact);
                }
 
                // Is "owning" user set?
-               if (department.getDepartmentUserOwner()instanceof User) {
+               if (department.getDepartmentUserOwner() instanceof User) {
                        // Get managed user
                        final User managedUser = this.createManaged(department.getDepartmentUserOwner());
 
@@ -143,6 +145,51 @@ public class AddressbookAdminDepartmentSessionBean extends BaseAddressbookEnterp
                return department;
        }
 
+       @Override
+       public Department updateDepartment (final Department department) throws DepartmentNotFoundException {
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateDepartment(): department={1} - CALLED!", this.getClass().getSimpleName(), department)); //NOI18N
+
+               // Validate parameter
+               if (null == department) {
+                       // Throw NPE
+                       throw new NullPointerException("department is null"); //NOI18N
+               } else if (department.getDepartmentId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("department.departmentId is null"); //NOI18N
+               } else if (department.getDepartmentId() < 1) {
+                       // Throw IAE
+                       throw new IllegalArgumentException(MessageFormat.format("department.departmentId={0} is not valid", department.getDepartmentId())); //NOI18N
+               } else if (department.getDepartmentCompany() == null) {
+                       // Throw NPE
+                       throw new NullPointerException("department.departmentCompany is null"); //NOI18N
+               } else if (department.getDepartmentCompany().getBasicDataId() == null) {
+                       // Throw NPE again
+                       throw new NullPointerException("department.departmentCompany.basicDataId is null"); //NOI18N
+               } else if (department.getDepartmentCompany().getBasicDataId() < 1) {
+                       // Throw IAE
+                       throw new IllegalArgumentException(MessageFormat.format("department.departmentCompany.basicDataId={0} is invalid", department.getDepartmentCompany().getBasicDataId())); //NOI18N
+               } else if (department.getDepartmentI18nKey() == null) {
+                       // Throw NPE
+                       throw new NullPointerException("department.departmentName is null"); //NOI18N
+               } else if (department.getDepartmentI18nKey().isEmpty()) {
+                       // Throw IAE
+                       throw new NullPointerException("department.departmentName is empty"); //NOI18N
+               } else if (!this.isDepartmentFound(department)) {
+                       // Already added, abort here
+                       throw new DepartmentNotFoundException(department);
+               }
+
+               // Merge data
+               final Department updatedDepartment = this.mergeDepartmentData(department);
+
+               // Trace message
+               this.getLoggerBeanLocal().logTrace(MessageFormat.format("{0}.updateDepartment(): updatedDepartment={1} - EXIT!", this.getClass().getSimpleName(), updatedDepartment));
+
+               // Return updated instance
+               return updatedDepartment;
+       }
+
        /**
         * Checks if given department's address is already persisted. The whole
         * (persisted) list is being loaded and each address is being matched
@@ -162,7 +209,7 @@ public class AddressbookAdminDepartmentSessionBean extends BaseAddressbookEnterp
                // Check all single addresses
                for (final Department dep : departments) {
                        // Is the same address found?
-                       if (Departments.isSameDepartment(dep, department)) {
+                       if (DepartmentUtils.isSameDepartment(dep, department)) {
                                // Found one
                                isFound = true;
                                break;