final Department department = this.createDepartment();
// Does current (not updated) and just created (maybe updated) match?
- System.out.println("this.currentDepartment.departmentLead=" + this.getCurrentDepartment().getDepartmentLead() + ",department.departmentLead=" + department.getDepartmentLead() + " - BEFORE!");
if (Objects.equals(this.getCurrentDepartment(), department)) {
// Yes, then output message
this.showFacesMessage("form-admin-edit-department:departmentI18nKey", "ADMIN_DEPARTMENT_NOT_UPDATED", FacesMessage.SEVERITY_WARN); //NOI18N
}
// Copy all fields
- Departments.copyDepartmentData(this.getCurrentDepartment(), department);
+ System.out.println("departmentLead=" + this.getCurrentDepartment().getDepartmentLead() + " - BEFORE!");
+ Departments.copyDepartmentData(department, this.getCurrentDepartment());
+ System.out.println("departmentLead=" + this.getCurrentDepartment().getDepartmentLead() + " - AFTER!");
// Initialize updated instance
final Department updatedDepartment;
// Try it
try {
// Invoke EJB
- updatedDepartment = this.adminDepartmentBean.updateDepartment(department);
+ updatedDepartment = this.adminDepartmentBean.updateDepartment(this.getCurrentDepartment());
} catch (final DepartmentNotFoundException ex) {
// Throw as a cause
throw new FacesException(ex);
department.setDepartmentBranchOffice(this.getDepartmentBranchOffice());
department.setDepartmentHeadquarter(this.getDepartmentHeadquarter());
department.setDepartmentId(this.getDepartmentId());
+ System.out.println("this.departmentLead=" + this.getDepartmentLead());
department.setDepartmentLead(this.getDepartmentLead());
department.setDepartmentUserOwner(this.getDepartmentUserOwner());
import fish.payara.cdi.jsr107.impl.NamedCache;
import java.text.MessageFormat;
import java.util.Comparator;
+import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
+import java.util.Objects;
import javax.annotation.PostConstruct;
import javax.cache.Cache;
import javax.ejb.EJB;
import javax.inject.Inject;
import javax.inject.Named;
import org.mxchange.jcontactsbusiness.events.department.added.ObservableAdminDepartmentAddedEvent;
+import org.mxchange.jcontactsbusiness.events.department.updated.ObservableAdminDepartmentUpdatedEvent;
import org.mxchange.jcontactsbusiness.exceptions.department.DepartmentNotFoundException;
import org.mxchange.jcontactsbusiness.model.department.Department;
import org.mxchange.jcontactsbusiness.model.department.DepartmentSessionBeanRemote;
}
/**
- * Observes events being fired when a department has been added.
+ * Observes events being fired when a department has been added by an
+ * administrator.
* <p>
* @param event Event being fired
- * <p>
- * @throws NullPointerException If the parameter or it's carried instance is
- * null
- * @throws IllegalArgumentException If the branchId is zero or lower
*/
- public void afterDepartmentAddedEvent (@Observes final ObservableAdminDepartmentAddedEvent event) {
+ public void afterAdminDepartmentAddedEvent (@Observes final ObservableAdminDepartmentAddedEvent event) {
// Validate parameter
if (null == event) {
// Throw NPE
throw new NullPointerException("event is null"); //NOI18N
} else if (event.getAddedDepartment() == null) {
// Throw NPE again
- throw new NullPointerException("event.department is null"); //NOI18N
+ throw new NullPointerException("event.addedDepartment is null"); //NOI18N
} else if (event.getAddedDepartment().getDepartmentId() == null) {
// Throw it again
- throw new NullPointerException("event.department.branchId is null"); //NOI18N
+ throw new NullPointerException("event.addedDepartment.branchId is null"); //NOI18N
} else if (event.getAddedDepartment().getDepartmentId() < 1) {
// Throw IAE
- throw new IllegalArgumentException(MessageFormat.format("event.department.branchId={0} is not valid", event.getAddedDepartment().getDepartmentId())); //NOI18N
+ throw new IllegalArgumentException(MessageFormat.format("event.addedDepartment.branchId={0} is not valid", event.getAddedDepartment().getDepartmentId())); //NOI18N
}
// Add instance to cache
this.departmentCache.put(event.getAddedDepartment().getDepartmentId(), event.getAddedDepartment());
- this.getAllDepartments().add(event.getAddedDepartment());
+ this.uniqueAddDepartment(event.getAddedDepartment());
+ }
+
+ /**
+ * Observes events being fired when a department has been updated by an
+ * administrator.
+ * <p>
+ * @param event Event being fired
+ */
+ public void afterAdminDepartmentUpdatedEvent (@Observes final ObservableAdminDepartmentUpdatedEvent event) {
+ // Validate parameter
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getUpdatedDepartment() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.updatedDepartment is null"); //NOI18N
+ } else if (event.getUpdatedDepartment().getDepartmentId() == null) {
+ // Throw it again
+ throw new NullPointerException("event.updatedDepartment.branchId is null"); //NOI18N
+ } else if (event.getUpdatedDepartment().getDepartmentId() < 1) {
+ // Throw IAE
+ throw new IllegalArgumentException(MessageFormat.format("event.updatedDepartment.branchId={0} is not valid", event.getUpdatedDepartment().getDepartmentId())); //NOI18N
+ }
+
+ // Add instance to cache
+ this.departmentCache.put(event.getUpdatedDepartment().getDepartmentId(), event.getUpdatedDepartment());
+
+ // Uniquely add (update) instance
+ this.uniqueAddDepartment(event.getUpdatedDepartment());
}
@Override
return isFound;
}
+ /**
+ * Uniquely adds given department instance to full list
+ * <p>
+ * @param department Department being uniquely added
+ */
+ private void uniqueAddDepartment (final Department department) {
+ // Get iterator
+ final Iterator<Department> iterator = this.getAllDepartments().iterator();
+
+ // Loop through all
+ while (iterator.hasNext()) {
+ // Get current entry
+ final Department currentDepartment = iterator.next();
+
+ // Does the primary key match?
+ if (Objects.equals(department.getDepartmentId(), currentDepartment.getDepartmentId())) {
+ // Yes, remove it and abort iteration
+ iterator.remove();
+ break;
+ }
+ }
+
+ // (Re-) Add department
+ this.getAllDepartments().add(department);
+ }
}