2dd5765dff63965b1b876acf3e301f996eba6b35
[cdmlib.git] / cdmlib-services / src / main / java / eu / etaxonomy / cdm / api / service / ServiceBase.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.cdm.api.service;
12
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import java.util.UUID;
19
20 import org.apache.log4j.Logger;
21 import org.hibernate.LockMode;
22 import org.hibernate.LockOptions;
23 import org.hibernate.Session;
24 import org.springframework.context.ApplicationContext;
25 import org.springframework.context.ApplicationContextAware;
26 import org.springframework.dao.DataAccessException;
27 import org.springframework.transaction.annotation.Transactional;
28
29 import eu.etaxonomy.cdm.api.service.exception.ReferencedObjectUndeletableException;
30 import eu.etaxonomy.cdm.api.service.pager.Pager;
31 import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
32 import eu.etaxonomy.cdm.model.common.CdmBase;
33 import eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao;
34 import eu.etaxonomy.cdm.persistence.query.Grouping;
35 import eu.etaxonomy.cdm.persistence.query.OrderHint;
36
37 public abstract class ServiceBase<T extends CdmBase, DAO extends ICdmEntityDao<T>> implements IService<T>, ApplicationContextAware {
38 @SuppressWarnings("unused")
39 private static final Logger logger = Logger.getLogger(ServiceBase.class);
40
41 //flush after saving this number of objects
42 int flushAfterNo = 2000;
43 protected ApplicationContext appContext;
44
45 protected DAO dao;
46
47 @Override
48 @Transactional(readOnly = true)
49 public void lock(T t, LockOptions lockOptions) {
50 dao.lock(t, lockOptions);
51 }
52
53 @Override
54 @Transactional(readOnly = true)
55 public void refresh(T t, LockOptions lockOptions, List<String> propertyPaths) {
56 dao.refresh(t, lockOptions, propertyPaths);
57 }
58
59 @Override
60 @Transactional(readOnly = false)
61 public void clear() {
62 dao.clear();
63 }
64
65 @Override
66 @Transactional(readOnly = true)
67 public int count(Class<? extends T> clazz) {
68 return dao.count(clazz);
69 }
70
71 @Override
72 @Transactional(readOnly = false)
73 public DeleteResult delete(T persistentObject) {
74 DeleteResult result = new DeleteResult();
75 try{
76 dao.delete(persistentObject);
77 } catch(DataAccessException e){
78 result.setError();
79 result.addException(e);
80 }
81 return result;
82 }
83
84 @Override
85 @Transactional(readOnly = true)
86 public boolean exists(UUID uuid) {
87 return dao.exists(uuid);
88 }
89
90 @Override
91 @Transactional(readOnly = true)
92 public List<T> find(Set<UUID> uuidSet) {
93 return dao.list(uuidSet, null, null, null, null);
94 }
95
96 @Override
97 @Transactional(readOnly = true)
98 public List<T> findById(Set<Integer> idSet) { //can't be called find(Set<Integer>) as this conflicts with find(Set<UUID)
99 return dao.listByIds(idSet, null, null, null, null);
100 }
101
102 @Override
103 @Transactional(readOnly = true)
104 public T find(UUID uuid) {
105 return uuid == null ? null : dao.findByUuid(uuid);
106 }
107
108 @Override
109 @Transactional(readOnly = true)
110 public T findWithoutFlush(UUID uuid) {
111 return uuid == null ? null : dao.findByUuidWithoutFlush(uuid);
112 }
113
114 @Override
115 @Transactional(readOnly = true)
116 public T find(int id) {
117 return dao.findById(id);
118 }
119
120 @Override
121 @Transactional(readOnly = true)
122 public Session getSession() {
123 return dao.getSession();
124 }
125
126 @Override
127 @Transactional(readOnly = true)
128 public List<Object[]> group(Class<? extends T> clazz,Integer limit, Integer start, List<Grouping> groups, List<String> propertyPaths) {
129 return dao.group(clazz, limit, start, groups, propertyPaths);
130 }
131
132 @Override
133 @Transactional(readOnly = true)
134 public <S extends T> List<S> list(Class<S> type, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths){
135 return dao.list(type,limit, start, orderHints,propertyPaths);
136 }
137
138 @Override
139 @Transactional(readOnly = true)
140 public T load(UUID uuid) {
141 return uuid == null ? null : dao.load(uuid);
142 }
143
144 @Override
145 @Transactional(readOnly = true)
146 public T load(UUID uuid, List<String> propertyPaths){
147 return uuid == null ? null : dao.load(uuid, propertyPaths);
148 }
149
150 @Override
151 @Transactional(readOnly = false)
152 public T merge(T newInstance) {
153 return dao.merge(newInstance);
154 }
155
156 @Override
157 @Transactional(readOnly = true)
158 public <S extends T> Pager<S> page(Class<S> type, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths){
159 Integer numberOfResults = dao.count(type);
160 List<S> results = new ArrayList<S>();
161 pageNumber = pageNumber == null ? 0 : pageNumber;
162 if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
163 Integer start = pageSize == null ? 0 : pageSize * pageNumber;
164 results = dao.list(type, pageSize, start, orderHints,propertyPaths);
165 }
166 return new DefaultPagerImpl<S>(pageNumber, numberOfResults, pageSize, results);
167 }
168
169 @Override
170 @Transactional(readOnly = true)
171 public UUID refresh(T persistentObject) {
172 return dao.refresh(persistentObject);
173 }
174
175 /**
176 * FIXME Candidate for harmonization
177 * is this method used, and if so, should it be exposed in the service layer?
178 * it seems a bit incongruous that we use an ORM to hide the fact that there is a
179 * database, then expose a method that talks about "rows" . . .
180 */
181 @Override
182 @Transactional(readOnly = true)
183 public List<T> rows(String tableName, int limit, int start) {
184 return dao.rows(tableName, limit, start);
185 }
186
187 @Override
188 @Transactional(readOnly = false)
189 public Map<UUID, T> save(Collection<T> newInstances) {
190 return dao.saveAll(newInstances);
191 }
192
193 @Override
194 @Transactional(readOnly = false)
195 public UUID save(T newInstance) {
196 return dao.save(newInstance);
197 }
198
199 @Override
200 @Transactional(readOnly = false)
201 public UUID saveOrUpdate(T transientObject) {
202 return dao.saveOrUpdate(transientObject);
203 }
204
205 @Override
206 @Transactional(readOnly = false)
207 public Map<UUID, T> saveOrUpdate(Collection<T> transientInstances) {
208 return dao.saveOrUpdateAll(transientInstances);
209 }
210
211 @Override
212 public void setApplicationContext(ApplicationContext appContext){
213 this.appContext = appContext;
214 }
215
216
217 protected abstract void setDao(DAO dao);
218
219 @Override
220 @Transactional(readOnly = false)
221 public UUID update(T transientObject) {
222 return dao.update(transientObject);
223 }
224
225 @Override
226 @Transactional(readOnly = true)
227 public List<T> list(T example, Set<String> includeProperties, Integer limit, Integer start, List<OrderHint> orderHints, List<String> propertyPaths) {
228 return dao.list(example, includeProperties, limit, start, orderHints, propertyPaths);
229 }
230
231
232
233 }