2 * Copyright (C) 2017 Roland Häder
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.jfinancials.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.jcontactsbusiness.events.branchoffice.added.ObservableBranchOfficeAddedEvent;
33 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeNotFoundException;
34 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
35 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote;
36 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
39 * A general bean for branch offices
41 * @author Roland Häder<roland@mxchange.org>
43 @Named ("branchOfficeController")
45 public class FinancialsBranchOfficeWebRequestBean extends BaseFinancialsBean implements FinancialsBranchOfficeWebRequestController {
50 private static final long serialVersionUID = 5_028_697_360_461L;
53 * A list of all branch offices
55 private final List<BranchOffice> allBranchOffices;
58 * EJB for administrative purposes
60 @EJB (lookup = "java:global/jfinancials-ejb/branchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote")
61 private BranchOfficeSessionBeanRemote branchOfficeBean;
64 * A list of all branch offices (globally)
67 @NamedCache (cacheName = "branchOfficeCache")
68 private Cache<Long, BranchOffice> branchOfficeCache;
71 * A list of filtered branch offices
73 private List<BranchOffice> filteredBranchOffices;
78 public FinancialsBranchOfficeWebRequestBean () {
79 // Call super constructor
83 this.allBranchOffices = new LinkedList<>();
87 * Observes events being fired when a branch office has been added.
89 * @param event Event being fired
91 * @throws NullPointerException If the parameter or it's carried instance is
93 * @throws IllegalArgumentException If the branchId is zero or lower
95 public void afterBranchOfficeAddedEvent (@Observes final ObservableBranchOfficeAddedEvent event) {
99 throw new NullPointerException("event is null"); //NOI18N
100 } else if (event.getBranchOffice() == null) {
102 throw new NullPointerException("event.branchOffice is null"); //NOI18N
103 } else if (event.getBranchOffice().getBranchId() == null) {
105 throw new NullPointerException("event.branchOffice.branchId is null"); //NOI18N
106 } else if (event.getBranchOffice().getBranchId() < 1) {
108 throw new IllegalArgumentException(MessageFormat.format("event.branchOffice.branchId={0} is not valid", event.getBranchOffice().getBranchId())); //NOI18N
111 // Add instance to cache
112 this.branchOfficeCache.put(event.getBranchOffice().getBranchId(), event.getBranchOffice());
113 this.allBranchOffices.add(event.getBranchOffice());
117 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
118 public List<BranchOffice> allBranchOffices () {
119 return this.allBranchOffices;
123 public BranchOffice findBranchOfficeById (final Long branchOfficeId) throws BranchOfficeNotFoundException {
124 // Validate parameter
125 if (null == branchOfficeId) {
127 throw new NullPointerException("branchOfficeId is null"); //NOI18N
128 } else if (branchOfficeId < 1) {
130 throw new IllegalArgumentException(MessageFormat.format("branchOfficeId={0} is invalid", branchOfficeId)); //NOI18N
131 } else if (!this.branchOfficeCache.containsKey(branchOfficeId)) {
133 throw new BranchOfficeNotFoundException(branchOfficeId);
137 final BranchOffice branchOffice = this.branchOfficeCache.get(branchOfficeId);
144 * Getter for a list of filtered branch offices
146 * @return Filtered branch offices
148 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
149 public List<BranchOffice> getFilteredBranchOffices () {
150 return this.filteredBranchOffices;
154 * Setter for a list of filtered branch offices
156 * @param filteredBranchOffices Filtered branch offices
158 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
159 public void setFilteredBranchOffices (final List<BranchOffice> filteredBranchOffices) {
160 this.filteredBranchOffices = filteredBranchOffices;
167 public void initializeList () {
169 if (!this.branchOfficeCache.iterator().hasNext()) {
171 final List<BranchOffice> branchOffices = this.branchOfficeBean.allBranchOffices();
174 for (final BranchOffice branchOffice : branchOffices) {
176 this.branchOfficeCache.put(branchOffice.getBranchId(), branchOffice);
180 // Is the list empty, but filled cache?
181 if (this.allBranchOffices.isEmpty() && this.branchOfficeCache.iterator().hasNext()) {
183 final Iterator<Cache.Entry<Long, BranchOffice>> iterator = this.branchOfficeCache.iterator();
186 while (iterator.hasNext()) {
188 final Cache.Entry<Long, BranchOffice> next = iterator.next();
191 this.allBranchOffices.add(next.getValue());
195 this.allBranchOffices.sort(new Comparator<BranchOffice>() {
197 public int compare (final BranchOffice o1, final BranchOffice o2) {
198 return o1.getBranchId() > o2.getBranchId() ? 1 : o1.getBranchId() < o2.getBranchId() ? -1 : 0;