2 * Copyright (C) 2016 - 2022 Free Software Foundation
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.Objects;
21 import javax.enterprise.context.RequestScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.inject.Any;
24 import javax.faces.FacesException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import org.mxchange.addressbook.beans.BaseAddressbookBean;
28 import org.mxchange.addressbook.beans.country.list.AddressbookCountryListWebViewController;
29 import org.mxchange.jcountry.events.added.AdminAddedCountryEvent;
30 import org.mxchange.jcountry.events.added.ObservableAdminAddedCountryEvent;
31 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
32 import org.mxchange.jcountry.model.data.AdminCountrySessionBeanRemote;
33 import org.mxchange.jcountry.model.data.Country;
34 import org.mxchange.jcountry.model.data.CountryData;
37 * An administrative country bean
39 * @author Roland Häder<roland@mxchange.org>
41 @Named ("adminCountryController")
43 public class AddressbookAdminCountryWebRequestBean extends BaseAddressbookBean implements AddressbookAdminCountryWebRequestController {
48 private static final long serialVersionUID = 18_598_175_719_603L;
51 * An event triggered when the administrator has added a country
55 private Event<ObservableAdminAddedCountryEvent> addedCountryEvent;
60 @EJB (lookup = "java:global/addressbook-ejb/adminCountry!org.mxchange.jcountry.model.data.AdminCountrySessionBeanRemote")
61 private AdminCountrySessionBeanRemote adminCountryBean;
66 private String countryAbroadDialPrefix;
69 * 2-letter country code
71 private String countryCode;
76 private String countryExternalDialPrefix;
81 private String countryI18nKey;
84 * Whether the local dial prefix is required to use
86 private Boolean countryIsLocalPrefixRequired;
89 * Regular country controller
92 private AddressbookCountryListWebViewController countryListController;
97 private Short countryPhoneCode;
100 * Default constructor
102 public AddressbookAdminCountryWebRequestBean () {
103 // Call super constructor
108 * Adds country to all relevant beans and sends it to the EJB. A redirect
109 * should happen after successful creation.
111 * @todo Add field validation
113 public void addCountry () {
114 // Create new country object
115 final Country country = new CountryData(
116 this.getCountryAbroadDialPrefix(),
117 this.getCountryCode(),
118 this.getCountryExternalDialPrefix(),
119 this.getCountryI18nKey(),
120 this.getCountryIsLocalPrefixRequired(),
121 this.getCountryPhoneCode()
124 // Does it already exist?
125 if (this.isCountryAdded(country)) {
126 // Yes, then abort here
127 throw new FacesException(new CountryAlreadyAddedException(country));
131 final Country updatedCountry;
134 // Send country to bean
135 updatedCountry = this.adminCountryBean.addCountry(country);
136 } catch (final CountryAlreadyAddedException ex) {
138 throw new FacesException(ex);
142 this.addedCountryEvent.fire(new AdminAddedCountryEvent(updatedCountry));
149 * Getter for abroad dial prefix
151 * @return Abroad dial prefix
153 public String getCountryAbroadDialPrefix () {
154 return this.countryAbroadDialPrefix;
158 * Setter for abroad dial prefix
160 * @param countryAbroadDialPrefix Abroad dial prefix
162 public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
163 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
167 * Getter for 2-characters country code
169 * @return Country code
171 public String getCountryCode () {
172 return this.countryCode;
176 * Setter for 2-characters country code
178 * @param countryCode Country code
180 public void setCountryCode (final String countryCode) {
181 this.countryCode = countryCode;
185 * Getter for external dial prefix
187 * @return External dial prefix
189 public String getCountryExternalDialPrefix () {
190 return this.countryExternalDialPrefix;
194 * Setter for external dial prefix
196 * @param countryExternalDialPrefix External dial prefix
198 public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
199 this.countryExternalDialPrefix = countryExternalDialPrefix;
203 * Getter for i18n key for country name
205 * @return i18n key for country name
207 public String getCountryI18nKey () {
208 return this.countryI18nKey;
212 * Setter for i18n key for country name
214 * @param countryI18nKey i18n key for country name
216 public void setCountryI18nKey (final String countryI18nKey) {
217 this.countryI18nKey = countryI18nKey;
221 * Getter for whether the local dial prefix is required for local calls
223 * @return Whether the local dial prefix is required
225 public Boolean getCountryIsLocalPrefixRequired () {
226 return this.countryIsLocalPrefixRequired;
230 * Setter for whether the local dial prefix is required for local calls
232 * @param countryIsLocalPrefixRequired Whether the local dial prefix is
235 public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
236 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
240 * Getter for country code (example: 49 for Germany, 63 for Philippines)
242 * @return Dial number without prefix
244 public Short getCountryPhoneCode () {
245 return this.countryPhoneCode;
249 * Setter for country code (example: 49 for Germany, 63 for Philippines)
251 * @param countryPhoneCode Country code
253 public void setCountryPhoneCode (final Short countryPhoneCode) {
254 this.countryPhoneCode = countryPhoneCode;
258 * Clears this bean's data. This should be called after a form has been
259 * submitted and the processing of the form was successful.
261 private void clear () {
263 this.setCountryAbroadDialPrefix(null);
264 this.setCountryCode(null);
265 this.setCountryExternalDialPrefix(null);
266 this.setCountryI18nKey(null);
267 this.setCountryIsLocalPrefixRequired(null);
268 this.setCountryPhoneCode(null);
272 * Checks if given country is already added by iterating over the whole list
273 * and try to find it.
275 * @param country Country instance to look for
277 * @return Whether the country was already found
279 private boolean isCountryAdded (final Country country) {
280 // Default is not found
281 boolean isAdded = false;
284 for (final Country currentCountry : this.countryListController.getAllCountries()) {
285 // Is country code or i18n the same?
286 if ((Objects.equals(country.getCountryCode(), currentCountry.getCountryCode())) || (Objects.equals(country.getCountryI18nKey(), currentCountry.getCountryI18nKey()))) {
287 // Yes, then abort search