View Javadoc

1   /*
2   * Copyright 2005 Arnaud Prost
3   * 
4   * Arnaud.prost@gmail.com
5   * 
6   * This software is a computer program whose purpose is to ease the 
7   * management of software project.
8   * 
9   * This software is governed by the CeCILL  license under French law and
10  * abiding by the rules of distribution of free software.  You can  use, 
11  * modify and/ or redistribute the software under the terms of the CeCILL
12  * license as circulated by CEA, CNRS and INRIA at the following URL
13  * "http://www.cecill.info". 
14  * 
15  * As a counterpart to the access to the source code and  rights to copy,
16  * modify and redistribute granted by the license, users are provided only
17  * with a limited warranty  and the software's author,  the holder of the
18  * economic rights,  and the successive licensors  have only  limited
19  * liability. 
20  * 
21  * In this respect, the user's attention is drawn to the risks associated
22  * with loading,  using,  modifying and/or developing or reproducing the
23  * software by the user in light of its specific status of free software,
24  * that may mean  that it is complicated to manipulate,  and  that  also
25  * therefore means  that it is reserved for developers  and  experienced
26  * professionals having in-depth computer knowledge. Users are therefore
27  * encouraged to load and test the software's suitability as regards their
28  * requirements in conditions enabling the security of their systems and/or 
29  * data to be ensured and,  more generally, to use and operate it in the 
30  * same conditions as regards security. 
31  * 
32  * The fact that you are presently reading this means that you have had
33  * knowledge of the CeCILL license and that you accept its terms.
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              // le login doit avoir entre 5 et 10 caractères
92              errors.rejectValue("login", "user.loginIncorrectSize");
93              
94          } else if (user.getPersistanceId() == 0 && userRepository.findUserByLogin(user.getLogin()) != null ) {
95              
96              // le login doit être unique
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             // le password doit avoir entre 5 et 10 caractères
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             //Le format de l'adresse email doit être valide
135             
136             errors.rejectValue("email", "user.incorrectEmail");
137             
138         }
139 
140          
141         return errors;
142         
143     }
144 
145 }