]> git.mxchange.org Git - addressbook-war.git/blob
467625e745f6c19a8d85b89daf9c6a77524c4452
[addressbook-war.git] /
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.Iterator;
20 import java.util.List;
21 import java.util.Objects;
22 import javax.ejb.EJB;
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.BaseAddressbookController;
30 import org.mxchange.jcountry.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.phonenumbers.mobileprovider.AdminMobileProviderSessionBeanRemote;
35 import org.mxchange.jphone.phonenumbers.mobileprovider.CellphoneProvider;
36 import org.mxchange.jphone.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 BaseAddressbookController 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.phonenumbers.mobileprovider.AdminMobileProviderSessionBeanRemote")
56         private AdminMobileProviderSessionBeanRemote adminRemoteBean;
57
58         /**
59          * Regular bean
60          */
61         @Inject
62         private AddressbookMobileProviderWebRequestController mobileController;
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          * @return Redirect outcome
105          * <p>
106          * @todo Add field validation
107          */
108         public String addMobileProvider () {
109                 // Create mobile provider instance
110                 MobileProvider mobileProvider = new CellphoneProvider(this.getProviderDialPrefix(), this.getProviderName(), this.getProviderCountry(), this.getProviderMailPattern());
111
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
116                         return ""; //NOI18N
117                 }
118
119                 // Init variable
120                 MobileProvider updatedProvider = null;
121
122                 try {
123                         // Call remote EJB and get back an updated instance
124                         updatedProvider = this.adminRemoteBean.addMobileProvider(mobileProvider);
125                 } catch (final MobileProviderAlreadyAddedException ex) {
126                         // Throw again
127                         throw new FaceletException(ex);
128                 }
129
130                 // Fire event
131                 this.providerAddedEvent.fire(new AdminMobileProviderAddedEvent(updatedProvider));
132
133                 // Redirect to list
134                 return "admin_list_mobile_provider"; //NOI18N
135         }
136
137         /**
138          * Getter for country instance ('s dial data)
139          * <p>
140          * @return Country instance
141          */
142         public Country getProviderCountry () {
143                 return this.providerCountry;
144         }
145
146         /**
147          * Setter for country instance ('s dial data)
148          * <p>
149          * @param providerCountry Country instance
150          */
151         public void setProviderCountry (final Country providerCountry) {
152                 this.providerCountry = providerCountry;
153         }
154
155         /**
156          * Getter for provider dial prefix
157          * <p>
158          * @return Provider dial prefix
159          */
160         public Long getProviderDialPrefix () {
161                 return this.providerDialPrefix;
162         }
163
164         /**
165          * Setter for provider dial prefix
166          * <p>
167          * @param providerDialPrefix Provider dial prefix
168          */
169         public void setProviderDialPrefix (final Long providerDialPrefix) {
170                 this.providerDialPrefix = providerDialPrefix;
171         }
172
173         /**
174          * Getter for pattern for mail gateway
175          * <p>
176          * @return Pattern for mail gateway
177          */
178         public String getProviderMailPattern () {
179                 return this.providerMailPattern;
180         }
181
182         /**
183          * Setter for pattern for mail gateway
184          * <p>
185          * @param providerMailPattern Pattern for mail gateway
186          */
187         public void setProviderMailPattern (final String providerMailPattern) {
188                 this.providerMailPattern = providerMailPattern;
189         }
190
191         /**
192          * Getter for provider name
193          * <p>
194          * @return Provider name
195          */
196         public String getProviderName () {
197                 return this.providerName;
198         }
199
200         /**
201          * Setter for provider name
202          * <p>
203          * @param providerName Provider name
204          */
205         public void setProviderName (final String providerName) {
206                 this.providerName = providerName;
207         }
208
209         /**
210          * Checks whether if the given mobile provider is already created by
211          * checking both dial prefix and country.
212          * <p>
213          * @param mobileProvider Mobile provider instance to check
214          * <p>
215          * @return Whether the given mobile provider instance is found
216          */
217         private boolean isMobileProviderCreated (final MobileProvider mobileProvider) {
218                 // Default is not found
219                 boolean isFound = false;
220
221                 // Get list of all providers
222                 List<MobileProvider> providers = this.mobileController.allMobileProviders();
223
224                 // Get iterator from it
225                 Iterator<MobileProvider> iterator = providers.iterator();
226
227                 // Loop through all
228                 while (iterator.hasNext()) {
229                         // Get next element
230                         MobileProvider next = iterator.next();
231
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
235                                 isFound = true;
236                                 break;
237                         }
238                 }
239
240                 // Return result
241                 return isFound;
242         }
243
244 }