]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/country/JobsCountryWebApplicationBean.java
Please cherry-pick:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / country / JobsCountryWebApplicationBean.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 java.text.MessageFormat;
20 import java.util.List;
21 import javax.annotation.PostConstruct;
22 import javax.ejb.EJB;
23 import javax.enterprise.context.ApplicationScoped;
24 import javax.enterprise.event.Observes;
25 import javax.inject.Named;
26 import org.mxchange.jcountry.data.Country;
27 import org.mxchange.jcountry.data.CountrySingletonBeanRemote;
28 import org.mxchange.jcountry.events.ObservableAdminAddedCountryEvent;
29 import org.mxchange.jjobs.beans.BaseJobsController;
30
31 /**
32  * A country bean
33  * <p>
34  * @author Roland Häder<roland@mxchange.org>
35  */
36 @Named ("countryController")
37 @ApplicationScoped
38 public class JobsCountryWebApplicationBean extends BaseJobsController implements JobsCountryWebApplicationController {
39
40         /**
41          * Serial number
42          */
43         private static final long serialVersionUID = 176_985_298_681_742_960L;
44
45         /**
46          * Remote country EJB
47          */
48         @EJB (lookup = "java:global/jjobs-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote")
49         private CountrySingletonBeanRemote countryBean;
50
51         /**
52          * List of all countries
53          */
54         private List<Country> countryList;
55
56         /**
57          * Default constructor
58          */
59         public JobsCountryWebApplicationBean () {
60                 // Call super constructor
61                 super();
62         }
63
64         /**
65          * Observing method when the event is fired that an administrator added a
66          * new country
67          * <p>
68          * @param event Event instance
69          */
70         public void afterAdminAddedCountryEvent (@Observes final ObservableAdminAddedCountryEvent event) {
71                 // Is all valid?
72                 if (null == event) {
73                         // Throw NPE
74                         throw new NullPointerException("event is null"); //NOI18N
75                 } else if (event.getAddedCountry() == null) {
76                         // Throw again ...
77                         throw new NullPointerException("event.addedCountry is null"); //NOI18N
78                 } else if (event.getAddedCountry().getCountryId() == null) {
79                         // And again ...
80                         throw new NullPointerException("event.addedCountry.countryId is null"); //NOI18N
81                 } else if (event.getAddedCountry().getCountryId() < 1) {
82                         // Id is invalid
83                         throw new IllegalArgumentException(MessageFormat.format("event.addedCountry.countryId={0} is not valid.", event.getAddedCountry().getCountryId())); //NOI18N
84                 }
85
86                 // Add the event
87                 this.countryList.add(event.getAddedCountry());
88         }
89
90         @Override
91         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
92         public List<Country> allCountries () {
93                 // Return "cached" version
94                 return this.countryList;
95         }
96
97         /**
98          * Post-construction method
99          */
100         @PostConstruct
101         public void init () {
102                 // "Cache" country list as this will not change so often.
103                 this.countryList = this.countryBean.allCountries();
104         }
105
106 }