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.jcountry.data.Country;
32 import org.mxchange.jcountry.data.CountryData;
33 import org.mxchange.jcountry.data.CountrySingletonBeanRemote;
34 import org.mxchange.jcountry.events.AdminAddedCountryEvent;
35 import org.mxchange.jcountry.events.AdminEventCountryAdded;
36 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
39 * An administrative country bean
41 * @author Roland Haeder<roland@mxchange.org>
43 @Named ("adminCountryController")
45 public class AddressbookAdminCountryWebRequestBean implements AddressbookAdminCountryWebRequestController {
50 private static final long serialVersionUID = 18_598_175_719_603L;
53 * An event triggered when the administrator has added a country
57 private Event<AdminAddedCountryEvent> addedCountryEvent;
62 private String countryAbroadDialPrefix;
67 private CountrySingletonBeanRemote countryBean;
70 * 2-letter country code
72 private String countryCode;
75 * Regular country controller
78 private AddressbookCountryWebApplicationController countryController;
83 private String countryExternalDialPrefix;
88 private String countryI18nKey;
91 * Whether the local dial prefix is required to use
93 private Boolean countryIsLocalPrefixRequired;
98 private Short countryPhoneCode;
101 * Default constructor
103 public AddressbookAdminCountryWebRequestBean () {
106 // Get initial context
107 Context context = new InitialContext();
109 // Try to lookup the bean
110 this.countryBean = (CountrySingletonBeanRemote) context.lookup("java:global/addressbook-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote"); //NOI18N
111 } catch (final NamingException ex) {
113 throw new FaceletException(ex);
118 public void addCountry () {
119 // Create new country object
120 Country country = new CountryData();
123 country.setCountryAbroadDialPrefix(this.getCountryAbroadDialPrefix());
124 country.setCountryCode(this.getCountryCode());
125 country.setCountryExternalDialPrefix(this.getCountryExternalDialPrefix());
126 country.setCountryI18nkey(this.getCountryI18nKey());
127 country.setCountryIsLocalPrefixRequired(this.getCountryIsLocalPrefixRequired());
128 country.setCountryPhoneCode(this.getCountryPhoneCode());
130 // Does it already exist?
131 if (this.isCountryAdded(country)) {
132 // Yes, then abort here
133 throw new FaceletException(new CountryAlreadyAddedException(country));
137 Country updatedCountry = null;
140 // Send country to bean
141 updatedCountry = this.countryBean.addCountry(country);
142 } catch (final CountryAlreadyAddedException ex) {
144 throw new FaceletException(ex);
148 this.addedCountryEvent.fire(new AdminEventCountryAdded(updatedCountry));
152 public List<Country> allCountries () {
153 // Return "cached" version
154 return this.countryController.allCountries();
158 public String getCountryAbroadDialPrefix () {
159 return this.countryAbroadDialPrefix;
163 public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
164 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
168 public String getCountryCode () {
169 return this.countryCode;
173 public void setCountryCode (final String countryCode) {
174 this.countryCode = countryCode;
178 public String getCountryExternalDialPrefix () {
179 return this.countryExternalDialPrefix;
183 public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
184 this.countryExternalDialPrefix = countryExternalDialPrefix;
188 public String getCountryI18nKey () {
189 return this.countryI18nKey;
193 public void setCountryI18nKey (final String countryI18nKey) {
194 this.countryI18nKey = countryI18nKey;
198 public Boolean getCountryIsLocalPrefixRequired () {
199 return this.countryIsLocalPrefixRequired;
203 public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
204 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
208 public Short getCountryPhoneCode () {
209 return this.countryPhoneCode;
213 public void setCountryPhoneCode (final Short countryPhoneCode) {
214 this.countryPhoneCode = countryPhoneCode;
218 public boolean hasCountries () {
219 return (!this.allCountries().isEmpty());
223 * Checks if given country is already added by iterating over the whole list
224 * and try to find it.
226 * @param country Country instance to look for
228 * @return Whether the country was already found
230 private boolean isCountryAdded (final Country country) {
231 // Default is not found
232 boolean isAdded = false;
235 List<Country> countries = this.countryController.allCountries();
237 // Get iterator from it
238 Iterator<Country> iterator = countries.iterator();
241 while (iterator.hasNext()) {
243 Country next = iterator.next();
245 // Is country code or i18n the same?
246 if ((Objects.equals(country.getCountryCode(), next.getCountryCode())) || (Objects.equals(country.getCountryI18nkey(), next.getCountryI18nkey()))) {
247 // Yes, then abort search