]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/country/JobsAdminCountryWebRequestBean.java
Closed internal TODO: (please cherry-pick this)
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / country / JobsAdminCountryWebRequestBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.events.AdminEventCountryAdded;
36 import org.mxchange.jcountry.exceptions.CountryAlreadyAddedException;
37 import org.mxchange.jjobs.beans.BaseJobsController;
38
39 /**
40  * An administrative country bean
41  * <p>
42  * @author Roland Haeder<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<AdminAddedCountryEvent> 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 AdminEventCountryAdded(updatedCountry));
150
151                 // Redirect to list
152                 return "admin_list_country"; //NOI18N
153         }
154
155         @Override
156         public List<Country> allCountries () {
157                 // Return "cached" version
158                 return this.countryController.allCountries();
159         }
160
161         @Override
162         public String getCountryAbroadDialPrefix () {
163                 return this.countryAbroadDialPrefix;
164         }
165
166         @Override
167         public void setCountryAbroadDialPrefix (final String countryAbroadDialPrefix) {
168                 this.countryAbroadDialPrefix = countryAbroadDialPrefix;
169         }
170
171         @Override
172         public String getCountryCode () {
173                 return this.countryCode;
174         }
175
176         @Override
177         public void setCountryCode (final String countryCode) {
178                 this.countryCode = countryCode;
179         }
180
181         @Override
182         public String getCountryExternalDialPrefix () {
183                 return this.countryExternalDialPrefix;
184         }
185
186         @Override
187         public void setCountryExternalDialPrefix (final String countryExternalDialPrefix) {
188                 this.countryExternalDialPrefix = countryExternalDialPrefix;
189         }
190
191         @Override
192         public String getCountryI18nKey () {
193                 return this.countryI18nKey;
194         }
195
196         @Override
197         public void setCountryI18nKey (final String countryI18nKey) {
198                 this.countryI18nKey = countryI18nKey;
199         }
200
201         @Override
202         public Boolean getCountryIsLocalPrefixRequired () {
203                 return this.countryIsLocalPrefixRequired;
204         }
205
206         @Override
207         public void setCountryIsLocalPrefixRequired (final Boolean countryIsLocalPrefixRequired) {
208                 this.countryIsLocalPrefixRequired = countryIsLocalPrefixRequired;
209         }
210
211         @Override
212         public Short getCountryPhoneCode () {
213                 return this.countryPhoneCode;
214         }
215
216         @Override
217         public void setCountryPhoneCode (final Short countryPhoneCode) {
218                 this.countryPhoneCode = countryPhoneCode;
219         }
220
221         @Override
222         public boolean hasCountries () {
223                 return (!this.allCountries().isEmpty());
224         }
225
226         /**
227          * Checks if given country is already added by iterating over the whole list
228          * and try to find it.
229          * <p>
230          * @param country Country instance to look for
231          * <p>
232          * @return Whether the country was already found
233          */
234         private boolean isCountryAdded (final Country country) {
235                 // Default is not found
236                 boolean isAdded = false;
237
238                 // Now get whole ist
239                 List<Country> countries = this.countryController.allCountries();
240
241                 // Get iterator from it
242                 Iterator<Country> iterator = countries.iterator();
243
244                 // Check whole list
245                 while (iterator.hasNext()) {
246                         // Get next country
247                         Country next = iterator.next();
248
249                         // Is country code or i18n the same?
250                         if ((Objects.equals(country.getCountryCode(), next.getCountryCode())) || (Objects.equals(country.getCountryI18nKey(), next.getCountryI18nKey()))) {
251                                 // Yes, then abort search
252                                 isAdded = true;
253                                 break;
254                         }
255                 }
256
257                 // Return result
258                 return isAdded;
259         }
260
261 }