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
;
45 @Transactional(readOnly
= true)
46 public void lock(T t
, LockMode lockMode
) {
47 dao
.lock(t
, lockMode
);
50 @Transactional(readOnly
= true)
51 public void refresh(T t
, LockMode lockMode
, List
<String
> propertyPaths
) {
52 dao
.refresh(t
, lockMode
, propertyPaths
);
55 @Transactional(readOnly
= false)
60 @Transactional(readOnly
= true)
61 public int count(Class
<?
extends T
> clazz
) {
62 return dao
.count(clazz
);
65 @Transactional(readOnly
= false)
66 public UUID
delete(T persistentObject
) throws ReferencedObjectUndeletableException
{
67 return dao
.delete(persistentObject
);
70 @Transactional(readOnly
= true)
71 public boolean exists(UUID uuid
) {
72 return dao
.exists(uuid
);
75 @Transactional(readOnly
= true)
76 public List
<T
> find(Set
<UUID
> uuidSet
) {
77 return dao
.list(uuidSet
, null, null, null, null);
80 @Transactional(readOnly
= true)
81 public List
<T
> findById(Set
<Integer
> idSet
) { //can't be called find(Set<Integer>) as this conflicts with find(Set<UUID)
82 return dao
.listByIds(idSet
, null, null, null, null);
85 @Transactional(readOnly
= true)
86 public T
find(UUID uuid
) {
87 return dao
.findByUuid(uuid
);
90 @Transactional(readOnly
= true)
91 public T
find(int id
) {
92 return dao
.findById(id
);
95 @Transactional(readOnly
= true)
96 public Session
getSession() {
97 return dao
.getSession();
100 @Transactional(readOnly
= true)
101 public List
<Object
[]> group(Class
<?
extends T
> clazz
,Integer limit
, Integer start
, List
<Grouping
> groups
, List
<String
> propertyPaths
) {
102 return dao
.group(clazz
, limit
, start
, groups
, propertyPaths
);
105 @Transactional(readOnly
= true)
106 public List
<T
> list(Class
<?
extends T
> type
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
107 return dao
.list(type
,limit
, start
, orderHints
,propertyPaths
);
110 @Transactional(readOnly
= true)
111 public T
load(UUID uuid
) {
112 return dao
.load(uuid
);
115 @Transactional(readOnly
= true)
116 public T
load(UUID uuid
, List
<String
> propertyPaths
){
117 return dao
.load(uuid
, propertyPaths
);
120 @Transactional(readOnly
= false)
121 public T
merge(T newInstance
) {
122 return dao
.merge(newInstance
);
125 @Transactional(readOnly
= true)
126 public Pager
<T
> page(Class
<?
extends T
> type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
127 Integer numberOfResults
= dao
.count(type
);
128 List
<T
> results
= new ArrayList
<T
>();
129 pageNumber
= pageNumber
== null ?
0 : pageNumber
;
130 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
131 Integer start
= pageSize
== null ?
0 : pageSize
* pageNumber
;
132 results
= dao
.list(type
, pageSize
, start
, orderHints
,propertyPaths
);
134 return new DefaultPagerImpl
<T
>(pageNumber
, numberOfResults
, pageSize
, results
);
137 @Transactional(readOnly
= true)
138 public UUID
refresh(T persistentObject
) {
139 return dao
.refresh(persistentObject
);
143 * FIXME Candidate for harmonization
144 * is this method used, and if so, should it be exposed in the service layer?
145 * it seems a bit incongruous that we use an ORM to hide the fact that there is a
146 * database, then expose a method that talks about "rows" . . .
148 @Transactional(readOnly
= true)
149 public List
<T
> rows(String tableName
, int limit
, int start
) {
150 return dao
.rows(tableName
, limit
, start
);
153 @Transactional(readOnly
= false)
154 public Map
<UUID
, T
> save(Collection
<T
> newInstances
) {
155 return dao
.saveAll(newInstances
);
158 @Transactional(readOnly
= false)
159 public UUID
save(T newInstance
) {
160 return dao
.save(newInstance
);
163 @Transactional(readOnly
= false)
164 public UUID
saveOrUpdate(T transientObject
) {
165 return dao
.saveOrUpdate(transientObject
);
168 @Transactional(readOnly
= false)
169 public Map
<UUID
, T
> saveOrUpdate(Collection
<T
> transientInstances
) {
170 return dao
.saveOrUpdateAll(transientInstances
);
174 * @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
176 public void setApplicationContext(ApplicationContext appContext
){
177 this.appContext
= appContext
;
181 protected abstract void setDao(DAO dao
);
183 @Transactional(readOnly
= false)
184 public UUID
update(T transientObject
) {
185 return dao
.update(transientObject
);
188 @Transactional(readOnly
= true)
189 public List
<T
> list(T example
, Set
<String
> includeProperties
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
190 return dao
.list(example
, includeProperties
, limit
, start
, orderHints
, propertyPaths
);