]> git.mxchange.org Git - addressbook-war.git/blob
b2655bdef66234dddedfe2e22e3396f8dd0cecd4
[addressbook-war.git] /
1 /*
2  * Copyright (C) 2016 - 2022 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.addressbook.beans.mobileprovider.list;
18
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;
26 import javax.ejb.EJB;
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.addressbook.beans.BaseAddressbookBean;
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;
36
37 /**
38  * A views-scoped bean for mobile providers
39  * <p>
40  * @author Roland Häder<roland@mxchange.org>
41  */
42 @Named ("mobileProviderListController")
43 @ViewScoped
44 public class AddressbookMobileProviderListWebViewBean extends BaseAddressbookBean implements AddressbookMobileProviderListWebViewController {
45
46         /**
47          * Serial number
48          */
49         private static final long serialVersionUID = 15_869_423_671_950_277L;
50
51         /**
52          * A list of all mobile providers
53          */
54         private final List<MobileProvider> allMobileProviders;
55
56         /**
57          * A list of filtered mobile providers
58          */
59         private List<MobileProvider> filteredMobileProviders;
60
61         /**
62          * Remote EJB for mobile providers (regular)
63          */
64         @EJB (lookup = "java:global/addressbook-ejb/mobileProvider!org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProviderSingletonBeanRemote")
65         private MobileProviderSingletonBeanRemote mobileProviderBean;
66
67         /**
68          * "Cached" list of mobile providers
69          */
70         @Inject
71         @NamedCache (cacheName = "mobileProviderCache")
72         private transient Cache<Long, MobileProvider> mobileProviderCache;
73
74         /**
75          * Selected mobile provider instance
76          */
77         private MobileProvider selectedMobileProvider;
78
79         /**
80          * Default constructor
81          */
82         public AddressbookMobileProviderListWebViewBean () {
83                 // Call super constructor
84                 super();
85
86                 // Init list
87                 this.allMobileProviders = new LinkedList<>();
88         }
89
90         /**
91          * Observes events being fired after the administrator has added a new
92          * mobile provider
93          * <p>
94          * @param event Event being fired
95          */
96         public void afterAdminAddedMobileProviderEvent (@Observes final AdminAddedMobileProviderEvent event) {
97                 // Is all valid?
98                 if (null == event) {
99                         // Throw NPE
100                         throw new NullPointerException("event is null"); //NOI18N
101                 } else if (event.getAddedMobileProvider() == null) {
102                         // Throw again ...
103                         throw new NullPointerException("event.addedMobileProvider is null"); //NOI18N
104                 } else if (event.getAddedMobileProvider().getProviderId() == null) {
105                         // And again ...
106                         throw new NullPointerException("event.addedMobileProvider.providerId is null"); //NOI18N
107                 } else if (event.getAddedMobileProvider().getProviderId() < 1) {
108                         // Id is invalid
109                         throw new IllegalArgumentException(MessageFormat.format("event.addedMobileProvider.providerId={0} is not valid.", event.getAddedMobileProvider().getProviderId())); //NOI18N
110                 }
111
112                 // Add it to the list
113                 this.mobileProviderCache.put(event.getAddedMobileProvider().getProviderId(), event.getAddedMobileProvider());
114                 this.getAllMobileProviders().add(event.getAddedMobileProvider());
115         }
116
117         @Override
118         public MobileProvider findMobileProviderById (final Long mobileProviderId) throws MobileProviderNotFoundException {
119                 // Validate parameter
120                 if (null == mobileProviderId) {
121                         // Throw NPE
122                         throw new NullPointerException("mobileProviderId is null"); //NOI18N
123                 } else if (mobileProviderId < 1) {
124                         // Throw IAE
125                         throw new IllegalArgumentException("mobileProviderId=" + mobileProviderId + " is invalid."); //NOI18N
126                 } else if (!this.mobileProviderCache.containsKey(mobileProviderId)) {
127                         // Not found
128                         throw new MobileProviderNotFoundException(mobileProviderId);
129                 }
130
131                 // Get it from cache
132                 final MobileProvider mobileProvider = this.mobileProviderCache.get(mobileProviderId);
133
134                 // Return it
135                 return mobileProvider;
136         }
137
138         @Override
139         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
140         public List<MobileProvider> getAllMobileProviders () {
141                 // Return it
142                 return this.allMobileProviders;
143         }
144
145         /**
146          * Getter for filtered mobile provider list
147          * <p>
148          * @return Filtered mobile providers
149          */
150         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
151         public List<MobileProvider> getFilteredMobileProviders () {
152                 return this.filteredMobileProviders;
153         }
154
155         /**
156          * Getter for filtered mobile provider list
157          * <p>
158          * @param filteredMobileProviders Filtered mobile providers
159          */
160         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
161         public void setFilteredMobileProviders (final List<MobileProvider> filteredMobileProviders) {
162                 this.filteredMobileProviders = filteredMobileProviders;
163         }
164
165         /**
166          * Getter for selected mobile provider instance
167          * <p>
168          * @return Selected mobile provider instance
169          */
170         public MobileProvider getSelectedMobileProvider () {
171                 return this.selectedMobileProvider;
172         }
173
174         /**
175          * Setter for selected mobile provider instance
176          * <p>
177          * @param selectedMobileProvider Selected mobile provider instance
178          */
179         public void setSelectedMobileProvider (final MobileProvider selectedMobileProvider) {
180                 this.selectedMobileProvider = selectedMobileProvider;
181         }
182
183         /**
184          * Post-construction method
185          */
186         @PostConstruct
187         public void initializeList () {
188                 // Is cache there?
189                 if (!this.mobileProviderCache.iterator().hasNext()) {
190                         // Add all
191                         for (final MobileProvider mobileProvider : this.mobileProviderBean.fetchAllMobileProviders()) {
192                                 // Add it to cache
193                                 this.mobileProviderCache.put(mobileProvider.getProviderId(), mobileProvider);
194                         }
195                 }
196
197                 // Is the list empty, but filled cache?
198                 if (this.getAllMobileProviders().isEmpty() && this.mobileProviderCache.iterator().hasNext()) {
199                         // Build up list
200                         for (final Cache.Entry<Long, MobileProvider> currentEntry : this.mobileProviderCache) {
201                                 // Add to list
202                                 this.getAllMobileProviders().add(currentEntry.getValue());
203                         }
204
205                         // Sort list
206                         this.getAllMobileProviders().sort(new Comparator<MobileProvider>() {
207                                 @Override
208                                 public int compare (final MobileProvider mobileProvider1, final MobileProvider mobileProvider2) {
209                                         return mobileProvider1.getProviderId() > mobileProvider2.getProviderId() ? 1 : mobileProvider1.getProviderId() < mobileProvider2.getProviderId() ? -1 : 0;
210                                 }
211                         });
212                 }
213         }
214 }