2 * Copyright (C) 2016 - 2020 Free Software Foundation
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;
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 org.mxchange.addressbook.beans.BaseAddressbookBean;
30 import org.mxchange.jcountry.model.data.Country;
31 import org.mxchange.jphone.events.mobileprovider.added.AdminAddedMobileProviderEvent;
32 import org.mxchange.jphone.events.mobileprovider.added.AdminMobileProviderAddedEvent;
33 import org.mxchange.jphone.exceptions.MobileProviderAlreadyAddedException;
34 import org.mxchange.jphone.model.phonenumbers.mobileprovider.AdminMobileProviderSessionBeanRemote;
35 import org.mxchange.jphone.model.phonenumbers.mobileprovider.CellphoneProvider;
36 import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
39 * Administrative bean (controller) for mobile provider
41 * @author Roland Häder<roland@mxchange.org>
43 @Named ("adminMobileProviderController")
45 public class AddressbookAdminMobileProviderWebRequestBean extends BaseAddressbookBean implements AddressbookAdminMobileProviderWebRequestController {
50 private static final long serialVersionUID = 184_598_175_371_269_016L;
53 * Remote EJB for mobile providers (administrative)
55 @EJB (lookup = "java:global/addressbook-ejb/adminMobileProvider!org.mxchange.jphone.model.phonenumbers.mobileprovider.AdminMobileProviderSessionBeanRemote")
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 () {
95 // Call super constructor
100 * Adds a mobile provider to database by calling the EJB. A pre-check on
101 * dial-prefix and country combination is done, if found, an exception is
102 * thrown. A redirect should take place after successfull creation.
104 * @return Redirect outcome
106 * @todo Add field validation
108 public String addMobileProvider () {
109 // Create mobile provider instance
110 final MobileProvider mobileProvider = new CellphoneProvider(this.getProviderDialPrefix(), this.getProviderName(), this.getProviderCountry(), this.getProviderMailPattern());
112 // Is the provider already created?
113 if (this.isMobileProviderCreated(mobileProvider)) {
114 // Then throw exception
115 this.showFacesMessage("form_add_mobile_provider:providerDialPrefix", new MobileProviderAlreadyAddedException(mobileProvider)); //NOI18N
120 final MobileProvider updatedProvider;
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 * Getter for country instance ('s dial data)
140 * @return Country instance
142 public Country getProviderCountry () {
143 return this.providerCountry;
147 * Setter for country instance ('s dial data)
149 * @param providerCountry Country instance
151 public void setProviderCountry (final Country providerCountry) {
152 this.providerCountry = providerCountry;
156 * Getter for provider dial prefix
158 * @return Provider dial prefix
160 public Long getProviderDialPrefix () {
161 return this.providerDialPrefix;
165 * Setter for provider dial prefix
167 * @param providerDialPrefix Provider dial prefix
169 public void setProviderDialPrefix (final Long providerDialPrefix) {
170 this.providerDialPrefix = providerDialPrefix;
174 * Getter for pattern for mail gateway
176 * @return Pattern for mail gateway
178 public String getProviderMailPattern () {
179 return this.providerMailPattern;
183 * Setter for pattern for mail gateway
185 * @param providerMailPattern Pattern for mail gateway
187 public void setProviderMailPattern (final String providerMailPattern) {
188 this.providerMailPattern = providerMailPattern;
192 * Getter for provider name
194 * @return Provider name
196 public String getProviderName () {
197 return this.providerName;
201 * Setter for provider name
203 * @param providerName Provider name
205 public void setProviderName (final String providerName) {
206 this.providerName = providerName;
210 * Checks whether if the given mobile provider is already created by
211 * checking both dial prefix and country.
213 * @param mobileProvider Mobile provider instance to check
215 * @return Whether the given mobile provider instance is found
217 private boolean isMobileProviderCreated (final MobileProvider mobileProvider) {
218 // Default is not found
219 boolean isFound = false;
221 // Get list of all providers
222 List<MobileProvider> providers = this.mobileController.allMobileProviders();
224 // Get iterator from it
225 Iterator<MobileProvider> iterator = providers.iterator();
228 while (iterator.hasNext()) {
230 MobileProvider next = iterator.next();
232 // Is the provider's dial prefix and country the same?
233 if ((Objects.equals(mobileProvider.getProviderDialPrefix(), next.getProviderDialPrefix())) && (Objects.equals(mobileProvider.getProviderCountry(), next.getProviderCountry()))) {
234 // Is the same, abort loop