2 * Copyright (C) 2016, 2017 Roland Häder
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.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;
42 * Administrative bean (controller) for mobile provider
44 * @author Roland Häder<roland@mxchange.org>
46 @Named ("adminMobileProviderController")
48 public class AddressbookAdminMobileProviderWebRequestBean extends BaseAddressbookController implements AddressbookAdminMobileProviderWebRequestController {
53 private static final long serialVersionUID = 184_598_175_371_269_016L;
56 * Remote EJB for mobile providers (administrative)
58 private AdminMobileProviderSessionBeanRemote adminRemoteBean;
64 private AddressbookMobileProviderWebRequestController mobileController;
67 * Event being fired when the mobile provider was added
71 private Event<AdminAddedMobileProviderEvent> providerAddedEvent;
74 * Country instance ('s dial data)
76 private Country providerCountry;
79 * Provider dial prefix (example: 0177 for German E+)
81 private Long providerDialPrefix;
84 * Pattern for mail gateway
86 private String providerMailPattern;
89 * Name of the provider
91 private String providerName;
96 public AddressbookAdminMobileProviderWebRequestBean () {
97 // Call super constructor
102 * Adds a mobile provider to database by calling the EJB. A pre-check on
103 * dial-prefix and country combination is done, if found, an exception is
104 * thrown. A redirect should take place after successfull creation.
106 * @return Redirect outcome
108 * @todo Add field validation
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("form_add_mobile_provider: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 * Getter for country instance ('s dial data)
142 * @return Country instance
144 public Country getProviderCountry () {
145 return this.providerCountry;
149 * Setter for country instance ('s dial data)
151 * @param providerCountry Country instance
153 public void setProviderCountry (final Country providerCountry) {
154 this.providerCountry = providerCountry;
158 * Getter for provider dial prefix
160 * @return Provider dial prefix
162 public Long getProviderDialPrefix () {
163 return this.providerDialPrefix;
167 * Setter for provider dial prefix
169 * @param providerDialPrefix Provider dial prefix
171 public void setProviderDialPrefix (final Long providerDialPrefix) {
172 this.providerDialPrefix = providerDialPrefix;
176 * Getter for pattern for mail gateway
178 * @return Pattern for mail gateway
180 public String getProviderMailPattern () {
181 return this.providerMailPattern;
185 * Setter for pattern for mail gateway
187 * @param providerMailPattern Pattern for mail gateway
189 public void setProviderMailPattern (final String providerMailPattern) {
190 this.providerMailPattern = providerMailPattern;
194 * Getter for provider name
196 * @return Provider name
198 public String getProviderName () {
199 return this.providerName;
203 * Setter for provider name
205 * @param providerName Provider name
207 public void setProviderName (final String providerName) {
208 this.providerName = providerName;
212 * Post-construction method
215 public void init () {
218 // Get initial context
219 Context context = new InitialContext();
221 // Try to lookup the beans
222 this.adminRemoteBean = (AdminMobileProviderSessionBeanRemote) context.lookup("java:global/addressbook-ejb/adminMobileProvider!org.mxchange.jphone.phonenumbers.mobileprovider.AdminMobileProviderSessionBeanRemote"); //NOI18N
223 } catch (final NamingException e) {
225 throw new FaceletException(e);
230 * Checks whether if the given mobile provider is already created by
231 * checking both dial prefix and country.
233 * @param mobileProvider Mobile provider instance to check
235 * @return Whether the given mobile provider instance is found
237 private boolean isMobileProviderCreated (final MobileProvider mobileProvider) {
238 // Default is not found
239 boolean isFound = false;
241 // Get list of all providers
242 List<MobileProvider> providers = this.mobileController.allMobileProviders();
244 // Get iterator from it
245 Iterator<MobileProvider> iterator = providers.iterator();
248 while (iterator.hasNext()) {
250 MobileProvider next = iterator.next();
252 // Is the provider's dial prefix and country the same?
253 if ((Objects.equals(mobileProvider.getProviderDialPrefix(), next.getProviderDialPrefix())) && (Objects.equals(mobileProvider.getProviderCountry(), next.getProviderCountry()))) {
254 // Is the same, abort loop