2 * Copyright (C) 2016 Roland Häder
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.
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.
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/>.
17 package org.mxchange.jjobs.beans.country;
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.jjobs.beans.BaseJobsController;
32 import org.mxchange.jcountry.events.ObservableAdminAddedCountryEvent;
37 * @author Roland Häder<roland@mxchange.org>
39 @Named ("countryController")
41 public class JobsCountryWebApplicationBean extends BaseJobsController implements JobsCountryWebApplicationController {
46 private static final long serialVersionUID = 176_985_298_681_742_960L;
51 private CountrySingletonBeanRemote countryBean;
54 * List of all countries
56 private List<Country> countryList;
61 public JobsCountryWebApplicationBean () {
64 // Get initial context
65 Context context = new InitialContext();
67 // Try to lookup the bean
68 this.countryBean = (CountrySingletonBeanRemote) context.lookup("java:global/jjobs-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote"); //NOI18N
69 } catch (final NamingException ex) {
71 throw new FaceletException(ex);
76 public void afterAdminAddedCountry (@Observes final ObservableAdminAddedCountryEvent event) {
80 throw new NullPointerException("event is null"); //NOI18N
81 } else if (event.getAddedCountry() == null) {
83 throw new NullPointerException("event.addedCountry is null"); //NOI18N
84 } else if (event.getAddedCountry().getCountryId() == null) {
86 throw new NullPointerException("event.addedCountry.countryId is null"); //NOI18N
87 } else if (event.getAddedCountry().getCountryId() < 1) {
89 throw new IllegalArgumentException(MessageFormat.format("event.addedCountry.countryId={0} is not valid.", event.getAddedCountry().getCountryId())); //NOI18N
93 this.countryList.add(event.getAddedCountry());
97 @SuppressWarnings ("ReturnOfCollectionOrArrayField")
98 public List<Country> allCountries () {
99 // Return "cached" version
100 return this.countryList;
104 * Post-initialization of this class
107 public void init () {
108 // "Cache" country list as this will not change so often.
109 this.countryList = this.countryBean.allCountries();