2 * Copyright (C) 2016 quix0r
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (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 General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.addressbook.beans.email_address;
19 import java.text.MessageFormat;
20 import java.util.GregorianCalendar;
21 import java.util.List;
22 import java.util.Objects;
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.login.UserLoginWebSessionController;
31 import org.mxchange.jcontacts.contact.Contact;
32 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
33 import org.mxchange.jusercore.model.email_address.ChangeableEmailAddress;
34 import org.mxchange.jusercore.model.email_address.EmailAddressChange;
35 import org.mxchange.jusercore.model.email_address.EmailChangeSessionBeanRemote;
36 import org.mxchange.jusercore.model.user.User;
39 * A web session bean for changing email addresses
41 * @author Roland Haeder<roland@mxchange.org>
43 @Named ("emailChangeController")
45 public class EmailChangeWebSessionBean implements EmailChangeWebSessionController {
50 private static final long serialVersionUID = 186_078_724_659_153L;
53 * Email address 1 (changing)
55 private String emailAddress1;
58 * Email address 2 (repeat in changing)
60 private String emailAddress2;
63 * Local list of already queued email addresses
65 private List<String> emailAddresses;
68 * Remote email change bean
70 private final EmailChangeSessionBeanRemote emailBean;
73 * Login bean (controller)
76 private UserLoginWebSessionController loginController;
81 public EmailChangeWebSessionBean () {
84 // Get initial context
85 Context context = new InitialContext();
88 this.emailBean = (EmailChangeSessionBeanRemote) context.lookup("ejb/stateless-jjobs-email-change"); //NOI18N
91 this.emailAddresses = this.emailBean.allQueuedAddressesAsList();
92 } catch (final NamingException e) {
94 throw new FaceletException(e);
99 public String doChangeEmailAddress () {
100 // This method shall only be called if the user is logged-in
101 if (!this.loginController.isUserLoggedIn()) {
103 throw new IllegalStateException("User is not logged-in"); //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.getEmailAddress1(), this.getEmailAddress2())) {
108 // Email address 1+2 mismatch
109 throw new FaceletException("Email address 1/2 are mismatching."); //NOI18N
110 } else if (!this.loginController.ifCurrentPasswordMatches()) {
111 // Password not matching
112 throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
116 User user = this.loginController.getLoggedInUser();
118 // It should be there, so run some tests on it
119 assert (user instanceof User) : "Instance loginController.loggedInUser is null"; //NOI18N
120 assert (user.getUserId() instanceof Long) : "Instance loginController.loggedInUser.userId is null"; //NOI18N
121 assert (user.getUserId() > 0) : MessageFormat.format("loginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
122 assert (user.getUserContact() instanceof Contact) : "Instance loginController.loggedInUser.userContact is null"; //NOI18N
123 assert (user.getUserContact().getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null"; //NOI18N
124 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
126 // Get dummy email address
127 String dummyEmail = this.getEmailAddress1();
129 // Unset all so the user is forced to re-enter it
132 // Check if the email address is already enqueued
133 if (this.isEmailAddressQueued(dummyEmail)) {
134 // Yes, then abort here
135 return "login_email_already_added"; //NOI18N
138 // Create change object, to save EJB calls, the hash is not generated here
139 ChangeableEmailAddress emailAddress = new EmailAddressChange(user, dummyEmail, new GregorianCalendar());
142 this.emailBean.enqueueEmailAddressForChange(emailAddress);
145 return "login_email_change_queued"; //NOI18N
149 public String getEmailAddress1 () {
150 return this.emailAddress1;
154 public void setEmailAddress1 (final String emailAddress1) {
155 this.emailAddress1 = emailAddress1;
159 public String getEmailAddress2 () {
160 return this.emailAddress2;
164 public void setEmailAddress2 (final String emailAddress2) {
165 this.emailAddress2 = emailAddress2;
169 public boolean isRequiredChangeEmailAddressSet () {
170 return ((this.getEmailAddress1() != null) &&
171 (this.getEmailAddress2() != null));
175 * Clears email address fields so the user has to re-enter them
177 private void clear () {
179 this.setEmailAddress1(null);
180 this.setEmailAddress2(null);
184 * Checks if given email address has already been queued. First a local list
185 * is being checked, if not found, the EJB is called. Only if found, the
186 * result is "cached" in the list.
188 * @param emailAddress Email address to verify
190 * @return Whether the email address in field emailAddress1 is already queued
192 private boolean isEmailAddressQueued (final String emailAddress) {
193 // It should be there
194 assert (emailAddress != null) : "emailAddress should not be null"; //NOI18N
195 assert (!emailAddress.trim().isEmpty()) : "emailAddress should not be empty"; //NOI18N
198 if (this.emailAddresses.contains(emailAddress)) {
204 boolean isQueued = this.emailBean.isEmailAddressEnqueued(emailAddress);
209 this.emailAddresses.add(emailAddress);