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.jjobs.beans.country.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.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.jjobs.beans.BaseJobsBean;
40 * @author Roland Häder<roland@mxchange.org>
42 @Named ("countryListController")
44 public class JobsCountryListWebViewBean extends BaseJobsBean implements JobsCountryListWebViewController {
49 private static final long serialVersionUID = 176_985_298_681_742_961L;
52 * A list of all countries
54 private final List<Country> allCountries;
59 @EJB (lookup = "java:global/jjobs-ejb/country!org.mxchange.jcountry.model.data.CountrySingletonBeanRemote")
60 private CountrySingletonBeanRemote countryBean;
63 * List of all countries
66 @NamedCache (cacheName = "countryCache")
67 private transient Cache<Long, Country> countryCache;
70 * A list of filtered countries
72 private List<Country> filteredCountries;
77 private Country selectedCountry;
82 public JobsCountryListWebViewBean () {
83 // Call super constructor
87 this.allCountries = new LinkedList<>();
91 * Observing method when the event is fired that an administrator added a
94 * @param event Event instance
96 public void afterAdminAddedCountryEvent (@Observes final ObservableAdminAddedCountryEvent event) {
100 throw new NullPointerException("event is null"); //NOI18N
101 } else if (event.getAddedCountry() == null) {
103 throw new NullPointerException("event.addedCountry is null"); //NOI18N
104 } else if (event.getAddedCountry().getCountryId() == null) {
106 throw new NullPointerException("event.addedCountry.countryId is null"); //NOI18N
107 } else if (event.getAddedCountry().getCountryId() < 1) {
109 throw new IllegalArgumentException(MessageFormat.format("event.addedCountry.countryId={0} is not valid.", event.getAddedCountry().getCountryId())); //NOI18N
113 this.countryCache.put(event.getAddedCountry().getCountryId(), event.getAddedCountry());
117 public Country findCountryById (final Long countryId) throws CountryNotFoundException {
118 // Validate parameter
119 if (null == countryId) {
121 throw new NullPointerException("countryId is null"); //NOI18N
122 } else if (countryId < 1) {
124 throw new IllegalArgumentException("countryId=" + countryId + " is invalid"); //NOI18N
125 } else if (!this.countryCache.containsKey(countryId)) {
127 throw new CountryNotFoundException(countryId);
131 final Country country = this.countryCache.get(countryId);
138 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
139 public List<Country> getAllCountries () {
140 return this.allCountries;
144 * Getter for filtered country list
146 * @return Filtered country list
148 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
149 public List<Country> getFilteredCountries () {
150 return this.filteredCountries;
154 * Setter for filtered countries list
156 * @param filteredCountries Filtered countries list
158 @SuppressWarnings ("AssignmentToCollectionOrArrayFieldFromParameter")
159 public void setFilteredCountries (final List<Country> filteredCountries) {
160 this.filteredCountries = filteredCountries;
164 * Getter for selected country
166 * @return Selected country
168 public Country getSelectedCountry () {
169 return this.selectedCountry;
173 * Setter for selected country
175 * @param selectedCountry Selected country
177 public void setSelectedCountry (final Country selectedCountry) {
178 this.selectedCountry = selectedCountry;
182 * Post-construction method
185 public void initializeList () {
187 if (!this.countryCache.iterator().hasNext()) {
189 for (final Country country : this.countryBean.fetchAllCountries()) {
191 this.countryCache.put(country.getCountryId(), country);
195 // Is cache there and list is not full?
196 if ((this.getAllCountries().isEmpty()) && (this.countryCache.iterator().hasNext())) {
198 for (final Cache.Entry<Long, Country> currentEntry : this.countryCache) {
200 this.getAllCountries().add(currentEntry.getValue());
204 this.getAllCountries().sort(new Comparator<Country>() {
206 public int compare (final Country country1, final Country country2) {
207 return country1.getCountryId() > country2.getCountryId() ? 1 : country1.getCountryId() < country2.getCountryId() ? -1 : 0;
212 this.setFilteredCountries(this.getAllCountries());