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