]> git.mxchange.org Git - addressbook-war.git/blob
34864ad222c789c5ea792bc0c762743fea5c4693
[addressbook-war.git] /
1 /*
2  * Copyright (C) 2016 - 2022 Free Software Foundation
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.addressbook.beans.mobileprovider;
18
19 import java.util.Objects;
20 import javax.ejb.EJB;
21 import javax.enterprise.context.RequestScoped;
22 import javax.enterprise.event.Event;
23 import javax.enterprise.inject.Any;
24 import javax.faces.FacesException;
25 import javax.faces.application.FacesMessage;
26 import javax.inject.Inject;
27 import javax.inject.Named;
28 import org.mxchange.addressbook.beans.BaseAddressbookBean;
29 import org.mxchange.addressbook.beans.mobileprovider.list.AddressbookMobileProviderListWebViewController;
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.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;
37
38 /**
39  * Administrative bean (controller) for mobile provider
40  * <p>
41  * @author Roland Häder<roland@mxchange.org>
42  */
43 @Named ("adminMobileProviderController")
44 @RequestScoped
45 public class AddressbookAdminMobileProviderWebRequestBean extends BaseAddressbookBean implements AddressbookAdminMobileProviderWebRequestController {
46
47         /**
48          * Serial number
49          */
50         private static final long serialVersionUID = 184_598_175_371_269_016L;
51
52         /**
53          * Remote EJB for mobile providers (administrative)
54          */
55         @EJB (lookup = "java:global/addressbook-ejb/adminMobileProvider!org.mxchange.jphone.model.phonenumbers.mobileprovider.AdminMobileProviderSessionBeanRemote")
56         private AdminMobileProviderSessionBeanRemote adminRemoteBean;
57
58         /**
59          * Mobile provider list view
60          */
61         @Inject
62         private AddressbookMobileProviderListWebViewController mobileProviderListController;
63
64         /**
65          * Event being fired when the mobile provider was added
66          */
67         @Inject
68         @Any
69         private Event<AdminAddedMobileProviderEvent> providerAddedEvent;
70
71         /**
72          * Country instance ('s dial data)
73          */
74         private Country providerCountry;
75
76         /**
77          * Provider dial prefix (example: 0177 for German E+)
78          */
79         private Long providerDialPrefix;
80
81         /**
82          * Pattern for mail gateway
83          */
84         private String providerMailPattern;
85
86         /**
87          * Name of the provider
88          */
89         private String providerName;
90
91         /**
92          * Default constructor
93          */
94         public AddressbookAdminMobileProviderWebRequestBean () {
95                 // Call super constructor
96                 super();
97         }
98
99         /**
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.
103          * <p>
104          * @todo Add field validation
105          */
106         public void addMobileProvider () {
107                 // Create mobile provider instance
108                 final MobileProvider mobileProvider = new CellphoneProvider(this.getProviderDialPrefix(), this.getProviderName(), this.getProviderCountry(), this.getProviderMailPattern());
109
110                 // Is the provider already created?
111                 if (this.isMobileProviderCreated(mobileProvider)) {
112                         // Then throw exception
113                         this.showFacesException("form_add_mobile_provider:providerDialPrefix", new MobileProviderAlreadyAddedException(mobileProvider), FacesMessage.SEVERITY_WARN); //NOI18N
114                         return;
115                 }
116
117                 // Init variable
118                 final MobileProvider updatedProvider;
119
120                 try {
121                         // Call remote EJB and get back an updated instance
122                         updatedProvider = this.adminRemoteBean.addMobileProvider(mobileProvider);
123                 } catch (final MobileProviderAlreadyAddedException ex) {
124                         // Throw again
125                         throw new FacesException(ex);
126                 }
127
128                 // Fire event
129                 this.providerAddedEvent.fire(new AdminMobileProviderAddedEvent(updatedProvider));
130         }
131
132         /**
133          * Getter for country instance ('s dial data)
134          * <p>
135          * @return Country instance
136          */
137         public Country getProviderCountry () {
138                 return this.providerCountry;
139         }
140
141         /**
142          * Setter for country instance ('s dial data)
143          * <p>
144          * @param providerCountry Country instance
145          */
146         public void setProviderCountry (final Country providerCountry) {
147                 this.providerCountry = providerCountry;
148         }
149
150         /**
151          * Getter for provider dial prefix
152          * <p>
153          * @return Provider dial prefix
154          */
155         public Long getProviderDialPrefix () {
156                 return this.providerDialPrefix;
157         }
158
159         /**
160          * Setter for provider dial prefix
161          * <p>
162          * @param providerDialPrefix Provider dial prefix
163          */
164         public void setProviderDialPrefix (final Long providerDialPrefix) {
165                 this.providerDialPrefix = providerDialPrefix;
166         }
167
168         /**
169          * Getter for pattern for mail gateway
170          * <p>
171          * @return Pattern for mail gateway
172          */
173         public String getProviderMailPattern () {
174                 return this.providerMailPattern;
175         }
176
177         /**
178          * Setter for pattern for mail gateway
179          * <p>
180          * @param providerMailPattern Pattern for mail gateway
181          */
182         public void setProviderMailPattern (final String providerMailPattern) {
183                 this.providerMailPattern = providerMailPattern;
184         }
185
186         /**
187          * Getter for provider name
188          * <p>
189          * @return Provider name
190          */
191         public String getProviderName () {
192                 return this.providerName;
193         }
194
195         /**
196          * Setter for provider name
197          * <p>
198          * @param providerName Provider name
199          */
200         public void setProviderName (final String providerName) {
201                 this.providerName = providerName;
202         }
203
204         /**
205          * Checks whether if the given mobile provider is already created by
206          * checking both dial prefix and country.
207          * <p>
208          * @param mobileProvider Mobile provider instance to check
209          * <p>
210          * @return Whether the given mobile provider instance is found
211          */
212         private boolean isMobileProviderCreated (final MobileProvider mobileProvider) {
213                 // Default is not found
214                 boolean isFound = false;
215
216                 // Loop through all
217                 for (final MobileProvider currentMobileProvider : this.mobileProviderListController.getAllMobileProviders()) {
218                         // Is the provider's dial prefix and country the same?
219                         if (Objects.equals(mobileProvider, currentMobileProvider)) {
220                                 // Is the same, abort loop
221                                 isFound = true;
222                                 break;
223                         }
224                 }
225
226                 // Return result
227                 return isFound;
228         }
229
230 }