]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/country/JobsAdminCountryWebRequestBean.java
Please cherry-pick:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / country / JobsAdminCountryWebRequestBean.java
1 /*
2  * Copyright (C) 2016 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.jjobs.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.jjobs.beans.BaseJobsController;
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 JobsAdminCountryWebRequestBean extends BaseJobsController implements JobsAdminCountryWebRequestController {
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 JobsCountryWebApplicationController 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 JobsAdminCountryWebRequestBean () {
106                 // Call super constructor
107                 super();
108         }
109
110         @Override
111         public String addCountry () {
112                 // Create new country object
113                 Country country = new CountryData();
114
115                 // Add all data
116                 country.setCountryAbroadDialPrefix(this.getCountryAbroadDialPrefix());
117                 country.setCountryCode(this.getCountryCode());
118                 country.setCountryExternalDialPrefix(this.getCountryExternalDialPrefix());
119                 country.setCountryI18nKey(this.getCountryI18nKey());
120                 country.setCountryIsLocalPrefixRequired(this.getCountryIsLocalPrefixRequired());
121                 country.setCountryPhoneCode(this.getCountryPhoneCode());
122
123                 // Does it already exist?
124                 if (this.isCountryAdded(country)) {
125                         // Yes, then abort here
126                         throw new FaceletException(new CountryAlreadyAddedException(country));
127                 }
128
129                 // Init variable
130                 Country updatedCountry = null;
131
132                 try {
133                         // Send country to bean
134                         updatedCountry = this.countryBean.addCountry(country);
135                 } catch (final CountryAlreadyAddedException ex) {
136                         // Throw again
137                         throw new FaceletException(ex);
138                 }
139
140                 // Fire event
141                 this.addedCountryEvent.fire(new AdminAddedCountryEvent(updatedCountry));
142
143                 // Clear this bean
144                 this.clear();
145
146                 // Redirect to list
147                 return "admin_list_country"; //NOI18N
148         }
149
150         @Override
151         public List<Country> allCountries () {
152                 // Return "cached" version
153                 return this.countryController.allCountries();
154         }
155
156         @Override
157         public String getCountryAbroadDialPrefix () {
158                 return this.countryAbroadDialPrefix;
159         }
160
161         @Override
162         public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
163                 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
164         }
165
166         @Override
167         public String getCountryCode () {
168                 return this.countryCode;
169         }
170
171         @Override
172         public void setCountryCode (final String countryCode) {
173                 this.countryCode = countryCode;
174         }
175
176         @Override
177         public String getCountryExternalDialPrefix () {
178                 return this.countryExternalDialPrefix;
179         }
180
181         @Override
182         public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
183                 this.countryExternalDialPrefix = countryExternalDialPrefix;
184         }
185
186         @Override
187         public String getCountryI18nKey () {
188                 return this.countryI18nKey;
189         }
190
191         @Override
192         public void setCountryI18nKey (final String countryI18nKey) {
193                 this.countryI18nKey = countryI18nKey;
194         }
195
196         @Override
197         public Boolean getCountryIsLocalPrefixRequired () {
198                 return this.countryIsLocalPrefixRequired;
199         }
200
201         @Override
202         public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
203                 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
204         }
205
206         @Override
207         public Short getCountryPhoneCode () {
208                 return this.countryPhoneCode;
209         }
210
211         @Override
212         public void setCountryPhoneCode (final Short countryPhoneCode) {
213                 this.countryPhoneCode = countryPhoneCode;
214         }
215
216         @Override
217         public boolean hasCountries () {
218                 return (!this.allCountries().isEmpty());
219         }
220
221         /**
222          * Post-construction method
223          */
224         @PostConstruct
225         public void init () {
226                 // Try this
227                 try {
228                         // Get initial context
229                         Context context = new InitialContext();
230
231                         // Try to lookup the bean
232                         this.countryBean = (CountrySingletonBeanRemote) context.lookup("java:global/jjobs-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote"); //NOI18N
233                 } catch (final NamingException ex) {
234                         // Continue to throw
235                         throw new FaceletException(ex);
236                 }
237         }
238
239         /**
240          * Clears this bean's data. This should be called after a form has been
241          * submitted and the processing of the form was successful.
242          */
243         private void clear () {
244                 // Clear fields
245                 this.setCountryAbroadDialPrefix(null);
246                 this.setCountryCode(null);
247                 this.setCountryExternalDialPrefix(null);
248                 this.setCountryI18nKey(null);
249                 this.setCountryIsLocalPrefixRequired(null);
250                 this.setCountryPhoneCode(null);
251         }
252
253         /**
254          * Checks if given country is already added by iterating over the whole list
255          * and try to find it.
256          * <p>
257          * @param country Country instance to look for
258          * <p>
259          * @return Whether the country was already found
260          */
261         private boolean isCountryAdded (final Country country) {
262                 // Default is not found
263                 boolean isAdded = false;
264
265                 // Now get whole ist
266                 List<Country> countries = this.countryController.allCountries();
267
268                 // Get iterator from it
269                 Iterator<Country> iterator = countries.iterator();
270
271                 // Check whole list
272                 while (iterator.hasNext()) {
273                         // Get next country
274                         Country next = iterator.next();
275
276                         // Is country code or i18n the same?
277                         if ((Objects.equals(country.getCountryCode(), next.getCountryCode())) || (Objects.equals(country.getCountryI18nKey(), next.getCountryI18nKey()))) {
278                                 // Yes, then abort search
279                                 isAdded = true;
280                                 break;
281                         }
282                 }
283
284                 // Return result
285                 return isAdded;
286         }
287
288 }