]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/country/JobsCountryWebRequestBean.java
a580d442046bf12c4ae220bdeb68cd08ea4d87c5
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / country / JobsCountryWebRequestBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.jjobs.beans.country;
18
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.ejb.EJB;
26 import javax.enterprise.context.RequestScoped;
27 import javax.enterprise.event.Observes;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import org.mxchange.jcountry.data.Country;
31 import org.mxchange.jcountry.data.CountrySingletonBeanRemote;
32 import org.mxchange.jcountry.events.ObservableAdminAddedCountryEvent;
33 import org.mxchange.jjobs.beans.BaseJobsController;
34
35 /**
36  * A country bean
37  * <p>
38  * @author Roland Häder<roland@mxchange.org>
39  */
40 @Named ("countryController")
41 @RequestScoped
42 public class JobsCountryWebRequestBean extends BaseJobsController implements JobsCountryWebRequestController {
43
44         /**
45          * Serial number
46          */
47         private static final long serialVersionUID = 176_985_298_681_742_960L;
48
49         /**
50          * Remote country EJB
51          */
52         @EJB (lookup = "java:global/jjobs-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote")
53         private CountrySingletonBeanRemote countryBean;
54
55         /**
56          * List of all countries
57          */
58         @Inject
59         @NamedCache (cacheName = "countryCache")
60         private Cache<Long, Country> countryCache;
61
62         /**
63          * Default constructor
64          */
65         public JobsCountryWebRequestBean () {
66                 // Call super constructor
67                 super();
68         }
69
70         /**
71          * Observing method when the event is fired that an administrator added a
72          * new country
73          * <p>
74          * @param event Event instance
75          */
76         public void afterAdminAddedCountryEvent (@Observes final ObservableAdminAddedCountryEvent event) {
77                 // Is all valid?
78                 if (null == event) {
79                         // Throw NPE
80                         throw new NullPointerException("event is null"); //NOI18N
81                 } else if (event.getAddedCountry() == null) {
82                         // Throw again ...
83                         throw new NullPointerException("event.addedCountry is null"); //NOI18N
84                 } else if (event.getAddedCountry().getCountryId() == null) {
85                         // And again ...
86                         throw new NullPointerException("event.addedCountry.countryId is null"); //NOI18N
87                 } else if (event.getAddedCountry().getCountryId() < 1) {
88                         // Id is invalid
89                         throw new IllegalArgumentException(MessageFormat.format("event.addedCountry.countryId={0} is not valid.", event.getAddedCountry().getCountryId())); //NOI18N
90                 }
91
92                 // Add the event
93                 this.countryCache.put(event.getAddedCountry().getCountryId(), event.getAddedCountry());
94         }
95
96         @Override
97         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
98         public List<Country> allCountries () {
99                 // Init list
100                 List<Country> list = new LinkedList<>();
101
102                 // Get iterator
103                 Iterator<Cache.Entry<Long, Country>> iterator = this.countryCache.iterator();
104
105                 // Loop over all
106                 while (iterator.hasNext()) {
107                         // Get next entry
108                         final Cache.Entry<Long, Country> next = iterator.next();
109
110                         // Add value to list
111                         list.add(next.getValue());
112                 }
113
114                 // Return it
115                 return list;
116         }
117
118         /**
119          * Post-construction method
120          */
121         @PostConstruct
122         public void init () {
123                 // Is cache there?
124                 if (!this.countryCache.iterator().hasNext()) {
125                         // Get whole list
126                         List<Country> list = this.countryBean.allCountries();
127
128                         // Add all
129                         for (final Iterator<Country> iterator = list.iterator(); iterator.hasNext();) {
130                                 // Get next element
131                                 final Country next = iterator.next();
132
133                                 // Add it to cache
134                                 this.countryCache.put(next.getCountryId(), next);
135                         }
136                 }
137         }
138
139 }