]> 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 - 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.Objects;
20 import javax.ejb.EJB;
21 import javax.enterprise.context.RequestScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.inject.Any;
24 import javax.faces.FacesException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import org.mxchange.jcountry.events.added.AdminAddedCountryEvent;
28 import org.mxchange.jcountry.events.added.ObservableAdminAddedCountryEvent;
29 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
30 import org.mxchange.jcountry.model.data.AdminCountrySessionBeanRemote;
31 import org.mxchange.jcountry.model.data.Country;
32 import org.mxchange.jcountry.model.data.CountryData;
33 import org.mxchange.jjobs.beans.BaseJobsBean;
34 import org.mxchange.jjobs.beans.country.list.JobsCountryListWebViewController;
35
36 /**
37  * An administrative country bean
38  * <p>
39  * @author Roland Häder<roland@mxchange.org>
40  */
41 @Named ("adminCountryController")
42 @RequestScoped
43 public class JobsAdminCountryWebRequestBean extends BaseJobsBean implements JobsAdminCountryWebRequestController {
44
45         /**
46          * Serial number
47          */
48         private static final long serialVersionUID = 18_598_175_719_603L;
49
50         /**
51          * An event triggered when the administrator has added a country
52          */
53         @Inject
54         @Any
55         private Event<ObservableAdminAddedCountryEvent> addedCountryEvent;
56
57         /**
58          * Remote country EJB
59          */
60         @EJB (lookup = "java:global/jjobs-ejb/adminCountry!org.mxchange.jcountry.model.data.AdminCountrySessionBeanRemote")
61         private AdminCountrySessionBeanRemote adminCountryBean;
62
63         /**
64          * Abroad dial prefix
65          */
66         private String countryAbroadDialPrefix;
67
68         /**
69          * 2-letter country code
70          */
71         private String countryCode;
72
73         /**
74          * Local dial prefix
75          */
76         private String countryExternalDialPrefix;
77
78         /**
79          * i18n bundle key
80          */
81         private String countryI18nKey;
82
83         /**
84          * Whether the local dial prefix is required to use
85          */
86         private Boolean countryIsLocalPrefixRequired;
87
88         /**
89          * Regular country controller
90          */
91         @Inject
92         private JobsCountryListWebViewController countryListController;
93
94         /**
95          * Phone code
96          */
97         private Short countryPhoneCode;
98
99         /**
100          * Default constructor
101          */
102         public JobsAdminCountryWebRequestBean () {
103                 // Call super constructor
104                 super();
105         }
106
107         /**
108          * Adds country to all relevant beans and sends it to the EJB. A redirect
109          * should happen after successfull creation.
110          * <p>
111          * @todo Add field validation
112          */
113         public void addCountry () {
114                 // Create new country object
115                 final Country country = new CountryData();
116
117                 // Add all data
118                 country.setCountryAbroadDialPrefix(this.getCountryAbroadDialPrefix());
119                 country.setCountryCode(this.getCountryCode());
120                 country.setCountryExternalDialPrefix(this.getCountryExternalDialPrefix());
121                 country.setCountryI18nKey(this.getCountryI18nKey());
122                 country.setCountryIsLocalPrefixRequired(this.getCountryIsLocalPrefixRequired());
123                 country.setCountryPhoneCode(this.getCountryPhoneCode());
124
125                 // Does it already exist?
126                 if (this.isCountryAdded(country)) {
127                         // Yes, then abort here
128                         throw new FacesException(new CountryAlreadyAddedException(country));
129                 }
130
131                 // Init variable
132                 final Country updatedCountry;
133
134                 try {
135                         // Send country to bean
136                         updatedCountry = this.adminCountryBean.addCountry(country);
137                 } catch (final CountryAlreadyAddedException ex) {
138                         // Throw again
139                         throw new FacesException(ex);
140                 }
141
142                 // Fire event
143                 this.addedCountryEvent.fire(new AdminAddedCountryEvent(updatedCountry));
144
145                 // Clear this bean
146                 this.clear();
147         }
148
149         /**
150          * Getter for abroad dial prefix
151          * <p>
152          * @return Abroad dial prefix
153          */
154         public String getCountryAbroadDialPrefix () {
155                 return this.countryAbroadDialPrefix;
156         }
157
158         /**
159          * Setter for abroad dial prefix
160          * <p>
161          * @param countryAbroadDialPrefix Abroad dial prefix
162          */
163         public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
164                 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
165         }
166
167         /**
168          * Getter for 2-characters country code
169          * <p>
170          * @return Country code
171          */
172         public String getCountryCode () {
173                 return this.countryCode;
174         }
175
176         /**
177          * Setter for 2-characters country code
178          * <p>
179          * @param countryCode Country code
180          */
181         public void setCountryCode (final String countryCode) {
182                 this.countryCode = countryCode;
183         }
184
185         /**
186          * Getter for external dial prefix
187          * <p>
188          * @return External dial prefix
189          */
190         public String getCountryExternalDialPrefix () {
191                 return this.countryExternalDialPrefix;
192         }
193
194         /**
195          * Setter for external dial prefix
196          * <p>
197          * @param countryExternalDialPrefix External dial prefix
198          */
199         public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
200                 this.countryExternalDialPrefix = countryExternalDialPrefix;
201         }
202
203         /**
204          * Getter for i18n key for country name
205          * <p>
206          * @return i18n key for country name
207          */
208         public String getCountryI18nKey () {
209                 return this.countryI18nKey;
210         }
211
212         /**
213          * Setter for i18n key for country name
214          * <p>
215          * @param countryI18nKey i18n key for country name
216          */
217         public void setCountryI18nKey (final String countryI18nKey) {
218                 this.countryI18nKey = countryI18nKey;
219         }
220
221         /**
222          * Getter for whether the local dial prefix is required for local calls
223          * <p>
224          * @return Whether the local dial prefix is required
225          */
226         public Boolean getCountryIsLocalPrefixRequired () {
227                 return this.countryIsLocalPrefixRequired;
228         }
229
230         /**
231          * Setter for whether the local dial prefix is required for local calls
232          * <p>
233          * @param countryIsLocalPrefixRequired Whether the local dial prefix is
234          *                                     required
235          */
236         public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
237                 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
238         }
239
240         /**
241          * Getter for country code (example: 49 for Germany, 63 for Philippines)
242          * <p>
243          * @return Dial number without prefix
244          */
245         public Short getCountryPhoneCode () {
246                 return this.countryPhoneCode;
247         }
248
249         /**
250          * Setter for country code (example: 49 for Germany, 63 for Philippines)
251          * <p>
252          * @param countryPhoneCode Country code
253          */
254         public void setCountryPhoneCode (final Short countryPhoneCode) {
255                 this.countryPhoneCode = countryPhoneCode;
256         }
257
258         /**
259          * Clears this bean's data. This should be called after a form has been
260          * submitted and the processing of the form was successful.
261          */
262         private void clear () {
263                 // Clear fields
264                 this.setCountryAbroadDialPrefix(null);
265                 this.setCountryCode(null);
266                 this.setCountryExternalDialPrefix(null);
267                 this.setCountryI18nKey(null);
268                 this.setCountryIsLocalPrefixRequired(null);
269                 this.setCountryPhoneCode(null);
270         }
271
272         /**
273          * Checks if given country is already added by iterating over the whole list
274          * and try to find it.
275          * <p>
276          * @param country Country instance to look for
277          * <p>
278          * @return Whether the country was already found
279          */
280         private boolean isCountryAdded (final Country country) {
281                 // Default is not found
282                 boolean isAdded = false;
283
284                 // Check whole list
285                 for (final Country currentCountry : this.countryListController.getAllCountries()) {
286                         // Is country code or i18n the same?
287                         if ((Objects.equals(country.getCountryCode(), currentCountry.getCountryCode())) || (Objects.equals(country.getCountryI18nKey(), currentCountry.getCountryI18nKey()))) {
288                                 // Yes, then abort search
289                                 isAdded = true;
290                                 break;
291                         }
292                 }
293
294                 // Return result
295                 return isAdded;
296         }
297
298 }