]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/mobileprovider/PizzaMobileProviderWebRequestBean.java
Updated copyright year
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / mobileprovider / PizzaMobileProviderWebRequestBean.java
1 /*
2  * Copyright (C) 2016 - 2020 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.pizzaapplication.beans.mobileprovider;
18
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;
27 import javax.ejb.EJB;
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;
36
37 /**
38  * A general bean for mobile providers
39  * <p>
40  * @author Roland Häder<roland@mxchange.org>
41  */
42 @Named ("mobileProviderController")
43 @RequestScoped
44 public class PizzaMobileProviderWebRequestBean extends BasePizzaBean implements PizzaMobileProviderWebRequestController {
45
46         /**
47          * Serial number
48          */
49         private static final long serialVersionUID = 15_869_423_671_950_276L;
50
51         /**
52          * A list of all mobile providers
53          */
54         private final List<MobileProvider> allMobileProviders;
55
56         /**
57          * Remote EJB for mobile providers (regular)
58          */
59         @EJB (lookup = "java:global/pizzaservice-ejb/mobileProvider!org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProviderSingletonBeanRemote")
60         private MobileProviderSingletonBeanRemote mobileProviderBean;
61
62         /**
63          * "Cached" list of mobile providers
64          */
65         @Inject
66         @NamedCache (cacheName = "mobileProviderCache")
67         private Cache<Long, MobileProvider> mobileProviderCache;
68
69         /**
70          * Default constructor
71          */
72         public PizzaMobileProviderWebRequestBean () {
73                 // Call super constructor
74                 super();
75
76                 // Init list
77                 this.allMobileProviders = new LinkedList<>();
78         }
79
80         /**
81          * Observes events being fired after the administrator has added a new
82          * mobile provider
83          * <p>
84          * @param event Event being fired
85          */
86         public void afterAdminAddedMobileProviderEvent (@Observes final AdminAddedMobileProviderEvent event) {
87                 // Is all valid?
88                 if (null == event) {
89                         // Throw NPE
90                         throw new NullPointerException("event is null"); //NOI18N
91                 } else if (event.getAddedMobileProvider() == null) {
92                         // Throw again ...
93                         throw new NullPointerException("event.addedMobileProvider is null"); //NOI18N
94                 } else if (event.getAddedMobileProvider().getProviderId() == null) {
95                         // And again ...
96                         throw new NullPointerException("event.addedMobileProvider.providerId is null"); //NOI18N
97                 } else if (event.getAddedMobileProvider().getProviderId() < 1) {
98                         // Id is invalid
99                         throw new IllegalArgumentException(MessageFormat.format("event.addedMobileProvider.providerId={0} is not valid.", event.getAddedMobileProvider().getProviderId())); //NOI18N
100                 }
101
102                 // Add it to the list
103                 this.mobileProviderCache.put(event.getAddedMobileProvider().getProviderId(), event.getAddedMobileProvider());
104                 this.allMobileProviders.add(event.getAddedMobileProvider());
105         }
106
107         @Override
108         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
109         public List<MobileProvider> allMobileProviders () {
110                 // Return it
111                 return this.allMobileProviders;
112         }
113
114         /**
115          * Post-construction method
116          */
117         @PostConstruct
118         public void init () {
119                 // Is cache there?
120                 if (!this.mobileProviderCache.iterator().hasNext()) {
121                         // Get whole list
122                         final List<MobileProvider> list = this.mobileProviderBean.allMobileProviders();
123
124                         // Add all
125                         for (final Iterator<MobileProvider> iterator = list.iterator(); iterator.hasNext();) {
126                                 // Get next element
127                                 final MobileProvider next = iterator.next();
128
129                                 // Add it to cache
130                                 this.mobileProviderCache.put(next.getProviderId(), next);
131                         }
132                 }
133
134                 // Is the list empty, but filled cache?
135                 if (this.allMobileProviders.isEmpty() && this.mobileProviderCache.iterator().hasNext()) {
136                         // Get iterator
137                         final Iterator<Cache.Entry<Long, MobileProvider>> iterator = this.mobileProviderCache.iterator();
138
139                         // Build up list
140                         while (iterator.hasNext()) {
141                                 // GEt next element
142                                 final Cache.Entry<Long, MobileProvider> next = iterator.next();
143
144                                 // Add to list
145                                 this.allMobileProviders.add(next.getValue());
146                         }
147
148                         // Sort list
149                         this.allMobileProviders.sort(new Comparator<MobileProvider>() {
150                                 @Override
151                                 public int compare (final MobileProvider o1, final MobileProvider o2) {
152                                         return o1.getProviderId() > o2.getProviderId() ? 1 : o1.getProviderId() < o2.getProviderId() ? -1 : 0;
153                                 }
154                         }
155                         );
156                 }
157         }
158 }