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(T persistentObject
) {
72 DeleteResult result
= new DeleteResult();
74 dao
.delete(persistentObject
);
75 result
.setCdmEntity(persistentObject
);
76 } catch(DataAccessException e
){
78 result
.addException(e
);
84 @Transactional(readOnly
= false)
85 public DeleteResult
delete(UUID uuid
) {
86 return delete(dao
.load(uuid
));
90 @Transactional(readOnly
= true)
91 public boolean exists(UUID uuid
) {
92 return dao
.exists(uuid
);
96 @Transactional(readOnly
= true)
97 public List
<T
> find(Set
<UUID
> uuidSet
) {
98 return dao
.list(uuidSet
, null, null, null, null);
102 @Transactional(readOnly
= true)
103 public List
<T
> findById(Set
<Integer
> idSet
) { //can't be called find(Set<Integer>) as this conflicts with find(Set<UUID)
104 return dao
.listByIds(idSet
, null, null, null, null);
108 @Transactional(readOnly
= true)
109 public T
find(UUID uuid
) {
110 return uuid
== null ?
null : dao
.findByUuid(uuid
);
114 @Transactional(readOnly
= true)
115 public T
findWithoutFlush(UUID uuid
) {
116 return uuid
== null ?
null : dao
.findByUuidWithoutFlush(uuid
);
120 @Transactional(readOnly
= true)
121 public T
find(int id
) {
122 return dao
.findById(id
);
126 @Transactional(readOnly
= true)
127 public Session
getSession() {
128 return dao
.getSession();
132 @Transactional(readOnly
= true)
133 public List
<Object
[]> group(Class
<?
extends T
> clazz
,Integer limit
, Integer start
, List
<Grouping
> groups
, List
<String
> propertyPaths
) {
134 return dao
.group(clazz
, limit
, start
, groups
, propertyPaths
);
138 @Transactional(readOnly
= true)
139 public <S
extends T
> List
<S
> list(Class
<S
> type
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
140 return dao
.list(type
,limit
, start
, orderHints
,propertyPaths
);
144 @Transactional(readOnly
= true)
145 public T
load(UUID uuid
) {
146 return uuid
== null ?
null : dao
.load(uuid
);
150 @Transactional(readOnly
= true)
151 public T
load(UUID uuid
, List
<String
> propertyPaths
){
152 return uuid
== null ?
null : dao
.load(uuid
, propertyPaths
);
156 @Transactional(readOnly
= false)
157 public T
merge(T newInstance
) {
158 return dao
.merge(newInstance
);
162 @Transactional(readOnly
= true)
163 public <S
extends T
> Pager
<S
> page(Class
<S
> type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
164 Integer numberOfResults
= dao
.count(type
);
165 List
<S
> results
= new ArrayList
<S
>();
166 pageNumber
= pageNumber
== null ?
0 : pageNumber
;
167 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
168 Integer start
= pageSize
== null ?
0 : pageSize
* pageNumber
;
169 results
= dao
.list(type
, pageSize
, start
, orderHints
,propertyPaths
);
171 return new DefaultPagerImpl
<S
>(pageNumber
, numberOfResults
, pageSize
, results
);
175 @Transactional(readOnly
= true)
176 public UUID
refresh(T persistentObject
) {
177 return dao
.refresh(persistentObject
);
181 * FIXME Candidate for harmonization
182 * is this method used, and if so, should it be exposed in the service layer?
183 * it seems a bit incongruous that we use an ORM to hide the fact that there is a
184 * database, then expose a method that talks about "rows" . . .
187 @Transactional(readOnly
= true)
188 public List
<T
> rows(String tableName
, int limit
, int start
) {
189 return dao
.rows(tableName
, limit
, start
);
193 @Transactional(readOnly
= false)
194 public Map
<UUID
, T
> save(Collection
<T
> newInstances
) {
195 return dao
.saveAll(newInstances
);
199 @Transactional(readOnly
= false)
200 public UUID
save(T newInstance
) {
201 return dao
.save(newInstance
);
205 @Transactional(readOnly
= false)
206 public UUID
saveOrUpdate(T transientObject
) {
207 return dao
.saveOrUpdate(transientObject
);
211 @Transactional(readOnly
= false)
212 public Map
<UUID
, T
> saveOrUpdate(Collection
<T
> transientInstances
) {
213 return dao
.saveOrUpdateAll(transientInstances
);
217 public void setApplicationContext(ApplicationContext appContext
){
218 this.appContext
= appContext
;
222 protected abstract void setDao(DAO dao
);
225 @Transactional(readOnly
= false)
226 public UUID
update(T transientObject
) {
227 return dao
.update(transientObject
);
231 @Transactional(readOnly
= true)
232 public List
<T
> list(T example
, Set
<String
> includeProperties
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
233 return dao
.list(example
, includeProperties
, limit
, start
, orderHints
, propertyPaths
);