]> git.mxchange.org Git - jfinancials-war.git/blob
954828dd47fad24f7790d35610dc97e47bd80fc7
[jfinancials-war.git] /
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.addressbook.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.AdminAddedMobileProviderEvent;
33 import org.mxchange.jphone.events.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
39 /**
40  * Administrative bean (controller) for mobile provider
41  * <p>
42  * @author Roland Haeder<roland@mxchange.org>
43  */
44 @Named ("adminMobileProviderController")
45 @RequestScoped
46 public class AddressbookAdminMobileProviderWebRequestBean implements AddressbookAdminMobileProviderWebRequestController {
47
48         /**
49          * Serial number
50          */
51         private static final long serialVersionUID = 184_598_175_371_269_016L;
52
53         /**
54          * Remote EJB for mobile providers (administrative)
55          */
56         private AdminMobileProviderSessionBeanRemote adminRemoteBean;
57
58         /**
59          * Regular bean
60          */
61         @Inject
62         private AddressbookMobileProviderWebRequestController 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 AddressbookAdminMobileProviderWebRequestBean () {
95                 // Try it
96                 try {
97                         // Get initial context
98                         Context context = new InitialContext();
99
100                         // Try to lookup the beans
101                         this.adminRemoteBean = (AdminMobileProviderSessionBeanRemote) context.lookup("java:global/addressbook-ejb/adminmobileprovider!org.mxchange.jphone.phonenumbers.mobileprovider.AdminMobileProviderSessionBeanRemote"); //NOI18N
102                 } catch (final NamingException e) {
103                         // Throw it again
104                         throw new FaceletException(e);
105                 }
106         }
107
108         @Override
109         public void addMobileProvider () {
110                 // Create mobile provider instance
111                 MobileProvider mobileProvider = new CellphoneProvider(this.getProviderDialPrefix(), this.getProviderName(), this.getProviderCountry(), this.getProviderMailPattern());
112
113                 // Is the provider already created?
114                 if (this.isMobileProviderCreated(mobileProvider)) {
115                         // Then throw exception
116                         throw new FaceletException(new MobileProviderAlreadyAddedException(mobileProvider));
117                 }
118
119                 // Init variable
120                 MobileProvider updatedProvider = null;
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
137         @Override
138         public List<MobileProvider> allMobileProvider () {
139                 return this.mobileController.allMobileProvider();
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         @Override
183         public boolean hasMobileProvider () {
184                 return (!this.allMobileProvider().isEmpty());
185         }
186
187         /**
188          * Clears this bean
189          */
190         private void clear () {
191                 // Clear all fields
192                 this.setProviderCountry(null);
193                 this.setProviderDialPrefix(null);
194                 this.setProviderMailPattern(null);
195                 this.setProviderName(null);
196         }
197
198         /**
199          * Checks whether if the given mobile provider is already created by
200          * checking both dial prefix and country.
201          * <p>
202          * @param mobileProvider Mobile provider instance to check
203          * <p>
204          * @return Whether the given mobile provider instance is found
205          */
206         private boolean isMobileProviderCreated (final MobileProvider mobileProvider) {
207                 // Default is not found
208                 boolean isFound = false;
209
210                 // Get list of all providers
211                 List<MobileProvider> providers = this.allMobileProvider();
212
213                 // Get iterator from it
214                 Iterator<MobileProvider> iterator = providers.iterator();
215
216                 // Loop through all
217                 while (iterator.hasNext()) {
218                         // Get next element
219                         MobileProvider next = iterator.next();
220
221                         // Is the provider's dial prefix and country the same?
222                         if ((Objects.equals(mobileProvider.getProviderDialPrefix(), next.getProviderDialPrefix())) && (Objects.equals(mobileProvider.getProviderCountry(), next.getProviderCountry()))) {
223                                 // Is the same, abort loop
224                                 isFound = true;
225                                 break;
226                         }
227                 }
228
229                 // Return result
230                 return isFound;
231         }
232
233 }