2 * Copyright (C) 2016 - 2020 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.jjobs.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.jcountry.events.added.AdminAddedCountryEvent;
28 import org.mxchange.jcountry.events.added.ObservableAdminAddedCountryEvent;
29 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
30 import org.mxchange.jcountry.model.data.AdminCountrySessionBeanRemote;
31 import org.mxchange.jcountry.model.data.Country;
32 import org.mxchange.jcountry.model.data.CountryData;
33 import org.mxchange.jjobs.beans.BaseJobsBean;
34 import org.mxchange.jjobs.beans.country.list.JobsCountryListWebViewController;
37 * An administrative country bean
39 * @author Roland Häder<roland@mxchange.org>
41 @Named ("adminCountryController")
43 public class JobsAdminCountryWebRequestBean extends BaseJobsBean implements JobsAdminCountryWebRequestController {
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/jjobs-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 JobsCountryListWebViewController countryListController;
97 private Short countryPhoneCode;
100 * Default constructor
102 public JobsAdminCountryWebRequestBean () {
103 // Call super constructor
108 * Adds country to all relevant beans and sends it to the EJB. A redirect
109 * should happen after successfull creation.
111 * @todo Add field validation
113 public void addCountry () {
114 // Create new country object
115 final Country country = new CountryData();
118 country.setCountryAbroadDialPrefix(this.getCountryAbroadDialPrefix());
119 country.setCountryCode(this.getCountryCode());
120 country.setCountryExternalDialPrefix(this.getCountryExternalDialPrefix());
121 country.setCountryI18nKey(this.getCountryI18nKey());
122 country.setCountryIsLocalPrefixRequired(this.getCountryIsLocalPrefixRequired());
123 country.setCountryPhoneCode(this.getCountryPhoneCode());
125 // Does it already exist?
126 if (this.isCountryAdded(country)) {
127 // Yes, then abort here
128 throw new FacesException(new CountryAlreadyAddedException(country));
132 final Country updatedCountry;
135 // Send country to bean
136 updatedCountry = this.adminCountryBean.addCountry(country);
137 } catch (final CountryAlreadyAddedException ex) {
139 throw new FacesException(ex);
143 this.addedCountryEvent.fire(new AdminAddedCountryEvent(updatedCountry));
150 * Getter for abroad dial prefix
152 * @return Abroad dial prefix
154 public String getCountryAbroadDialPrefix () {
155 return this.countryAbroadDialPrefix;
159 * Setter for abroad dial prefix
161 * @param countryAbroadDialPrefix Abroad dial prefix
163 public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
164 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
168 * Getter for 2-characters country code
170 * @return Country code
172 public String getCountryCode () {
173 return this.countryCode;
177 * Setter for 2-characters country code
179 * @param countryCode Country code
181 public void setCountryCode (final String countryCode) {
182 this.countryCode = countryCode;
186 * Getter for external dial prefix
188 * @return External dial prefix
190 public String getCountryExternalDialPrefix () {
191 return this.countryExternalDialPrefix;
195 * Setter for external dial prefix
197 * @param countryExternalDialPrefix External dial prefix
199 public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
200 this.countryExternalDialPrefix = countryExternalDialPrefix;
204 * Getter for i18n key for country name
206 * @return i18n key for country name
208 public String getCountryI18nKey () {
209 return this.countryI18nKey;
213 * Setter for i18n key for country name
215 * @param countryI18nKey i18n key for country name
217 public void setCountryI18nKey (final String countryI18nKey) {
218 this.countryI18nKey = countryI18nKey;
222 * Getter for whether the local dial prefix is required for local calls
224 * @return Whether the local dial prefix is required
226 public Boolean getCountryIsLocalPrefixRequired () {
227 return this.countryIsLocalPrefixRequired;
231 * Setter for whether the local dial prefix is required for local calls
233 * @param countryIsLocalPrefixRequired Whether the local dial prefix is
236 public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
237 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
241 * Getter for country code (example: 49 for Germany, 63 for Philippines)
243 * @return Dial number without prefix
245 public Short getCountryPhoneCode () {
246 return this.countryPhoneCode;
250 * Setter for country code (example: 49 for Germany, 63 for Philippines)
252 * @param countryPhoneCode Country code
254 public void setCountryPhoneCode (final Short countryPhoneCode) {
255 this.countryPhoneCode = countryPhoneCode;
259 * Clears this bean's data. This should be called after a form has been
260 * submitted and the processing of the form was successful.
262 private void clear () {
264 this.setCountryAbroadDialPrefix(null);
265 this.setCountryCode(null);
266 this.setCountryExternalDialPrefix(null);
267 this.setCountryI18nKey(null);
268 this.setCountryIsLocalPrefixRequired(null);
269 this.setCountryPhoneCode(null);
273 * Checks if given country is already added by iterating over the whole list
274 * and try to find it.
276 * @param country Country instance to look for
278 * @return Whether the country was already found
280 private boolean isCountryAdded (final Country country) {
281 // Default is not found
282 boolean isAdded = false;
285 for (final Country currentCountry : this.countryListController.getAllCountries()) {
286 // Is country code or i18n the same?
287 if ((Objects.equals(country.getCountryCode(), currentCountry.getCountryCode())) || (Objects.equals(country.getCountryI18nKey(), currentCountry.getCountryI18nKey()))) {
288 // Yes, then abort search