/*
- * Copyright (C) 2017, 2018 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 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
- * <p>
- * @throws NullPointerException If the parameter or it's carried instance is
- * null
- * @throws IllegalArgumentException If the branchId is zero or lower
*/
- public void afterBranchOfficeAddedEvent (@Observes final ObservableBranchOfficeAddedEvent event) {
+ public void afterAdminBranchOfficeAddedEvent (@Observes final ObservableAdminBranchOfficeAddedEvent event) {
// Validate parameter
if (null == event) {
// Throw NPE
throw new NullPointerException("event is null"); //NOI18N
- } else if (event.getBranchOffice() == null) {
+ } else if (event.getAddedBranchOffice() == null) {
// Throw NPE again
- throw new NullPointerException("event.branchOffice is null"); //NOI18N
- } else if (event.getBranchOffice().getBranchId() == null) {
+ throw new NullPointerException("event.addedBranchOffice is null"); //NOI18N
+ } else if (event.getAddedBranchOffice().getBranchId() == null) {
// Throw it again
- throw new NullPointerException("event.branchOffice.branchId is null"); //NOI18N
- } else if (event.getBranchOffice().getBranchId() < 1) {
+ throw new NullPointerException("event.addedBranchOffice .branchId is null"); //NOI18N
+ } else if (event.getAddedBranchOffice().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.addedBranchOffice .branchId={0} is not valid", event.getAddedBranchOffice().getBranchId())); //NOI18N
}
- // Add instance to cache
- this.branchOfficeCache.put(event.getBranchOffice().getBranchId(), event.getBranchOffice());
- this.allBranchOffices.add(event.getBranchOffice());
+ // Uniquely add branch office
+ this.uniqueAddBranchOffice(event.getAddedBranchOffice());
}
- @Override
- @SuppressWarnings ("ReturnOfCollectionOrArrayField")
- public List<BranchOffice> allBranchOffices () {
- return this.allBranchOffices;
+ /**
+ * Observes events being fired when a branch office has been updated by an
+ * administrator.
+ * <p>
+ * @param event Event being fired
+ */
+ public void afterAdminBranchOfficeUpdatedEvent (@Observes final ObservableAdminBranchOfficeUpdatedEvent event) {
+ // Validate parameter
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getUpdatedBranchOffice() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.updatedBranchOffice is null"); //NOI18N
+ } else if (event.getUpdatedBranchOffice().getBranchId() == null) {
+ // Throw it again
+ throw new NullPointerException("event.updatedBranchOffice .branchId is null"); //NOI18N
+ } else if (event.getUpdatedBranchOffice().getBranchId() < 1) {
+ // Throw IAE
+ throw new IllegalArgumentException(MessageFormat.format("event.updatedBranchOffice .branchId={0} is not valid", event.getUpdatedBranchOffice().getBranchId())); //NOI18N
+ }
+
+ // 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;
}
+ @Override
+ @SuppressWarnings ("ReturnOfCollectionOrArrayField")
+ public List<BranchOffice> getAllBranchOffices () {
+ return this.allBranchOffices;
+ }
+
/**
* Getter for a list of filtered branch offices
* <p>
public void initializeList () {
// Is cache there?
if (!this.branchOfficeCache.iterator().hasNext()) {
- // Get whole list from EJB
- final List<BranchOffice> branchOffices = this.branchOfficeBean.allBranchOffices();
-
// Add all
- for (final BranchOffice branchOffice : branchOffices) {
+ for (final BranchOffice branchOffice : this.branchOfficeBean.fetchAllBranchOffices()) {
// Add it to cache
this.branchOfficeCache.put(branchOffice.getBranchId(), branchOffice);
}
}
// Is the list empty, but filled cache?
- if (this.allBranchOffices.isEmpty() && this.branchOfficeCache.iterator().hasNext()) {
- // Get iterator
- final Iterator<Cache.Entry<Long, BranchOffice>> iterator = this.branchOfficeCache.iterator();
-
+ if (this.getAllBranchOffices().isEmpty() && this.branchOfficeCache.iterator().hasNext()) {
// Build up list
- while (iterator.hasNext()) {
- // GEt next element
- final Cache.Entry<Long, BranchOffice> next = iterator.next();
-
+ for (final Cache.Entry<Long, BranchOffice> currentEntry : this.branchOfficeCache) {
// Add to list
- this.allBranchOffices.add(next.getValue());
+ this.getAllBranchOffices().add(currentEntry.getValue());
}
// Sort list
- this.allBranchOffices.sort(new Comparator<BranchOffice>() {
+ this.getAllBranchOffices().sort(new Comparator<BranchOffice>() {
@Override
- public int compare (final BranchOffice o1, final BranchOffice o2) {
- return o1.getBranchId() > o2.getBranchId() ? 1 : o1.getBranchId() < o2.getBranchId() ? -1 : 0;
+ public int compare (final BranchOffice branchOffice1, final BranchOffice branchOffice2) {
+ return branchOffice1.getBranchId() > branchOffice2.getBranchId() ? 1 : branchOffice1.getBranchId() < branchOffice2.getBranchId() ? -1 : 0;
}
});
}
boolean isFound = false;
// Check all entries
- for (final BranchOffice branchOffice : this.allBranchOffices()) {
+ for (final BranchOffice branchOffice : this.getAllBranchOffices()) {
// Is email address used?
if (Objects.equals(branchOffice.getBranchEmailAddress(), emailAddress)) {
// Found it
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.