]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/country/JobsAdminCountryWebRequestBean.java
eed8f3b2173366a568a3b0d432c475264f127b2d
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / country / JobsAdminCountryWebRequestBean.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.jjobs.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  * An administrative country bean
40  * <p>
41  * @author Roland Haeder<roland@mxchange.org>
42  */
43 @Named ("adminCountryController")
44 @RequestScoped
45 public class JobsAdminCountryWebRequestBean implements JobsAdminCountryWebRequestController {
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 JobsCountryWebApplicationController 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 JobsAdminCountryWebRequestBean () {
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/jjobs-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                 // 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 bean
151                 this.clear();
152         }
153
154         @Override
155         public List<Country> allCountries () {
156                 // Return "cached" version
157                 return this.countryController.allCountries();
158         }
159
160         @Override
161         public String getCountryAbroadDialPrefix () {
162                 return this.countryAbroadDialPrefix;
163         }
164
165         @Override
166         public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
167                 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
168         }
169
170         @Override
171         public String getCountryCode () {
172                 return this.countryCode;
173         }
174
175         @Override
176         public void setCountryCode (final String countryCode) {
177                 this.countryCode = countryCode;
178         }
179
180         @Override
181         public String getCountryExternalDialPrefix () {
182                 return this.countryExternalDialPrefix;
183         }
184
185         @Override
186         public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
187                 this.countryExternalDialPrefix = countryExternalDialPrefix;
188         }
189
190         @Override
191         public String getCountryI18nKey () {
192                 return this.countryI18nKey;
193         }
194
195         @Override
196         public void setCountryI18nKey (final String countryI18nKey) {
197                 this.countryI18nKey = countryI18nKey;
198         }
199
200         @Override
201         public Boolean getCountryIsLocalPrefixRequired () {
202                 return this.countryIsLocalPrefixRequired;
203         }
204
205         @Override
206         public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
207                 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
208         }
209
210         @Override
211         public Short getCountryPhoneCode () {
212                 return this.countryPhoneCode;
213         }
214
215         @Override
216         public void setCountryPhoneCode (final Short countryPhoneCode) {
217                 this.countryPhoneCode = countryPhoneCode;
218         }
219
220         @Override
221         public boolean hasCountries () {
222                 return (!this.allCountries().isEmpty());
223         }
224
225         /**
226          * Clears this bean
227          */
228         private void clear () {
229                 // Clear all
230                 this.setCountryAbroadDialPrefix(null);
231                 this.setCountryCode(null);
232                 this.setCountryExternalDialPrefix(null);
233                 this.setCountryI18nKey(null);
234                 this.setCountryIsLocalPrefixRequired(null);
235                 this.setCountryPhoneCode(null);
236         }
237
238         /**
239          * Checks if given country is already added by iterating over the whole list
240          * and try to find it.
241          * <p>
242          * @param country Country instance to look for
243          * <p>
244          * @return Whether the country was already found
245          */
246         private boolean isCountryAdded (final Country country) {
247                 // Default is not found
248                 boolean isAdded = false;
249
250                 // Now get whole ist
251                 List<Country> countries = this.countryController.allCountries();
252
253                 // Get iterator from it
254                 Iterator<Country> iterator = countries.iterator();
255
256                 // Check whole list
257                 while (iterator.hasNext()) {
258                         // Get next country
259                         Country next = iterator.next();
260
261                         // Is country code or i18n the same?
262                         if ((Objects.equals(country.getCountryCode(), next.getCountryCode())) || (Objects.equals(country.getCountryI18nkey(), next.getCountryI18nkey()))) {
263                                 // Yes, then abort search
264                                 isAdded = true;
265                                 break;
266                         }
267                 }
268
269                 // Return result
270                 return isAdded;
271         }
272
273 }