package org.mxchange.jjobs.beans.email_address;
import java.text.MessageFormat;
+import java.util.GregorianCalendar;
+import java.util.List;
import java.util.Objects;
import javax.enterprise.context.SessionScoped;
import javax.faces.view.facelets.FaceletException;
import org.mxchange.jcontacts.contact.Contact;
import org.mxchange.jjobs.beans.login.UserLoginWebSessionController;
import org.mxchange.jusercore.exceptions.UserPasswordMismatchException;
+import org.mxchange.jusercore.model.email_address.ChangeableEmailAddress;
+import org.mxchange.jusercore.model.email_address.EmailAddressChange;
import org.mxchange.jusercore.model.email_address.EmailChangeSessionBeanRemote;
import org.mxchange.jusercore.model.user.User;
*/
private String emailAddress2;
+ /**
+ * Local list of already queued email addresses
+ */
+ private List<String> emailAddresses;
+
/**
* Remote email change bean
*/
// Try to lookup
this.emailBean = (EmailChangeSessionBeanRemote) context.lookup("ejb/stateless-jjobs-email-change"); //NOI18N
+
+ // Init list
+ this.emailAddresses = this.emailBean.allQueuedAddressesAsList();
} catch (final NamingException e) {
// Throw again
throw new FaceletException(e);
User user = this.loginController.getLoggedInUser();
// It should be there, so run some tests on it
- assert (user instanceof User) : "Instance loginController.loggedInUser is null";
- assert (user.getUserId() instanceof Long) : "Instance loginController.loggedInUser.userId is null";
- assert (user.getUserId() > 0) : MessageFormat.format("loginController.loggedInUser.userId={0} is invalid", user.getUserId());
- assert (user.getUserContact() instanceof Contact) : "Instance loginController.loggedInUser.userContact is null";
- assert (user.getUserContact().getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null";
- assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId());
+ assert (user instanceof User) : "Instance loginController.loggedInUser is null"; //NOI18N
+ assert (user.getUserId() instanceof Long) : "Instance loginController.loggedInUser.userId is null"; //NOI18N
+ assert (user.getUserId() > 0) : MessageFormat.format("loginController.loggedInUser.userId={0} is invalid", user.getUserId()); //NOI18N
+ assert (user.getUserContact() instanceof Contact) : "Instance loginController.loggedInUser.userContact is null"; //NOI18N
+ assert (user.getUserContact().getContactId() instanceof Long) : "Instance loginController.userContact.contactId is null"; //NOI18N
+ assert (user.getUserContact().getContactId() > 0) : MessageFormat.format("Instance loginController.userContact.contactId={0} is invalid", user.getUserContact().getContactId()); //NOI18N
+
+ // Check if the email address is already enqueued
+ if (this.isEmailAddressQueued()) {
+ // Yes, then abort here
+ return "login_email_already_added"; //NOI18N
+ }
- // Update email address
- user.getUserContact().setContactEmailAddress(this.getEmailAddress1());
+ // Create change object
+ ChangeableEmailAddress emailAddress = new EmailAddressChange(user, this.getEmailAddress1(), new GregorianCalendar());
// Call EJB
- this.emailBean.enqueueEmailAddressForChange(user);
+ this.emailBean.enqueueEmailAddressForChange(emailAddress);
// All fine
return "login_email_change_queued"; //NOI18N
(this.getEmailAddress2() != null));
}
+ /**
+ * Checks if current emailAddress1 has already been queued. First a local
+ * list is being checked, if not found, the EJB is called. Only if found,
+ * the result is "cached" in the list.
+ * <p>
+ * @return Whether the email address in field emailAddress1 is already queued
+ */
+ private boolean isEmailAddressQueued () {
+ // It should be there
+ assert (this.getEmailAddress1() != null) : "emailAddress1 should not be null"; //NOI18N
+ assert (!this.getEmailAddress1().trim().isEmpty()) : "emailAddress1 should not be empty"; //NOI18N
+
+ // Check list
+ if (this.emailAddresses.contains(this.getEmailAddress1())) {
+ // Okay, found it
+ return true;
+ }
+
+ // Check EJB
+ boolean isQueued = this.emailBean.isEmailAddressEnqueued(this.getEmailAddress1());
+
+ // Is it there?
+ if (isQueued) {
+ // Add to list
+ this.emailAddresses.add(this.getEmailAddress1());
+ }
+
+ // Return status
+ return isQueued;
+ }
+
}
this.loggerBeanLocal.logTrace(MessageFormat.format("validate: context={0},component={1},value={2} - CALLED!", context, component, value)); //NOI18N
// The required field
- String[] requiredFileds = {"currentPassword"}; //NOI18N
+ String[] requiredFields = {"currentPassword"}; //NOI18N
// Pre-validation (example: not null, not a string, empty string ...)
- super.preValidate(context, component, value, requiredFileds, false);
+ super.preValidate(context, component, value, requiredFields, false);
// value is known to be an entered password, so instance login container
LoginContainer container = new UserLoginContainer(this.loginController.getLoggedInUser(), (String) value);