View Javadoc

1   package org.appfuse.service.impl;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   import org.appfuse.dao.GenericDao;
6   import org.appfuse.service.GenericManager;
7   import org.springframework.beans.factory.annotation.Autowired;
8   
9   import java.io.Serializable;
10  import java.util.ArrayList;
11  import java.util.List;
12  
13  /**
14   * This class serves as the Base class for all other Managers - namely to hold
15   * common CRUD methods that they might all use. You should only need to extend
16   * this class when your require custom CRUD logic.
17   * <p/>
18   * <p>To register this class in your Spring context file, use the following XML.
19   * <pre>
20   *     &lt;bean id="userManager" class="org.appfuse.service.impl.GenericManagerImpl"&gt;
21   *         &lt;constructor-arg&gt;
22   *             &lt;bean class="org.appfuse.dao.hibernate.GenericDaoHibernate"&gt;
23   *                 &lt;constructor-arg value="org.appfuse.model.User"/&gt;
24   *                 &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
25   *             &lt;/bean&gt;
26   *         &lt;/constructor-arg&gt;
27   *     &lt;/bean&gt;
28   * </pre>
29   * <p/>
30   * <p>If you're using iBATIS instead of Hibernate, use:
31   * <pre>
32   *     &lt;bean id="userManager" class="org.appfuse.service.impl.GenericManagerImpl"&gt;
33   *         &lt;constructor-arg&gt;
34   *             &lt;bean class="org.appfuse.dao.ibatis.GenericDaoiBatis"&gt;
35   *                 &lt;constructor-arg value="org.appfuse.model.User"/&gt;
36   *                 &lt;property name="dataSource" ref="dataSource"/&gt;
37   *                 &lt;property name="sqlMapClient" ref="sqlMapClient"/&gt;
38   *             &lt;/bean&gt;
39   *         &lt;/constructor-arg&gt;
40   *     &lt;/bean&gt;
41   * </pre>
42   *
43   * @param <T>  a type variable
44   * @param <PK> the primary key for that type
45   * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
46   *  Updated by jgarcia: added full text search + reindexing
47   */
48  public class GenericManagerImpl<T, PK extends Serializable> implements GenericManager<T, PK> {
49      /**
50       * Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging
51       */
52      protected final Log log = LogFactory.getLog(getClass());
53  
54      /**
55       * GenericDao instance, set by constructor of child classes
56       */
57      protected GenericDao<T, PK> dao;
58  
59  
60      public GenericManagerImpl() {
61      }
62  
63      public GenericManagerImpl(GenericDao<T, PK> genericDao) {
64          this.dao = genericDao;
65      }
66  
67      /**
68       * {@inheritDoc}
69       */
70      public List<T> getAll() {
71          return dao.getAll();
72      }
73  
74      /**
75       * {@inheritDoc}
76       */
77      public T get(PK id) {
78          return dao.get(id);
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      public boolean exists(PK id) {
85          return dao.exists(id);
86      }
87  
88      /**
89       * {@inheritDoc}
90       */
91      public T save(T object) {
92          return dao.save(object);
93      }
94  
95      /**
96       * {@inheritDoc}
97       */
98      public void remove(T object) {
99          dao.remove(object);
100     }
101 
102     /**
103      * {@inheritDoc}
104      */
105     public void remove(PK id) {
106         dao.remove(id);
107     }
108 
109     /**
110      * {@inheritDoc}
111      * <p/>
112      * Search implementation using Hibernate Search.
113      */
114     @SuppressWarnings("unchecked")
115     public List<T> search(String q, Class clazz) {
116         if (q == null || "".equals(q.trim())) {
117             return getAll();
118         }
119 
120         return dao.search(q);
121     }
122 
123     /**
124      * {@inheritDoc}
125      */
126     public void reindex() {
127         dao.reindex();
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     public void reindexAll(boolean async) {
134         dao.reindexAll(async);
135     }
136 }