]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/country/JobsAdminCountryWebRequestBean.java
renamed to better names
[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.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.exceptions.CountryAlreadyAddedException;
36 import org.mxchange.jjobs.beans.BaseJobsController;
37 import org.mxchange.jcountry.events.ObservableAdminAddedCountryEvent;
38
39 /**
40  * An administrative country bean
41  * <p>
42  * @author Roland Häder<roland@mxchange.org>
43  */
44 @Named ("adminCountryController")
45 @RequestScoped
46 public class JobsAdminCountryWebRequestBean extends BaseJobsController implements JobsAdminCountryWebRequestController {
47
48         /**
49          * Serial number
50          */
51         private static final long serialVersionUID = 18_598_175_719_603L;
52
53         /**
54          * An event triggered when the administrator has added a country
55          */
56         @Inject
57         @Any
58         private Event<ObservableAdminAddedCountryEvent> addedCountryEvent;
59
60         /**
61          * Abroad dial prefix
62          */
63         private String countryAbroadDialPrefix;
64
65         /**
66          * Remote country EJB
67          */
68         private CountrySingletonBeanRemote countryBean;
69
70         /**
71          * 2-letter country code
72          */
73         private String countryCode;
74
75         /**
76          * Regular country controller
77          */
78         @Inject
79         private JobsCountryWebApplicationController countryController;
80
81         /**
82          * Local dial prefix
83          */
84         private String countryExternalDialPrefix;
85
86         /**
87          * i18n bundle key
88          */
89         private String countryI18nKey;
90
91         /**
92          * Whether the local dial prefix is required to use
93          */
94         private Boolean countryIsLocalPrefixRequired;
95
96         /**
97          * Phone code
98          */
99         private Short countryPhoneCode;
100
101         /**
102          * Default constructor
103          */
104         public JobsAdminCountryWebRequestBean () {
105                 // Try this
106                 try {
107                         // Get initial context
108                         Context context = new InitialContext();
109
110                         // Try to lookup the bean
111                         this.countryBean = (CountrySingletonBeanRemote) context.lookup("java:global/jjobs-ejb/country!org.mxchange.jcountry.data.CountrySingletonBeanRemote"); //NOI18N
112                 } catch (final NamingException ex) {
113                         // Continue to throw
114                         throw new FaceletException(ex);
115                 }
116         }
117
118         @Override
119         public String addCountry () {
120                 // Create new country object
121                 Country country = new CountryData();
122
123                 // Add all data
124                 country.setCountryAbroadDialPrefix(this.getCountryAbroadDialPrefix());
125                 country.setCountryCode(this.getCountryCode());
126                 country.setCountryExternalDialPrefix(this.getCountryExternalDialPrefix());
127                 country.setCountryI18nKey(this.getCountryI18nKey());
128                 country.setCountryIsLocalPrefixRequired(this.getCountryIsLocalPrefixRequired());
129                 country.setCountryPhoneCode(this.getCountryPhoneCode());
130
131                 // Does it already exist?
132                 if (this.isCountryAdded(country)) {
133                         // Yes, then abort here
134                         throw new FaceletException(new CountryAlreadyAddedException(country));
135                 }
136
137                 // Init variable
138                 Country updatedCountry = null;
139
140                 try {
141                         // Send country to bean
142                         updatedCountry = this.countryBean.addCountry(country);
143                 } catch (final CountryAlreadyAddedException ex) {
144                         // Throw again
145                         throw new FaceletException(ex);
146                 }
147
148                 // Fire event
149                 this.addedCountryEvent.fire(new AdminAddedCountryEvent(updatedCountry));
150
151                 // Clear this bean
152                 this.clear();
153
154                 // Redirect to list
155                 return "admin_list_country"; //NOI18N
156         }
157
158         @Override
159         public List<Country> allCountries () {
160                 // Return "cached" version
161                 return this.countryController.allCountries();
162         }
163
164         @Override
165         public String getCountryAbroadDialPrefix () {
166                 return this.countryAbroadDialPrefix;
167         }
168
169         @Override
170         public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
171                 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
172         }
173
174         @Override
175         public String getCountryCode () {
176                 return this.countryCode;
177         }
178
179         @Override
180         public void setCountryCode (final String countryCode) {
181                 this.countryCode = countryCode;
182         }
183
184         @Override
185         public String getCountryExternalDialPrefix () {
186                 return this.countryExternalDialPrefix;
187         }
188
189         @Override
190         public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
191                 this.countryExternalDialPrefix = countryExternalDialPrefix;
192         }
193
194         @Override
195         public String getCountryI18nKey () {
196                 return this.countryI18nKey;
197         }
198
199         @Override
200         public void setCountryI18nKey (final String countryI18nKey) {
201                 this.countryI18nKey = countryI18nKey;
202         }
203
204         @Override
205         public Boolean getCountryIsLocalPrefixRequired () {
206                 return this.countryIsLocalPrefixRequired;
207         }
208
209         @Override
210         public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
211                 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
212         }
213
214         @Override
215         public Short getCountryPhoneCode () {
216                 return this.countryPhoneCode;
217         }
218
219         @Override
220         public void setCountryPhoneCode (final Short countryPhoneCode) {
221                 this.countryPhoneCode = countryPhoneCode;
222         }
223
224         @Override
225         public boolean hasCountries () {
226                 return (!this.allCountries().isEmpty());
227         }
228
229         /**
230          * Clears this bean's data. This should be called after a form has been
231          * submitted and the processing of the form was successful.
232          */
233         private void clear () {
234                 // Clear fields
235                 this.setCountryAbroadDialPrefix(null);
236                 this.setCountryCode(null);
237                 this.setCountryExternalDialPrefix(null);
238                 this.setCountryI18nKey(null);
239                 this.setCountryIsLocalPrefixRequired(null);
240                 this.setCountryPhoneCode(null);
241         }
242
243         /**
244          * Checks if given country is already added by iterating over the whole list
245          * and try to find it.
246          * <p>
247          * @param country Country instance to look for
248          * <p>
249          * @return Whether the country was already found
250          */
251         private boolean isCountryAdded (final Country country) {
252                 // Default is not found
253                 boolean isAdded = false;
254
255                 // Now get whole ist
256                 List<Country> countries = this.countryController.allCountries();
257
258                 // Get iterator from it
259                 Iterator<Country> iterator = countries.iterator();
260
261                 // Check whole list
262                 while (iterator.hasNext()) {
263                         // Get next country
264                         Country next = iterator.next();
265
266                         // Is country code or i18n the same?
267                         if ((Objects.equals(country.getCountryCode(), next.getCountryCode())) || (Objects.equals(country.getCountryI18nKey(), next.getCountryI18nKey()))) {
268                                 // Yes, then abort search
269                                 isAdded = true;
270                                 break;
271                         }
272                 }
273
274                 // Return result
275                 return isAdded;
276         }
277
278 }