View Javadoc

1   /*
2    * Copyright (C) 2007 Alf Mikula
3    * 
4    * This file is part of PromoteGo.
5    *
6    * PromoteGo is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * PromoteGo is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with PromoteGo.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.promotego.validators;
20  
21  import org.apache.commons.lang.StringUtils;
22  import org.promotego.beans.User;
23  import org.promotego.dao.interfaces.UserDao;
24  import org.promotego.viewbeans.UserInfoBean;
25  import org.springframework.beans.factory.annotation.Required;
26  import org.springframework.validation.Errors;
27  import org.springframework.validation.ValidationUtils;
28  import org.springframework.validation.Validator;
29  
30  public class UserInfoValidator implements Validator
31  {
32  	private UserDao m_userDao;
33  	
34  	public boolean supports(@SuppressWarnings("unchecked") Class clazz)
35      {
36          return UserInfoBean.class.isAssignableFrom(clazz);
37      }
38  
39      public void validate(Object target, Errors errors)
40      {
41          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstName", "required", "Field is required.");
42          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "lastName", "required", "Field is required.");
43          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "emailAddress", "required", "Field is required.");
44          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "username", "required", "Field is required.");
45          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "required", "Field is required.");
46          ValidationUtils.rejectIfEmptyOrWhitespace(errors, "confirmPassword", "required", "Field is required.");
47          
48          UserInfoBean userInfo = (UserInfoBean)target;
49          
50          // Force user name to lowercase.
51          userInfo.setUsername(userInfo.getUsername().toLowerCase());
52          
53          if (!userInfo.getPassword().equals(userInfo.getConfirmPassword()))
54          {
55              errors.rejectValue("password", "mustMatch", "Password entry fields must match");
56              errors.rejectValue("confirmPassword", "mustMatch", "Password entry fields must match");
57          }
58          
59          String emailAddress = StringUtils.strip(userInfo.getEmailAddress());
60          if (emailAddress != null && emailAddress.length() > 0)
61          {
62          	// Verify email address nonexistence
63          	User theUser = m_userDao.getUserByEmailAddress(emailAddress);
64          	if (theUser != null)
65          	{
66          		errors.rejectValue("emailAddress", "emailAddress.exists", "The email address already exists in our system.  Please use a different address.");
67          	}
68          }
69          
70          String username = StringUtils.strip(userInfo.getUsername());
71          if (username != null && username.length() > 0)
72          {
73          	// Verify username nonexistence
74          	User theUser = m_userDao.getUserByUsername(username);
75          	if (theUser != null)
76          	{
77          		errors.rejectValue("username", "username.exists", "The username already exists in our system.  Please use a different username.");
78          	}
79          }
80      }
81  
82      @Required
83  	public void setUserDao(UserDao userDao)
84  	{
85  		m_userDao = userDao;
86  	}
87  }