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