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