]> git.mxchange.org Git - pizzaservice-war.git/blob
2c6a6a0c3be5447ff273dc43a2b18007391c3382
[pizzaservice-war.git] /
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.pizzaapplication.beans.mobileprovider;
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.jphone.events.mobileprovider.added.AdminAddedMobileProviderEvent;
33 import org.mxchange.jphone.events.mobileprovider.added.AdminMobileProviderAddedEvent;
34 import org.mxchange.jphone.exceptions.MobileProviderAlreadyAddedException;
35 import org.mxchange.jphone.phonenumbers.mobileprovider.AdminMobileProviderSessionBeanRemote;
36 import org.mxchange.jphone.phonenumbers.mobileprovider.CellphoneProvider;
37 import org.mxchange.jphone.phonenumbers.mobileprovider.MobileProvider;
38 import org.mxchange.pizzaapplication.beans.BasePizzaController;
39
40 /**
41  * Administrative bean (controller) for mobile provider
42  * <p>
43  * @author Roland Häder<roland@mxchange.org>
44  */
45 @Named ("adminMobileProviderController")
46 @RequestScoped
47 public class PizzaAdminMobileProviderWebRequestBean extends BasePizzaController implements PizzaAdminMobileProviderWebRequestController {
48
49         /**
50          * Serial number
51          */
52         private static final long serialVersionUID = 184_598_175_371_269_016L;
53
54         /**
55          * Remote EJB for mobile providers (administrative)
56          */
57         private AdminMobileProviderSessionBeanRemote adminRemoteBean;
58
59         /**
60          * Regular bean
61          */
62         @Inject
63         private PizzaMobileProviderWebRequestController mobileController;
64
65         /**
66          * Event being fired when the mobile provider was added
67          */
68         @Inject
69         @Any
70         private Event<AdminAddedMobileProviderEvent> providerAddedEvent;
71
72         /**
73          * Country instance ('s dial data)
74          */
75         private Country providerCountry;
76
77         /**
78          * Provider dial prefix (example: 0177 for German E+)
79          */
80         private Long providerDialPrefix;
81
82         /**
83          * Pattern for mail gateway
84          */
85         private String providerMailPattern;
86
87         /**
88          * Name of the provider
89          */
90         private String providerName;
91
92         /**
93          * Default constructor
94          */
95         public PizzaAdminMobileProviderWebRequestBean () {
96                 // Try it
97                 try {
98                         // Get initial context
99                         Context context = new InitialContext();
100
101                         // Try to lookup the beans
102                         this.adminRemoteBean = (AdminMobileProviderSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/adminmobileprovider!org.mxchange.jphone.phonenumbers.mobileprovider.AdminMobileProviderSessionBeanRemote"); //NOI18N
103                 } catch (final NamingException e) {
104                         // Throw it again
105                         throw new FaceletException(e);
106                 }
107         }
108
109         @Override
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         @Override
143         public Country getProviderCountry () {
144                 return this.providerCountry;
145         }
146
147         @Override
148         public void setProviderCountry (final Country providerCountry) {
149                 this.providerCountry = providerCountry;
150         }
151
152         @Override
153         public Long getProviderDialPrefix () {
154                 return this.providerDialPrefix;
155         }
156
157         @Override
158         public void setProviderDialPrefix (final Long providerDialPrefix) {
159                 this.providerDialPrefix = providerDialPrefix;
160         }
161
162         @Override
163         public String getProviderMailPattern () {
164                 return this.providerMailPattern;
165         }
166
167         @Override
168         public void setProviderMailPattern (final String providerMailPattern) {
169                 this.providerMailPattern = providerMailPattern;
170         }
171
172         @Override
173         public String getProviderName () {
174                 return this.providerName;
175         }
176
177         @Override
178         public void setProviderName (final String providerName) {
179                 this.providerName = providerName;
180         }
181
182         /**
183          * Clears this bean
184          */
185         private void clear () {
186                 // Clear all fields
187                 this.setProviderCountry(null);
188                 this.setProviderDialPrefix(null);
189                 this.setProviderMailPattern(null);
190                 this.setProviderName(null);
191         }
192
193         /**
194          * Checks whether if the given mobile provider is already created by
195          * checking both dial prefix and country.
196          * <p>
197          * @param mobileProvider Mobile provider instance to check
198          * <p>
199          * @return Whether the given mobile provider instance is found
200          */
201         private boolean isMobileProviderCreated (final MobileProvider mobileProvider) {
202                 // Default is not found
203                 boolean isFound = false;
204
205                 // Get list of all providers
206                 List<MobileProvider> providers = this.mobileController.allMobileProviders();
207
208                 // Get iterator from it
209                 Iterator<MobileProvider> iterator = providers.iterator();
210
211                 // Loop through all
212                 while (iterator.hasNext()) {
213                         // Get next element
214                         MobileProvider next = iterator.next();
215
216                         // Is the provider's dial prefix and country the same?
217                         if ((Objects.equals(mobileProvider.getProviderDialPrefix(), next.getProviderDialPrefix())) && (Objects.equals(mobileProvider.getProviderCountry(), next.getProviderCountry()))) {
218                                 // Is the same, abort loop
219                                 isFound = true;
220                                 break;
221                         }
222                 }
223
224                 // Return result
225                 return isFound;
226         }
227
228 }