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.domain.basicProject;
37  
38  import java.util.ArrayList;
39  import java.util.Iterator;
40  import java.util.List;
41  
42  import net.sf.pmr.core.CoreObjectFactory;
43  import net.sf.pmr.core.data.basicProject.BasicProjectMapper;
44  
45  import org.springframework.aop.TargetSource;
46  import org.springframework.aop.framework.Advised;
47  import org.springframework.aop.target.SingletonTargetSource;
48  import org.springframework.beans.BeanUtils;
49  
50  /***
51   * @author Arnaud</p>
52   * Cette classe fait l'interface entre le domaine métier et les données <br>
53   * Le project étant un proxy pour déclancher le lazy loading sur la méthode getMembers,
54   * il faut :<ul>
55   * <li>avant d'enregistrer l'objet en base, récupérer la target</li>
56   * <li>après la récupération de l'object de la base, récupérer un objet de la factory est le peupler avec l'objet venant de la base </li> 
57   * </ul>
58   */
59  public class BasicProjectRepositoryImpl implements BasicProjectRepository {
60  
61      /***
62       * project mapper
63       */
64      private BasicProjectMapper basicProjectMapper;
65  
66      /***
67       * @param basicProjectMapper basicProjectMapper
68       */
69      public BasicProjectRepositoryImpl(
70              final BasicProjectMapper basicProjectMapper) {
71          this.basicProjectMapper = basicProjectMapper;
72      }
73  
74      /***
75       * @see net.sf.pmr.core.domain.basicProject.BasicProjectRepositoryTest#countAll()
76       */
77      public Integer countAll() {
78          return basicProjectMapper.countAll();
79      }
80  
81      /***
82       * @see net.sf.pmr.core.domain.basicProject.BasicProjectRepository#add(net.sf.pmr.core.domain.project.BasicProject)
83       */
84      public void addOrUpdate(final BasicProject basicProject) {
85          
86          basicProjectMapper.addOrUpdate(getTarget(basicProject));
87  
88      }
89  
90      /***
91       * @see net.sf.pmr.core.domain.basicProject.BasicProjectRepository#findAll()
92       */
93      public List findAll() {
94  
95          List list = basicProjectMapper.findAll();
96  
97          List listToReturn = new ArrayList();
98  
99          for (Iterator iterator = list.iterator(); iterator.hasNext();) {
100             BasicProject basicProject = (BasicProject) iterator.next();
101 
102             listToReturn.add(injectDependencies(basicProject));
103 
104         }
105 
106         return listToReturn;
107 
108     }
109 
110     
111     /***
112      * BasicProject est un proxy, il faut récupérer la target
113      * 
114      * @param object
115      *            basicProject proxy
116      * @return basicProject target
117      */
118     private BasicProject getTarget(Object basicProject) {
119         
120 	    // BasicProject est un proxy, il faut récupérer la target
121         TargetSource targetSource = ((Advised) basicProject).getTargetSource();        
122         Object target = ((SingletonTargetSource) targetSource).getTarget();
123 
124         // retour de la target
125         return (BasicProject) target;
126         
127     }
128     
129     
130     /***
131      * inject dependencies for basicProject
132      * @param basicProject
133      */
134     private BasicProject injectDependencies (final BasicProject basicProject) {
135 
136         BasicProject basicProjectFromFactory = CoreObjectFactory.getBasicProject();
137 
138         BeanUtils.copyProperties(basicProject, basicProjectFromFactory);
139         
140         return basicProjectFromFactory;
141         
142     }
143 
144     public BasicProject findByPersistanceId(final int persistanceId) {
145         
146         BasicProject basicProject =  (BasicProject) basicProjectMapper.findById(persistanceId);
147         
148         return injectDependencies(basicProject);
149         
150     }
151     
152 }