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.pizzaapplication.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.jphone.events.mobileprovider.added.AdminAddedMobileProviderEvent;
31 import org.mxchange.jphone.events.mobileprovider.added.AdminMobileProviderAddedEvent;
32 import org.mxchange.jphone.exceptions.MobileProviderAlreadyAddedException;
33 import org.mxchange.jphone.model.phonenumbers.mobileprovider.AdminMobileProviderSessionBeanRemote;
34 import org.mxchange.jphone.model.phonenumbers.mobileprovider.CellphoneProvider;
35 import org.mxchange.jphone.model.phonenumbers.mobileprovider.MobileProvider;
36 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
39 * Administrative bean (controller) for mobile provider
41 * @author Roland Häder<roland@mxchange.org>
43 @Named ("adminMobileProviderController")
45 public class PizzaAdminMobileProviderWebRequestBean extends BasePizzaBean implements PizzaAdminMobileProviderWebRequestController {
50 private static final long serialVersionUID = 184_598_175_371_269_016L;
53 * Remote EJB for mobile providers (administrative)
55 @EJB (lookup = "java:global/pizzaservice-ejb/adminMobileProvider!org.mxchange.jphone.model.phonenumbers.mobileprovider.AdminMobileProviderSessionBeanRemote")
56 private AdminMobileProviderSessionBeanRemote adminRemoteBean;
62 private PizzaMobileProviderWebRequestController 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 PizzaAdminMobileProviderWebRequestBean () {
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));
137 return "admin_list_mobile_provider"; //NOI18N
141 * Getter for country instance ('s dial data)
143 * @return Country instance
145 public Country getProviderCountry () {
146 return this.providerCountry;
150 * Setter for country instance ('s dial data)
152 * @param providerCountry Country instance
154 public void setProviderCountry (final Country providerCountry) {
155 this.providerCountry = providerCountry;
159 * Getter for provider dial prefix
161 * @return Provider dial prefix
163 public Long getProviderDialPrefix () {
164 return this.providerDialPrefix;
168 * Setter for provider dial prefix
170 * @param providerDialPrefix Provider dial prefix
172 public void setProviderDialPrefix (final Long providerDialPrefix) {
173 this.providerDialPrefix = providerDialPrefix;
177 * Getter for pattern for mail gateway
179 * @return Pattern for mail gateway
181 public String getProviderMailPattern () {
182 return this.providerMailPattern;
186 * Setter for pattern for mail gateway
188 * @param providerMailPattern Pattern for mail gateway
190 public void setProviderMailPattern (final String providerMailPattern) {
191 this.providerMailPattern = providerMailPattern;
195 * Getter for provider name
197 * @return Provider name
199 public String getProviderName () {
200 return this.providerName;
204 * Setter for provider name
206 * @param providerName Provider name
208 public void setProviderName (final String providerName) {
209 this.providerName = providerName;
215 private void clear () {
217 this.setProviderCountry(null);
218 this.setProviderDialPrefix(null);
219 this.setProviderMailPattern(null);
220 this.setProviderName(null);
224 * Checks whether if the given mobile provider is already created by
225 * checking both dial prefix and country.
227 * @param mobileProvider Mobile provider instance to check
229 * @return Whether the given mobile provider instance is found
231 private boolean isMobileProviderCreated (final MobileProvider mobileProvider) {
232 // Default is not found
233 boolean isFound = false;
235 // Get list of all providers
236 List<MobileProvider> providers = this.mobileController.allMobileProviders();
238 // Get iterator from it
239 Iterator<MobileProvider> iterator = providers.iterator();
242 while (iterator.hasNext()) {
244 MobileProvider next = iterator.next();
246 // Is the provider's dial prefix and country the same?
247 if ((Objects.equals(mobileProvider.getProviderDialPrefix(), next.getProviderDialPrefix())) && (Objects.equals(mobileProvider.getProviderCountry(), next.getProviderCountry()))) {
248 // Is the same, abort loop