2 * Copyright (C) 2017, 2018 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.jfinancials.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.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 list bean for branch offices
42 * @author Roland Häder<roland@mxchange.org>
44 @Named ("branchOfficeListController")
46 public class FinancialsBranchOfficeListWebViewBean extends BaseFinancialsBean implements FinancialsBranchOfficeListWebViewController {
51 private static final long serialVersionUID = 5_028_697_360_462L;
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 transient Cache<Long, BranchOffice> branchOfficeCache;
72 * A list of filtered branch offices
74 private List<BranchOffice> filteredBranchOffices;
77 * Selected branch office instance
79 private BranchOffice selectedBranchOffice;
84 public FinancialsBranchOfficeListWebViewBean () {
85 // Call super constructor
89 this.allBranchOffices = new LinkedList<>();
93 * Observes events being fired when a branch office has been added.
95 * @param event Event being fired
97 * @throws NullPointerException If the parameter or it's carried instance is
99 * @throws IllegalArgumentException If the branchId is zero or lower
101 public void afterBranchOfficeAddedEvent (@Observes final ObservableBranchOfficeAddedEvent event) {
102 // Validate parameter
105 throw new NullPointerException("event is null"); //NOI18N
106 } else if (event.getBranchOffice() == null) {
108 throw new NullPointerException("event.branchOffice is null"); //NOI18N
109 } else if (event.getBranchOffice().getBranchId() == null) {
111 throw new NullPointerException("event.branchOffice.branchId is null"); //NOI18N
112 } else if (event.getBranchOffice().getBranchId() < 1) {
114 throw new IllegalArgumentException(MessageFormat.format("event.branchOffice.branchId={0} is not valid", event.getBranchOffice().getBranchId())); //NOI18N
117 // Add instance to cache
118 this.branchOfficeCache.put(event.getBranchOffice().getBranchId(), event.getBranchOffice());
119 this.allBranchOffices.add(event.getBranchOffice());
123 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
124 public List<BranchOffice> allBranchOffices () {
125 return this.allBranchOffices;
129 public BranchOffice findBranchOfficeById (final Long branchOfficeId) throws BranchOfficeNotFoundException {
130 // Validate parameter
131 if (null == branchOfficeId) {
133 throw new NullPointerException("branchOfficeId is null"); //NOI18N
134 } else if (branchOfficeId < 1) {
136 throw new IllegalArgumentException(MessageFormat.format("branchOfficeId={0} is invalid", branchOfficeId)); //NOI18N
137 } else if (!this.branchOfficeCache.containsKey(branchOfficeId)) {
139 throw new BranchOfficeNotFoundException(branchOfficeId);
143 final BranchOffice branchOffice = this.branchOfficeCache.get(branchOfficeId);
150 * Getter for a list of filtered branch offices
152 * @return Filtered branch offices
154 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
155 public List<BranchOffice> getFilteredBranchOffices () {
156 return this.filteredBranchOffices;
160 * Setter for a list of filtered branch offices
162 * @param filteredBranchOffices Filtered branch offices
164 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
165 public void setFilteredBranchOffices (final List<BranchOffice> filteredBranchOffices) {
166 this.filteredBranchOffices = filteredBranchOffices;
170 * Getter for selected branch office
172 * @return Selected branch office
174 public BranchOffice getSelectedBranchOffice () {
175 return this.selectedBranchOffice;
179 * Setter for selected branch office
181 * @param selectedBranchOffice Selected branch office
183 public void setSelectedBranchOffice (final BranchOffice selectedBranchOffice) {
184 this.selectedBranchOffice = selectedBranchOffice;
191 public void initializeList () {
193 if (!this.branchOfficeCache.iterator().hasNext()) {
195 final List<BranchOffice> branchOffices = this.branchOfficeBean.allBranchOffices();
198 for (final BranchOffice branchOffice : branchOffices) {
200 this.branchOfficeCache.put(branchOffice.getBranchId(), branchOffice);
204 // Is the list empty, but filled cache?
205 if (this.allBranchOffices.isEmpty() && this.branchOfficeCache.iterator().hasNext()) {
207 final Iterator<Cache.Entry<Long, BranchOffice>> iterator = this.branchOfficeCache.iterator();
210 while (iterator.hasNext()) {
212 final Cache.Entry<Long, BranchOffice> next = iterator.next();
215 this.allBranchOffices.add(next.getValue());
219 this.allBranchOffices.sort(new Comparator<BranchOffice>() {
221 public int compare (final BranchOffice o1, final BranchOffice o2) {
222 return o1.getBranchId() > o2.getBranchId() ? 1 : o1.getBranchId() < o2.getBranchId() ? -1 : 0;
229 public Boolean isEmailAddressRegistered (final String emailAddress) {
230 // Validate parameter
231 if (null == emailAddress) {
233 throw new NullPointerException("emailAddress is null"); //NOI18N
234 } else if (emailAddress.isEmpty()) {
236 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
239 // Default is not found
240 boolean isFound = false;
243 for (final BranchOffice branchOffice : this.allBranchOffices()) {
244 // Is email address used?
245 if (Objects.equals(branchOffice.getBranchEmailAddress(), emailAddress)) {
257 protected Object clone () throws CloneNotSupportedException {
258 return super.clone(); //To change body of generated methods, choose Tools | Templates.