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