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.addressbook.beans.country;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Objects;
22 import javax.enterprise.context.RequestScoped;
23 import javax.enterprise.event.Event;
24 import javax.enterprise.inject.Any;
25 import javax.faces.view.facelets.FaceletException;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import org.mxchange.addressbook.beans.BaseAddressbookController;
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.AdminEventCountryAdded;
37 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
40 * An administrative country bean
42 * @author Roland Haeder<roland@mxchange.org>
44 @Named ("adminCountryController")
46 public class AddressbookAdminCountryWebRequestBean extends BaseAddressbookController implements AddressbookAdminCountryWebRequestController {
51 private static final long serialVersionUID = 18_598_175_719_603L;
54 * An event triggered when the administrator has added a country
58 private Event<AdminAddedCountryEvent> addedCountryEvent;
63 private String countryAbroadDialPrefix;
68 private CountrySingletonBeanRemote countryBean;
71 * 2-letter country code
73 private String countryCode;
76 * Regular country controller
79 private AddressbookCountryWebApplicationController countryController;
84 private String countryExternalDialPrefix;
89 private String countryI18nKey;
92 * Whether the local dial prefix is required to use
94 private Boolean countryIsLocalPrefixRequired;
99 private Short countryPhoneCode;
102 * Default constructor
104 public AddressbookAdminCountryWebRequestBean () {
107 // Get initial context
108 Context context = new InitialContext();
110 // Try to lookup the bean
111 this.countryBean = (CountrySingletonBeanRemote) context.lookup("java:global/addressbook-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote"); //NOI18N
112 } catch (final NamingException ex) {
114 throw new FaceletException(ex);
119 public String addCountry () {
120 // Create new country object
121 Country country = new CountryData();
124 country.setCountryAbroadDialPrefix(this.getCountryAbroadDialPrefix());
125 country.setCountryCode(this.getCountryCode());
126 country.setCountryExternalDialPrefix(this.getCountryExternalDialPrefix());
127 country.setCountryI18nKey(this.getCountryI18nKey());
128 country.setCountryIsLocalPrefixRequired(this.getCountryIsLocalPrefixRequired());
129 country.setCountryPhoneCode(this.getCountryPhoneCode());
131 // Does it already exist?
132 if (this.isCountryAdded(country)) {
133 // Yes, then abort here
134 throw new FaceletException(new CountryAlreadyAddedException(country));
138 Country updatedCountry = null;
141 // Send country to bean
142 updatedCountry = this.countryBean.addCountry(country);
143 } catch (final CountryAlreadyAddedException ex) {
145 throw new FaceletException(ex);
149 this.addedCountryEvent.fire(new AdminEventCountryAdded(updatedCountry));
152 return "admin_list_country"; //NOI18N
156 public String getCountryAbroadDialPrefix () {
157 return this.countryAbroadDialPrefix;
161 public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
162 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
166 public String getCountryCode () {
167 return this.countryCode;
171 public void setCountryCode (final String countryCode) {
172 this.countryCode = countryCode;
176 public String getCountryExternalDialPrefix () {
177 return this.countryExternalDialPrefix;
181 public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
182 this.countryExternalDialPrefix = countryExternalDialPrefix;
186 public String getCountryI18nKey () {
187 return this.countryI18nKey;
191 public void setCountryI18nKey (final String countryI18nKey) {
192 this.countryI18nKey = countryI18nKey;
196 public Boolean getCountryIsLocalPrefixRequired () {
197 return this.countryIsLocalPrefixRequired;
201 public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
202 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
206 public Short getCountryPhoneCode () {
207 return this.countryPhoneCode;
211 public void setCountryPhoneCode (final Short countryPhoneCode) {
212 this.countryPhoneCode = countryPhoneCode;
216 * Checks if given country is already added by iterating over the whole list
217 * and try to find it.
219 * @param country Country instance to look for
221 * @return Whether the country was already found
223 private boolean isCountryAdded (final Country country) {
224 // Default is not found
225 boolean isAdded = false;
228 List<Country> countries = this.countryController.allCountries();
230 // Get iterator from it
231 Iterator<Country> iterator = countries.iterator();
234 while (iterator.hasNext()) {
236 Country next = iterator.next();
238 // Is country code or i18n the same?
239 if ((Objects.equals(country.getCountryCode(), next.getCountryCode())) || (Objects.equals(country.getCountryI18nKey(), next.getCountryI18nKey()))) {
240 // Yes, then abort search