X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2Fjava%2Forg%2Fmxchange%2Fjjobs%2Fbeans%2Fbusiness%2Fbranchoffice%2Flist%2FJobsBranchOfficeListWebViewBean.java;h=1a400a6a3bbfa24e109536571a3425eeddc564ad;hb=HEAD;hp=aea07075c1fd35125c6db2158667b59be0694578;hpb=401f4e980968567f4052626b1c83e3ef53baee4e;p=jjobs-war.git diff --git a/src/java/org/mxchange/jjobs/beans/business/branchoffice/list/JobsBranchOfficeListWebViewBean.java b/src/java/org/mxchange/jjobs/beans/business/branchoffice/list/JobsBranchOfficeListWebViewBean.java index aea07075..1a400a6a 100644 --- a/src/java/org/mxchange/jjobs/beans/business/branchoffice/list/JobsBranchOfficeListWebViewBean.java +++ b/src/java/org/mxchange/jjobs/beans/business/branchoffice/list/JobsBranchOfficeListWebViewBean.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017 - 2020 Free Software Foundation + * Copyright (C) 2017 - 2024 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 @@ -19,6 +19,7 @@ package org.mxchange.jjobs.beans.business.branchoffice.list; 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; @@ -29,7 +30,8 @@ import javax.enterprise.event.Observes; 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; @@ -47,7 +49,7 @@ public class JobsBranchOfficeListWebViewBean extends BaseJobsBean implements Job /** * 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 @@ -89,62 +91,84 @@ public class JobsBranchOfficeListWebViewBean extends BaseJobsBean implements Job } /** - * 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. *

* @param event Event being fired - *

- * @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 allBranchOffices () { - return this.allBranchOffices; + /** + * Observes events being fired when a branch office has been updated by an + * administrator. + *

+ * @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 getAllBranchOffices () { + return this.allBranchOffices; + } + /** * Getter for a list of filtered branch offices *

@@ -191,22 +215,22 @@ public class JobsBranchOfficeListWebViewBean extends BaseJobsBean implements Job // Is cache there? if (!this.branchOfficeCache.iterator().hasNext()) { // Add all - for (final BranchOffice branchOffice : this.branchOfficeBean.allBranchOffices()) { + 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()) { + if (this.getAllBranchOffices().isEmpty() && this.branchOfficeCache.iterator().hasNext()) { // Build up list for (final Cache.Entry currentEntry : this.branchOfficeCache) { // Add to list - this.allBranchOffices.add(currentEntry.getValue()); + this.getAllBranchOffices().add(currentEntry.getValue()); } // Sort list - this.allBranchOffices.sort(new Comparator() { + this.getAllBranchOffices().sort(new Comparator() { @Override public int compare (final BranchOffice branchOffice1, final BranchOffice branchOffice2) { return branchOffice1.getBranchId() > branchOffice2.getBranchId() ? 1 : branchOffice1.getBranchId() < branchOffice2.getBranchId() ? -1 : 0; @@ -230,7 +254,7 @@ public class JobsBranchOfficeListWebViewBean extends BaseJobsBean implements Job 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 @@ -243,6 +267,37 @@ public class JobsBranchOfficeListWebViewBean extends BaseJobsBean implements Job return isFound; } + /** + * Uniquely add branch office instance to allBranchOffices property + *

+ * @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 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.