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 private static final Logger logger
= Logger
.getLogger(ServiceBase
.class);
37 //flush after saving this number of objects
38 int flushAfterNo
= 2000;
39 protected ApplicationContext appContext
;
43 @Transactional(readOnly
= true)
44 public void lock(T t
, LockMode lockMode
) {
45 dao
.lock(t
, lockMode
);
48 @Transactional(readOnly
= true)
49 public void refresh(T t
, LockMode lockMode
) {
50 dao
.refresh(t
, lockMode
);
53 @Transactional(readOnly
= false)
58 @Transactional(readOnly
= true)
59 public int count(Class
<?
extends T
> clazz
) {
60 return dao
.count(clazz
);
63 @Transactional(readOnly
= false)
64 public UUID
delete(T persistentObject
) {
65 return dao
.delete(persistentObject
);
68 @Transactional(readOnly
= true)
69 public boolean exists(UUID uuid
) {
70 return dao
.exists(uuid
);
73 @Transactional(readOnly
= true)
74 public List
<T
> find(Set
<UUID
> uuidSet
) {
75 return dao
.findByUuid(uuidSet
);
78 @Transactional(readOnly
= true)
79 public T
find(UUID uuid
) {
80 return dao
.findByUuid(uuid
);
83 @Transactional(readOnly
= true)
84 public Session
getSession() {
85 return dao
.getSession();
88 @Transactional(readOnly
= true)
89 public List
<Object
[]> group(Class
<?
extends T
> clazz
,Integer limit
, Integer start
, List
<Grouping
> groups
, List
<String
> propertyPaths
) {
90 return dao
.group(clazz
, limit
, start
, groups
, propertyPaths
);
93 @Transactional(readOnly
= true)
94 public List
<T
> list(Class
<?
extends T
> type
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
95 return dao
.list(type
,limit
, start
, orderHints
,propertyPaths
);
98 @Transactional(readOnly
= true)
99 public T
load(UUID uuid
) {
100 return dao
.load(uuid
);
103 @Transactional(readOnly
= true)
104 public T
load(UUID uuid
, List
<String
> propertyPaths
){
105 return dao
.load(uuid
, propertyPaths
);
108 @Transactional(readOnly
= false)
109 public UUID
merge(T newInstance
) {
110 return dao
.merge(newInstance
);
113 @Transactional(readOnly
= true)
114 public Pager
<T
> page(Class
<?
extends T
> type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
115 Integer numberOfResults
= dao
.count(type
);
116 List
<T
> results
= new ArrayList
<T
>();
117 pageNumber
= pageNumber
== null ?
0 : pageNumber
;
118 if(numberOfResults
> 0) { // no point checking again
119 Integer start
= pageSize
== null ?
0 : pageSize
* pageNumber
;
120 results
= dao
.list(type
, pageSize
, start
, orderHints
,propertyPaths
);
122 return new DefaultPagerImpl
<T
>(pageNumber
, numberOfResults
, pageSize
, results
);
125 @Transactional(readOnly
= true)
126 public UUID
refresh(T persistentObject
) {
127 return dao
.refresh(persistentObject
);
131 * FIXME Candidate for harmonization
132 * is this method used, and if so, should it be exposed in the service layer?
133 * it seems a bit incongruous that we use an ORM to hide the fact that there is a
134 * database, then expose a method that talks about "rows" . . .
136 @Transactional(readOnly
= true)
137 public List
<T
> rows(String tableName
, int limit
, int start
) {
138 return dao
.rows(tableName
, limit
, start
);
141 @Transactional(readOnly
= false)
142 public Map
<UUID
, T
> save(Collection
<T
> newInstances
) {
143 return dao
.saveAll(newInstances
);
146 @Transactional(readOnly
= false)
147 public UUID
save(T newInstance
) {
148 return dao
.save(newInstance
);
151 @Transactional(readOnly
= false)
152 public UUID
saveOrUpdate(T transientObject
) {
153 return dao
.saveOrUpdate(transientObject
);
157 * @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
159 public void setApplicationContext(ApplicationContext appContext
){
160 this.appContext
= appContext
;
164 protected abstract void setDao(DAO dao
);
166 @Transactional(readOnly
= false)
167 public UUID
update(T transientObject
) {
168 return dao
.update(transientObject
);
171 @Transactional(readOnly
= true)
172 public List
<T
> list(T example
, Set
<String
> includeProperties
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
173 return dao
.list(example
, includeProperties
, limit
, start
, orderHints
, propertyPaths
);