]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/country/JobsAdminCountryWebRequestBean.java
9dcd1fed493185dc75fc6292a2ec68f79e399680
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / country / JobsAdminCountryWebRequestBean.java
1 /*
2  * Copyright (C) 2016, 2017 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.ejb.EJB;
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 org.mxchange.jcountry.model.data.Country;
30 import org.mxchange.jcountry.model.data.CountryData;
31 import org.mxchange.jcountry.model.data.CountrySingletonBeanRemote;
32 import org.mxchange.jcountry.events.AdminAddedCountryEvent;
33 import org.mxchange.jcountry.events.ObservableAdminAddedCountryEvent;
34 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
35 import org.mxchange.jjobs.beans.BaseJobsController;
36
37 /**
38  * An administrative country bean
39  * <p>
40  * @author Roland Häder<roland@mxchange.org>
41  */
42 @Named ("adminCountryController")
43 @RequestScoped
44 public class JobsAdminCountryWebRequestBean extends BaseJobsController implements JobsAdminCountryWebRequestController {
45
46         /**
47          * Serial number
48          */
49         private static final long serialVersionUID = 18_598_175_719_603L;
50
51         /**
52          * An event triggered when the administrator has added a country
53          */
54         @Inject
55         @Any
56         private Event<ObservableAdminAddedCountryEvent> addedCountryEvent;
57
58         /**
59          * Abroad dial prefix
60          */
61         private String countryAbroadDialPrefix;
62
63         /**
64          * Remote country EJB
65          */
66         @EJB (lookup = "java:global/jjobs-ejb/country!org.mxchange.jcountry.model.data.CountrySingletonBeanRemote")
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 JobsCountryWebRequestController 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                 // Call super constructor
105                 super();
106         }
107
108         /**
109          * Adds country to all relevant beans and sends it to the EJB. A redirect
110          * should happen after successfull creation.
111          * <p>
112          * @return Redirect outcome
113          * <p>
114          * @todo Add field validation
115          */
116         public String addCountry () {
117                 // Create new country object
118                 final Country country = new CountryData();
119
120                 // Add all data
121                 country.setCountryAbroadDialPrefix(this.getCountryAbroadDialPrefix());
122                 country.setCountryCode(this.getCountryCode());
123                 country.setCountryExternalDialPrefix(this.getCountryExternalDialPrefix());
124                 country.setCountryI18nKey(this.getCountryI18nKey());
125                 country.setCountryIsLocalPrefixRequired(this.getCountryIsLocalPrefixRequired());
126                 country.setCountryPhoneCode(this.getCountryPhoneCode());
127
128                 // Does it already exist?
129                 if (this.isCountryAdded(country)) {
130                         // Yes, then abort here
131                         throw new FaceletException(new CountryAlreadyAddedException(country));
132                 }
133
134                 // Init variable
135                 final Country updatedCountry;
136
137                 try {
138                         // Send country to bean
139                         updatedCountry = this.countryBean.addCountry(country);
140                 } catch (final CountryAlreadyAddedException ex) {
141                         // Throw again
142                         throw new FaceletException(ex);
143                 }
144
145                 // Fire event
146                 this.addedCountryEvent.fire(new AdminAddedCountryEvent(updatedCountry));
147
148                 // Clear this bean
149                 this.clear();
150
151                 // Redirect to list
152                 return "admin_list_country"; //NOI18N
153         }
154
155         /**
156          * Getter for abroad dial prefix
157          * <p>
158          * @return Abroad dial prefix
159          */
160         public String getCountryAbroadDialPrefix () {
161                 return this.countryAbroadDialPrefix;
162         }
163
164         /**
165          * Setter for abroad dial prefix
166          * <p>
167          * @param countryAbroadDialPrefix Abroad dial prefix
168          */
169         public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
170                 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
171         }
172
173         /**
174          * Getter for 2-characters country code
175          * <p>
176          * @return Country code
177          */
178         public String getCountryCode () {
179                 return this.countryCode;
180         }
181
182         /**
183          * Setter for 2-characters country code
184          * <p>
185          * @param countryCode Country code
186          */
187         public void setCountryCode (final String countryCode) {
188                 this.countryCode = countryCode;
189         }
190
191         /**
192          * Getter for external dial prefix
193          * <p>
194          * @return External dial prefix
195          */
196         public String getCountryExternalDialPrefix () {
197                 return this.countryExternalDialPrefix;
198         }
199
200         /**
201          * Setter for external dial prefix
202          * <p>
203          * @param countryExternalDialPrefix External dial prefix
204          */
205         public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
206                 this.countryExternalDialPrefix = countryExternalDialPrefix;
207         }
208
209         /**
210          * Getter for i18n key for country name
211          * <p>
212          * @return i18n key for country name
213          */
214         public String getCountryI18nKey () {
215                 return this.countryI18nKey;
216         }
217
218         /**
219          * Setter for i18n key for country name
220          * <p>
221          * @param countryI18nKey i18n key for country name
222          */
223         public void setCountryI18nKey (final String countryI18nKey) {
224                 this.countryI18nKey = countryI18nKey;
225         }
226
227         /**
228          * Getter for whether the local dial prefix is required for local calls
229          * <p>
230          * @return Whether the local dial prefix is required
231          */
232         public Boolean getCountryIsLocalPrefixRequired () {
233                 return this.countryIsLocalPrefixRequired;
234         }
235
236         /**
237          * Setter for whether the local dial prefix is required for local calls
238          * <p>
239          * @param countryIsLocalPrefixRequired Whether the local dial prefix is
240          *                                     required
241          */
242         public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
243                 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
244         }
245
246         /**
247          * Getter for country code (example: 49 for Germany, 63 for Philippines)
248          * <p>
249          * @return Dial number without prefix
250          */
251         public Short getCountryPhoneCode () {
252                 return this.countryPhoneCode;
253         }
254
255         /**
256          * Setter for country code (example: 49 for Germany, 63 for Philippines)
257          * <p>
258          * @param countryPhoneCode Country code
259          */
260         public void setCountryPhoneCode (final Short countryPhoneCode) {
261                 this.countryPhoneCode = countryPhoneCode;
262         }
263
264         /**
265          * Clears this bean's data. This should be called after a form has been
266          * submitted and the processing of the form was successful.
267          */
268         private void clear () {
269                 // Clear fields
270                 this.setCountryAbroadDialPrefix(null);
271                 this.setCountryCode(null);
272                 this.setCountryExternalDialPrefix(null);
273                 this.setCountryI18nKey(null);
274                 this.setCountryIsLocalPrefixRequired(null);
275                 this.setCountryPhoneCode(null);
276         }
277
278         /**
279          * Checks if given country is already added by iterating over the whole list
280          * and try to find it.
281          * <p>
282          * @param country Country instance to look for
283          * <p>
284          * @return Whether the country was already found
285          */
286         private boolean isCountryAdded (final Country country) {
287                 // Default is not found
288                 boolean isAdded = false;
289
290                 // Now get whole ist
291                 final List<Country> countries = this.countryController.allCountries();
292
293                 // Get iterator from it
294                 final Iterator<Country> iterator = countries.iterator();
295
296                 // Check whole list
297                 while (iterator.hasNext()) {
298                         // Get next country
299                         final Country next = iterator.next();
300
301                         // Is country code or i18n the same?
302                         if ((Objects.equals(country.getCountryCode(), next.getCountryCode())) || (Objects.equals(country.getCountryI18nKey(), next.getCountryI18nKey()))) {
303                                 // Yes, then abort search
304                                 isAdded = true;
305                                 break;
306                         }
307                 }
308
309                 // Return result
310                 return isAdded;
311         }
312
313 }