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.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.ObservableAdminBranchOfficeAddedEvent;
34 import org.mxchange.jcontactsbusiness.events.branchoffice.updated.ObservableAdminBranchOfficeUpdatedEvent;
35 import org.mxchange.jcontactsbusiness.exceptions.branchoffice.BranchOfficeNotFoundException;
36 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOffice;
37 import org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote;
38 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
41 * A list bean for branch offices
43 * @author Roland Häder<roland@mxchange.org>
45 @Named ("branchOfficeListController")
47 public class FinancialsBranchOfficeListWebViewBean extends BaseFinancialsBean implements FinancialsBranchOfficeListWebViewController {
52 private static final long serialVersionUID = 5_028_697_360_468L;
55 * A list of all branch offices
57 private final List<BranchOffice> allBranchOffices;
60 * EJB for administrative purposes
62 @EJB (lookup = "java:global/jfinancials-ejb/branchOffice!org.mxchange.jcontactsbusiness.model.branchoffice.BranchOfficeSessionBeanRemote")
63 private BranchOfficeSessionBeanRemote branchOfficeBean;
66 * A list of all branch offices (globally)
69 @NamedCache (cacheName = "branchOfficeCache")
70 private transient Cache<Long, BranchOffice> branchOfficeCache;
73 * A list of filtered branch offices
75 private List<BranchOffice> filteredBranchOffices;
78 * Selected branch office instance
80 private BranchOffice selectedBranchOffice;
85 public FinancialsBranchOfficeListWebViewBean () {
86 // Call super constructor
90 this.allBranchOffices = new LinkedList<>();
94 * Observes events being fired when a branch office has been added by an
97 * @param event Event being fired
99 public void afterAdminBranchOfficeAddedEvent (@Observes final ObservableAdminBranchOfficeAddedEvent event) {
100 // Validate parameter
103 throw new NullPointerException("event is null"); //NOI18N
104 } else if (event.getAddedBranchOffice() == null) {
106 throw new NullPointerException("event.addedBranchOffice is null"); //NOI18N
107 } else if (event.getAddedBranchOffice().getBranchId() == null) {
109 throw new NullPointerException("event.addedBranchOffice .branchId is null"); //NOI18N
110 } else if (event.getAddedBranchOffice().getBranchId() < 1) {
112 throw new IllegalArgumentException(MessageFormat.format("event.addedBranchOffice .branchId={0} is not valid", event.getAddedBranchOffice().getBranchId())); //NOI18N
115 // Add instance to cache
116 this.branchOfficeCache.put(event.getAddedBranchOffice().getBranchId(), event.getAddedBranchOffice());
117 this.uniqueAddBranchOffice(event.getAddedBranchOffice());
121 * Observes events being fired when a branch office has been updated by an
124 * @param event Event being fired
126 public void afterAdminBranchOfficeUpdatedEvent (@Observes final ObservableAdminBranchOfficeUpdatedEvent event) {
127 // Validate parameter
130 throw new NullPointerException("event is null"); //NOI18N
131 } else if (event.getUpdatedBranchOffice() == null) {
133 throw new NullPointerException("event.updatedBranchOffice is null"); //NOI18N
134 } else if (event.getUpdatedBranchOffice().getBranchId() == null) {
136 throw new NullPointerException("event.updatedBranchOffice .branchId is null"); //NOI18N
137 } else if (event.getUpdatedBranchOffice().getBranchId() < 1) {
139 throw new IllegalArgumentException(MessageFormat.format("event.updatedBranchOffice .branchId={0} is not valid", event.getUpdatedBranchOffice().getBranchId())); //NOI18N
142 // Add instance to cache
143 this.branchOfficeCache.put(event.getUpdatedBranchOffice().getBranchId(), event.getUpdatedBranchOffice());
144 this.uniqueAddBranchOffice(event.getUpdatedBranchOffice());
148 public BranchOffice findBranchOfficeById (final Long branchId) throws BranchOfficeNotFoundException {
149 // Validate parameter
150 if (null == branchId) {
152 throw new NullPointerException("branchId is null"); //NOI18N
153 } else if (branchId < 1) {
155 throw new IllegalArgumentException(MessageFormat.format("branchId={0} is invalid", branchId)); //NOI18N
156 } else if (!this.branchOfficeCache.containsKey(branchId)) {
158 throw new BranchOfficeNotFoundException(branchId);
162 final BranchOffice branchOffice = this.branchOfficeCache.get(branchId);
169 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
170 public List<BranchOffice> getAllBranchOffices () {
171 return this.allBranchOffices;
175 * Getter for a list of filtered branch offices
177 * @return Filtered branch offices
179 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
180 public List<BranchOffice> getFilteredBranchOffices () {
181 return this.filteredBranchOffices;
185 * Setter for a list of filtered branch offices
187 * @param filteredBranchOffices Filtered branch offices
189 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
190 public void setFilteredBranchOffices (final List<BranchOffice> filteredBranchOffices) {
191 this.filteredBranchOffices = filteredBranchOffices;
195 * Getter for selected branch office
197 * @return Selected branch office
199 public BranchOffice getSelectedBranchOffice () {
200 return this.selectedBranchOffice;
204 * Setter for selected branch office
206 * @param selectedBranchOffice Selected branch office
208 public void setSelectedBranchOffice (final BranchOffice selectedBranchOffice) {
209 this.selectedBranchOffice = selectedBranchOffice;
216 public void initializeList () {
218 if (!this.branchOfficeCache.iterator().hasNext()) {
220 for (final BranchOffice branchOffice : this.branchOfficeBean.fetchAllBranchOffices()) {
222 this.branchOfficeCache.put(branchOffice.getBranchId(), branchOffice);
226 // Is the list empty, but filled cache?
227 if (this.getAllBranchOffices().isEmpty() && this.branchOfficeCache.iterator().hasNext()) {
229 for (final Cache.Entry<Long, BranchOffice> currentEntry : this.branchOfficeCache) {
231 this.getAllBranchOffices().add(currentEntry.getValue());
235 this.getAllBranchOffices().sort(new Comparator<BranchOffice>() {
237 public int compare (final BranchOffice branchOffice1, final BranchOffice branchOffice2) {
238 return branchOffice1.getBranchId() > branchOffice2.getBranchId() ? 1 : branchOffice1.getBranchId() < branchOffice2.getBranchId() ? -1 : 0;
245 public Boolean isEmailAddressRegistered (final String emailAddress) {
246 // Validate parameter
247 if (null == emailAddress) {
249 throw new NullPointerException("emailAddress is null"); //NOI18N
250 } else if (emailAddress.isEmpty()) {
252 throw new IllegalArgumentException("emailAddress is empty"); //NOI18N
255 // Default is not found
256 boolean isFound = false;
259 for (final BranchOffice branchOffice : this.getAllBranchOffices()) {
260 // Is email address used?
261 if (Objects.equals(branchOffice.getBranchEmailAddress(), emailAddress)) {
273 * Uniquely add branch office instance to allBranchOffices property
275 * @param branchOffice Branch office being added
277 private void uniqueAddBranchOffice (final BranchOffice branchOffice) {
279 final Iterator<BranchOffice> iterator = this.getAllBranchOffices().iterator();
282 while (iterator.hasNext()) {
283 // Get current element
284 final BranchOffice currentBranchOffice = iterator.next();
286 // Does primary key match?
287 if (Objects.equals(branchOffice.getBranchId(), currentBranchOffice.getBranchId())) {
288 // Yes then remove this one
291 // Re-add maybe updated version
292 this.getAllBranchOffices().add(branchOffice);
301 protected Object clone () throws CloneNotSupportedException {
302 return super.clone(); //To change body of generated methods, choose Tools | Templates.