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