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
.Session
;
23 import org
.springframework
.context
.ApplicationContext
;
24 import org
.springframework
.context
.ApplicationContextAware
;
25 import org
.springframework
.transaction
.annotation
.Transactional
;
27 import eu
.etaxonomy
.cdm
.api
.service
.exception
.ReferencedObjectUndeletableException
;
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
, LockMode lockMode
) {
48 dao
.lock(t
, lockMode
);
52 @Transactional(readOnly
= true)
53 public void refresh(T t
, LockMode lockMode
, List
<String
> propertyPaths
) {
54 dao
.refresh(t
, lockMode
, 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 String
delete(T persistentObject
) {
72 return dao
.delete(persistentObject
).toString();
76 @Transactional(readOnly
= true)
77 public boolean exists(UUID uuid
) {
78 return dao
.exists(uuid
);
82 @Transactional(readOnly
= true)
83 public List
<T
> find(Set
<UUID
> uuidSet
) {
84 return dao
.list(uuidSet
, null, null, null, null);
88 @Transactional(readOnly
= true)
89 public List
<T
> findById(Set
<Integer
> idSet
) { //can't be called find(Set<Integer>) as this conflicts with find(Set<UUID)
90 return dao
.listByIds(idSet
, null, null, null, null);
94 @Transactional(readOnly
= true)
95 public T
find(UUID uuid
) {
96 return dao
.findByUuid(uuid
);
100 @Transactional(readOnly
= true)
101 public T
findWithoutFlush(UUID uuid
) {
102 return dao
.findByUuidWithoutFlush(uuid
);
106 @Transactional(readOnly
= true)
107 public T
find(int id
) {
108 return dao
.findById(id
);
112 @Transactional(readOnly
= true)
113 public Session
getSession() {
114 return dao
.getSession();
118 @Transactional(readOnly
= true)
119 public List
<Object
[]> group(Class
<?
extends T
> clazz
,Integer limit
, Integer start
, List
<Grouping
> groups
, List
<String
> propertyPaths
) {
120 return dao
.group(clazz
, limit
, start
, groups
, propertyPaths
);
124 @Transactional(readOnly
= true)
125 public <S
extends T
> List
<S
> list(Class
<S
> type
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
126 return dao
.list(type
,limit
, start
, orderHints
,propertyPaths
);
130 @Transactional(readOnly
= true)
131 public T
load(UUID uuid
) {
132 return dao
.load(uuid
);
136 @Transactional(readOnly
= true)
137 public T
load(UUID uuid
, List
<String
> propertyPaths
){
138 return dao
.load(uuid
, propertyPaths
);
142 @Transactional(readOnly
= false)
143 public T
merge(T newInstance
) {
144 return dao
.merge(newInstance
);
148 @Transactional(readOnly
= true)
149 public <S
extends T
> Pager
<S
> page(Class
<S
> type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
150 Integer numberOfResults
= dao
.count(type
);
151 List
<S
> results
= new ArrayList
<S
>();
152 pageNumber
= pageNumber
== null ?
0 : pageNumber
;
153 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
154 Integer start
= pageSize
== null ?
0 : pageSize
* pageNumber
;
155 results
= dao
.list(type
, pageSize
, start
, orderHints
,propertyPaths
);
157 return new DefaultPagerImpl
<S
>(pageNumber
, numberOfResults
, pageSize
, results
);
161 @Transactional(readOnly
= true)
162 public UUID
refresh(T persistentObject
) {
163 return dao
.refresh(persistentObject
);
167 * FIXME Candidate for harmonization
168 * is this method used, and if so, should it be exposed in the service layer?
169 * it seems a bit incongruous that we use an ORM to hide the fact that there is a
170 * database, then expose a method that talks about "rows" . . .
173 @Transactional(readOnly
= true)
174 public List
<T
> rows(String tableName
, int limit
, int start
) {
175 return dao
.rows(tableName
, limit
, start
);
179 @Transactional(readOnly
= false)
180 public Map
<UUID
, T
> save(Collection
<T
> newInstances
) {
181 return dao
.saveAll(newInstances
);
185 @Transactional(readOnly
= false)
186 public UUID
save(T newInstance
) {
187 return dao
.save(newInstance
);
191 @Transactional(readOnly
= false)
192 public UUID
saveOrUpdate(T transientObject
) {
193 return dao
.saveOrUpdate(transientObject
);
197 @Transactional(readOnly
= false)
198 public Map
<UUID
, T
> saveOrUpdate(Collection
<T
> transientInstances
) {
199 return dao
.saveOrUpdateAll(transientInstances
);
203 public void setApplicationContext(ApplicationContext appContext
){
204 this.appContext
= appContext
;
208 protected abstract void setDao(DAO dao
);
211 @Transactional(readOnly
= false)
212 public UUID
update(T transientObject
) {
213 return dao
.update(transientObject
);
217 @Transactional(readOnly
= true)
218 public List
<T
> list(T example
, Set
<String
> includeProperties
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
219 return dao
.list(example
, includeProperties
, limit
, start
, orderHints
, propertyPaths
);