]> git.mxchange.org Git - pizzaservice-war.git/blob
f2fd5eece021d9c533c27843758e8a2067344bb6
[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.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         }
98
99         @Override
100         public String addMobileProvider () {
101                 // Create mobile provider instance
102                 MobileProvider mobileProvider = new CellphoneProvider(this.getProviderDialPrefix(), this.getProviderName(), this.getProviderCountry(), this.getProviderMailPattern());
103
104                 // Is the provider already created?
105                 if (this.isMobileProviderCreated(mobileProvider)) {
106                         // Then throw exception
107                         this.showFacesMessage("form_add_mobile_provider:providerDialPrefix", new MobileProviderAlreadyAddedException(mobileProvider)); //NOI18N
108                         return ""; //NOI18N
109                 }
110
111                 // Init variable
112                 MobileProvider updatedProvider = null;
113
114                 try {
115                         // Call remote EJB and get back an updated instance
116                         updatedProvider = this.adminRemoteBean.addMobileProvider(mobileProvider);
117                 } catch (final MobileProviderAlreadyAddedException ex) {
118                         // Throw again
119                         throw new FaceletException(ex);
120                 }
121
122                 // Fire event
123                 this.providerAddedEvent.fire(new AdminMobileProviderAddedEvent(updatedProvider));
124
125                 // Clear this bean
126                 this.clear();
127
128                 // Redirect to list
129                 return "admin_list_mobile_provider"; //NOI18N
130         }
131
132         @Override
133         public Country getProviderCountry () {
134                 return this.providerCountry;
135         }
136
137         @Override
138         public void setProviderCountry (final Country providerCountry) {
139                 this.providerCountry = providerCountry;
140         }
141
142         @Override
143         public Long getProviderDialPrefix () {
144                 return this.providerDialPrefix;
145         }
146
147         @Override
148         public void setProviderDialPrefix (final Long providerDialPrefix) {
149                 this.providerDialPrefix = providerDialPrefix;
150         }
151
152         @Override
153         public String getProviderMailPattern () {
154                 return this.providerMailPattern;
155         }
156
157         @Override
158         public void setProviderMailPattern (final String providerMailPattern) {
159                 this.providerMailPattern = providerMailPattern;
160         }
161
162         @Override
163         public String getProviderName () {
164                 return this.providerName;
165         }
166
167         @Override
168         public void setProviderName (final String providerName) {
169                 this.providerName = providerName;
170         }
171
172         /**
173          * Post-construction method
174          */
175         @PostConstruct
176         public void init () {
177                 // Try it
178                 try {
179                         // Get initial context
180                         Context context = new InitialContext();
181
182                         // Try to lookup the beans
183                         this.adminRemoteBean = (AdminMobileProviderSessionBeanRemote) context.lookup("java:global/pizzaservice-ejb/adminMobileProvider!org.mxchange.jphone.phonenumbers.mobileprovider.AdminMobileProviderSessionBeanRemote"); //NOI18N
184                 } catch (final NamingException e) {
185                         // Throw it again
186                         throw new FaceletException(e);
187                 }
188         }
189
190         /**
191          * Clears this bean
192          */
193         private void clear () {
194                 // Clear all fields
195                 this.setProviderCountry(null);
196                 this.setProviderDialPrefix(null);
197                 this.setProviderMailPattern(null);
198                 this.setProviderName(null);
199         }
200
201         /**
202          * Checks whether if the given mobile provider is already created by
203          * checking both dial prefix and country.
204          * <p>
205          * @param mobileProvider Mobile provider instance to check
206          * <p>
207          * @return Whether the given mobile provider instance is found
208          */
209         private boolean isMobileProviderCreated (final MobileProvider mobileProvider) {
210                 // Default is not found
211                 boolean isFound = false;
212
213                 // Get list of all providers
214                 List<MobileProvider> providers = this.mobileController.allMobileProviders();
215
216                 // Get iterator from it
217                 Iterator<MobileProvider> iterator = providers.iterator();
218
219                 // Loop through all
220                 while (iterator.hasNext()) {
221                         // Get next element
222                         MobileProvider next = iterator.next();
223
224                         // Is the provider's dial prefix and country the same?
225                         if ((Objects.equals(mobileProvider.getProviderDialPrefix(), next.getProviderDialPrefix())) && (Objects.equals(mobileProvider.getProviderCountry(), next.getProviderCountry()))) {
226                                 // Is the same, abort loop
227                                 isFound = true;
228                                 break;
229                         }
230                 }
231
232                 // Return result
233                 return isFound;
234         }
235
236 }