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.Iterator;
22 import java.util.LinkedList;
23 import java.util.List;
24 import javax.annotation.PostConstruct;
25 import javax.cache.Cache;
27 import javax.enterprise.context.RequestScoped;
28 import javax.enterprise.event.Observes;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import org.mxchange.jcontactsbusiness.events.branchoffice.added.ObservableBranchOfficeAddedEvent;
32 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
33 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote;
34 import org.mxchange.jfinancials.beans.BaseFinancialsController;
37 * A general bean for branch offices
39 * @author Roland Häder<roland@mxchange.org>
41 @Named ("branchOfficeController")
43 public class FinancialsBranchOfficeWebRequestBean extends BaseFinancialsController implements FinancialsBranchOfficeWebRequestController {
48 private static final long serialVersionUID = 5_028_697_360_461L;
51 * EJB for administrative purposes
53 @EJB (lookup = "java:global/jfinancials-ejb/branchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote")
54 private BranchOfficeSessionBeanRemote branchOfficeBean;
57 * A list of all branch offices (globally)
60 @NamedCache (cacheName = "branchOfficeCache")
61 private Cache<Long, BranchOffice> branchOfficeCache;
66 public FinancialsBranchOfficeWebRequestBean () {
67 // Call super constructor
72 * Observes events being fired when a branch office has been added.
74 * @param event Event being fired
76 * @throws NullPointerException If the parameter or it's carried instance is null
77 * @throws IllegalArgumentException If the branchId is zero or lower
79 public void afterBranchOfficeAddedEvent (@Observes final ObservableBranchOfficeAddedEvent event) {
83 throw new NullPointerException("event is null"); //NOI18N
84 } else if (event.getBranchOffice() == null) {
86 throw new NullPointerException("event.branchOffice is null"); //NOI18N
87 } else if (event.getBranchOffice().getBranchId() == null) {
89 throw new NullPointerException("event.branchOffice.branchId is null"); //NOI18N
90 } else if (event.getBranchOffice().getBranchId() < 1) {
92 throw new IllegalArgumentException(MessageFormat.format("event.branchOffice.branchId={0} is not valid", event.getBranchOffice().getBranchId())); //NOI18N
95 // Add instance to cache
96 this.branchOfficeCache.put(event.getBranchOffice().getBranchId(), event.getBranchOffice());
100 public List<BranchOffice> allBranchOffices () {
102 final List<BranchOffice> list = new LinkedList<>();
105 final Iterator<Cache.Entry<Long, BranchOffice>> iterator = this.branchOfficeCache.iterator();
108 while (iterator.hasNext()) {
110 final Cache.Entry<Long, BranchOffice> next = iterator.next();
113 list.add(next.getValue());
124 public void initializeList () {
126 if (!this.branchOfficeCache.iterator().hasNext()) {
128 final List<BranchOffice> list = this.branchOfficeBean.allBranchOffices();
131 for (final Iterator<BranchOffice> iterator = list.iterator(); iterator.hasNext();) {
133 final BranchOffice next = iterator.next();
136 this.branchOfficeCache.put(next.getBranchId(), next);