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 net.sf.pmr.core.CoreObjectFactory;
39  import net.sf.pmr.core.domain.user.company.Company;
40  import net.sf.pmr.core.domain.user.company.CompanyRepository;
41  import net.sf.pmr.keopsframework.domain.validation.Errors;
42  import net.sf.pmr.keopsframework.domain.validation.Validator;
43  
44  /***
45   * @author Arnaud Prost (arnaud.prost@gmail.com)
46   */
47  public class CompanyServiceImpl implements CompanyService {
48  
49      /***
50       * Company repository
51       */
52      private CompanyRepository companyRepository;
53  
54      /***
55       * company validator
56       */
57      private Validator companyValidator;
58  
59      /***
60       * address validator
61       */
62      private Validator addressValidator;
63  
64      /***
65       * @param companyRepository
66       * @param companyValidator
67       * @param addressValidator
68       */
69      public CompanyServiceImpl(final CompanyRepository companyRepository,
70              final Validator companyValidator, final Validator addressValidator) {
71          super();
72          this.companyRepository = companyRepository;
73          this.companyValidator = companyValidator;
74          this.addressValidator = addressValidator;
75      }
76  
77      /***
78       * @see net.sf.pmr.core.service.CompanyService#addOrUpdate(int,
79       *      java.lang.String, java.lang.String, java.lang.String,
80       *      java.lang.String, java.lang.String, java.lang.String, long)
81       */
82      public Errors addOrUpdate(final int id, final String name,
83              final String streetNumber, final String streetName,
84              final String postalCode, final String city, final String country,
85              final long persistanceVersion) {
86  
87          // get the company to update
88          Company company = CoreObjectFactory.getCompany();
89  
90          // populate
91          company.setPersistanceId(id);
92          company.setName(name);
93          company.getAddress().setStreetNumber(streetNumber);
94          company.getAddress().setStreetNumber(streetName);
95          company.getAddress().setPostalCode(postalCode);
96          company.getAddress().setCity(city);
97          company.getAddress().setCountry(country);
98          company.setPersistanceVersion(persistanceVersion);
99  
100         // validate
101         Errors errors = companyValidator.validate(company);
102 
103         // if no errors are found
104         if (!errors.hasErrors()) {
105 
106             // validate addresse
107             errors = addressValidator.validate(company);
108 
109             if (!errors.hasErrors()) {
110 
111                 companyRepository.addOrUpdate(company);
112             }
113 
114         }
115 
116         return errors;
117 
118     }
119 
120 }