]> git.mxchange.org Git - addressbook-war.git/commitdiff
Continued:
authorRoland Haeder <roland@mxchange.org>
Sat, 12 Mar 2016 17:09:31 +0000 (18:09 +0100)
committerRoland Haeder <roland@mxchange.org>
Sat, 12 Mar 2016 17:10:47 +0000 (18:10 +0100)
- added local caching of all enqueued email addresses (this can become big)
- updated jar(s)

lib/juser-core.jar
lib/juser-lib.jar
src/java/org/mxchange/addressbook/beans/email_address/EmailChangeWebSessionBean.java [new file with mode: 0644]
src/java/org/mxchange/addressbook/beans/email_address/EmailChangeWebSessionController.java [new file with mode: 0644]
src/java/org/mxchange/addressbook/beans/login/UserLoginWebSessionBean.java
src/java/org/mxchange/addressbook/beans/login/UserLoginWebSessionController.java
src/java/org/mxchange/addressbook/validators/password/UserPasswordValidator.java
web/login/login_change_email_address.xhtml

index 8488d71d29bac0c83be02e3c7e5e3129e0b51f47..8ce84feb27b6c1ae7308aae243d964e112f069d6 100644 (file)
Binary files a/lib/juser-core.jar and b/lib/juser-core.jar differ
index dd11c240493a216c77ed2b313e537ab78f64da1e..d70697aaa2ef33810501b060d2c314cca0e8cf1f 100644 (file)
Binary files a/lib/juser-lib.jar and b/lib/juser-lib.jar differ
diff --git a/src/java/org/mxchange/addressbook/beans/email_address/EmailChangeWebSessionBean.java b/src/java/org/mxchange/addressbook/beans/email_address/EmailChangeWebSessionBean.java
new file mode 100644 (file)
index 0000000..afce883
--- /dev/null
@@ -0,0 +1,199 @@
+/*
+ * Copyright (C) 2016 quix0r
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.addressbook.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 javax.inject.Inject;
+import javax.inject.Named;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import org.mxchange.addressbook.beans.login.UserLoginWebSessionController;
+import org.mxchange.jcontacts.contact.Contact;
+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;
+
+/**
+ * A web session bean for changing email addresses
+ * <p>
+ * @author Roland Haeder<roland@mxchange.org>
+ */
+@Named ("emailChangeController")
+@SessionScoped
+public class EmailChangeWebSessionBean implements EmailChangeWebSessionController {
+
+       /**
+        * Serial number
+        */
+       private static final long serialVersionUID = 186_078_724_659_153L;
+
+       /**
+        * Email address 1 (changing)
+        */
+       private String emailAddress1;
+
+       /**
+        * Email address 2 (repeat in changing)
+        */
+       private String emailAddress2;
+
+       /**
+        * Local list of already queued email addresses
+        */
+       private List<String> emailAddresses;
+
+       /**
+        * Remote email change bean
+        */
+       private final EmailChangeSessionBeanRemote emailBean;
+
+       /**
+        * Login bean (controller)
+        */
+       @Inject
+       private UserLoginWebSessionController loginController;
+
+       /**
+        * Default constructor
+        */
+       public EmailChangeWebSessionBean () {
+               // Try it
+               try {
+                       // Get initial context
+                       Context context = new InitialContext();
+
+                       // 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);
+               }
+       }
+
+       @Override
+       public String doChangeEmailAddress () {
+               // This method shall only be called if the user is logged-in
+               if (!this.loginController.isUserLoggedIn()) {
+                       // Not logged-in
+                       throw new IllegalStateException("User is not logged-in"); //NOI18N
+               } else if (!this.isRequiredChangeEmailAddressSet()) {
+                       // Not all required fields are set
+                       throw new FaceletException("Not all required fields are set."); //NOI18N
+               } else if (!Objects.equals(this.getEmailAddress1(), this.getEmailAddress2())) {
+                       // Email address 1+2 mismatch
+                       throw new FaceletException("Email address 1/2 are mismatching."); //NOI18N
+               } else if (!this.loginController.ifCurrentPasswordMatches()) {
+                       // Password not matching
+                       throw new FaceletException(new UserPasswordMismatchException(this.loginController.getLoggedInUser()));
+               }
+
+               // Get user instance
+               User user = this.loginController.getLoggedInUser();
+
+               // It should be there, so run some tests on it
+               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
+               }
+
+               // Create change object
+               ChangeableEmailAddress emailAddress = new EmailAddressChange(user, this.getEmailAddress1(), new GregorianCalendar());
+
+               // Call EJB
+               this.emailBean.enqueueEmailAddressForChange(emailAddress);
+
+               // All fine
+               return "login_email_change_queued"; //NOI18N
+       }
+
+       @Override
+       public String getEmailAddress1 () {
+               return this.emailAddress1;
+       }
+
+       @Override
+       public void setEmailAddress1 (final String emailAddress1) {
+               this.emailAddress1 = emailAddress1;
+       }
+
+       @Override
+       public String getEmailAddress2 () {
+               return this.emailAddress2;
+       }
+
+       @Override
+       public void setEmailAddress2 (final String emailAddress2) {
+               this.emailAddress2 = emailAddress2;
+       }
+
+       @Override
+       public boolean isRequiredChangeEmailAddressSet () {
+               return ((this.getEmailAddress1() != null) &&
+                               (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;
+       }
+
+}
diff --git a/src/java/org/mxchange/addressbook/beans/email_address/EmailChangeWebSessionController.java b/src/java/org/mxchange/addressbook/beans/email_address/EmailChangeWebSessionController.java
new file mode 100644 (file)
index 0000000..92af092
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2016 quix0r
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+package org.mxchange.addressbook.beans.email_address;
+
+import java.io.Serializable;
+
+/**
+ * An interface for an email change controller
+ * <p>
+ * @author Roland Haeder<roland@mxchange.org>
+ */
+public interface EmailChangeWebSessionController extends Serializable {
+
+       /**
+        * Getter for email address 1 (changing)
+        * <p>
+        * @return Email address
+        */
+       String getEmailAddress1 ();
+
+       /**
+        * Setter for email address 1 (changing)
+        * <p>
+        * @param emailAddress1 Email address 1
+        */
+       void setEmailAddress1 (final String emailAddress1);
+
+       /**
+        * Getter for email address 2 (repeat changing)
+        * <p>
+        * @return Email address 2
+        */
+       String getEmailAddress2 ();
+
+       /**
+        * Setter for email address 2 (repeat changing)
+        * <p>
+        * @param emailAddress2 Email address 2
+        */
+       void setEmailAddress2 (final String emailAddress2);
+
+       /**
+        * Checks whether all required are set for changing email address
+        * <p>
+        * @return Whether the required personal data is set
+        */
+       boolean isRequiredChangeEmailAddressSet ();
+
+       /**
+        * Changes logged-in user's email address if the current password matches.
+        * <p>
+        * @return New target page
+        */
+       String doChangeEmailAddress ();
+
+}
index 00ff14bc18e819c05dac9430f73c0701cec4a31a..1b2055a89da129e8a83bec490858bee8a6180545 100644 (file)
@@ -37,6 +37,7 @@ import org.mxchange.jusercore.exceptions.UserStatusLockedException;
 import org.mxchange.jusercore.exceptions.UserStatusUnconfirmedException;
 import org.mxchange.jusercore.model.login.UserLoginSessionBeanRemote;
 import org.mxchange.jusercore.model.user.User;
+import org.mxchange.jusercore.model.user.UserUtils;
 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
 import org.mxchange.jusercore.model.user.status.UserAccountStatus;
 
@@ -140,7 +141,7 @@ public class UserLoginWebSessionBean implements UserLoginWebSessionController {
 
        @Override
        public String getCurrentPassword () {
-               return currentPassword;
+               return this.currentPassword;
        }
 
        @Override
@@ -168,6 +169,24 @@ public class UserLoginWebSessionBean implements UserLoginWebSessionController {
                this.templateType = templateType;
        }
 
+       @Override
+       public boolean ifCurrentPasswordMatches () {
+               // The current password must be set and not empty
+               if (this.getCurrentPassword() == null) {
+                       // Is not set
+                       throw new NullPointerException("this.currentPassword is null"); //NOI18N
+               } else if (this.getCurrentPassword().isEmpty()) {
+                       // Is set empty
+                       throw new IllegalStateException("this.currentPassword is empty."); //NOI18N
+               }
+
+               // Create "container"
+               LoginContainer container = new UserLoginContainer(this.getLoggedInUser(), this.getCurrentPassword());
+
+               // Now check if it matches
+               return UserUtils.ifPasswordMatches(container, this.getLoggedInUser());
+       }
+
        @Override
        public boolean isGuest () {
                return (!this.isUserLoggedIn());
index e48465541a782b8b2a2784119f78317d14367057..2d7da58146180ecabea04ddcc02de3e7e5093130 100644 (file)
@@ -95,4 +95,12 @@ public interface UserLoginWebSessionController extends Serializable {
         * @return Current password
         */
        String getCurrentPassword ();
+
+       /**
+        * Checks whether the (previously entered) current password matches with from
+        * the user instance.
+        * <p>
+        * @return If current password matches
+        */
+       boolean ifCurrentPasswordMatches ();
 }
index 89d2828d94188501c603aaea3965c00d94452ac7..cece958d74e69ae03f5ad5a64c1c0b7d3a5ca7c4 100644 (file)
@@ -54,10 +54,10 @@ public class UserPasswordValidator extends BaseStringValidator implements Valida
                //this.getLogger().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);
index 33afeae1d45aa3e5e7de67fbb9bddbb6cbceb3dc..adc9608f0fd3e5238da2c792d3e6ff6b178c164b 100644 (file)
@@ -43,7 +43,7 @@
                                                                        </div>
 
                                                                        <div class="table_right">
-                                                                               <h:inputText class="input" id="emailAddress1" size="20" maxlength="255" value="#{userController.emailAddress1}" required="true" />
+                                                                               <h:inputText class="input" id="emailAddress1" size="20" maxlength="255" value="#{emailChangeController.emailAddress1}" required="true" />
                                                                        </div>
 
                                                                        <div class="clear"></div>
@@ -55,7 +55,7 @@
                                                                        </div>
 
                                                                        <div class="table_right">
-                                                                               <h:inputText class="input" id="emailAddress2" size="20" maxlength="255" value="#{userController.emailAddress2}" required="true" />
+                                                                               <h:inputText class="input" id="emailAddress2" size="20" maxlength="255" value="#{emailChangeController.emailAddress2}" required="true" />
                                                                        </div>
 
                                                                        <div class="clear"></div>
@@ -67,7 +67,7 @@
 
                                                <div class="table_footer">
                                                        <h:commandButton class="reset" type="reset" value="#{msg.BUTTON_RESET_FORM}" />
-                                                       <h:commandButton class="submit" type="submit" id="change_email" value="#{msg.BUTTON_CHANGE_EMAIL_ADDRESS}" action="#{userController.doChangeEmailAddress()}" />
+                                                       <h:commandButton class="submit" type="submit" id="change_email" value="#{msg.BUTTON_CHANGE_EMAIL_ADDRESS}" action="#{emailChangeController.doChangeEmailAddress()}" />
                                                </div>
                                        </h:form>
                                </div>