]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/country/JobsCountryWebApplicationBean.java
Continued with adding features:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / country / JobsCountryWebApplicationBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.AdminAddedCountryEvent;
32
33 /**
34  * A country bean
35  * <p>
36  * @author Roland Haeder<roland@mxchange.org>
37  */
38 @Named ("country")
39 @ApplicationScoped
40 public class JobsCountryWebApplicationBean implements JobsCountryWebApplicationController {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 176_985_298_681_742_960L;
46
47         /**
48          * Remote country EJB
49          */
50         private CountrySingletonBeanRemote countryBean;
51
52         /**
53          * List of all countries
54          */
55         private List<Country> countryList;
56
57         /**
58          * Default constructor
59          */
60         public JobsCountryWebApplicationBean () {
61                 // Try this
62                 try {
63                         // Get initial context
64                         Context context = new InitialContext();
65
66                         // Try to lookup the bean
67                         this.countryBean = (CountrySingletonBeanRemote) context.lookup("java:global/jjobs-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote"); //NOI18N
68                 } catch (final NamingException ex) {
69                         // Continue to throw
70                         throw new FaceletException(ex);
71                 }
72         }
73
74         @Override
75         public void afterAdminAddedCountry (@Observes final AdminAddedCountryEvent event) {
76                 // Is all valid?
77                 if (null == event) {
78                         // Throw NPE
79                         throw new NullPointerException("event is null"); //NOI18N
80                 } else if (event.getAddedCountry() == null) {
81                         // Throw again ...
82                         throw new NullPointerException("event.addedCountry is null"); //NOI18N
83                 } else if (event.getAddedCountry().getCountryId() == null) {
84                         // And again ...
85                         throw new NullPointerException("event.addedCountry.countryId is null"); //NOI18N
86                 } else if (event.getAddedCountry().getCountryId() < 1) {
87                         // Id is invalid
88                         throw new IllegalArgumentException(MessageFormat.format("event.addedCountry.countryId={0} is not valid.", event.getAddedCountry().getCountryId())); //NOI18N
89                 }
90
91                 // Add the event
92                 this.countryList.add(event.getAddedCountry());
93         }
94
95         @Override
96         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
97         public List<Country> allCountries () {
98                 // Return "cached" version
99                 return this.countryList;
100         }
101
102         /**
103          * Post-initialization of this class
104          */
105         @PostConstruct
106         public void init () {
107                 // "Cache" country list as this will not change so often.
108                 this.countryList = this.countryBean.allCountries();
109         }
110 }