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.HashSet;
39  import java.util.List;
40  import java.util.Set;
41  
42  import net.sf.pmr.core.CoreObjectFactory;
43  import net.sf.pmr.core.domain.basicProject.BasicProject;
44  import net.sf.pmr.core.domain.basicProject.BasicProjectRepository;
45  import net.sf.pmr.core.domain.user.User;
46  import net.sf.pmr.core.domain.user.UserRepository;
47  import net.sf.pmr.keopsframework.domain.validation.Errors;
48  import net.sf.pmr.keopsframework.domain.validation.Validator;
49  
50  
51  /***
52   * @author Arnaud Prost (arnaud.prost@gmail.com)
53   */
54  public class BasicProjectServiceImpl implements BasicProjectService {
55  
56      /***
57       * basicProject validator
58       */
59      private Validator basicProjectValidator;
60  
61      /***
62       * basicProject repository
63       */
64      private BasicProjectRepository basicProjectRepository;
65  
66      /***
67       * user repository
68       */
69      private UserRepository userRepository;
70  
71      /***
72       * @param basicProjectValidator
73       * @param basicProjectRepository
74       * @param userRepository
75       */
76      public BasicProjectServiceImpl(final Validator basicProjectValidator,
77              final BasicProjectRepository basicProjectRepository,
78              final UserRepository userRepository) {
79          super();
80          this.basicProjectValidator = basicProjectValidator;
81          this.basicProjectRepository = basicProjectRepository;
82          this.userRepository = userRepository;
83      }
84      
85  
86      /***
87       * @see BasicProjectService#add(String, String, int)
88       */
89      public Errors add(final String code, final String name, final int defaultMemberPersistanceId) {
90  
91        // First call for a project instance
92        BasicProject basicProject = CoreObjectFactory.getBasicProject();
93  
94        // populate the instance
95        basicProject.setCode(code);
96        basicProject.setName(name);
97  
98        // then find the default member in the user repository
99        User user = userRepository.findUserById(defaultMemberPersistanceId);
100 
101       // add the user to the set of members
102       Set members = new HashSet();
103       members.add(user);
104       basicProject.setMembers(members);
105 
106       // validation of the basicProject
107       Errors errors = basicProjectValidator.validate(basicProject);
108 
109       if (!errors.hasErrors()) {
110 
111           // if no error, add to repository
112           try {
113               basicProjectRepository.addOrUpdate(basicProject);
114           } catch (Exception exception) {
115               // in case of exception, it put in the error struture
116               errors.reject(exception.getClass().getName());
117           }
118       }
119 
120       return errors;
121 
122     }
123 
124 
125 
126 
127     public Errors update(final int persistanceId, final String code, final String name, final long persistanceVersion) {
128 
129         // First call for a project instance
130         BasicProject basicProject = CoreObjectFactory.getBasicProject();
131 
132         // populate the instance
133         basicProject.setPersistanceId(persistanceId);
134         basicProject.setCode(code);
135         basicProject.setName(name);
136         basicProject.setPersistanceVersion(persistanceVersion);
137 
138         // validation of the basicProject
139         Errors errors = basicProjectValidator.validate(basicProject);
140 
141         if (!errors.hasErrors()) {
142 
143             // if no error, add to repository
144             try {
145                 basicProjectRepository.addOrUpdate(basicProject);
146             } catch (Exception exception) {
147                 // in case of exception, it put in the error struture
148                 errors.reject(exception.getClass().getName());
149             }
150         }
151 
152         return errors;
153     }
154 
155     /***
156      * @see net.sf.pmr.core.service.BasicProjectService#countAll()
157      */
158     public Integer countAll() {
159         return basicProjectRepository.countAll();
160     }
161     
162     
163     
164     
165     /***
166      * @see net.sf.pmr.core.service.BasicProjectService#findAll()
167      */
168     public List findAll() {
169         return basicProjectRepository.findAll();
170     }
171 
172 
173     /*** 
174      * @see net.sf.pmr.core.service.BasicProjectService#findByPersistanceId(int)
175      */
176     public BasicProject findByPersistanceId(final int persistanceId) {
177         return basicProjectRepository.findByPersistanceId(persistanceId);
178     }
179     
180 }