2 * Copyright (C) 2017 - 2020 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.LinkedList;
23 import java.util.List;
24 import java.util.Objects;
25 import javax.annotation.PostConstruct;
26 import javax.cache.Cache;
28 import javax.enterprise.event.Observes;
29 import javax.faces.view.ViewScoped;
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 list bean for branch offices
41 * @author Roland Häder<roland@mxchange.org>
43 @Named ("branchOfficeListController")
45 public class FinancialsBranchOfficeListWebViewBean extends BaseFinancialsBean implements FinancialsBranchOfficeListWebViewController {
50 private static final long serialVersionUID = 5_028_697_360_462L;
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 transient Cache<Long, BranchOffice> branchOfficeCache;
71 * A list of filtered branch offices
73 private List<BranchOffice> filteredBranchOffices;
76 * Selected branch office instance
78 private BranchOffice selectedBranchOffice;
83 public FinancialsBranchOfficeListWebViewBean () {
84 // Call super constructor
88 this.allBranchOffices = new LinkedList<>();
92 * Observes events being fired when a branch office has been added.
94 * @param event Event being fired
96 * @throws NullPointerException If the parameter or it's carried instance is
98 * @throws IllegalArgumentException If the branchId is zero or lower
100 public void afterBranchOfficeAddedEvent (@Observes final ObservableBranchOfficeAddedEvent event) {
101 // Validate parameter
104 throw new NullPointerException("event is null"); //NOI18N
105 } else if (event.getBranchOffice() == null) {
107 throw new NullPointerException("event.branchOffice is null"); //NOI18N
108 } else if (event.getBranchOffice().getBranchId() == null) {
110 throw new NullPointerException("event.branchOffice.branchId is null"); //NOI18N
111 } else if (event.getBranchOffice().getBranchId() < 1) {
113 throw new IllegalArgumentException(MessageFormat.format("event.branchOffice.branchId={0} is not valid", event.getBranchOffice().getBranchId())); //NOI18N
116 // Add instance to cache
117 this.branchOfficeCache.put(event.getBranchOffice().getBranchId(), event.getBranchOffice());
118 this.getAllBranchOffices().add(event.getBranchOffice());
122 public BranchOffice findBranchOfficeById (final Long branchOfficeId) throws BranchOfficeNotFoundException {
123 // Validate parameter
124 if (null == branchOfficeId) {
126 throw new NullPointerException("branchOfficeId is null"); //NOI18N
127 } else if (branchOfficeId < 1) {
129 throw new IllegalArgumentException(MessageFormat.format("branchOfficeId={0} is invalid", branchOfficeId)); //NOI18N
130 } else if (!this.branchOfficeCache.containsKey(branchOfficeId)) {
132 throw new BranchOfficeNotFoundException(branchOfficeId);
136 final BranchOffice branchOffice = this.branchOfficeCache.get(branchOfficeId);
143 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
144 public List<BranchOffice> getAllBranchOffices () {
145 return this.allBranchOffices;
149 * Getter for a list of filtered branch offices
151 * @return Filtered branch offices
153 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
154 public List<BranchOffice> getFilteredBranchOffices () {
155 return this.filteredBranchOffices;
159 * Setter for a list of filtered branch offices
161 * @param filteredBranchOffices Filtered branch offices
163 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
164 public void setFilteredBranchOffices (final List<BranchOffice> filteredBranchOffices) {
165 this.filteredBranchOffices = filteredBranchOffices;
169 * Getter for selected branch office
171 * @return Selected branch office
173 public BranchOffice getSelectedBranchOffice () {
174 return this.selectedBranchOffice;
178 * Setter for selected branch office
180 * @param selectedBranchOffice Selected branch office
182 public void setSelectedBranchOffice (final BranchOffice selectedBranchOffice) {
183 this.selectedBranchOffice = selectedBranchOffice;
190 public void initializeList () {
192 if (!this.branchOfficeCache.iterator().hasNext()) {
194 for (final BranchOffice branchOffice : this.branchOfficeBean.fetchAllBranchOffices()) {
196 this.branchOfficeCache.put(branchOffice.getBranchId(), branchOffice);
200 // Is the list empty, but filled cache?
201 if (this.getAllBranchOffices().isEmpty() && this.branchOfficeCache.iterator().hasNext()) {
203 for (final Cache.Entry<Long, BranchOffice> currentEntry : this.branchOfficeCache) {
205 this.getAllBranchOffices().add(currentEntry.getValue());
209 this.getAllBranchOffices().sort(new Comparator<BranchOffice>() {
211 public int compare (final BranchOffice branchOffice1, final BranchOffice branchOffice2) {
212 return branchOffice1.getBranchId() > branchOffice2.getBranchId() ? 1 : branchOffice1.getBranchId() < branchOffice2.getBranchId() ? -1 : 0;
219 public Boolean isEmailAddressRegistered (final String emailAddress) {
220 // Validate parameter
221 if (null == emailAddress) {
223 throw new NullPointerException("emailAddress is null"); //NOI18N
224 } else if (emailAddress.isEmpty()) {
226 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
229 // Default is not found
230 boolean isFound = false;
233 for (final BranchOffice branchOffice : this.getAllBranchOffices()) {
234 // Is email address used?
235 if (Objects.equals(branchOffice.getBranchEmailAddress(), emailAddress)) {
247 protected Object clone () throws CloneNotSupportedException {
248 return super.clone(); //To change body of generated methods, choose Tools | Templates.