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 java.util.Objects;
26 import javax.annotation.PostConstruct;
27 import javax.cache.Cache;
29 import javax.enterprise.context.RequestScoped;
30 import javax.enterprise.event.Observes;
31 import javax.inject.Inject;
32 import javax.inject.Named;
33 import org.mxchange.jcontactsbusiness.events.branchoffice.added.ObservableBranchOfficeAddedEvent;
34 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeNotFoundException;
35 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
36 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote;
37 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
40 * A general bean for branch offices
42 * @author Roland Häder<roland@mxchange.org>
44 @Named ("branchOfficeController")
46 public class FinancialsBranchOfficeWebRequestBean extends BaseFinancialsBean implements FinancialsBranchOfficeWebRequestController {
51 private static final long serialVersionUID = 5_028_697_360_461L;
54 * A list of all branch offices
56 private final List<BranchOffice> allBranchOffices;
59 * EJB for administrative purposes
61 @EJB (lookup = "java:global/jfinancials-ejb/branchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote")
62 private BranchOfficeSessionBeanRemote branchOfficeBean;
65 * A list of all branch offices (globally)
68 @NamedCache (cacheName = "branchOfficeCache")
69 private Cache<Long, BranchOffice> branchOfficeCache;
72 * A list of filtered branch offices
74 private List<BranchOffice> filteredBranchOffices;
79 public FinancialsBranchOfficeWebRequestBean () {
80 // Call super constructor
84 this.allBranchOffices = new LinkedList<>();
88 * Observes events being fired when a branch office has been added.
90 * @param event Event being fired
92 * @throws NullPointerException If the parameter or it's carried instance is
94 * @throws IllegalArgumentException If the branchId is zero or lower
96 public void afterBranchOfficeAddedEvent (@Observes final ObservableBranchOfficeAddedEvent event) {
100 throw new NullPointerException("event is null"); //NOI18N
101 } else if (event.getBranchOffice() == null) {
103 throw new NullPointerException("event.branchOffice is null"); //NOI18N
104 } else if (event.getBranchOffice().getBranchId() == null) {
106 throw new NullPointerException("event.branchOffice.branchId is null"); //NOI18N
107 } else if (event.getBranchOffice().getBranchId() < 1) {
109 throw new IllegalArgumentException(MessageFormat.format("event.branchOffice.branchId={0} is not valid", event.getBranchOffice().getBranchId())); //NOI18N
112 // Add instance to cache
113 this.branchOfficeCache.put(event.getBranchOffice().getBranchId(), event.getBranchOffice());
114 this.allBranchOffices.add(event.getBranchOffice());
118 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
119 public List<BranchOffice> allBranchOffices () {
120 return this.allBranchOffices;
124 public BranchOffice findBranchOfficeById (final Long branchOfficeId) throws BranchOfficeNotFoundException {
125 // Validate parameter
126 if (null == branchOfficeId) {
128 throw new NullPointerException("branchOfficeId is null"); //NOI18N
129 } else if (branchOfficeId < 1) {
131 throw new IllegalArgumentException(MessageFormat.format("branchOfficeId={0} is invalid", branchOfficeId)); //NOI18N
132 } else if (!this.branchOfficeCache.containsKey(branchOfficeId)) {
134 throw new BranchOfficeNotFoundException(branchOfficeId);
138 final BranchOffice branchOffice = this.branchOfficeCache.get(branchOfficeId);
145 * Getter for a list of filtered branch offices
147 * @return Filtered branch offices
149 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
150 public List<BranchOffice> getFilteredBranchOffices () {
151 return this.filteredBranchOffices;
155 * Setter for a list of filtered branch offices
157 * @param filteredBranchOffices Filtered branch offices
159 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
160 public void setFilteredBranchOffices (final List<BranchOffice> filteredBranchOffices) {
161 this.filteredBranchOffices = filteredBranchOffices;
168 public void initializeList () {
170 if (!this.branchOfficeCache.iterator().hasNext()) {
172 final List<BranchOffice> branchOffices = this.branchOfficeBean.allBranchOffices();
175 for (final BranchOffice branchOffice : branchOffices) {
177 this.branchOfficeCache.put(branchOffice.getBranchId(), branchOffice);
181 // Is the list empty, but filled cache?
182 if (this.allBranchOffices.isEmpty() && this.branchOfficeCache.iterator().hasNext()) {
184 final Iterator<Cache.Entry<Long, BranchOffice>> iterator = this.branchOfficeCache.iterator();
187 while (iterator.hasNext()) {
189 final Cache.Entry<Long, BranchOffice> next = iterator.next();
192 this.allBranchOffices.add(next.getValue());
196 this.allBranchOffices.sort(new Comparator<BranchOffice>() {
198 public int compare (final BranchOffice o1, final BranchOffice o2) {
199 return o1.getBranchId() > o2.getBranchId() ? 1 : o1.getBranchId() < o2.getBranchId() ? -1 : 0;
206 public Boolean isEmailAddressRegistered (final String emailAddress) {
207 // Default is not found
208 boolean isFound = false;
211 for (final BranchOffice branchOffice : this.allBranchOffices()) {
212 // Is email address used?
213 if (Objects.equals(branchOffice.getBranchEmailAddress(), emailAddress)) {