3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.cdm
.api
.service
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Collection
;
15 import java
.util
.List
;
18 import java
.util
.UUID
;
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
;
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
;
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);
39 //flush after saving this number of objects
40 int flushAfterNo
= 2000;
41 protected ApplicationContext appContext
;
46 @Transactional(readOnly
= true)
47 public void lock(T t
, LockOptions lockOptions
) {
48 dao
.lock(t
, lockOptions
);
52 @Transactional(readOnly
= true)
53 public void refresh(T t
, LockOptions lockOptions
, List
<String
> propertyPaths
) {
54 dao
.refresh(t
, lockOptions
, propertyPaths
);
58 @Transactional(readOnly
= false)
64 @Transactional(readOnly
= true)
65 public int count(Class
<?
extends T
> clazz
) {
66 return dao
.count(clazz
);
70 @Transactional(readOnly
= false)
71 public DeleteResult
delete(UUID persistentObjectUUID
) {
72 DeleteResult result
= new DeleteResult();
74 T persistentObject
= dao
.load(persistentObjectUUID
);
75 dao
.delete(persistentObject
);
76 } catch(DataAccessException e
){
78 result
.addException(e
);
83 @Transactional(readOnly
= false)
84 public DeleteResult
delete(T persistentObject
) {
85 DeleteResult result
= new DeleteResult();
87 dao
.delete(persistentObject
);
88 result
.setCdmEntity(persistentObject
);
89 } catch(DataAccessException e
){
91 result
.addException(e
);
99 @Transactional(readOnly
= true)
100 public boolean exists(UUID uuid
) {
101 return dao
.exists(uuid
);
105 @Transactional(readOnly
= true)
106 public List
<T
> find(Set
<UUID
> uuidSet
) {
107 return dao
.list(uuidSet
, null, null, null, null);
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);
117 @Transactional(readOnly
= true)
118 public T
find(UUID uuid
) {
119 return uuid
== null ?
null : dao
.findByUuid(uuid
);
123 @Transactional(readOnly
= true)
124 public T
findWithoutFlush(UUID uuid
) {
125 return uuid
== null ?
null : dao
.findByUuidWithoutFlush(uuid
);
129 @Transactional(readOnly
= true)
130 public T
find(int id
) {
131 return dao
.findById(id
);
135 @Transactional(readOnly
= true)
136 public Session
getSession() {
137 return dao
.getSession();
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
);
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
);
153 @Transactional(readOnly
= true)
154 public T
load(UUID uuid
) {
155 return uuid
== null ?
null : dao
.load(uuid
);
159 @Transactional(readOnly
= true)
160 public T
load(UUID uuid
, List
<String
> propertyPaths
){
161 return uuid
== null ?
null : dao
.load(uuid
, propertyPaths
);
165 @Transactional(readOnly
= false)
166 public T
merge(T newInstance
) {
167 return dao
.merge(newInstance
);
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
);
180 return new DefaultPagerImpl
<S
>(pageNumber
, numberOfResults
, pageSize
, results
);
184 @Transactional(readOnly
= true)
185 public UUID
refresh(T persistentObject
) {
186 return dao
.refresh(persistentObject
);
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" . . .
196 @Transactional(readOnly
= true)
197 public List
<T
> rows(String tableName
, int limit
, int start
) {
198 return dao
.rows(tableName
, limit
, start
);
202 @Transactional(readOnly
= false)
203 public Map
<UUID
, T
> save(Collection
<T
> newInstances
) {
204 return dao
.saveAll(newInstances
);
208 @Transactional(readOnly
= false)
209 public UUID
save(T newInstance
) {
210 return dao
.save(newInstance
);
214 @Transactional(readOnly
= false)
215 public UUID
saveOrUpdate(T transientObject
) {
216 return dao
.saveOrUpdate(transientObject
);
220 @Transactional(readOnly
= false)
221 public Map
<UUID
, T
> saveOrUpdate(Collection
<T
> transientInstances
) {
222 return dao
.saveOrUpdateAll(transientInstances
);
226 public void setApplicationContext(ApplicationContext appContext
){
227 this.appContext
= appContext
;
231 protected abstract void setDao(DAO dao
);
234 @Transactional(readOnly
= false)
235 public UUID
update(T transientObject
) {
236 return dao
.update(transientObject
);
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
);