2 * Copyright (C) 2016 Roland Haeder
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.Collections;
21 import java.util.List;
22 import javax.annotation.PostConstruct;
23 import javax.enterprise.context.ApplicationScoped;
24 import javax.enterprise.event.Observes;
25 import javax.faces.view.facelets.FaceletException;
26 import javax.inject.Named;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30 import org.mxchange.jcountry.data.Country;
31 import org.mxchange.jcountry.data.CountrySingletonBeanRemote;
32 import org.mxchange.jcountry.events.AdminAddedCountryEvent;
33 import org.mxchange.jjobs.beans.BaseJobsController;
38 * @author Roland Haeder<roland@mxchange.org>
40 @Named ("countryController")
42 public class JobsCountryWebApplicationBean extends BaseJobsController implements JobsCountryWebApplicationController {
47 private static final long serialVersionUID = 176_985_298_681_742_960L;
52 private CountrySingletonBeanRemote countryBean;
55 * List of all countries
57 private List<Country> countryList;
62 public JobsCountryWebApplicationBean () {
65 // Get initial context
66 Context context = new InitialContext();
68 // Try to lookup the bean
69 this.countryBean = (CountrySingletonBeanRemote) context.lookup("java:global/jjobs-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote"); //NOI18N
70 } catch (final NamingException ex) {
72 throw new FaceletException(ex);
77 public void afterAdminAddedCountry (@Observes final AdminAddedCountryEvent event) {
81 throw new NullPointerException("event is null"); //NOI18N
82 } else if (event.getAddedCountry() == null) {
84 throw new NullPointerException("event.addedCountry is null"); //NOI18N
85 } else if (event.getAddedCountry().getCountryId() == null) {
87 throw new NullPointerException("event.addedCountry.countryId is null"); //NOI18N
88 } else if (event.getAddedCountry().getCountryId() < 1) {
90 throw new IllegalArgumentException(MessageFormat.format("event.addedCountry.countryId={0} is not valid.", event.getAddedCountry().getCountryId())); //NOI18N
94 this.countryList.add(event.getAddedCountry());
98 public List<Country> allCountries () {
99 // Return "cached" version
100 return Collections.unmodifiableList(this.countryList);
104 public boolean hasCountries () {
105 return (!this.countryList.isEmpty());
109 * Post-initialization of this class
112 public void init () {
113 // "Cache" country list as this will not change so often.
114 this.countryList = this.countryBean.allCountries();