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