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.addressbook.beans.BaseAddressbookController;
32 import org.mxchange.jcountry.data.Country;
33 import org.mxchange.jphone.events.AdminAddedMobileProviderEvent;
34 import org.mxchange.jphone.events.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;
41 * Administrative bean (controller) for mobile provider
43 * @author Roland Haeder<roland@mxchange.org>
45 @Named ("adminMobileProviderController")
47 public class AddressbookAdminMobileProviderWebRequestBean extends BaseAddressbookController implements AddressbookAdminMobileProviderWebRequestController {
52 private static final long serialVersionUID = 184_598_175_371_269_016L;
55 * Remote EJB for mobile providers (administrative)
57 private AdminMobileProviderSessionBeanRemote adminRemoteBean;
63 private AddressbookMobileProviderWebRequestController mobileController;
66 * Event being fired when the mobile provider was added
70 private Event<AdminAddedMobileProviderEvent> providerAddedEvent;
73 * Country instance ('s dial data)
75 private Country providerCountry;
78 * Provider dial prefix (example: 0177 for German E+)
80 private Long providerDialPrefix;
83 * Pattern for mail gateway
85 private String providerMailPattern;
88 * Name of the provider
90 private String providerName;
95 public AddressbookAdminMobileProviderWebRequestBean () {
98 // Get initial context
99 Context context = new InitialContext();
101 // Try to lookup the beans
102 this.adminRemoteBean = (AdminMobileProviderSessionBeanRemote) context.lookup("java:global/addressbook-ejb/adminmobileprovider!org.mxchange.jphone.phonenumbers.mobileprovider.AdminMobileProviderSessionBeanRemote"); //NOI18N
103 } catch (final NamingException e) {
105 throw new FaceletException(e);
110 public String addMobileProvider () {
111 // Create mobile provider instance
112 MobileProvider mobileProvider = new CellphoneProvider(this.getProviderDialPrefix(), this.getProviderName(), this.getProviderCountry(), this.getProviderMailPattern());
114 // Is the provider already created?
115 if (this.isMobileProviderCreated(mobileProvider)) {
116 // Then throw exception
117 this.showFacesMessage("add_mobile_provider_form:providerDialPrefix", new MobileProviderAlreadyAddedException(mobileProvider)); //NOI18N
122 MobileProvider updatedProvider = null;
125 // Call remote EJB and get back an updated instance
126 updatedProvider = this.adminRemoteBean.addMobileProvider(mobileProvider);
127 } catch (final MobileProviderAlreadyAddedException ex) {
129 throw new FaceletException(ex);
133 this.providerAddedEvent.fire(new AdminMobileProviderAddedEvent(updatedProvider));
136 return "admin_list_mobile_provider"; //NOI18N
140 public Country getProviderCountry () {
141 return this.providerCountry;
145 public void setProviderCountry (final Country providerCountry) {
146 this.providerCountry = providerCountry;
150 public Long getProviderDialPrefix () {
151 return this.providerDialPrefix;
155 public void setProviderDialPrefix (final Long providerDialPrefix) {
156 this.providerDialPrefix = providerDialPrefix;
160 public String getProviderMailPattern () {
161 return this.providerMailPattern;
165 public void setProviderMailPattern (final String providerMailPattern) {
166 this.providerMailPattern = providerMailPattern;
170 public String getProviderName () {
171 return this.providerName;
175 public void setProviderName (final String providerName) {
176 this.providerName = providerName;
180 * Checks whether if the given mobile provider is already created by
181 * checking both dial prefix and country.
183 * @param mobileProvider Mobile provider instance to check
185 * @return Whether the given mobile provider instance is found
187 private boolean isMobileProviderCreated (final MobileProvider mobileProvider) {
188 // Default is not found
189 boolean isFound = false;
191 // Get list of all providers
192 List<MobileProvider> providers = this.mobileController.allMobileProvider();
194 // Get iterator from it
195 Iterator<MobileProvider> iterator = providers.iterator();
198 while (iterator.hasNext()) {
200 MobileProvider next = iterator.next();
202 // Is the provider's dial prefix and country the same?
203 if ((Objects.equals(mobileProvider.getProviderDialPrefix(), next.getProviderDialPrefix())) && (Objects.equals(mobileProvider.getProviderCountry(), next.getProviderCountry()))) {
204 // Is the same, abort loop