2 * Copyright (C) 2016, 2017 Roland Häder
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.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.jjobs.beans.BaseJobsBean;
38 * An administrative country bean
40 * @author Roland Häder<roland@mxchange.org>
42 @Named ("adminCountryController")
44 public class JobsAdminCountryWebRequestBean extends BaseJobsBean implements JobsAdminCountryWebRequestController {
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/jjobs-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 JobsCountryWebRequestController 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 JobsAdminCountryWebRequestBean () {
104 // Call super constructor
109 * Adds country to all relevant beans and sends it to the EJB. A redirect
110 * should happen after successfull 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();
121 country.setCountryAbroadDialPrefix(this.getCountryAbroadDialPrefix());
122 country.setCountryCode(this.getCountryCode());
123 country.setCountryExternalDialPrefix(this.getCountryExternalDialPrefix());
124 country.setCountryI18nKey(this.getCountryI18nKey());
125 country.setCountryIsLocalPrefixRequired(this.getCountryIsLocalPrefixRequired());
126 country.setCountryPhoneCode(this.getCountryPhoneCode());
128 // Does it already exist?
129 if (this.isCountryAdded(country)) {
130 // Yes, then abort here
131 throw new FaceletException(new CountryAlreadyAddedException(country));
135 final Country updatedCountry;
138 // Send country to bean
139 updatedCountry = this.countryBean.addCountry(country);
140 } catch (final CountryAlreadyAddedException ex) {
142 throw new FaceletException(ex);
146 this.addedCountryEvent.fire(new AdminAddedCountryEvent(updatedCountry));
152 return "admin_list_country"; //NOI18N
156 * Getter for abroad dial prefix
158 * @return Abroad dial prefix
160 public String getCountryAbroadDialPrefix () {
161 return this.countryAbroadDialPrefix;
165 * Setter for abroad dial prefix
167 * @param countryAbroadDialPrefix Abroad dial prefix
169 public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
170 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
174 * Getter for 2-characters country code
176 * @return Country code
178 public String getCountryCode () {
179 return this.countryCode;
183 * Setter for 2-characters country code
185 * @param countryCode Country code
187 public void setCountryCode (final String countryCode) {
188 this.countryCode = countryCode;
192 * Getter for external dial prefix
194 * @return External dial prefix
196 public String getCountryExternalDialPrefix () {
197 return this.countryExternalDialPrefix;
201 * Setter for external dial prefix
203 * @param countryExternalDialPrefix External dial prefix
205 public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
206 this.countryExternalDialPrefix = countryExternalDialPrefix;
210 * Getter for i18n key for country name
212 * @return i18n key for country name
214 public String getCountryI18nKey () {
215 return this.countryI18nKey;
219 * Setter for i18n key for country name
221 * @param countryI18nKey i18n key for country name
223 public void setCountryI18nKey (final String countryI18nKey) {
224 this.countryI18nKey = countryI18nKey;
228 * Getter for whether the local dial prefix is required for local calls
230 * @return Whether the local dial prefix is required
232 public Boolean getCountryIsLocalPrefixRequired () {
233 return this.countryIsLocalPrefixRequired;
237 * Setter for whether the local dial prefix is required for local calls
239 * @param countryIsLocalPrefixRequired Whether the local dial prefix is
242 public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
243 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
247 * Getter for country code (example: 49 for Germany, 63 for Philippines)
249 * @return Dial number without prefix
251 public Short getCountryPhoneCode () {
252 return this.countryPhoneCode;
256 * Setter for country code (example: 49 for Germany, 63 for Philippines)
258 * @param countryPhoneCode Country code
260 public void setCountryPhoneCode (final Short countryPhoneCode) {
261 this.countryPhoneCode = countryPhoneCode;
265 * Clears this bean's data. This should be called after a form has been
266 * submitted and the processing of the form was successful.
268 private void clear () {
270 this.setCountryAbroadDialPrefix(null);
271 this.setCountryCode(null);
272 this.setCountryExternalDialPrefix(null);
273 this.setCountryI18nKey(null);
274 this.setCountryIsLocalPrefixRequired(null);
275 this.setCountryPhoneCode(null);
279 * Checks if given country is already added by iterating over the whole list
280 * and try to find it.
282 * @param country Country instance to look for
284 * @return Whether the country was already found
286 private boolean isCountryAdded (final Country country) {
287 // Default is not found
288 boolean isAdded = false;
291 final List<Country> countries = this.countryController.allCountries();
293 // Get iterator from it
294 final Iterator<Country> iterator = countries.iterator();
297 while (iterator.hasNext()) {
299 final Country next = iterator.next();
301 // Is country code or i18n the same?
302 if ((Objects.equals(country.getCountryCode(), next.getCountryCode())) || (Objects.equals(country.getCountryI18nKey(), next.getCountryI18nKey()))) {
303 // Yes, then abort search