]> git.mxchange.org Git - addressbook-war.git/blob
ca732f4b3da96d28b740c43d30dc2009af29e8b9
[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.user.email_address;
18
19 import java.text.MessageFormat;
20 import java.util.List;
21 import java.util.Objects;
22 import javax.annotation.PostConstruct;
23 import javax.enterprise.context.SessionScoped;
24 import javax.faces.view.facelets.FaceletException;
25 import javax.inject.Inject;
26 import javax.inject.Named;
27 import javax.naming.Context;
28 import javax.naming.InitialContext;
29 import javax.naming.NamingException;
30 import org.mxchange.addressbook.beans.BaseAddressbookController;
31 import org.mxchange.addressbook.beans.features.AddressbookFeaturesWebApplicationController;
32 import org.mxchange.addressbook.beans.login.user.AddressbookUserLoginWebSessionController;
33 import org.mxchange.jcontacts.contact.Contact;
34 import org.mxchange.jcoreee.utils.FacesUtils;
35 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
36 import org.mxchange.jusercore.model.email_address.ChangeableEmailAddress;
37 import org.mxchange.jusercore.model.email_address.EmailAddressChange;
38 import org.mxchange.jusercore.model.user.email_address.UserEmailChangeSessionBeanRemote;
39 import org.mxchange.jusercore.model.user.User;
40
41 /**
42  * A web session-scoped bean for changing email addresses
43  * <p>
44  * @author Roland Häder<roland@mxchange.org>
45  */
46 @Named ("userEmailChangeController")
47 @SessionScoped
48 public class AddressbookEmailChangeWebSessionBean extends BaseAddressbookController implements AddressbookEmailChangeWebSessionController {
49
50         /**
51          * Serial number
52          */
53         private static final long serialVersionUID = 186_078_724_659_153L;
54
55         /**
56          * Email address 1 (changing)
57          */
58         private String emailAddress;
59
60         /**
61          * Email address 2 (repeat in changing)
62          */
63         private String emailAddressRepeat;
64
65         /**
66          * Local list of already queued email addresses
67          */
68         private List<String> emailAddresses;
69
70         /**
71          * Remote email change bean
72          */
73         private UserEmailChangeSessionBeanRemote emailChangeBean;
74
75         /**
76          * Features controller
77          */
78         @Inject
79         private AddressbookFeaturesWebApplicationController featureController;
80
81         /**
82          * Login controller (bean)
83          */
84         @Inject
85         private AddressbookUserLoginWebSessionController userLoginController;
86
87         /**
88          * Default constructor
89          */
90         public AddressbookEmailChangeWebSessionBean () {
91                 // Call super constructor
92                 super();
93         }
94
95         @Override
96         public String doUserChangeEmailAddress () {
97                 // This method shall only be called if the user is logged-in
98                 if (!this.userLoginController.isUserLoggedIn()) {
99                         // Not logged-in
100                         throw new IllegalStateException("User is not logged-in"); //NOI18N
101                 } else if (!this.featureController.isFeatureEnabled("user_change_email_address")) { //NOI18N
102                         // Editing is not allowed
103                         throw new IllegalStateException("User tried to change email address"); //NOI18N
104                 } else if (!this.isRequiredChangeEmailAddressSet()) {
105                         // Not all required fields are set
106                         throw new FaceletException("Not all required fields are set."); //NOI18N
107                 } else if (!Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat())) {
108                         // Email address 1+2 mismatch
109                         this.showFacesMessage("form_user_change_email_address:emailAddressRepeat", "ERROR_USER_EMAIL_ADDRESSES_MISMATCH"); //NOI18N
110                         return ""; //NOI18N
111                 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
112                         // Password not matching
113                         this.showFacesMessage("form_login_user_change_email_address:currentPassword", new UserPasswordMismatchException(this.userLoginController.getLoggedInUser())); //NOI18N
114                         return ""; //NOI18N
115                 }
116
117                 // Get user instance
118                 User user = this.userLoginController.getLoggedInUser();
119
120                 // It should be there, so run some tests on it
121                 assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N
122                 assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N
123                 assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
124                 assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
125                 assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
126                 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
127
128                 // Check if the email address is already enqueued
129                 if (this.isEmailAddressQueued(this.getEmailAddress())) {
130                         // Clear both email addresses
131                         this.setEmailAddress(null);
132                         this.setEmailAddressRepeat(null);
133
134                         // Yes, then abort here
135                         this.showFacesMessage("form_user_change_email_address:emailAddress", "ERROR_USER_CHANGE_EMAIL_ADDRESS_ALREADY_QUEUED"); //NOI18N
136                         return ""; //NOI18N
137                 }
138
139                 // Create change object, to save EJB calls, the hash is not generated here
140                 ChangeableEmailAddress emailChange = new EmailAddressChange(user, this.getEmailAddress());
141
142                 // Get base URL
143                 String baseUrl = FacesUtils.generateBaseUrl();
144
145                 // Call EJB
146                 this.emailChangeBean.enqueueEmailAddressForChange(emailChange, baseUrl);
147
148                 // Unset all so the user is forced to re-enter it
149                 this.clear();
150
151                 // All fine
152                 return "user_login_email_change_queued"; //NOI18N
153         }
154
155         @Override
156         public String getEmailAddress () {
157                 return this.emailAddress;
158         }
159
160         @Override
161         public void setEmailAddress (final String emailAddress) {
162                 this.emailAddress = emailAddress;
163         }
164
165         @Override
166         public String getEmailAddressRepeat () {
167                 return this.emailAddressRepeat;
168         }
169
170         @Override
171         public void setEmailAddressRepeat (final String emailAddressRepeat) {
172                 this.emailAddressRepeat = emailAddressRepeat;
173         }
174
175         /**
176          * Post-construction
177          */
178         @PostConstruct
179         public void init () {
180                 // Try it
181                 try {
182                         // Get initial context
183                         Context context = new InitialContext();
184
185                         // Try to lookup
186                         this.emailChangeBean = (UserEmailChangeSessionBeanRemote) context.lookup("java:global/addressbook-ejb/userEmailChange!org.mxchange.jusercore.model.email_address.EmailChangeSessionBeanRemote"); //NOI18N
187                 } catch (final NamingException e) {
188                         // Throw again
189                         throw new FaceletException(e);
190                 }
191
192                 // Init list
193                 this.emailAddresses = this.emailChangeBean.allQueuedAddresses();
194         }
195
196         @Override
197         public boolean isRequiredChangeEmailAddressSet () {
198                 return ((this.getEmailAddress() != null) &&
199                                 (this.getEmailAddressRepeat() != null));
200         }
201
202         /**
203          * Clears email address fields so the user has to re-enter them
204          */
205         private void clear () {
206                 // Clear fields
207                 this.setEmailAddress(null);
208                 this.setEmailAddressRepeat(null);
209         }
210
211         /**
212          * Checks if given email address has already been queued. First a local list
213          * is being checked, if not found, the EJB is called. Only if found, the
214          * result is "cached" in the list.
215          * <p>
216          * @param emailAddress Email address to verify
217          * <p>
218          * @return Whether the email address in field emailAddress is already queued
219          */
220         private boolean isEmailAddressQueued (final String emailAddress) {
221                 // It should be there
222                 assert (emailAddress != null) : "emailAddress should not be null"; //NOI18N
223                 assert (!emailAddress.trim().isEmpty()) : "emailAddress should not be empty"; //NOI18N
224
225                 // Check list
226                 if (this.emailAddresses.contains(emailAddress)) {
227                         // Okay, found it
228                         return true;
229                 }
230
231                 // Check EJB
232                 boolean isQueued = this.emailChangeBean.isEmailAddressEnqueued(emailAddress);
233
234                 // Is it there?
235                 if (isQueued) {
236                         // Add to list
237                         this.emailAddresses.add(emailAddress);
238                 }
239
240                 // Return status
241                 return isQueued;
242         }
243
244 }