2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.jjobs.beans.email_address;
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.jcontacts.contact.Contact;
30 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
31 import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
32 import org.mxchange.jusercore.model.email_address.ChangeableEmailAddress;
33 import org.mxchange.jusercore.model.email_address.EmailAddressChange;
34 import org.mxchange.jusercore.model.email_address.EmailChangeSessionBeanRemote;
35 import org.mxchange.jusercore.model.user.User;
38 * A web session bean for changing email addresses
40 * @author Roland Haeder<roland@mxchange.org>
42 @Named ("emailChangeController")
44 public class JobsEmailChangeWebSessionBean implements JobsEmailChangeWebSessionController {
49 private static final long serialVersionUID = 186_078_724_659_153L;
52 * Email address 1 (changing)
54 private String emailAddress;
57 * Email address 2 (repeat in changing)
59 private String emailAddressRepeat;
62 * Local list of already queued email addresses
64 private List<String> emailAddresses;
67 * Remote email change bean
69 private final EmailChangeSessionBeanRemote emailBean;
72 * Login bean (controller)
75 private JobsUserLoginWebSessionController userLoginController;
80 public JobsEmailChangeWebSessionBean () {
83 // Get initial context
84 Context context = new InitialContext();
87 this.emailBean = (EmailChangeSessionBeanRemote) context.lookup("java:global/jjobs-ejb/email-change!org.mxchange.jusercore.model.email_address.EmailChangeSessionBeanRemote"); //NOI18N
90 this.emailAddresses = this.emailBean.allQueuedAddressesAsList();
91 } catch (final NamingException e) {
93 throw new FaceletException(e);
98 public String doChangeEmailAddress () {
99 // This method shall only be called if the user is logged-in
100 if (!this.userLoginController.isUserLoggedIn()) {
102 throw new IllegalStateException("User is not logged-in"); //NOI18N
103 } else if (!this.isRequiredChangeEmailAddressSet()) {
104 // Not all required fields are set
105 throw new FaceletException("Not all required fields are set."); //NOI18N
106 } else if (!Objects.equals(this.getEmailAddress(), this.getEmailAddressRepeat())) {
107 // Email address 1+2 mismatch
108 throw new FaceletException("Email address 1/2 are mismatching."); //NOI18N
109 } else if (!this.userLoginController.ifCurrentPasswordMatches()) {
110 // Password not matching
111 throw new FaceletException(new UserPasswordMismatchException(this.userLoginController.getLoggedInUser()));
115 User user = this.userLoginController.getLoggedInUser();
117 // It should be there, so run some tests on it
118 assert (user instanceof User) : "Instance userLoginController.loggedInUser is null"; //NOI18N
119 assert (user.getUserId() instanceof Long) : "Instance userLoginController.loggedInUser.userId is null"; //NOI18N
120 assert (user.getUserId() > 0) : MessageFormat.format("userLoginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
121 assert (user.getUserContact() instanceof Contact) : "Instance userLoginController.loggedInUser.userContact is null"; //NOI18N
122 assert (user.getUserContact().getContactId() instanceof Long) : "Instance userLoginController.userContact.contactId is null"; //NOI18N
123 assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance userLoginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
125 // Get dummy email address
126 String dummyEmail = this.getEmailAddress();
128 // Unset all so the user is forced to re-enter it
131 // Check if the email address is already enqueued
132 if (this.isEmailAddressQueued(dummyEmail)) {
133 // Yes, then abort here
134 return "login_email_already_added"; //NOI18N
137 // Create change object, to save EJB calls, the hash is not generated here
138 ChangeableEmailAddress emailAddress = new EmailAddressChange(user, dummyEmail);
141 this.emailBean.enqueueEmailAddressForChange(emailAddress);
144 return "login_email_change_queued"; //NOI18N
148 public String getEmailAddress () {
149 return this.emailAddress;
153 public void setEmailAddress (final String emailAddress) {
154 this.emailAddress = emailAddress;
158 public String getEmailAddressRepeat () {
159 return this.emailAddressRepeat;
163 public void setEmailAddressRepeat (final String emailAddressRepeat) {
164 this.emailAddressRepeat = emailAddressRepeat;
168 public boolean isRequiredChangeEmailAddressSet () {
169 return ((this.getEmailAddress() != null) &&
170 (this.getEmailAddressRepeat() != null));
174 * Clears email address fields so the user has to re-enter them
176 private void clear () {
178 this.setEmailAddress(null);
179 this.setEmailAddressRepeat(null);
183 * Checks if given email address has already been queued. First a local list
184 * is being checked, if not found, the EJB is called. Only if found, the
185 * result is "cached" in the list.
187 * @param emailAddress Email address to verify
189 * @return Whether the email address in field emailAddress is already queued
191 private boolean isEmailAddressQueued (final String emailAddress) {
192 // It should be there
193 assert (emailAddress != null) : "emailAddress should not be null"; //NOI18N
194 assert (!emailAddress.trim().isEmpty()) : "emailAddress should not be empty"; //NOI18N
197 if (this.emailAddresses.contains(emailAddress)) {
203 boolean isQueued = this.emailBean.isEmailAddressEnqueued(emailAddress);
208 this.emailAddresses.add(emailAddress);