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  
36  package net.sf.pmr.core.service;
37  
38  import java.util.List;
39  
40  import net.sf.pmr.core.CoreObjectFactory;
41  import net.sf.pmr.core.domain.user.User;
42  import net.sf.pmr.core.domain.user.UserRepository;
43  import net.sf.pmr.core.domain.user.company.CompanyRepository;
44  import net.sf.pmr.keopsframework.domain.validation.Errors;
45  import net.sf.pmr.keopsframework.domain.validation.Validator;
46  
47  /***
48   * @author Arnaud Prost (arnaud.prost@gmail.com)
49   */
50  public class UserServiceImpl implements UserService {
51  
52      /***
53       * User repository
54       */
55      private UserRepository userRepository;
56  
57      /***
58       * user validator
59       */
60      private Validator userValidator;
61  
62      /***
63       * Company repository
64       */
65      private CompanyRepository companyRepository;
66  
67      /***
68       * company validator
69       */
70      private Validator companyValidator;
71  
72      /***
73       * Constructor
74       * @param userRepository userRepository
75       * @param userValidator userValidator
76       * @param companyRepository companyRepository
77       * @param companyValidator companyValidator
78       */
79      public UserServiceImpl(final UserRepository userRepository,
80              final Validator userValidator, final CompanyRepository companyRepository) {
81          super();
82          this.userRepository = userRepository;
83          this.userValidator = userValidator;
84          this.companyRepository = companyRepository;
85      }
86  
87      /***
88       * @see net.sf.pmr.core.service.UserService#update(int, java.lang.String, java.lang.String, java.lang.String, java.lang.String, java.lang.String, long)
89       */
90      public Errors addOrUpdate(final int id, final String firstName, final String lastName,
91              final String login, final String password, final String email, final long persistanceVersion) {
92  
93          // get a user instance from the factory
94          User user = CoreObjectFactory.getUser();
95  
96          // populate user
97          user.setPersistanceId(id);
98          user.setFirstName(firstName);
99          user.setLastName(lastName);
100         user.setLogin(login);
101         user.setPassword(password);
102         user.setEmail(email);
103         user.setPersistanceVersion(persistanceVersion);
104 
105         // validate user
106         Errors errors = userValidator.validate(user);
107 
108         if (!errors.hasErrors()) {
109             // if no errors are found,
110             // then save user
111             userRepository.addOrUpdate(user);
112         }
113 
114         return errors;
115 
116     }
117 
118     /***
119      * @see net.sf.pmr.core.service.UserService#findById(java.lang.Integer)
120      */
121     public User findById(int id) {
122         return userRepository.findUserById(id);
123     }
124     
125     /***
126      * @see net.sf.pmr.core.service.UserService#findByLogin(java.lang.String)
127      */
128     public User findByLogin(String login) {
129         return userRepository.findUserByLogin(login);
130     }
131 
132     
133     /***
134      * @see net.sf.pmr.core.service.UserService#findAll()
135      */
136     public List findAll() {
137         return userRepository.findAll();
138     }
139     
140     
141     /***
142      * @see net.sf.pmr.core.service.UserService#countAll()
143      */
144     public Integer countAll() {
145         return userRepository.countAll();
146     }
147     
148     /***
149      * @see net.sf.pmr.core.service.UserService#delete(java.lang.Integer)
150      */
151     public Errors delete(Integer id) {
152         // TODO ŕ tester et implémenter
153         return null;
154     }
155 }