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
;
26 import org
.springframework
.transaction
.annotation
.Transactional
;
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
) {
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
.findByUuid(uuidSet
);
80 @Transactional(readOnly
= true)
81 public T
find(UUID uuid
) {
82 return dao
.findByUuid(uuid
);
85 @Transactional(readOnly
= true)
86 public Session
getSession() {
87 return dao
.getSession();
90 @Transactional(readOnly
= true)
91 public List
<Object
[]> group(Class
<?
extends T
> clazz
,Integer limit
, Integer start
, List
<Grouping
> groups
, List
<String
> propertyPaths
) {
92 return dao
.group(clazz
, limit
, start
, groups
, propertyPaths
);
95 @Transactional(readOnly
= true)
96 public List
<T
> list(Class
<?
extends T
> type
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
97 return dao
.list(type
,limit
, start
, orderHints
,propertyPaths
);
100 @Transactional(readOnly
= true)
101 public T
load(UUID uuid
) {
102 return dao
.load(uuid
);
105 @Transactional(readOnly
= true)
106 public T
load(UUID uuid
, List
<String
> propertyPaths
){
107 return dao
.load(uuid
, propertyPaths
);
110 @Transactional(readOnly
= false)
111 public UUID
merge(T newInstance
) {
112 return dao
.merge(newInstance
);
115 @Transactional(readOnly
= true)
116 public Pager
<T
> page(Class
<?
extends T
> type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
117 Integer numberOfResults
= dao
.count(type
);
118 List
<T
> results
= new ArrayList
<T
>();
119 pageNumber
= pageNumber
== null ?
0 : pageNumber
;
120 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
121 Integer start
= pageSize
== null ?
0 : pageSize
* pageNumber
;
122 results
= dao
.list(type
, pageSize
, start
, orderHints
,propertyPaths
);
124 return new DefaultPagerImpl
<T
>(pageNumber
, numberOfResults
, pageSize
, results
);
127 @Transactional(readOnly
= true)
128 public UUID
refresh(T persistentObject
) {
129 return dao
.refresh(persistentObject
);
133 * FIXME Candidate for harmonization
134 * is this method used, and if so, should it be exposed in the service layer?
135 * it seems a bit incongruous that we use an ORM to hide the fact that there is a
136 * database, then expose a method that talks about "rows" . . .
138 @Transactional(readOnly
= true)
139 public List
<T
> rows(String tableName
, int limit
, int start
) {
140 return dao
.rows(tableName
, limit
, start
);
143 @Transactional(readOnly
= false)
144 public Map
<UUID
, T
> save(Collection
<T
> newInstances
) {
145 return dao
.saveAll(newInstances
);
148 @Transactional(readOnly
= false)
149 public UUID
save(T newInstance
) {
150 return dao
.save(newInstance
);
153 //@PostFilter("hasPermission(filterObject, 'edit')")
154 //@PreAuthorize("hasRole('ROLE_EDIT')")
155 @Transactional(readOnly
= false)
156 public UUID
saveOrUpdate(T transientObject
) {
157 return dao
.saveOrUpdate(transientObject
);
160 @Transactional(readOnly
= false)
161 public Map
<UUID
, T
> saveOrUpdate(Collection
<T
> transientInstances
) {
162 return dao
.saveOrUpdateAll(transientInstances
);
166 * @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
168 public void setApplicationContext(ApplicationContext appContext
){
169 this.appContext
= appContext
;
173 protected abstract void setDao(DAO dao
);
175 @Transactional(readOnly
= false)
176 public UUID
update(T transientObject
) {
177 return dao
.update(transientObject
);
180 @Transactional(readOnly
= true)
181 public List
<T
> list(T example
, Set
<String
> includeProperties
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
182 return dao
.list(example
, includeProperties
, limit
, start
, orderHints
, propertyPaths
);