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