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.pizzaapplication.beans.mobileprovider;
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 javax.annotation.PostConstruct;
26 import javax.cache.Cache;
28 import javax.enterprise.context.RequestScoped;
29 import javax.enterprise.event.Observes;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import org.mxchange.jphone.events.mobileprovider.added.AdminAddedMobileProviderEvent;
33 import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
34 import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProviderSingletonBeanRemote;
35 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
38 * A general bean for mobile providers
40 * @author Roland Häder<roland@mxchange.org>
42 @Named ("mobileProviderController")
44 public class PizzaMobileProviderWebRequestBean extends BasePizzaBean implements PizzaMobileProviderWebRequestController {
49 private static final long serialVersionUID = 15_869_423_671_950_276L;
52 * A list of all mobile providers
54 private final List<MobileProvider> allMobileProviders;
57 * Remote EJB for mobile providers (regular)
59 @EJB (lookup = "java:global/pizzaservice-ejb/mobileProvider!org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProviderSingletonBeanRemote")
60 private MobileProviderSingletonBeanRemote mobileProviderBean;
63 * "Cached" list of mobile providers
66 @NamedCache (cacheName = "mobileProviderCache")
67 private Cache<Long, MobileProvider> mobileProviderCache;
72 public PizzaMobileProviderWebRequestBean () {
73 // Call super constructor
77 this.allMobileProviders = new LinkedList<>();
81 * Observes events being fired after the administrator has added a new
84 * @param event Event being fired
86 public void afterAdminAddedMobileProviderEvent (@Observes final AdminAddedMobileProviderEvent event) {
90 throw new NullPointerException("event is null"); //NOI18N
91 } else if (event.getAddedMobileProvider() == null) {
93 throw new NullPointerException("event.addedMobileProvider is null"); //NOI18N
94 } else if (event.getAddedMobileProvider().getProviderId() == null) {
96 throw new NullPointerException("event.addedMobileProvider.providerId is null"); //NOI18N
97 } else if (event.getAddedMobileProvider().getProviderId() < 1) {
99 throw new IllegalArgumentException(MessageFormat.format("event.addedMobileProvider.providerId={0} is not valid.", event.getAddedMobileProvider().getProviderId())); //NOI18N
102 // Add it to the list
103 this.mobileProviderCache.put(event.getAddedMobileProvider().getProviderId(), event.getAddedMobileProvider());
104 this.allMobileProviders.add(event.getAddedMobileProvider());
108 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
109 public List<MobileProvider> allMobileProviders () {
111 return this.allMobileProviders;
115 * Post-construction method
118 public void init () {
120 if (!this.mobileProviderCache.iterator().hasNext()) {
122 final List<MobileProvider> list = this.mobileProviderBean.allMobileProviders();
125 for (final Iterator<MobileProvider> iterator = list.iterator(); iterator.hasNext();) {
127 final MobileProvider next = iterator.next();
130 this.mobileProviderCache.put(next.getProviderId(), next);
134 // Is the list empty, but filled cache?
135 if (this.allMobileProviders.isEmpty() && this.mobileProviderCache.iterator().hasNext()) {
137 final Iterator<Cache.Entry<Long, MobileProvider>> iterator = this.mobileProviderCache.iterator();
140 while (iterator.hasNext()) {
142 final Cache.Entry<Long, MobileProvider> next = iterator.next();
145 this.allMobileProviders.add(next.getValue());
149 this.allMobileProviders.sort(new Comparator<MobileProvider>() {
151 public int compare (final MobileProvider o1, final MobileProvider o2) {
152 return o1.getProviderId() > o2.getProviderId() ? 1 : o1.getProviderId() < o2.getProviderId() ? -1 : 0;