2 * Copyright (C) 2016, 2017 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.util.Iterator;
20 import java.util.List;
21 import java.util.Objects;
22 import javax.annotation.PostConstruct;
23 import javax.enterprise.context.RequestScoped;
24 import javax.enterprise.event.Event;
25 import javax.enterprise.inject.Any;
26 import javax.faces.view.facelets.FaceletException;
27 import javax.inject.Inject;
28 import javax.inject.Named;
29 import javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32 import org.mxchange.jcountry.data.Country;
33 import org.mxchange.jcountry.data.CountryData;
34 import org.mxchange.jcountry.data.CountrySingletonBeanRemote;
35 import org.mxchange.jcountry.events.AdminAddedCountryEvent;
36 import org.mxchange.jcountry.events.ObservableAdminAddedCountryEvent;
37 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
38 import org.mxchange.jjobs.beans.BaseJobsController;
41 * An administrative country bean
43 * @author Roland Häder<roland@mxchange.org>
45 @Named ("adminCountryController")
47 public class JobsAdminCountryWebRequestBean extends BaseJobsController implements JobsAdminCountryWebRequestController {
52 private static final long serialVersionUID = 18_598_175_719_603L;
55 * An event triggered when the administrator has added a country
59 private Event<ObservableAdminAddedCountryEvent> addedCountryEvent;
64 private String countryAbroadDialPrefix;
69 private CountrySingletonBeanRemote countryBean;
72 * 2-letter country code
74 private String countryCode;
77 * Regular country controller
80 private JobsCountryWebApplicationController countryController;
85 private String countryExternalDialPrefix;
90 private String countryI18nKey;
93 * Whether the local dial prefix is required to use
95 private Boolean countryIsLocalPrefixRequired;
100 private Short countryPhoneCode;
103 * Default constructor
105 public JobsAdminCountryWebRequestBean () {
106 // Call super constructor
111 public String addCountry () {
112 // Create new country object
113 Country country = new CountryData();
116 country.setCountryAbroadDialPrefix(this.getCountryAbroadDialPrefix());
117 country.setCountryCode(this.getCountryCode());
118 country.setCountryExternalDialPrefix(this.getCountryExternalDialPrefix());
119 country.setCountryI18nKey(this.getCountryI18nKey());
120 country.setCountryIsLocalPrefixRequired(this.getCountryIsLocalPrefixRequired());
121 country.setCountryPhoneCode(this.getCountryPhoneCode());
123 // Does it already exist?
124 if (this.isCountryAdded(country)) {
125 // Yes, then abort here
126 throw new FaceletException(new CountryAlreadyAddedException(country));
130 Country updatedCountry = null;
133 // Send country to bean
134 updatedCountry = this.countryBean.addCountry(country);
135 } catch (final CountryAlreadyAddedException ex) {
137 throw new FaceletException(ex);
141 this.addedCountryEvent.fire(new AdminAddedCountryEvent(updatedCountry));
147 return "admin_list_country"; //NOI18N
151 public List<Country> allCountries () {
152 // Return "cached" version
153 return this.countryController.allCountries();
157 public String getCountryAbroadDialPrefix () {
158 return this.countryAbroadDialPrefix;
162 public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
163 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
167 public String getCountryCode () {
168 return this.countryCode;
172 public void setCountryCode (final String countryCode) {
173 this.countryCode = countryCode;
177 public String getCountryExternalDialPrefix () {
178 return this.countryExternalDialPrefix;
182 public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
183 this.countryExternalDialPrefix = countryExternalDialPrefix;
187 public String getCountryI18nKey () {
188 return this.countryI18nKey;
192 public void setCountryI18nKey (final String countryI18nKey) {
193 this.countryI18nKey = countryI18nKey;
197 public Boolean getCountryIsLocalPrefixRequired () {
198 return this.countryIsLocalPrefixRequired;
202 public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
203 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
207 public Short getCountryPhoneCode () {
208 return this.countryPhoneCode;
212 public void setCountryPhoneCode (final Short countryPhoneCode) {
213 this.countryPhoneCode = countryPhoneCode;
217 public boolean hasCountries () {
218 return (!this.allCountries().isEmpty());
222 * Post-construction method
225 public void init () {
228 // Get initial context
229 Context context = new InitialContext();
231 // Try to lookup the bean
232 this.countryBean = (CountrySingletonBeanRemote) context.lookup("java:global/jjobs-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote"); //NOI18N
233 } catch (final NamingException ex) {
235 throw new FaceletException(ex);
240 * Clears this bean's data. This should be called after a form has been
241 * submitted and the processing of the form was successful.
243 private void clear () {
245 this.setCountryAbroadDialPrefix(null);
246 this.setCountryCode(null);
247 this.setCountryExternalDialPrefix(null);
248 this.setCountryI18nKey(null);
249 this.setCountryIsLocalPrefixRequired(null);
250 this.setCountryPhoneCode(null);
254 * Checks if given country is already added by iterating over the whole list
255 * and try to find it.
257 * @param country Country instance to look for
259 * @return Whether the country was already found
261 private boolean isCountryAdded (final Country country) {
262 // Default is not found
263 boolean isAdded = false;
266 List<Country> countries = this.countryController.allCountries();
268 // Get iterator from it
269 Iterator<Country> iterator = countries.iterator();
272 while (iterator.hasNext()) {
274 Country next = iterator.next();
276 // Is country code or i18n the same?
277 if ((Objects.equals(country.getCountryCode(), next.getCountryCode())) || (Objects.equals(country.getCountryI18nKey(), next.getCountryI18nKey()))) {
278 // Yes, then abort search