View Javadoc

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