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
.pager
.Pager
;
28 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.DefaultPagerImpl
;
29 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
30 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ICdmEntityDao
;
31 import eu
.etaxonomy
.cdm
.persistence
.query
.Grouping
;
32 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
34 public abstract class ServiceBase
<T
extends CdmBase
, DAO
extends ICdmEntityDao
<T
>> implements IService
<T
>, ApplicationContextAware
{
35 @SuppressWarnings("unused")
36 private static final Logger logger
= Logger
.getLogger(ServiceBase
.class);
38 //flush after saving this number of objects
39 int flushAfterNo
= 2000;
40 protected ApplicationContext appContext
;
44 @Transactional(readOnly
= true)
45 public void lock(T t
, LockMode lockMode
) {
46 dao
.lock(t
, lockMode
);
49 @Transactional(readOnly
= true)
50 public void refresh(T t
, LockMode lockMode
, List
<String
> propertyPaths
) {
51 dao
.refresh(t
, lockMode
, propertyPaths
);
54 @Transactional(readOnly
= false)
59 @Transactional(readOnly
= true)
60 public int count(Class
<?
extends T
> clazz
) {
61 return dao
.count(clazz
);
64 @Transactional(readOnly
= false)
65 public UUID
delete(T persistentObject
) {
66 return dao
.delete(persistentObject
);
69 @Transactional(readOnly
= true)
70 public boolean exists(UUID uuid
) {
71 return dao
.exists(uuid
);
74 @Transactional(readOnly
= true)
75 public List
<T
> find(Set
<UUID
> uuidSet
) {
76 return dao
.findByUuid(uuidSet
);
79 @Transactional(readOnly
= true)
80 public T
find(UUID uuid
) {
81 return dao
.findByUuid(uuid
);
84 @Transactional(readOnly
= true)
85 public Session
getSession() {
86 return dao
.getSession();
89 @Transactional(readOnly
= true)
90 public List
<Object
[]> group(Class
<?
extends T
> clazz
,Integer limit
, Integer start
, List
<Grouping
> groups
, List
<String
> propertyPaths
) {
91 return dao
.group(clazz
, limit
, start
, groups
, propertyPaths
);
94 @Transactional(readOnly
= true)
95 public List
<T
> list(Class
<?
extends T
> type
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
96 return dao
.list(type
,limit
, start
, orderHints
,propertyPaths
);
99 @Transactional(readOnly
= true)
100 public T
load(UUID uuid
) {
101 return dao
.load(uuid
);
104 @Transactional(readOnly
= true)
105 public T
load(UUID uuid
, List
<String
> propertyPaths
){
106 return dao
.load(uuid
, propertyPaths
);
109 @Transactional(readOnly
= false)
110 public UUID
merge(T newInstance
) {
111 return dao
.merge(newInstance
);
114 @Transactional(readOnly
= true)
115 public Pager
<T
> page(Class
<?
extends T
> type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
116 Integer numberOfResults
= dao
.count(type
);
117 List
<T
> results
= new ArrayList
<T
>();
118 pageNumber
= pageNumber
== null ?
0 : pageNumber
;
119 if(numberOfResults
> 0) { // no point checking again
120 Integer start
= pageSize
== null ?
0 : pageSize
* pageNumber
;
121 results
= dao
.list(type
, pageSize
, start
, orderHints
,propertyPaths
);
123 return new DefaultPagerImpl
<T
>(pageNumber
, numberOfResults
, pageSize
, results
);
126 @Transactional(readOnly
= true)
127 public UUID
refresh(T persistentObject
) {
128 return dao
.refresh(persistentObject
);
132 * FIXME Candidate for harmonization
133 * is this method used, and if so, should it be exposed in the service layer?
134 * it seems a bit incongruous that we use an ORM to hide the fact that there is a
135 * database, then expose a method that talks about "rows" . . .
137 @Transactional(readOnly
= true)
138 public List
<T
> rows(String tableName
, int limit
, int start
) {
139 return dao
.rows(tableName
, limit
, start
);
142 @Transactional(readOnly
= false)
143 public Map
<UUID
, T
> save(Collection
<T
> newInstances
) {
144 return dao
.saveAll(newInstances
);
147 @Transactional(readOnly
= false)
148 public UUID
save(T newInstance
) {
149 return dao
.save(newInstance
);
152 @Transactional(readOnly
= false)
153 public UUID
saveOrUpdate(T transientObject
) {
154 return dao
.saveOrUpdate(transientObject
);
157 @Transactional(readOnly
= false)
158 public Map
<UUID
, T
> saveOrUpdateAll(Collection
<T
> transientInstances
) {
159 return dao
.saveOrUpdateAll(transientInstances
);
163 * @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
165 public void setApplicationContext(ApplicationContext appContext
){
166 this.appContext
= appContext
;
170 protected abstract void setDao(DAO dao
);
172 @Transactional(readOnly
= false)
173 public UUID
update(T transientObject
) {
174 return dao
.update(transientObject
);
177 @Transactional(readOnly
= true)
178 public List
<T
> list(T example
, Set
<String
> includeProperties
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
179 return dao
.list(example
, includeProperties
, limit
, start
, orderHints
, propertyPaths
);