]> git.mxchange.org Git - pizzaservice-war.git/blob
6fe58d02137d2b039c7230abd94b55152a87fba8
[pizzaservice-war.git] /
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.pizzaapplication.beans.mobileprovider;
18
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Objects;
22 import javax.annotation.PostConstruct;
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 javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32 import org.mxchange.jcountry.data.Country;
33 import org.mxchange.jphone.events.mobileprovider.added.AdminAddedMobileProviderEvent;
34 import org.mxchange.jphone.events.mobileprovider.added.AdminMobileProviderAddedEvent;
35 import org.mxchange.jphone.exceptions.MobileProviderAlreadyAddedException;
36 import org.mxchange.jphone.phonenumbers.mobileprovider.AdminMobileProviderSessionBeanRemote;
37 import org.mxchange.jphone.phonenumbers.mobileprovider.CellphoneProvider;
38 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
39 import org.mxchange.pizzaapplication.beans.BasePizzaController;
40
41 /**
42  * Administrative bean (controller) for mobile provider
43  * <p>
44  * @author Roland Häder<roland@mxchange.org>
45  */
46 @Named ("adminMobileProviderController")
47 @RequestScoped
48 public class PizzaAdminMobileProviderWebRequestBean extends BasePizzaController implements PizzaAdminMobileProviderWebRequestController {
49
50         /**
51          * Serial number
52          */
53         private static final long serialVersionUID = 184_598_175_371_269_016L;
54
55         /**
56          * Remote EJB for mobile providers (administrative)
57          */
58         private AdminMobileProviderSessionBeanRemote adminRemoteBean;
59
60         /**
61          * Regular bean
62          */
63         @Inject
64         private PizzaMobileProviderWebRequestController mobileController;
65
66         /**
67          * Event being fired when the mobile provider was added
68          */
69         @Inject
70         @Any
71         private Event<AdminAddedMobileProviderEvent> providerAddedEvent;
72
73         /**
74          * Country instance ('s dial data)
75          */
76         private Country providerCountry;
77
78         /**
79          * Provider dial prefix (example: 0177 for German E+)
80          */
81         private Long providerDialPrefix;
82
83         /**
84          * Pattern for mail gateway
85          */
86         private String providerMailPattern;
87
88         /**
89          * Name of the provider
90          */
91         private String providerName;
92
93         /**
94          * Default constructor
95          */
96         public PizzaAdminMobileProviderWebRequestBean () {
97                 // Call super constructor
98                 super();
99         }
100
101         /**
102          * Adds a mobile provider to database by calling the EJB. A pre-check on
103          * dial-prefix and country combination is done, if found, an exception is
104          * thrown. A redirect should take place after successfull creation.
105          * <p>
106          * @return Redirect outcome
107          * <p>
108          * @todo Add field validation
109          */
110         public String addMobileProvider () {
111                 // Create mobile provider instance
112                 MobileProvider mobileProvider = new CellphoneProvider(this.getProviderDialPrefix(), this.getProviderName(), this.getProviderCountry(), this.getProviderMailPattern());
113
114                 // Is the provider already created?
115                 if (this.isMobileProviderCreated(mobileProvider)) {
116                         // Then throw exception
117                         this.showFacesMessage("form_add_mobile_provider:providerDialPrefix", new MobileProviderAlreadyAddedException(mobileProvider)); //NOI18N
118                         return ""; //NOI18N
119                 }
120
121                 // Init variable
122                 MobileProvider updatedProvider = null;
123
124                 try {
125                         // Call remote EJB and get back an updated instance
126                         updatedProvider = this.adminRemoteBean.addMobileProvider(mobileProvider);
127                 } catch (final MobileProviderAlreadyAddedException ex) {
128                         // Throw again
129                         throw new FaceletException(ex);
130                 }
131
132                 // Fire event
133                 this.providerAddedEvent.fire(new AdminMobileProviderAddedEvent(updatedProvider));
134
135                 // Clear this bean
136                 this.clear();
137
138                 // Redirect to list
139                 return "admin_list_mobile_provider"; //NOI18N
140         }
141
142         /**
143          * Getter for country instance ('s dial data)
144          * <p>
145          * @return Country instance
146          */
147         public Country getProviderCountry () {
148                 return this.providerCountry;
149         }
150
151         /**
152          * Setter for country instance ('s dial data)
153          * <p>
154          * @param providerCountry Country instance
155          */
156         public void setProviderCountry (final Country providerCountry) {
157                 this.providerCountry = providerCountry;
158         }
159
160         /**
161          * Getter for provider dial prefix
162          * <p>
163          * @return Provider dial prefix
164          */
165         public Long getProviderDialPrefix () {
166                 return this.providerDialPrefix;
167         }
168
169         /**
170          * Setter for provider dial prefix
171          * <p>
172          * @param providerDialPrefix Provider dial prefix
173          */
174         public void setProviderDialPrefix (final Long providerDialPrefix) {
175                 this.providerDialPrefix = providerDialPrefix;
176         }
177
178         /**
179          * Getter for pattern for mail gateway
180          * <p>
181          * @return Pattern for mail gateway
182          */
183         public String getProviderMailPattern () {
184                 return this.providerMailPattern;
185         }
186
187         /**
188          * Setter for pattern for mail gateway
189          * <p>
190          * @param providerMailPattern Pattern for mail gateway
191          */
192         public void setProviderMailPattern (final String providerMailPattern) {
193                 this.providerMailPattern = providerMailPattern;
194         }
195
196         /**
197          * Getter for provider name
198          * <p>
199          * @return Provider name
200          */
201         public String getProviderName () {
202                 return this.providerName;
203         }
204
205         /**
206          * Setter for provider name
207          * <p>
208          * @param providerName Provider name
209          */
210         public void setProviderName (final String providerName) {
211                 this.providerName = providerName;
212         }
213
214         /**
215          * Post-construction method
216          */
217         @PostConstruct
218         public void init () {
219                 // Try it
220                 try {
221                         // Get initial context
222                         Context context = new InitialContext();
223
224                         // Try to lookup the beans
225                         this.adminRemoteBean = (AdminMobileProviderSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/adminMobileProvider!org.mxchange.jphone.phonenumbers.mobileprovider.AdminMobileProviderSessionBeanRemote"); //NOI18N
226                 } catch (final NamingException e) {
227                         // Throw it again
228                         throw new FaceletException(e);
229                 }
230         }
231
232         /**
233          * Clears this bean
234          */
235         private void clear () {
236                 // Clear all fields
237                 this.setProviderCountry(null);
238                 this.setProviderDialPrefix(null);
239                 this.setProviderMailPattern(null);
240                 this.setProviderName(null);
241         }
242
243         /**
244          * Checks whether if the given mobile provider is already created by
245          * checking both dial prefix and country.
246          * <p>
247          * @param mobileProvider Mobile provider instance to check
248          * <p>
249          * @return Whether the given mobile provider instance is found
250          */
251         private boolean isMobileProviderCreated (final MobileProvider mobileProvider) {
252                 // Default is not found
253                 boolean isFound = false;
254
255                 // Get list of all providers
256                 List<MobileProvider> providers = this.mobileController.allMobileProviders();
257
258                 // Get iterator from it
259                 Iterator<MobileProvider> iterator = providers.iterator();
260
261                 // Loop through all
262                 while (iterator.hasNext()) {
263                         // Get next element
264                         MobileProvider next = iterator.next();
265
266                         // Is the provider's dial prefix and country the same?
267                         if ((Objects.equals(mobileProvider.getProviderDialPrefix(), next.getProviderDialPrefix())) && (Objects.equals(mobileProvider.getProviderCountry(), next.getProviderCountry()))) {
268                                 // Is the same, abort loop
269                                 isFound = true;
270                                 break;
271                         }
272                 }
273
274                 // Return result
275                 return isFound;
276         }
277
278 }