]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/country/PizzaAdminCountryWebApplicationBean.java
Handle over the updated country instance to avoid NPE (countryId must be set)
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / country / PizzaAdminCountryWebApplicationBean.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.ApplicationScoped;
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  * An administrative country bean
40  * <p>
41  * @author Roland Haeder<roland@mxchange.org>
42  */
43 @Named ("adminCountryController")
44 @ApplicationScoped
45 public class PizzaAdminCountryWebApplicationBean implements PizzaAdminCountryWebApplicationController {
46
47         /**
48          * Serial number
49          */
50         private static final long serialVersionUID = 18_598_175_719_603L;
51
52         /**
53          * Abroad dial prefix
54          */
55         private String countryAbroadDialPrefix;
56
57         /**
58          * Remote country EJB
59          */
60         private CountrySingletonBeanRemote countryBean;
61
62         /**
63          * 2-letter country code
64          */
65         private String countryCode;
66
67         /**
68          * Regular country controller
69          */
70         @Inject
71         private PizzaCountryWebApplicationController countryController;
72
73         /**
74          * An event triggered when the administrator has added a country
75          */
76         @Inject
77         @Any
78         private Event<AdminAddedCountryEvent> addedCountryEvent;
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 PizzaAdminCountryWebApplicationBean () {
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 void 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                 try {
137                         // Send country to bean
138                         Country updatedCountry = this.countryBean.addCountry(country);
139
140                         // Fire event
141                         this.addedCountryEvent.fire(new AdminEventCountryAdded(updatedCountry));
142
143                         // Clear bean
144                         this.clear();
145                 } catch (final CountryAlreadyAddedException ex) {
146                         // Throw again
147                         throw new FaceletException(ex);
148                 }
149         }
150
151         @Override
152         public List<Country> allCountries () {
153                 // Return "cached" version
154                 return this.countryController.allCountries();
155         }
156
157         @Override
158         public String getCountryAbroadDialPrefix () {
159                 return this.countryAbroadDialPrefix;
160         }
161
162         @Override
163         public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
164                 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
165         }
166
167         @Override
168         public String getCountryCode () {
169                 return this.countryCode;
170         }
171
172         @Override
173         public void setCountryCode (final String countryCode) {
174                 this.countryCode = countryCode;
175         }
176
177         @Override
178         public String getCountryExternalDialPrefix () {
179                 return this.countryExternalDialPrefix;
180         }
181
182         @Override
183         public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
184                 this.countryExternalDialPrefix = countryExternalDialPrefix;
185         }
186
187         @Override
188         public String getCountryI18nKey () {
189                 return this.countryI18nKey;
190         }
191
192         @Override
193         public void setCountryI18nKey (final String countryI18nKey) {
194                 this.countryI18nKey = countryI18nKey;
195         }
196
197         @Override
198         public Boolean getCountryIsLocalPrefixRequired () {
199                 return this.countryIsLocalPrefixRequired;
200         }
201
202         @Override
203         public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
204                 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
205         }
206
207         @Override
208         public Short getCountryPhoneCode () {
209                 return this.countryPhoneCode;
210         }
211
212         @Override
213         public void setCountryPhoneCode (final Short countryPhoneCode) {
214                 this.countryPhoneCode = countryPhoneCode;
215         }
216
217         @Override
218         public boolean hasCountries () {
219                 return (!this.allCountries().isEmpty());
220         }
221
222         /**
223          * Clears this bean
224          */
225         private void clear () {
226                 // Clear all
227                 this.setCountryAbroadDialPrefix(null);
228                 this.setCountryCode(null);
229                 this.setCountryExternalDialPrefix(null);
230                 this.setCountryI18nKey(null);
231                 this.setCountryIsLocalPrefixRequired(null);
232                 this.setCountryPhoneCode(null);
233         }
234
235         /**
236          * Checks if given country is already added by iterating over the whole list
237          * and try to find it.
238          * <p>
239          * @param country Country instance to look for
240          * <p>
241          * @return Whether the country was already found
242          */
243         private boolean isCountryAdded (final Country country) {
244                 // Default is not found
245                 boolean isAdded = false;
246
247                 // Now get whole ist
248                 List<Country> countries = this.countryController.allCountries();
249
250                 // Get iterator from it
251                 Iterator<Country> iterator = countries.iterator();
252
253                 // Check whole list
254                 while (iterator.hasNext()) {
255                         // Get next country
256                         Country next = iterator.next();
257
258                         // Is country code or i18n the same?
259                         if ((Objects.equals(country.getCountryCode(), next.getCountryCode())) || (Objects.equals(country.getCountryI18nkey(), next.getCountryI18nkey()))) {
260                                 // Yes, then abort search
261                                 isAdded = true;
262                                 break;
263                         }
264                 }
265
266                 // Return result
267                 return isAdded;
268         }
269
270 }