]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/country/AddressbookAdminCountryWebRequestBean.java
Don't cherry-pick:
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / country / AddressbookAdminCountryWebRequestBean.java
1 /*
2  * Copyright (C) 2016 - 2022 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.addressbook.beans.country;
18
19 import java.util.Objects;
20 import javax.ejb.EJB;
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;
35
36 /**
37  * An administrative country bean
38  * <p>
39  * @author Roland Häder<roland@mxchange.org>
40  */
41 @Named ("adminCountryController")
42 @RequestScoped
43 public class AddressbookAdminCountryWebRequestBean extends BaseAddressbookBean implements AddressbookAdminCountryWebRequestController {
44
45         /**
46          * Serial number
47          */
48         private static final long serialVersionUID = 18_598_175_719_603L;
49
50         /**
51          * An event triggered when the administrator has added a country
52          */
53         @Inject
54         @Any
55         private Event<ObservableAdminAddedCountryEvent> addedCountryEvent;
56
57         /**
58          * Remote country EJB
59          */
60         @EJB (lookup = "java:global/addressbook-ejb/adminCountry!org.mxchange.jcountry.model.data.AdminCountrySessionBeanRemote")
61         private AdminCountrySessionBeanRemote adminCountryBean;
62
63         /**
64          * Abroad dial prefix
65          */
66         private String countryAbroadDialPrefix;
67
68         /**
69          * 2-letter country code
70          */
71         private String countryCode;
72
73         /**
74          * Local dial prefix
75          */
76         private String countryExternalDialPrefix;
77
78         /**
79          * i18n bundle key
80          */
81         private String countryI18nKey;
82
83         /**
84          * Whether the local dial prefix is required to use
85          */
86         private Boolean countryIsLocalPrefixRequired;
87
88         /**
89          * Regular country controller
90          */
91         @Inject
92         private AddressbookCountryListWebViewController countryListController;
93
94         /**
95          * Phone code
96          */
97         private Short countryPhoneCode;
98
99         /**
100          * Default constructor
101          */
102         public AddressbookAdminCountryWebRequestBean () {
103                 // Call super constructor
104                 super();
105         }
106
107         /**
108          * Adds country to all relevant beans and sends it to the EJB. A redirect
109          * should happen after successful creation.
110          * <p>
111          * @todo Add field validation
112          */
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()
122                           );
123
124                 // Does it already exist?
125                 if (this.isCountryAdded(country)) {
126                         // Yes, then abort here
127                         throw new FacesException(new CountryAlreadyAddedException(country));
128                 }
129
130                 // Init variable
131                 final Country updatedCountry;
132
133                 try {
134                         // Send country to bean
135                         updatedCountry = this.adminCountryBean.addCountry(country);
136                 } catch (final CountryAlreadyAddedException ex) {
137                         // Throw again
138                         throw new FacesException(ex);
139                 }
140
141                 // Fire event
142                 this.addedCountryEvent.fire(new AdminAddedCountryEvent(updatedCountry));
143
144                 // Clear this bean
145                 this.clear();
146         }
147
148         /**
149          * Getter for abroad dial prefix
150          * <p>
151          * @return Abroad dial prefix
152          */
153         public String getCountryAbroadDialPrefix () {
154                 return this.countryAbroadDialPrefix;
155         }
156
157         /**
158          * Setter for abroad dial prefix
159          * <p>
160          * @param countryAbroadDialPrefix Abroad dial prefix
161          */
162         public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
163                 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
164         }
165
166         /**
167          * Getter for 2-characters country code
168          * <p>
169          * @return Country code
170          */
171         public String getCountryCode () {
172                 return this.countryCode;
173         }
174
175         /**
176          * Setter for 2-characters country code
177          * <p>
178          * @param countryCode Country code
179          */
180         public void setCountryCode (final String countryCode) {
181                 this.countryCode = countryCode;
182         }
183
184         /**
185          * Getter for external dial prefix
186          * <p>
187          * @return External dial prefix
188          */
189         public String getCountryExternalDialPrefix () {
190                 return this.countryExternalDialPrefix;
191         }
192
193         /**
194          * Setter for external dial prefix
195          * <p>
196          * @param countryExternalDialPrefix External dial prefix
197          */
198         public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
199                 this.countryExternalDialPrefix = countryExternalDialPrefix;
200         }
201
202         /**
203          * Getter for i18n key for country name
204          * <p>
205          * @return i18n key for country name
206          */
207         public String getCountryI18nKey () {
208                 return this.countryI18nKey;
209         }
210
211         /**
212          * Setter for i18n key for country name
213          * <p>
214          * @param countryI18nKey i18n key for country name
215          */
216         public void setCountryI18nKey (final String countryI18nKey) {
217                 this.countryI18nKey = countryI18nKey;
218         }
219
220         /**
221          * Getter for whether the local dial prefix is required for local calls
222          * <p>
223          * @return Whether the local dial prefix is required
224          */
225         public Boolean getCountryIsLocalPrefixRequired () {
226                 return this.countryIsLocalPrefixRequired;
227         }
228
229         /**
230          * Setter for whether the local dial prefix is required for local calls
231          * <p>
232          * @param countryIsLocalPrefixRequired Whether the local dial prefix is
233          *                                     required
234          */
235         public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
236                 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
237         }
238
239         /**
240          * Getter for country code (example: 49 for Germany, 63 for Philippines)
241          * <p>
242          * @return Dial number without prefix
243          */
244         public Short getCountryPhoneCode () {
245                 return this.countryPhoneCode;
246         }
247
248         /**
249          * Setter for country code (example: 49 for Germany, 63 for Philippines)
250          * <p>
251          * @param countryPhoneCode Country code
252          */
253         public void setCountryPhoneCode (final Short countryPhoneCode) {
254                 this.countryPhoneCode = countryPhoneCode;
255         }
256
257         /**
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.
260          */
261         private void clear () {
262                 // Clear fields
263                 this.setCountryAbroadDialPrefix(null);
264                 this.setCountryCode(null);
265                 this.setCountryExternalDialPrefix(null);
266                 this.setCountryI18nKey(null);
267                 this.setCountryIsLocalPrefixRequired(null);
268                 this.setCountryPhoneCode(null);
269         }
270
271         /**
272          * Checks if given country is already added by iterating over the whole list
273          * and try to find it.
274          * <p>
275          * @param country Country instance to look for
276          * <p>
277          * @return Whether the country was already found
278          */
279         private boolean isCountryAdded (final Country country) {
280                 // Default is not found
281                 boolean isAdded = false;
282
283                 // Check whole list
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
288                                 isAdded = true;
289                                 break;
290                         }
291                 }
292
293                 // Return result
294                 return isAdded;
295         }
296
297 }