2 * Copyright (C) 2016 - 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.mobileprovider.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 javax.annotation.PostConstruct;
25 import javax.cache.Cache;
27 import javax.enterprise.event.Observes;
28 import javax.faces.view.ViewScoped;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
32 import org.mxchange.jphone.events.mobileprovider.added.AdminAddedMobileProviderEvent;
33 import org.mxchange.jphone.exceptions.mobileprovider.MobileProviderNotFoundException;
34 import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
35 import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProviderSingletonBeanRemote;
38 * A views-scoped bean for mobile providers
40 * @author Roland Häder<roland@mxchange.org>
42 @Named ("mobileProviderListController")
44 public class FinancialsMobileProviderListWebViewBean extends BaseFinancialsBean implements FinancialsMobileProviderListWebViewController {
49 private static final long serialVersionUID = 15_869_423_671_950_277L;
52 * A list of all mobile providers
54 private final List<MobileProvider> allMobileProviders;
57 * A list of filtered mobile providers
59 private List<MobileProvider> filteredMobileProviders;
62 * Remote EJB for mobile providers (regular)
64 @EJB (lookup = "java:global/jfinancials-ejb/mobileProvider!org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProviderSingletonBeanRemote")
65 private MobileProviderSingletonBeanRemote mobileProviderBean;
68 * "Cached" list of mobile providers
71 @NamedCache (cacheName = "mobileProviderCache")
72 private transient Cache<Long, MobileProvider> mobileProviderCache;
75 * Selected mobile provider instance
77 private MobileProvider selectedMobileProvider;
82 public FinancialsMobileProviderListWebViewBean () {
83 // Call super constructor
87 this.allMobileProviders = new LinkedList<>();
91 * Observes events being fired after the administrator has added a new
94 * @param event Event being fired
96 public void afterAdminAddedMobileProviderEvent (@Observes final AdminAddedMobileProviderEvent event) {
100 throw new NullPointerException("event is null"); //NOI18N
101 } else if (event.getAddedMobileProvider() == null) {
103 throw new NullPointerException("event.addedMobileProvider is null"); //NOI18N
104 } else if (event.getAddedMobileProvider().getProviderId() == null) {
106 throw new NullPointerException("event.addedMobileProvider.providerId is null"); //NOI18N
107 } else if (event.getAddedMobileProvider().getProviderId() < 1) {
109 throw new IllegalArgumentException(MessageFormat.format("event.addedMobileProvider.providerId={0} is not valid.", event.getAddedMobileProvider().getProviderId())); //NOI18N
112 // Add it to the list
113 this.mobileProviderCache.put(event.getAddedMobileProvider().getProviderId(), event.getAddedMobileProvider());
114 this.getAllMobileProviders().add(event.getAddedMobileProvider());
118 public MobileProvider findMobileProviderById (final Long mobileProviderId) throws MobileProviderNotFoundException {
119 // Validate parameter
120 if (null == mobileProviderId) {
122 throw new NullPointerException("mobileProviderId is null"); //NOI18N
123 } else if (mobileProviderId < 1) {
125 throw new IllegalArgumentException("mobileProviderId=" + mobileProviderId + " is invalid."); //NOI18N
126 } else if (!this.mobileProviderCache.containsKey(mobileProviderId)) {
128 throw new MobileProviderNotFoundException(mobileProviderId);
132 final MobileProvider mobileProvider = this.mobileProviderCache.get(mobileProviderId);
135 return mobileProvider;
139 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
140 public List<MobileProvider> getAllMobileProviders () {
142 return this.allMobileProviders;
146 * Getter for filtered mobile provider list
148 * @return Filtered mobile providers
150 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
151 public List<MobileProvider> getFilteredMobileProviders () {
152 return this.filteredMobileProviders;
156 * Getter for filtered mobile provider list
158 * @param filteredMobileProviders Filtered mobile providers
160 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
161 public void setFilteredMobileProviders (final List<MobileProvider> filteredMobileProviders) {
162 this.filteredMobileProviders = filteredMobileProviders;
166 * Getter for selected mobile provider instance
168 * @return Selected mobile provider instance
170 public MobileProvider getSelectedMobileProvider () {
171 return this.selectedMobileProvider;
175 * Setter for selected mobile provider instance
177 * @param selectedMobileProvider Selected mobile provider instance
179 public void setSelectedMobileProvider (final MobileProvider selectedMobileProvider) {
180 this.selectedMobileProvider = selectedMobileProvider;
184 * Post-construction method
187 public void initializeList () {
189 if (!this.mobileProviderCache.iterator().hasNext()) {
191 for (final MobileProvider mobileProvider : this.mobileProviderBean.fetchAllMobileProviders()) {
193 this.mobileProviderCache.put(mobileProvider.getProviderId(), mobileProvider);
197 // Is the list empty, but filled cache?
198 if (this.getAllMobileProviders().isEmpty() && this.mobileProviderCache.iterator().hasNext()) {
200 for (final Cache.Entry<Long, MobileProvider> currentEntry : this.mobileProviderCache) {
202 this.getAllMobileProviders().add(currentEntry.getValue());
206 this.getAllMobileProviders().sort(new Comparator<MobileProvider>() {
208 public int compare (final MobileProvider mobileProvider1, final MobileProvider mobileProvider2) {
209 return mobileProvider1.getProviderId() > mobileProvider2.getProviderId() ? 1 : mobileProvider1.getProviderId() < mobileProvider2.getProviderId() ? -1 : 0;