]> git.mxchange.org Git - jfinancials-war.git/blob - src/java/org/mxchange/jfinancials/beans/country/list/FinancialsCountryListWebViewBean.java
Updated copyright year
[jfinancials-war.git] / src / java / org / mxchange / jfinancials / beans / country / list / FinancialsCountryListWebViewBean.java
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.jfinancials.beans.country.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.jcountry.events.added.ObservableAdminAddedCountryEvent;
32 import org.mxchange.jcountry.exceptions.CountryNotFoundException;
33 import org.mxchange.jcountry.model.data.Country;
34 import org.mxchange.jcountry.model.data.CountrySingletonBeanRemote;
35 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
36
37 /**
38  * A country-list bean
39  * <p>
40  * @author Roland Häder<roland@mxchange.org>
41  */
42 @Named ("countryListController")
43 @ViewScoped
44 public class FinancialsCountryListWebViewBean extends BaseFinancialsBean implements FinancialsCountryListWebViewController {
45
46         /**
47          * Serial number
48          */
49         private static final long serialVersionUID = 176_985_298_681_742_961L;
50
51         /**
52          * A list of all countries
53          */
54         private final List<Country> allCountries;
55
56         /**
57          * Remote country EJB
58          */
59         @EJB (lookup = "java:global/jfinancials-ejb/country!org.mxchange.jcountry.model.data.CountrySingletonBeanRemote")
60         private CountrySingletonBeanRemote countryBean;
61
62         /**
63          * List of all countries
64          */
65         @Inject
66         @NamedCache (cacheName = "countryCache")
67         private transient Cache<Long, Country> countryCache;
68
69         /**
70          * A list of filtered countries
71          */
72         private List<Country> filteredCountries;
73
74         /**
75          * Selected country
76          */
77         private Country selectedCountry;
78
79         /**
80          * Default constructor
81          */
82         public FinancialsCountryListWebViewBean () {
83                 // Call super constructor
84                 super();
85
86                 // Init list
87                 this.allCountries = new LinkedList<>();
88         }
89
90         /**
91          * Observing method when the event is fired that an administrator added a
92          * new country
93          * <p>
94          * @param event Event instance
95          */
96         public void afterAdminAddedCountryEvent (@Observes final ObservableAdminAddedCountryEvent event) {
97                 // Is all valid?
98                 if (null == event) {
99                         // Throw NPE
100                         throw new NullPointerException("event is null"); //NOI18N
101                 } else if (event.getAddedCountry() == null) {
102                         // Throw again ...
103                         throw new NullPointerException("event.addedCountry is null"); //NOI18N
104                 } else if (event.getAddedCountry().getCountryId() == null) {
105                         // And again ...
106                         throw new NullPointerException("event.addedCountry.countryId is null"); //NOI18N
107                 } else if (event.getAddedCountry().getCountryId() < 1) {
108                         // Id is invalid
109                         throw new IllegalArgumentException(MessageFormat.format("event.addedCountry.countryId={0} is not valid.", event.getAddedCountry().getCountryId())); //NOI18N
110                 }
111
112                 // Add the event
113                 this.countryCache.put(event.getAddedCountry().getCountryId(), event.getAddedCountry());
114         }
115
116         @Override
117         public Country findCountryById (final Long countryId) throws CountryNotFoundException {
118                 // Validate parameter
119                 if (null == countryId) {
120                         // Throw NPE
121                         throw new NullPointerException("countryId is null"); //NOI18N
122                 } else if (countryId < 1) {
123                         // Throw IAE
124                         throw new IllegalArgumentException("countryId=" + countryId + " is invalid"); //NOI18N
125                 } else if (!this.countryCache.containsKey(countryId)) {
126                         // Not found
127                         throw new CountryNotFoundException(countryId);
128                 }
129
130                 // Get it from cache
131                 final Country country = this.countryCache.get(countryId);
132
133                 // Return it
134                 return country;
135         }
136
137         @Override
138         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
139         public List<Country> getAllCountries () {
140                 return this.allCountries;
141         }
142
143         /**
144          * Getter for filtered country list
145          * <p>
146          * @return Filtered country list
147          */
148         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
149         public List<Country> getFilteredCountries () {
150                 return this.filteredCountries;
151         }
152
153         /**
154          * Setter for filtered countries list
155          * <p>
156          * @param filteredCountries Filtered countries list
157          */
158         @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
159         public void setFilteredCountries (final List<Country> filteredCountries) {
160                 this.filteredCountries = filteredCountries;
161         }
162
163         /**
164          * Getter for selected country
165          * <p>
166          * @return Selected country
167          */
168         public Country getSelectedCountry () {
169                 return this.selectedCountry;
170         }
171
172         /**
173          * Setter for selected country
174          * <p>
175          * @param selectedCountry Selected country
176          */
177         public void setSelectedCountry (final Country selectedCountry) {
178                 this.selectedCountry = selectedCountry;
179         }
180
181         /**
182          * Post-construction method
183          */
184         @PostConstruct
185         public void initializeList () {
186                 // Is cache there?
187                 if (!this.countryCache.iterator().hasNext()) {
188                         // Add all
189                         for (final Country country : this.countryBean.fetchAllCountries()) {
190                                 // Add it to cache
191                                 this.countryCache.put(country.getCountryId(), country);
192                         }
193                 }
194
195                 // Is cache there and list is not full?
196                 if ((this.getAllCountries().isEmpty()) && (this.countryCache.iterator().hasNext())) {
197                         // Build up list
198                         for (final Cache.Entry<Long, Country> currentEntry : this.countryCache) {
199                                 // Add to list
200                                 this.getAllCountries().add(currentEntry.getValue());
201                         }
202
203                         // Sort list
204                         this.getAllCountries().sort(new Comparator<Country>() {
205                                 @Override
206                                 public int compare (final Country country1, final Country country2) {
207                                         return country1.getCountryId() > country2.getCountryId() ? 1 : country1.getCountryId() < country2.getCountryId() ? -1 : 0;
208                                 }
209                         });
210
211                         // Set full list
212                         this.setFilteredCountries(this.getAllCountries());
213                 }
214         }
215
216 }