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