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.jfinancials.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.jcountry.model.data.Country;
30 import org.mxchange.jfinancials.beans.BaseFinancialsBean;
31 import org.mxchange.jphone.events.mobileprovider.added.AdminAddedMobileProviderEvent;
32 import org.mxchange.jphone.events.mobileprovider.added.AdminMobileProviderAddedEvent;
33 import org.mxchange.jphone.exceptions.mobileprovider.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 FinancialsAdminMobileProviderWebRequestBean extends BaseFinancialsBean implements FinancialsAdminMobileProviderWebRequestController {
50 private static final long serialVersionUID = 184_598_175_371_269_016L;
53 * Remote EJB for mobile providers (administrative)
55 @EJB (lookup = "java:global/jfinancials-ejb/adminMobileProvider!org.mxchange.jphone.model.phonenumbers.mobileprovider.AdminMobileProviderSessionBeanRemote")
56 private AdminMobileProviderSessionBeanRemote adminRemoteBean;
62 private FinancialsMobileProviderWebRequestController 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 FinancialsAdminMobileProviderWebRequestBean () {
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 * @todo Add field validation
106 public void addMobileProvider () {
107 // Create mobile provider instance
108 final MobileProvider mobileProvider = new CellphoneProvider(this.getProviderDialPrefix(), this.getProviderName(), this.getProviderCountry(), this.getProviderMailPattern());
110 // Is the provider already created?
111 if (this.isMobileProviderCreated(mobileProvider)) {
112 // Then throw exception
113 this.showFacesMessage("form_add_mobile_provider:providerDialPrefix", new MobileProviderAlreadyAddedException(mobileProvider)); //NOI18N
118 final MobileProvider updatedProvider;
121 // Call remote EJB and get back an updated instance
122 updatedProvider = this.adminRemoteBean.addMobileProvider(mobileProvider);
123 } catch (final MobileProviderAlreadyAddedException ex) {
125 throw new FaceletException(ex);
129 this.providerAddedEvent.fire(new AdminMobileProviderAddedEvent(updatedProvider));
133 * Getter for country instance ('s dial data)
135 * @return Country instance
137 public Country getProviderCountry () {
138 return this.providerCountry;
142 * Setter for country instance ('s dial data)
144 * @param providerCountry Country instance
146 public void setProviderCountry (final Country providerCountry) {
147 this.providerCountry = providerCountry;
151 * Getter for provider dial prefix
153 * @return Provider dial prefix
155 public Long getProviderDialPrefix () {
156 return this.providerDialPrefix;
160 * Setter for provider dial prefix
162 * @param providerDialPrefix Provider dial prefix
164 public void setProviderDialPrefix (final Long providerDialPrefix) {
165 this.providerDialPrefix = providerDialPrefix;
169 * Getter for pattern for mail gateway
171 * @return Pattern for mail gateway
173 public String getProviderMailPattern () {
174 return this.providerMailPattern;
178 * Setter for pattern for mail gateway
180 * @param providerMailPattern Pattern for mail gateway
182 public void setProviderMailPattern (final String providerMailPattern) {
183 this.providerMailPattern = providerMailPattern;
187 * Getter for provider name
189 * @return Provider name
191 public String getProviderName () {
192 return this.providerName;
196 * Setter for provider name
198 * @param providerName Provider name
200 public void setProviderName (final String providerName) {
201 this.providerName = providerName;
205 * Checks whether if the given mobile provider is already created by
206 * checking both dial prefix and country.
208 * @param mobileProvider Mobile provider instance to check
210 * @return Whether the given mobile provider instance is found
212 private boolean isMobileProviderCreated (final MobileProvider mobileProvider) {
213 // Default is not found
214 boolean isFound = false;
216 // Get list of all providers
217 List<MobileProvider> providers = this.mobileController.allMobileProviders();
219 // Get iterator from it
220 Iterator<MobileProvider> iterator = providers.iterator();
223 while (iterator.hasNext()) {
225 MobileProvider next = iterator.next();
227 // Is the provider's dial prefix and country the same?
228 if ((Objects.equals(mobileProvider.getProviderDialPrefix(), next.getProviderDialPrefix())) && (Objects.equals(mobileProvider.getProviderCountry(), next.getProviderCountry()))) {
229 // Is the same, abort loop