2 * Copyright (C) 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;
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 javax.annotation.PostConstruct;
26 import javax.cache.Cache;
28 import javax.enterprise.context.RequestScoped;
29 import javax.enterprise.event.Observes;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import org.mxchange.addressbook.beans.BaseAddressbookBean;
33 import org.mxchange.jcontactsbusiness.events.branchoffice.added.ObservableBranchOfficeAddedEvent;
34 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
35 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote;
38 * A general bean for branch offices
40 * @author Roland Häder<roland@mxchange.org>
42 @Named ("branchOfficeController")
44 public class AddressbookBranchOfficeWebRequestBean extends BaseAddressbookBean implements AddressbookBranchOfficeWebRequestController {
49 private static final long serialVersionUID = 5_028_697_360_461L;
52 * A list of all branch offices
54 private final List<BranchOffice> allBranchOffices;
57 * EJB for administrative purposes
59 @EJB (lookup = "java:global/addressbook-ejb/branchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote")
60 private BranchOfficeSessionBeanRemote branchOfficeBean;
63 * A list of all branch offices (globally)
66 @NamedCache (cacheName = "branchOfficeCache")
67 private Cache<Long, BranchOffice> branchOfficeCache;
70 * A list of filtered branch offices
72 private List<BranchOffice> filteredBranchOffices;
77 public AddressbookBranchOfficeWebRequestBean () {
78 // Call super constructor
82 this.allBranchOffices = new LinkedList<>();
86 * Observes events being fired when a branch office has been added.
88 * @param event Event being fired
90 * @throws NullPointerException If the parameter or it's carried instance is
92 * @throws IllegalArgumentException If the branchId is zero or lower
94 public void afterBranchOfficeAddedEvent (@Observes final ObservableBranchOfficeAddedEvent event) {
98 throw new NullPointerException("event is null"); //NOI18N
99 } else if (event.getBranchOffice() == null) {
101 throw new NullPointerException("event.branchOffice is null"); //NOI18N
102 } else if (event.getBranchOffice().getBranchId() == null) {
104 throw new NullPointerException("event.branchOffice.branchId is null"); //NOI18N
105 } else if (event.getBranchOffice().getBranchId() < 1) {
107 throw new IllegalArgumentException(MessageFormat.format("event.branchOffice.branchId={0} is not valid", event.getBranchOffice().getBranchId())); //NOI18N
110 // Add instance to cache
111 this.branchOfficeCache.put(event.getBranchOffice().getBranchId(), event.getBranchOffice());
112 this.allBranchOffices.add(event.getBranchOffice());
116 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
117 public List<BranchOffice> allBranchOffices () {
118 return this.allBranchOffices;
122 * Getter for a list of filtered branch offices
124 * @return Filtered branch offices
126 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
127 public List<BranchOffice> getFilteredBranchOffices () {
128 return this.filteredBranchOffices;
132 * Setter for a list of filtered branch offices
134 * @param filteredBranchOffices Filtered branch offices
136 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
137 public void setFilteredBranchOffices (final List<BranchOffice> filteredBranchOffices) {
138 this.filteredBranchOffices = filteredBranchOffices;
145 public void initializeList () {
147 if (!this.branchOfficeCache.iterator().hasNext()) {
149 final List<BranchOffice> list = this.branchOfficeBean.allBranchOffices();
152 for (final Iterator<BranchOffice> iterator = list.iterator(); iterator.hasNext();) {
154 final BranchOffice next = iterator.next();
157 this.branchOfficeCache.put(next.getBranchId(), next);
161 // Is the list empty, but filled cache?
162 if (this.allBranchOffices.isEmpty() && this.branchOfficeCache.iterator().hasNext()) {
164 final Iterator<Cache.Entry<Long, BranchOffice>> iterator = this.branchOfficeCache.iterator();
167 while (iterator.hasNext()) {
169 final Cache.Entry<Long, BranchOffice> next = iterator.next();
172 this.allBranchOffices.add(next.getValue());
176 this.allBranchOffices.sort(new Comparator<BranchOffice>() {
178 public int compare (final BranchOffice o1, final BranchOffice o2) {
179 return o1.getBranchId() > o2.getBranchId() ? 1 : o1.getBranchId() < o2.getBranchId() ? -1 : 0;