2 * Copyright (C) 2016 - 2024 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.pizzaapplication.beans.country;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Objects;
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 org.mxchange.jcountry.events.AdminAddedCountryEvent;
30 import org.mxchange.jcountry.events.ObservableAdminAddedCountryEvent;
31 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
32 import org.mxchange.jcountry.model.data.Country;
33 import org.mxchange.jcountry.model.data.CountryData;
34 import org.mxchange.jcountry.model.data.CountrySingletonBeanRemote;
35 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
38 * An administrative country bean
40 * @author Roland Häder<roland@mxchange.org>
42 @Named ("adminCountryController")
44 public class PizzaAdminCountryWebRequestBean extends BasePizzaBean implements PizzaAdminCountryWebRequestController {
49 private static final long serialVersionUID = 18_598_175_719_603L;
52 * An event triggered when the administrator has added a country
56 private Event<ObservableAdminAddedCountryEvent> addedCountryEvent;
61 private String countryAbroadDialPrefix;
66 @EJB (lookup = "java:global/pizzaservice-ejb/country!org.mxchange.jcountry.model.data.CountrySingletonBeanRemote")
67 private CountrySingletonBeanRemote countryBean;
70 * 2-letter country code
72 private String countryCode;
75 * Regular country controller
78 private PizzaCountryWebRequestController 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 PizzaAdminCountryWebRequestBean () {
104 // Call super constructor
109 * Adds country to all relevant beans and sends it to the EJB. A redirect
110 * should happen after successful creation.
112 * @return Redirect outcome
114 * @todo Add field validation
116 public String addCountry () {
117 // Create new country object
118 final Country country = new CountryData(
119 this.getCountryAbroadDialPrefix(),
120 this.getCountryCode(),
121 this.getCountryExternalDialPrefix(),
122 this.getCountryI18nKey(),
123 this.getCountryIsLocalPrefixRequired(),
124 this.getCountryPhoneCode()
127 // Does it already exist?
128 if (this.isCountryAdded(country)) {
129 // Yes, then abort here
130 throw new FaceletException(new CountryAlreadyAddedException(country));
134 final Country updatedCountry;
137 // Send country to bean
138 updatedCountry = this.countryBean.addCountry(country);
139 } catch (final CountryAlreadyAddedException ex) {
141 throw new FaceletException(ex);
145 this.addedCountryEvent.fire(new AdminAddedCountryEvent(updatedCountry));
151 return "admin_list_country"; //NOI18N
155 * Getter for abroad dial prefix
157 * @return Abroad dial prefix
159 public String getCountryAbroadDialPrefix () {
160 return this.countryAbroadDialPrefix;
164 * Setter for abroad dial prefix
166 * @param countryAbroadDialPrefix Abroad dial prefix
168 public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
169 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
173 * Getter for 2-characters country code
175 * @return Country code
177 public String getCountryCode () {
178 return this.countryCode;
182 * Setter for 2-characters country code
184 * @param countryCode Country code
186 public void setCountryCode (final String countryCode) {
187 this.countryCode = countryCode;
191 * Getter for external dial prefix
193 * @return External dial prefix
195 public String getCountryExternalDialPrefix () {
196 return this.countryExternalDialPrefix;
200 * Setter for external dial prefix
202 * @param countryExternalDialPrefix External dial prefix
204 public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
205 this.countryExternalDialPrefix = countryExternalDialPrefix;
209 * Getter for i18n key for country name
211 * @return i18n key for country name
213 public String getCountryI18nKey () {
214 return this.countryI18nKey;
218 * Setter for i18n key for country name
220 * @param countryI18nKey i18n key for country name
222 public void setCountryI18nKey (final String countryI18nKey) {
223 this.countryI18nKey = countryI18nKey;
227 * Getter for whether the local dial prefix is required for local calls
229 * @return Whether the local dial prefix is required
231 public Boolean getCountryIsLocalPrefixRequired () {
232 return this.countryIsLocalPrefixRequired;
236 * Setter for whether the local dial prefix is required for local calls
238 * @param countryIsLocalPrefixRequired Whether the local dial prefix is
241 public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
242 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
246 * Getter for country code (example: 49 for Germany, 63 for Philippines)
248 * @return Dial number without prefix
250 public Short getCountryPhoneCode () {
251 return this.countryPhoneCode;
255 * Setter for country code (example: 49 for Germany, 63 for Philippines)
257 * @param countryPhoneCode Country code
259 public void setCountryPhoneCode (final Short countryPhoneCode) {
260 this.countryPhoneCode = countryPhoneCode;
264 * Clears this bean's data. This should be called after a form has been
265 * submitted and the processing of the form was successful.
267 private void clear () {
269 this.setCountryAbroadDialPrefix(null);
270 this.setCountryCode(null);
271 this.setCountryExternalDialPrefix(null);
272 this.setCountryI18nKey(null);
273 this.setCountryIsLocalPrefixRequired(null);
274 this.setCountryPhoneCode(null);
278 * Checks if given country is already added by iterating over the whole list
279 * and try to find it.
281 * @param country Country instance to look for
283 * @return Whether the country was already found
285 private boolean isCountryAdded (final Country country) {
286 // Default is not found
287 boolean isAdded = false;
290 final List<Country> countries = this.countryController.allCountries();
292 // Get iterator from it
293 final Iterator<Country> iterator = countries.iterator();
296 while (iterator.hasNext()) {
298 final Country next = iterator.next();
300 // Is country code or i18n the same?
301 if ((Objects.equals(country.getCountryCode(), next.getCountryCode())) || (Objects.equals(country.getCountryI18nKey(), next.getCountryI18nKey()))) {
302 // Yes, then abort search