]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/country/JobsAdminCountryWebRequestBean.java
Refactured a lot:
[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
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          * Checks if given country is already added by iterating over the whole list
224          * and try to find it.
225          * <p>
226          * @param country Country instance to look for
227          * <p>
228          * @return Whether the country was already found
229          */
230         private boolean isCountryAdded (final Country country) {
231                 // Default is not found
232                 boolean isAdded = false;
233
234                 // Now get whole ist
235                 List<Country> countries = this.countryController.allCountries();
236
237                 // Get iterator from it
238                 Iterator<Country> iterator = countries.iterator();
239
240                 // Check whole list
241                 while (iterator.hasNext()) {
242                         // Get next country
243                         Country next = iterator.next();
244
245                         // Is country code or i18n the same?
246                         if ((Objects.equals(country.getCountryCode(), next.getCountryCode())) || (Objects.equals(country.getCountryI18nkey(), next.getCountryI18nkey()))) {
247                                 // Yes, then abort search
248                                 isAdded = true;
249                                 break;
250                         }
251                 }
252
253                 // Return result
254                 return isAdded;
255         }
256
257 }