2 * Copyright (C) 2016, 2017 Roland Häder
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;
19 import fish.payara.cdi.jsr107.impl.NamedCache;
20 import java.text.MessageFormat;
21 import java.util.Iterator;
22 import java.util.LinkedList;
23 import java.util.List;
24 import javax.annotation.PostConstruct;
25 import javax.cache.Cache;
27 import javax.enterprise.context.RequestScoped;
28 import javax.enterprise.event.Observes;
29 import javax.inject.Inject;
30 import javax.inject.Named;
31 import org.mxchange.jcountry.events.ObservableAdminAddedCountryEvent;
32 import org.mxchange.jcountry.model.data.Country;
33 import org.mxchange.jcountry.model.data.CountrySingletonBeanRemote;
34 import org.mxchange.jcountry.events.ObservableAdminAddedCountryEvent;
35 import org.mxchange.jjobs.beans.BaseJobsBean;
40 * @author Roland Häder<roland@mxchange.org>
42 @Named ("countryController")
44 public class JobsCountryWebRequestBean extends BaseJobsBean implements JobsCountryWebRequestController {
49 private static final long serialVersionUID = 176_985_298_681_742_960L;
54 @EJB (lookup = "java:global/jjobs-ejb/country!org.mxchange.jcountry.model.data.CountrySingletonBeanRemote")
55 private CountrySingletonBeanRemote countryBean;
58 * List of all countries
61 @NamedCache (cacheName = "countryCache")
62 private Cache<Long, Country> countryCache;
67 public JobsCountryWebRequestBean () {
68 // Call super constructor
73 * Observing method when the event is fired that an administrator added a
76 * @param event Event instance
78 public void afterAdminAddedCountryEvent (@Observes final ObservableAdminAddedCountryEvent event) {
82 throw new NullPointerException("event is null"); //NOI18N
83 } else if (event.getAddedCountry() == null) {
85 throw new NullPointerException("event.addedCountry is null"); //NOI18N
86 } else if (event.getAddedCountry().getCountryId() == null) {
88 throw new NullPointerException("event.addedCountry.countryId is null"); //NOI18N
89 } else if (event.getAddedCountry().getCountryId() < 1) {
91 throw new IllegalArgumentException(MessageFormat.format("event.addedCountry.countryId={0} is not valid.", event.getAddedCountry().getCountryId())); //NOI18N
95 this.countryCache.put(event.getAddedCountry().getCountryId(), event.getAddedCountry());
99 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
100 public List<Country> allCountries () {
102 final List<Country> list = new LinkedList<>();
105 final Iterator<Cache.Entry<Long, Country>> iterator = this.countryCache.iterator();
108 while (iterator.hasNext()) {
110 final Cache.Entry<Long, Country> next = iterator.next();
113 list.add(next.getValue());
121 * Post-construction method
124 public void init () {
126 if (!this.countryCache.iterator().hasNext()) {
128 final List<Country> list = this.countryBean.allCountries();
131 for (final Iterator<Country> iterator = list.iterator(); iterator.hasNext();) {
133 final Country next = iterator.next();
136 this.countryCache.put(next.getCountryId(), next);