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
.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
;
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
;
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);
41 //flush after saving this number of objects
42 int flushAfterNo
= 2000;
43 protected ApplicationContext appContext
;
48 @Transactional(readOnly
= true)
49 public void lock(T t
, LockOptions lockOptions
) {
50 dao
.lock(t
, lockOptions
);
54 @Transactional(readOnly
= true)
55 public void refresh(T t
, LockOptions lockOptions
, List
<String
> propertyPaths
) {
56 dao
.refresh(t
, lockOptions
, propertyPaths
);
60 @Transactional(readOnly
= false)
66 @Transactional(readOnly
= true)
67 public int count(Class
<?
extends T
> clazz
) {
68 return dao
.count(clazz
);
72 @Transactional(readOnly
= false)
73 public DeleteResult
delete(T persistentObject
) {
74 DeleteResult result
= new DeleteResult();
76 dao
.delete(persistentObject
);
77 } catch(DataAccessException e
){
79 result
.addException(e
);
85 @Transactional(readOnly
= true)
86 public boolean exists(UUID uuid
) {
87 return dao
.exists(uuid
);
91 @Transactional(readOnly
= true)
92 public List
<T
> find(Set
<UUID
> uuidSet
) {
93 return dao
.list(uuidSet
, null, null, null, null);
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);
103 @Transactional(readOnly
= true)
104 public T
find(UUID uuid
) {
105 return uuid
== null ?
null : dao
.findByUuid(uuid
);
109 @Transactional(readOnly
= true)
110 public T
findWithoutFlush(UUID uuid
) {
111 return uuid
== null ?
null : dao
.findByUuidWithoutFlush(uuid
);
115 @Transactional(readOnly
= true)
116 public T
find(int id
) {
117 return dao
.findById(id
);
121 @Transactional(readOnly
= true)
122 public Session
getSession() {
123 return dao
.getSession();
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
);
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
);
139 @Transactional(readOnly
= true)
140 public T
load(UUID uuid
) {
141 return uuid
== null ?
null : dao
.load(uuid
);
145 @Transactional(readOnly
= true)
146 public T
load(UUID uuid
, List
<String
> propertyPaths
){
147 return uuid
== null ?
null : dao
.load(uuid
, propertyPaths
);
151 @Transactional(readOnly
= false)
152 public T
merge(T newInstance
) {
153 return dao
.merge(newInstance
);
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
);
166 return new DefaultPagerImpl
<S
>(pageNumber
, numberOfResults
, pageSize
, results
);
170 @Transactional(readOnly
= true)
171 public UUID
refresh(T persistentObject
) {
172 return dao
.refresh(persistentObject
);
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" . . .
182 @Transactional(readOnly
= true)
183 public List
<T
> rows(String tableName
, int limit
, int start
) {
184 return dao
.rows(tableName
, limit
, start
);
188 @Transactional(readOnly
= false)
189 public Map
<UUID
, T
> save(Collection
<T
> newInstances
) {
190 return dao
.saveAll(newInstances
);
194 @Transactional(readOnly
= false)
195 public UUID
save(T newInstance
) {
196 return dao
.save(newInstance
);
200 @Transactional(readOnly
= false)
201 public UUID
saveOrUpdate(T transientObject
) {
202 return dao
.saveOrUpdate(transientObject
);
206 @Transactional(readOnly
= false)
207 public Map
<UUID
, T
> saveOrUpdate(Collection
<T
> transientInstances
) {
208 return dao
.saveOrUpdateAll(transientInstances
);
212 public void setApplicationContext(ApplicationContext appContext
){
213 this.appContext
= appContext
;
217 protected abstract void setDao(DAO dao
);
220 @Transactional(readOnly
= false)
221 public UUID
update(T transientObject
) {
222 return dao
.update(transientObject
);
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
);