]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/country/PizzaAdminCountryWebRequestBean.java
Rewrites:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / country / PizzaAdminCountryWebRequestBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.enterprise.context.RequestScoped;
23 import javax.enterprise.event.Event;
24 import javax.enterprise.inject.Any;
25 import javax.faces.view.facelets.FaceletException;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import javax.naming.Context;
29 import javax.naming.InitialContext;
30 import javax.naming.NamingException;
31 import org.mxchange.jcountry.data.Country;
32 import org.mxchange.jcountry.data.CountryData;
33 import org.mxchange.jcountry.data.CountrySingletonBeanRemote;
34 import org.mxchange.jcountry.events.AdminAddedCountryEvent;
35 import org.mxchange.jcountry.events.AdminEventCountryAdded;
36 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
37
38 /**
39  * Administrative country bean
40  * <p>
41  * @author Roland Haeder<roland@mxchange.org>
42  */
43 @Named ("adminCountryController")
44 @RequestScoped
45 public class PizzaAdminCountryWebRequestBean implements PizzaAdminCountryWebRequestController {
46
47         /**
48          * Serial number
49          */
50         private static final long serialVersionUID = 18_598_175_719_603L;
51
52         /**
53          * An event triggered when the administrator has added a country
54          */
55         @Inject
56         @Any
57         private Event<AdminAddedCountryEvent> addedCountryEvent;
58
59         /**
60          * Abroad dial prefix
61          */
62         private String countryAbroadDialPrefix;
63
64         /**
65          * Remote country EJB
66          */
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 PizzaCountryWebApplicationController 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                 // Try this
105                 try {
106                         // Get initial context
107                         Context context = new InitialContext();
108
109                         // Try to lookup the bean
110                         this.countryBean = (CountrySingletonBeanRemote) context.lookup("java:global/PizzaService-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote"); //NOI18N
111                 } catch (final NamingException ex) {
112                         // Continue to throw
113                         throw new FaceletException(ex);
114                 }
115         }
116
117         @Override
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 AdminEventCountryAdded(updatedCountry));
149
150                 // Clear this bean
151                 this.clear();
152
153                 // Redirect to list
154                 return "admin_list_country"; //NOI18N
155         }
156
157         @Override
158         public List<Country> allCountries () {
159                 // Return "cached" version
160                 return this.countryController.allCountries();
161         }
162
163         @Override
164         public String getCountryAbroadDialPrefix () {
165                 return this.countryAbroadDialPrefix;
166         }
167
168         @Override
169         public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
170                 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
171         }
172
173         @Override
174         public String getCountryCode () {
175                 return this.countryCode;
176         }
177
178         @Override
179         public void setCountryCode (final String countryCode) {
180                 this.countryCode = countryCode;
181         }
182
183         @Override
184         public String getCountryExternalDialPrefix () {
185                 return this.countryExternalDialPrefix;
186         }
187
188         @Override
189         public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
190                 this.countryExternalDialPrefix = countryExternalDialPrefix;
191         }
192
193         @Override
194         public String getCountryI18nKey () {
195                 return this.countryI18nKey;
196         }
197
198         @Override
199         public void setCountryI18nKey (final String countryI18nKey) {
200                 this.countryI18nKey = countryI18nKey;
201         }
202
203         @Override
204         public Boolean getCountryIsLocalPrefixRequired () {
205                 return this.countryIsLocalPrefixRequired;
206         }
207
208         @Override
209         public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
210                 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
211         }
212
213         @Override
214         public Short getCountryPhoneCode () {
215                 return this.countryPhoneCode;
216         }
217
218         @Override
219         public void setCountryPhoneCode (final Short countryPhoneCode) {
220                 this.countryPhoneCode = countryPhoneCode;
221         }
222
223         @Override
224         public boolean hasCountries () {
225                 return (!this.allCountries().isEmpty());
226         }
227
228         /**
229          * Clears this bean
230          */
231         private void clear () {
232                 // Clear all fields
233                 this.setCountryAbroadDialPrefix(null);
234                 this.setCountryCode(null);
235                 this.setCountryExternalDialPrefix(null);
236                 this.setCountryI18nKey(null);
237                 this.setCountryIsLocalPrefixRequired(Boolean.FALSE);
238                 this.setCountryPhoneCode(null);
239         }
240
241         /**
242          * Checks if given country is already added by iterating over the whole list
243          * and try to find it.
244          * <p>
245          * @param country Country instance to look for
246          * <p>
247          * @return Whether the country was already found
248          */
249         private boolean isCountryAdded (final Country country) {
250                 // Default is not found
251                 boolean isAdded = false;
252
253                 // Now get whole ist
254                 List<Country> countries = this.countryController.allCountries();
255
256                 // Get iterator from it
257                 Iterator<Country> iterator = countries.iterator();
258
259                 // Check whole list
260                 while (iterator.hasNext()) {
261                         // Get next country
262                         Country next = iterator.next();
263
264                         // Is country code or i18n the same?
265                         if ((Objects.equals(country.getCountryCode(), next.getCountryCode())) || (Objects.equals(country.getCountryI18nkey(), next.getCountryI18nkey()))) {
266                                 // Yes, then abort search
267                                 isAdded = true;
268                                 break;
269                         }
270                 }
271
272                 // Return result
273                 return isAdded;
274         }
275
276 }