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