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;
22 import javax.annotation.PostConstruct;
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 javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32 import org.mxchange.jcountry.data.Country;
33 import org.mxchange.jcountry.data.CountryData;
34 import org.mxchange.jcountry.data.CountrySingletonBeanRemote;
35 import org.mxchange.jcountry.events.AdminAddedCountryEvent;
36 import org.mxchange.jcountry.events.ObservableAdminAddedCountryEvent;
37 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
38 import org.mxchange.jjobs.beans.BaseJobsController;
41 * An administrative country bean
43 * @author Roland Häder<roland@mxchange.org>
45 @Named ("adminCountryController")
47 public class JobsAdminCountryWebRequestBean extends BaseJobsController implements JobsAdminCountryWebRequestController {
52 private static final long serialVersionUID = 18_598_175_719_603L;
55 * An event triggered when the administrator has added a country
59 private Event<ObservableAdminAddedCountryEvent> addedCountryEvent;
64 private String countryAbroadDialPrefix;
69 private CountrySingletonBeanRemote countryBean;
72 * 2-letter country code
74 private String countryCode;
77 * Regular country controller
80 private JobsCountryWebApplicationController countryController;
85 private String countryExternalDialPrefix;
90 private String countryI18nKey;
93 * Whether the local dial prefix is required to use
95 private Boolean countryIsLocalPrefixRequired;
100 private Short countryPhoneCode;
103 * Default constructor
105 public JobsAdminCountryWebRequestBean () {
106 // Call super constructor
111 * Adds country to all relevant beans and sends it to the EJB. A redirect
112 * should happen after successfull creation.
114 * @return Redirect outcome
116 * @todo Add field validation
118 public String 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 AdminAddedCountryEvent(updatedCountry));
154 return "admin_list_country"; //NOI18N
158 * Getter for abroad dial prefix
160 * @return Abroad dial prefix
162 public String getCountryAbroadDialPrefix () {
163 return this.countryAbroadDialPrefix;
167 * Setter for abroad dial prefix
169 * @param countryAbroadDialPrefix Abroad dial prefix
171 public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
172 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
176 * Getter for 2-characters country code
178 * @return Country code
180 public String getCountryCode () {
181 return this.countryCode;
185 * Setter for 2-characters country code
187 * @param countryCode Country code
189 public void setCountryCode (final String countryCode) {
190 this.countryCode = countryCode;
194 * Getter for external dial prefix
196 * @return External dial prefix
198 public String getCountryExternalDialPrefix () {
199 return this.countryExternalDialPrefix;
203 * Setter for external dial prefix
205 * @param countryExternalDialPrefix External dial prefix
207 public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
208 this.countryExternalDialPrefix = countryExternalDialPrefix;
212 * Getter for i18n key for country name
214 * @return i18n key for country name
216 public String getCountryI18nKey () {
217 return this.countryI18nKey;
221 * Setter for i18n key for country name
223 * @param countryI18nKey i18n key for country name
225 public void setCountryI18nKey (final String countryI18nKey) {
226 this.countryI18nKey = countryI18nKey;
230 * Getter for whether the local dial prefix is required for local calls
232 * @return Whether the local dial prefix is required
234 public Boolean getCountryIsLocalPrefixRequired () {
235 return this.countryIsLocalPrefixRequired;
239 * Setter for whether the local dial prefix is required for local calls
241 * @param countryIsLocalPrefixRequired Whether the local dial prefix is
244 public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
245 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
249 * Getter for country code (example: 49 for Germany, 63 for Philippines)
251 * @return Dial number without prefix
253 public Short getCountryPhoneCode () {
254 return this.countryPhoneCode;
258 * Setter for country code (example: 49 for Germany, 63 for Philippines)
260 * @param countryPhoneCode Country code
262 public void setCountryPhoneCode (final Short countryPhoneCode) {
263 this.countryPhoneCode = countryPhoneCode;
267 * Post-construction method
270 public void init () {
273 // Get initial context
274 Context context = new InitialContext();
276 // Try to lookup the bean
277 this.countryBean = (CountrySingletonBeanRemote) context.lookup("java:global/jjobs-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote"); //NOI18N
278 } catch (final NamingException ex) {
280 throw new FaceletException(ex);
285 * Clears this bean's data. This should be called after a form has been
286 * submitted and the processing of the form was successful.
288 private void clear () {
290 this.setCountryAbroadDialPrefix(null);
291 this.setCountryCode(null);
292 this.setCountryExternalDialPrefix(null);
293 this.setCountryI18nKey(null);
294 this.setCountryIsLocalPrefixRequired(null);
295 this.setCountryPhoneCode(null);
299 * Checks if given country is already added by iterating over the whole list
300 * and try to find it.
302 * @param country Country instance to look for
304 * @return Whether the country was already found
306 private boolean isCountryAdded (final Country country) {
307 // Default is not found
308 boolean isAdded = false;
311 List<Country> countries = this.countryController.allCountries();
313 // Get iterator from it
314 Iterator<Country> iterator = countries.iterator();
317 while (iterator.hasNext()) {
319 Country next = iterator.next();
321 // Is country code or i18n the same?
322 if ((Objects.equals(country.getCountryCode(), next.getCountryCode())) || (Objects.equals(country.getCountryI18nKey(), next.getCountryI18nKey()))) {
323 // Yes, then abort search