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