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
find(int id
) {
102 return dao
.findById(id
);
106 @Transactional(readOnly
= true)
107 public Session
getSession() {
108 return dao
.getSession();
112 @Transactional(readOnly
= true)
113 public List
<Object
[]> group(Class
<?
extends T
> clazz
,Integer limit
, Integer start
, List
<Grouping
> groups
, List
<String
> propertyPaths
) {
114 return dao
.group(clazz
, limit
, start
, groups
, propertyPaths
);
118 @Transactional(readOnly
= true)
119 public List
<T
> list(Class
<?
extends T
> type
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
120 return dao
.list(type
,limit
, start
, orderHints
,propertyPaths
);
124 @Transactional(readOnly
= true)
125 public T
load(UUID uuid
) {
126 return dao
.load(uuid
);
130 @Transactional(readOnly
= true)
131 public T
load(UUID uuid
, List
<String
> propertyPaths
){
132 return dao
.load(uuid
, propertyPaths
);
136 @Transactional(readOnly
= false)
137 public T
merge(T newInstance
) {
138 return dao
.merge(newInstance
);
142 @Transactional(readOnly
= true)
143 public Pager
<T
> page(Class
<?
extends T
> type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
144 Integer numberOfResults
= dao
.count(type
);
145 List
<T
> results
= new ArrayList
<T
>();
146 pageNumber
= pageNumber
== null ?
0 : pageNumber
;
147 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
148 Integer start
= pageSize
== null ?
0 : pageSize
* pageNumber
;
149 results
= dao
.list(type
, pageSize
, start
, orderHints
,propertyPaths
);
151 return new DefaultPagerImpl
<T
>(pageNumber
, numberOfResults
, pageSize
, results
);
155 @Transactional(readOnly
= true)
156 public UUID
refresh(T persistentObject
) {
157 return dao
.refresh(persistentObject
);
161 * FIXME Candidate for harmonization
162 * is this method used, and if so, should it be exposed in the service layer?
163 * it seems a bit incongruous that we use an ORM to hide the fact that there is a
164 * database, then expose a method that talks about "rows" . . .
167 @Transactional(readOnly
= true)
168 public List
<T
> rows(String tableName
, int limit
, int start
) {
169 return dao
.rows(tableName
, limit
, start
);
173 @Transactional(readOnly
= false)
174 public Map
<UUID
, T
> save(Collection
<T
> newInstances
) {
175 return dao
.saveAll(newInstances
);
179 @Transactional(readOnly
= false)
180 public UUID
save(T newInstance
) {
181 return dao
.save(newInstance
);
185 @Transactional(readOnly
= false)
186 public UUID
saveOrUpdate(T transientObject
) {
187 return dao
.saveOrUpdate(transientObject
);
191 @Transactional(readOnly
= false)
192 public Map
<UUID
, T
> saveOrUpdate(Collection
<T
> transientInstances
) {
193 return dao
.saveOrUpdateAll(transientInstances
);
197 public void setApplicationContext(ApplicationContext appContext
){
198 this.appContext
= appContext
;
202 protected abstract void setDao(DAO dao
);
205 @Transactional(readOnly
= false)
206 public UUID
update(T transientObject
) {
207 return dao
.update(transientObject
);
211 @Transactional(readOnly
= true)
212 public List
<T
> list(T example
, Set
<String
> includeProperties
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
213 return dao
.list(example
, includeProperties
, limit
, start
, orderHints
, propertyPaths
);