1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
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 }