1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package net.sf.pmr.core.domain.user;
36
37 import org.apache.commons.validator.EmailValidator;
38
39 import net.sf.pmr.core.CoreObjectFactory;
40 import net.sf.pmr.keopsframework.domain.validation.Errors;
41 import net.sf.pmr.keopsframework.domain.validation.Validator;
42
43
44 /***
45 * @author Arnaud Prost (arnaud.prost@gmail.com)
46 *
47 * Validate user
48 */
49 public class UserValidatorImpl implements Validator {
50
51 private UserRepository userRepository;
52
53 /***
54 * @param userRepository
55 */
56 public UserValidatorImpl(final UserRepository userRepository) {
57 super();
58 this.userRepository = userRepository;
59 }
60
61 /***
62 * Validation de l'object métier User.<br>
63 * Les règles sont les suivantes:
64 * <ul>
65 * <li>le login est obligatoire</li>
66 * <li>le login est unique</li>
67 * <li>le mot de passse est obligatoire</li>
68 * <li>le nom est obligatoire</li>
69 * <li>le prénom est obligatoire</li>
70 * <li>l'email est obligatoire </li>
71 * <li>Le login doit avoir entre 5 et 10 caratères.</li>
72 * <li>Le mot de passe doit avoir entre 5 et 10 caractères.</li>
73 * <li>Le format de l'adresse email doit être valide</li>
74 * </ul>
75 * @param object user à valider
76 */
77 public Errors validate(final Object object) {
78
79 User user = (User) object;
80
81 Errors errors = CoreObjectFactory.getErrors();
82
83
84 if (user.getLogin() == null
85 || user.getLogin().trim() == "") {
86
87 errors.rejectValue("login", "user.loginMandatory");
88
89 } else if (user.getLogin().length() < 5 || user.getLogin().length() > 10) {
90
91
92 errors.rejectValue("login", "user.loginIncorrectSize");
93
94 } else if (user.getPersistanceId() == 0 && userRepository.findUserByLogin(user.getLogin()) != null ) {
95
96
97 errors.rejectValue("login", "user.loginAlreadyExists");
98
99 }
100
101 if (user.getPassword() == null
102 || user.getPassword().trim() == "") {
103
104 errors.rejectValue("password", "user.passwordMandatory");
105
106
107 } else if (user.getPassword().length() < 5 || user.getPassword().length() > 10) {
108
109
110 errors.rejectValue("password", "user.passwordIncorrectSize");
111 }
112
113 if (user.getLastName() == null
114 || user.getLastName().trim() == "") {
115
116 errors.rejectValue("lastName", "user.lastNameMandatory");
117
118 }
119
120 if (user.getFirstName() == null
121 || user.getFirstName().trim() == "") {
122
123 errors.rejectValue("firstName", "user.firstNameMandatory");
124
125 }
126
127 if (user.getEmail() == null
128 || user.getEmail().trim() == "") {
129
130 errors.rejectValue("email", "user.emailMandatory");
131
132 } else if (!EmailValidator.getInstance().isValid(user.getEmail())) {
133
134
135
136 errors.rejectValue("email", "user.incorrectEmail");
137
138 }
139
140
141 return errors;
142
143 }
144
145 }