/*
- * Copyright (C) 2017 - 2020 Free Software Foundation
+ * Copyright (C) 2017 - 2022 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
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.faces.view.ViewScoped;
import javax.inject.Inject;
import javax.inject.Named;
-import org.mxchange.jcontactsbusiness.events.branchoffice.added.ObservableBranchOfficeAddedEvent;
+import org.mxchange.jcontactsbusiness.events.branchoffice.added.ObservableAdminBranchOfficeAddedEvent;
+import org.mxchange.jcontactsbusiness.events.branchoffice.updated.ObservableAdminBranchOfficeUpdatedEvent;
import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeNotFoundException;
import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote;
/**
* Serial number
*/
- private static final long serialVersionUID = 5_028_697_360_462L;
+ private static final long serialVersionUID = 5_028_697_360_468L;
/**
* A list of all branch offices
}
/**
- * Observes events being fired when a branch office has been added.
+ * Observes events being fired when a branch office has been added by an
+ * administrator.
* <p>
* @param event Event being fired
+ */
+ public void afterAdminBranchOfficeAddedEvent (@Observes final ObservableAdminBranchOfficeAddedEvent event) {
+ // Validate parameter
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getAddedBranchOffice() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.addedBranchOffice is null"); //NOI18N
+ } else if (event.getAddedBranchOffice().getBranchId() == null) {
+ // Throw it again
+ throw new NullPointerException("event.addedBranchOffice .branchId is null"); //NOI18N
+ } else if (event.getAddedBranchOffice().getBranchId() < 1) {
+ // Throw IAE
+ throw new IllegalArgumentException(MessageFormat.format("event.addedBranchOffice .branchId={0} is not valid", event.getAddedBranchOffice().getBranchId())); //NOI18N
+ }
+
+ // Uniquely add branch office
+ this.uniqueAddBranchOffice(event.getAddedBranchOffice());
+ }
+
+ /**
+ * Observes events being fired when a branch office has been updated by an
+ * administrator.
* <p>
- * @throws NullPointerException If the parameter or it's carried instance is
- * null
- * @throws IllegalArgumentException If the branchId is zero or lower
+ * @param event Event being fired
*/
- public void afterBranchOfficeAddedEvent (@Observes final ObservableBranchOfficeAddedEvent event) {
+ public void afterAdminBranchOfficeUpdatedEvent (@Observes final ObservableAdminBranchOfficeUpdatedEvent event) {
// Validate parameter
if (null == event) {
// Throw NPE
throw new NullPointerException("event is null"); //NOI18N
- } else if (event.getBranchOffice() == null) {
+ } else if (event.getUpdatedBranchOffice() == null) {
// Throw NPE again
- throw new NullPointerException("event.branchOffice is null"); //NOI18N
- } else if (event.getBranchOffice().getBranchId() == null) {
+ throw new NullPointerException("event.updatedBranchOffice is null"); //NOI18N
+ } else if (event.getUpdatedBranchOffice().getBranchId() == null) {
// Throw it again
- throw new NullPointerException("event.branchOffice.branchId is null"); //NOI18N
- } else if (event.getBranchOffice().getBranchId() < 1) {
+ throw new NullPointerException("event.updatedBranchOffice .branchId is null"); //NOI18N
+ } else if (event.getUpdatedBranchOffice().getBranchId() < 1) {
// Throw IAE
- throw new IllegalArgumentException(MessageFormat.format("event.branchOffice.branchId={0} is not valid", event.getBranchOffice().getBranchId())); //NOI18N
+ throw new IllegalArgumentException(MessageFormat.format("event.updatedBranchOffice .branchId={0} is not valid", event.getUpdatedBranchOffice().getBranchId())); //NOI18N
}
- // Add instance to cache
- this.branchOfficeCache.put(event.getBranchOffice().getBranchId(), event.getBranchOffice());
- this.getAllBranchOffices().add(event.getBranchOffice());
+ // Uniquely update branch office
+ this.uniqueAddBranchOffice(event.getUpdatedBranchOffice());
}
@Override
- public BranchOffice findBranchOfficeById (final Long branchOfficeId) throws BranchOfficeNotFoundException {
+ public BranchOffice findBranchOfficeById (final Long branchId) throws BranchOfficeNotFoundException {
// Validate parameter
- if (null == branchOfficeId) {
+ if (null == branchId) {
// Throw NPE
- throw new NullPointerException("branchOfficeId is null"); //NOI18N
- } else if (branchOfficeId < 1) {
+ throw new NullPointerException("branchId is null"); //NOI18N
+ } else if (branchId < 1) {
// Throw IAE
- throw new IllegalArgumentException(MessageFormat.format("branchOfficeId={0} is invalid", branchOfficeId)); //NOI18N
- } else if (!this.branchOfficeCache.containsKey(branchOfficeId)) {
+ throw new IllegalArgumentException(MessageFormat.format("branchId={0} is invalid", branchId)); //NOI18N
+ } else if (!this.branchOfficeCache.containsKey(branchId)) {
// Not found
- throw new BranchOfficeNotFoundException(branchOfficeId);
+ throw new BranchOfficeNotFoundException(branchId);
}
// Get it from cache
- final BranchOffice branchOffice = this.branchOfficeCache.get(branchOfficeId);
+ final BranchOffice branchOffice = this.branchOfficeCache.get(branchId);
// Return it
return branchOffice;
return isFound;
}
+ /**
+ * Uniquely add branch office instance to allBranchOffices property
+ * <p>
+ * @param branchOffice Branch office being added
+ */
+ private void uniqueAddBranchOffice (final BranchOffice branchOffice) {
+ // Add instance to cache
+ this.branchOfficeCache.put(branchOffice.getBranchId(), branchOffice);
+
+ // Get iterator
+ final Iterator<BranchOffice> iterator = this.getAllBranchOffices().iterator();
+
+ // Iterate over all
+ while (iterator.hasNext()) {
+ // Get current element
+ final BranchOffice currentBranchOffice = iterator.next();
+
+ // Does primary key match?
+ if (Objects.equals(branchOffice.getBranchId(), currentBranchOffice.getBranchId())) {
+ // Yes then remove this one
+ iterator.remove();
+
+ // Re-add maybe updated version
+ this.getAllBranchOffices().add(branchOffice);
+
+ // Stop iteration
+ break;
+ }
+ }
+ }
+
@Override
protected Object clone () throws CloneNotSupportedException {
return super.clone(); //To change body of generated methods, choose Tools | Templates.