View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.service.impl;
24  
25  import java.io.Serializable;
26  import java.util.List;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  import com.gisgraphy.dao.GenericDao;
32  import com.gisgraphy.service.GenericManager;
33  
34  /**
35   * This class serves as the Base class for all other Managers - namely to hold
36   * common CRUD methods that they might all use. You should only need to extend
37   * this class when your require custom CRUD logic.
38   * <p>
39   * To register this class in your Spring context file, use the following XML.
40   * 
41   * <pre>
42   *     &lt;bean id=&quot;userManager&quot; class=&quot;com.gisgraphy.service.impl.GenericManagerImpl&quot;&gt;
43   *         &lt;constructor-arg&gt;
44   *             &lt;bean class=&quot;com.gisgraphy.dao.hibernate.GenericDaoHibernate&quot;&gt;
45   *                 &lt;constructor-arg value=&quot;com.gisgraphy.model.User&quot;/&gt;
46   *                 &lt;property name=&quot;sessionFactory&quot; ref=&quot;sessionFactory&quot;/&gt;
47   *             &lt;/bean&gt;
48   *         &lt;/constructor-arg&gt;
49   *     &lt;/bean&gt;
50   * </pre>
51   * 
52   * <p>
53   * If you're using iBATIS instead of Hibernate, use:
54   * 
55   * <pre>
56   *     &lt;bean id=&quot;userManager&quot; class=&quot;com.gisgraphy.service.impl.GenericManagerImpl&quot;&gt;
57   *         &lt;constructor-arg&gt;
58   *             &lt;bean class=&quot;com.gisgraphy.dao.ibatis.GenericDaoiBatis&quot;&gt;
59   *                 &lt;constructor-arg value=&quot;com.gisgraphy.model.User&quot;/&gt;
60   *                 &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;/&gt;
61   *                 &lt;property name=&quot;sqlMapClient&quot; ref=&quot;sqlMapClient&quot;/&gt;
62   *             &lt;/bean&gt;
63   *         &lt;/constructor-arg&gt;
64   *     &lt;/bean&gt;
65   * </pre>
66   * 
67   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
68   * @param <T>
69   *                a type variable
70   * @param <PK>
71   *                the primary key for that type
72   */
73  public class GenericManagerImpl<T, PK extends Serializable> implements
74  	GenericManager<T, PK> {
75      /**
76       * Log variable for all child classes. Uses LogFactory.getLog(getClass())
77       * from Commons Logging
78       */
79      protected final Log log = LogFactory.getLog(getClass());
80  
81      /**
82       * GenericDao instance, set by constructor of this class
83       */
84      protected GenericDao<T, PK> genericDao;
85  
86      /**
87       * Public constructor for creating a new GenericManagerImpl.
88       * 
89       * @param genericDao
90       *                the GenericDao to use for persistence
91       */
92      public GenericManagerImpl(final GenericDao<T, PK> genericDao) {
93  	this.genericDao = genericDao;
94      }
95  
96      /**
97       * {@inheritDoc}
98       */
99      public List<T> getAll() {
100 	return genericDao.getAll();
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     public T get(PK id) {
107 	return genericDao.get(id);
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     public boolean exists(PK id) {
114 	return genericDao.exists(id);
115     }
116 
117     /**
118      * {@inheritDoc}
119      */
120     public T save(T object) {
121 	return genericDao.save(object);
122     }
123 
124     /**
125      * {@inheritDoc}
126      */
127     public void remove(PK id) {
128 	genericDao.remove(id);
129     }
130 }