2 * Copyright (C) 2017 - 2022 Free Software Foundation
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.addressbook.beans.business.branchoffice.list;
19 import fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.text.MessageFormat;
21 import java.util.Comparator;
22 import java.util.Iterator;
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Objects;
26 import javax.annotation.PostConstruct;
27 import javax.cache.Cache;
29 import javax.enterprise.event.Observes;
30 import javax.faces.view.ViewScoped;
31 import javax.inject.Inject;
32 import javax.inject.Named;
33 import org.mxchange.addressbook.beans.BaseAddressbookBean;
34 import org.mxchange.jcontactsbusiness.events.branchoffice.added.ObservableAdminBranchOfficeAddedEvent;
35 import org.mxchange.jcontactsbusiness.events.branchoffice.updated.ObservableAdminBranchOfficeUpdatedEvent;
36 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeNotFoundException;
37 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
38 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote;
41 * A list bean for branch offices
43 * @author Roland Häder<roland@mxchange.org>
45 @Named ("branchOfficeListController")
47 public class AddressbookBranchOfficeListWebViewBean extends BaseAddressbookBean implements AddressbookBranchOfficeListWebViewController {
52 private static final long serialVersionUID = 5_028_697_360_468L;
55 * A list of all branch offices
57 private final List<BranchOffice> allBranchOffices;
60 * EJB for administrative purposes
62 @EJB (lookup = "java:global/addressbook-ejb/branchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote")
63 private BranchOfficeSessionBeanRemote branchOfficeBean;
66 * A list of all branch offices (globally)
69 @NamedCache (cacheName = "branchOfficeCache")
70 private transient Cache<Long, BranchOffice> branchOfficeCache;
73 * A list of filtered branch offices
75 private List<BranchOffice> filteredBranchOffices;
78 * Selected branch office instance
80 private BranchOffice selectedBranchOffice;
85 public AddressbookBranchOfficeListWebViewBean () {
86 // Call super constructor
90 this.allBranchOffices = new LinkedList<>();
94 * Observes events being fired when a branch office has been added by an
97 * @param event Event being fired
99 public void afterAdminBranchOfficeAddedEvent (@Observes final ObservableAdminBranchOfficeAddedEvent event) {
100 // Validate parameter
103 throw new NullPointerException("event is null"); //NOI18N
104 } else if (event.getAddedBranchOffice() == null) {
106 throw new NullPointerException("event.addedBranchOffice is null"); //NOI18N
107 } else if (event.getAddedBranchOffice().getBranchId() == null) {
109 throw new NullPointerException("event.addedBranchOffice .branchId is null"); //NOI18N
110 } else if (event.getAddedBranchOffice().getBranchId() < 1) {
112 throw new IllegalArgumentException(MessageFormat.format("event.addedBranchOffice .branchId={0} is not valid", event.getAddedBranchOffice().getBranchId())); //NOI18N
115 // Uniquely add branch office
116 this.uniqueAddBranchOffice(event.getAddedBranchOffice());
120 * Observes events being fired when a branch office has been updated by an
123 * @param event Event being fired
125 public void afterAdminBranchOfficeUpdatedEvent (@Observes final ObservableAdminBranchOfficeUpdatedEvent event) {
126 // Validate parameter
129 throw new NullPointerException("event is null"); //NOI18N
130 } else if (event.getUpdatedBranchOffice() == null) {
132 throw new NullPointerException("event.updatedBranchOffice is null"); //NOI18N
133 } else if (event.getUpdatedBranchOffice().getBranchId() == null) {
135 throw new NullPointerException("event.updatedBranchOffice .branchId is null"); //NOI18N
136 } else if (event.getUpdatedBranchOffice().getBranchId() < 1) {
138 throw new IllegalArgumentException(MessageFormat.format("event.updatedBranchOffice .branchId={0} is not valid", event.getUpdatedBranchOffice().getBranchId())); //NOI18N
141 // Uniquely update branch office
142 this.uniqueAddBranchOffice(event.getUpdatedBranchOffice());
146 public BranchOffice findBranchOfficeById (final Long branchId) throws BranchOfficeNotFoundException {
147 // Validate parameter
148 if (null == branchId) {
150 throw new NullPointerException("branchId is null"); //NOI18N
151 } else if (branchId < 1) {
153 throw new IllegalArgumentException(MessageFormat.format("branchId={0} is invalid", branchId)); //NOI18N
154 } else if (!this.branchOfficeCache.containsKey(branchId)) {
156 throw new BranchOfficeNotFoundException(branchId);
160 final BranchOffice branchOffice = this.branchOfficeCache.get(branchId);
167 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
168 public List<BranchOffice> getAllBranchOffices () {
169 return this.allBranchOffices;
173 * Getter for a list of filtered branch offices
175 * @return Filtered branch offices
177 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
178 public List<BranchOffice> getFilteredBranchOffices () {
179 return this.filteredBranchOffices;
183 * Setter for a list of filtered branch offices
185 * @param filteredBranchOffices Filtered branch offices
187 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
188 public void setFilteredBranchOffices (final List<BranchOffice> filteredBranchOffices) {
189 this.filteredBranchOffices = filteredBranchOffices;
193 * Getter for selected branch office
195 * @return Selected branch office
197 public BranchOffice getSelectedBranchOffice () {
198 return this.selectedBranchOffice;
202 * Setter for selected branch office
204 * @param selectedBranchOffice Selected branch office
206 public void setSelectedBranchOffice (final BranchOffice selectedBranchOffice) {
207 this.selectedBranchOffice = selectedBranchOffice;
214 public void initializeList () {
216 if (!this.branchOfficeCache.iterator().hasNext()) {
218 for (final BranchOffice branchOffice : this.branchOfficeBean.fetchAllBranchOffices()) {
220 this.branchOfficeCache.put(branchOffice.getBranchId(), branchOffice);
224 // Is the list empty, but filled cache?
225 if (this.getAllBranchOffices().isEmpty() && this.branchOfficeCache.iterator().hasNext()) {
227 for (final Cache.Entry<Long, BranchOffice> currentEntry : this.branchOfficeCache) {
229 this.getAllBranchOffices().add(currentEntry.getValue());
233 this.getAllBranchOffices().sort(new Comparator<BranchOffice>() {
235 public int compare (final BranchOffice branchOffice1, final BranchOffice branchOffice2) {
236 return branchOffice1.getBranchId() > branchOffice2.getBranchId() ? 1 : branchOffice1.getBranchId() < branchOffice2.getBranchId() ? -1 : 0;
243 public Boolean isEmailAddressRegistered (final String emailAddress) {
244 // Validate parameter
245 if (null == emailAddress) {
247 throw new NullPointerException("emailAddress is null"); //NOI18N
248 } else if (emailAddress.isEmpty()) {
250 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
253 // Default is not found
254 boolean isFound = false;
257 for (final BranchOffice branchOffice : this.getAllBranchOffices()) {
258 // Is email address used?
259 if (Objects.equals(branchOffice.getBranchEmailAddress(), emailAddress)) {
271 * Uniquely add branch office instance to allBranchOffices property
273 * @param branchOffice Branch office being added
275 private void uniqueAddBranchOffice (final BranchOffice branchOffice) {
276 // Add instance to cache
277 this.branchOfficeCache.put(branchOffice.getBranchId(), branchOffice);
280 final Iterator<BranchOffice> iterator = this.getAllBranchOffices().iterator();
283 while (iterator.hasNext()) {
284 // Get current element
285 final BranchOffice currentBranchOffice = iterator.next();
287 // Does primary key match?
288 if (Objects.equals(branchOffice.getBranchId(), currentBranchOffice.getBranchId())) {
289 // Yes then remove this one
292 // Re-add maybe updated version
293 this.getAllBranchOffices().add(branchOffice);
302 protected Object clone () throws CloneNotSupportedException {
303 return super.clone(); //To change body of generated methods, choose Tools | Templates.