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
.security
.access
.prepost
.PreAuthorize
;
26 import org
.springframework
.security
.core
.Authentication
;
27 import org
.springframework
.transaction
.annotation
.Transactional
;
29 import eu
.etaxonomy
.cdm
.api
.service
.pager
.Pager
;
30 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.DefaultPagerImpl
;
31 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
32 import eu
.etaxonomy
.cdm
.permission
.CdmPermission
;
33 import eu
.etaxonomy
.cdm
.permission
.CdmPermissionEvaluator
;
34 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ICdmEntityDao
;
35 import eu
.etaxonomy
.cdm
.persistence
.query
.Grouping
;
36 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
38 public abstract class ServiceBase
<T
extends CdmBase
, DAO
extends ICdmEntityDao
<T
>> implements IService
<T
>, ApplicationContextAware
{
39 @SuppressWarnings("unused")
40 private static final Logger logger
= Logger
.getLogger(ServiceBase
.class);
42 //flush after saving this number of objects
43 int flushAfterNo
= 2000;
44 protected ApplicationContext appContext
;
48 @Transactional(readOnly
= true)
49 public void lock(T t
, LockMode lockMode
) {
50 dao
.lock(t
, lockMode
);
53 @Transactional(readOnly
= true)
54 public void refresh(T t
, LockMode lockMode
, List
<String
> propertyPaths
) {
55 dao
.refresh(t
, lockMode
, propertyPaths
);
58 @Transactional(readOnly
= false)
63 @Transactional(readOnly
= true)
64 public int count(Class
<?
extends T
> clazz
) {
65 return dao
.count(clazz
);
68 @Transactional(readOnly
= false)
69 public UUID
delete(T persistentObject
) {
70 return dao
.delete(persistentObject
);
73 @Transactional(readOnly
= true)
74 public boolean exists(UUID uuid
) {
75 return dao
.exists(uuid
);
78 @Transactional(readOnly
= true)
79 public List
<T
> find(Set
<UUID
> uuidSet
) {
80 return dao
.findByUuid(uuidSet
);
83 @Transactional(readOnly
= true)
84 public T
find(UUID uuid
) {
85 return dao
.findByUuid(uuid
);
88 @Transactional(readOnly
= true)
89 public Session
getSession() {
90 return dao
.getSession();
93 @Transactional(readOnly
= true)
94 public List
<Object
[]> group(Class
<?
extends T
> clazz
,Integer limit
, Integer start
, List
<Grouping
> groups
, List
<String
> propertyPaths
) {
95 return dao
.group(clazz
, limit
, start
, groups
, propertyPaths
);
98 @Transactional(readOnly
= true)
99 public List
<T
> list(Class
<?
extends T
> type
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
100 return dao
.list(type
,limit
, start
, orderHints
,propertyPaths
);
103 @Transactional(readOnly
= true)
104 public T
load(UUID uuid
) {
105 return dao
.load(uuid
);
108 @Transactional(readOnly
= true)
109 public T
load(UUID uuid
, List
<String
> propertyPaths
){
110 return dao
.load(uuid
, propertyPaths
);
113 @Transactional(readOnly
= false)
114 public T
merge(T newInstance
) {
115 return dao
.merge(newInstance
);
118 @Transactional(readOnly
= true)
119 public Pager
<T
> page(Class
<?
extends T
> type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
120 Integer numberOfResults
= dao
.count(type
);
121 List
<T
> results
= new ArrayList
<T
>();
122 pageNumber
= pageNumber
== null ?
0 : pageNumber
;
123 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
124 Integer start
= pageSize
== null ?
0 : pageSize
* pageNumber
;
125 results
= dao
.list(type
, pageSize
, start
, orderHints
,propertyPaths
);
127 return new DefaultPagerImpl
<T
>(pageNumber
, numberOfResults
, pageSize
, results
);
130 @Transactional(readOnly
= true)
131 public UUID
refresh(T persistentObject
) {
132 return dao
.refresh(persistentObject
);
136 * FIXME Candidate for harmonization
137 * is this method used, and if so, should it be exposed in the service layer?
138 * it seems a bit incongruous that we use an ORM to hide the fact that there is a
139 * database, then expose a method that talks about "rows" . . .
141 @Transactional(readOnly
= true)
142 public List
<T
> rows(String tableName
, int limit
, int start
) {
143 return dao
.rows(tableName
, limit
, start
);
146 @Transactional(readOnly
= false)
147 public Map
<UUID
, T
> save(Collection
<T
> newInstances
) {
148 return dao
.saveAll(newInstances
);
151 @Transactional(readOnly
= false)
152 public UUID
save(T newInstance
) {
153 return dao
.save(newInstance
);
156 @Transactional(readOnly
= false)
157 public UUID
saveOrUpdate(T transientObject
) {
158 return dao
.saveOrUpdate(transientObject
);
161 @Transactional(readOnly
= false)
162 public Map
<UUID
, T
> saveOrUpdate(Collection
<T
> transientInstances
) {
163 return dao
.saveOrUpdateAll(transientInstances
);
167 * @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
169 public void setApplicationContext(ApplicationContext appContext
){
170 this.appContext
= appContext
;
174 protected abstract void setDao(DAO dao
);
176 @Transactional(readOnly
= false)
177 public UUID
update(T transientObject
) {
178 return dao
.update(transientObject
);
181 @Transactional(readOnly
= true)
182 public List
<T
> list(T example
, Set
<String
> includeProperties
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
183 return dao
.list(example
, includeProperties
, limit
, start
, orderHints
, propertyPaths
);
186 @Transactional(readOnly
= true)
187 public boolean hasPermission(Authentication authentication
, T target
, CdmPermission permission
) {
188 CdmPermissionEvaluator permissionEvaluator
= new CdmPermissionEvaluator();
189 return permissionEvaluator
.hasPermission(authentication
, target
, permission
);