2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.addressbook.beans.mobileprovider;
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;
40 * Administrative bean (controller) for mobile provider
42 * @author Roland Haeder<roland@mxchange.org>
44 @Named ("adminMobileProviderController")
46 public class AddressbookAdminMobileProviderWebRequestBean implements AddressbookAdminMobileProviderWebRequestController {
51 private static final long serialVersionUID = 184_598_175_371_269_016L;
54 * Remote EJB for mobile providers (administrative)
56 private AdminMobileProviderSessionBeanRemote adminRemoteBean;
62 private AddressbookMobileProviderWebRequestController mobileController;
65 * Event being fired when the mobile provider was added
69 private Event<AdminAddedMobileProviderEvent> providerAddedEvent;
72 * Country instance ('s dial data)
74 private Country providerCountry;
77 * Provider dial prefix (example: 0177 for German E+)
79 private Long providerDialPrefix;
82 * Pattern for mail gateway
84 private String providerMailPattern;
87 * Name of the provider
89 private String providerName;
94 public AddressbookAdminMobileProviderWebRequestBean () {
97 // Get initial context
98 Context context = new InitialContext();
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) {
104 throw new FaceletException(e);
109 public String addMobileProvider () {
110 // Create mobile provider instance
111 MobileProvider mobileProvider = new CellphoneProvider(this.getProviderDialPrefix(), this.getProviderName(), this.getProviderCountry(), this.getProviderMailPattern());
113 // Is the provider already created?
114 if (this.isMobileProviderCreated(mobileProvider)) {
115 // Then throw exception
116 throw new FaceletException(new MobileProviderAlreadyAddedException(mobileProvider));
120 MobileProvider updatedProvider = null;
123 // Call remote EJB and get back an updated instance
124 updatedProvider = this.adminRemoteBean.addMobileProvider(mobileProvider);
125 } catch (final MobileProviderAlreadyAddedException ex) {
127 throw new FaceletException(ex);
131 this.providerAddedEvent.fire(new AdminMobileProviderAddedEvent(updatedProvider));
134 return "admin_list_mobile_provider"; //NOI18N
138 public Country getProviderCountry () {
139 return this.providerCountry;
143 public void setProviderCountry (final Country providerCountry) {
144 this.providerCountry = providerCountry;
148 public Long getProviderDialPrefix () {
149 return this.providerDialPrefix;
153 public void setProviderDialPrefix (final Long providerDialPrefix) {
154 this.providerDialPrefix = providerDialPrefix;
158 public String getProviderMailPattern () {
159 return this.providerMailPattern;
163 public void setProviderMailPattern (final String providerMailPattern) {
164 this.providerMailPattern = providerMailPattern;
168 public String getProviderName () {
169 return this.providerName;
173 public void setProviderName (final String providerName) {
174 this.providerName = providerName;
178 * Checks whether if the given mobile provider is already created by
179 * checking both dial prefix and country.
181 * @param mobileProvider Mobile provider instance to check
183 * @return Whether the given mobile provider instance is found
185 private boolean isMobileProviderCreated (final MobileProvider mobileProvider) {
186 // Default is not found
187 boolean isFound = false;
189 // Get list of all providers
190 List<MobileProvider> providers = this.mobileController.allMobileProvider();
192 // Get iterator from it
193 Iterator<MobileProvider> iterator = providers.iterator();
196 while (iterator.hasNext()) {
198 MobileProvider next = iterator.next();
200 // Is the provider's dial prefix and country the same?
201 if ((Objects.equals(mobileProvider.getProviderDialPrefix(), next.getProviderDialPrefix())) && (Objects.equals(mobileProvider.getProviderCountry(), next.getProviderCountry()))) {
202 // Is the same, abort loop