2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
10 package eu
.etaxonomy
.cdm
.api
.service
;
12 import java
.util
.ArrayList
;
13 import java
.util
.Collection
;
14 import java
.util
.List
;
17 import java
.util
.UUID
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.hibernate
.LockOptions
;
21 import org
.hibernate
.Session
;
22 import org
.hibernate
.criterion
.Criterion
;
23 import org
.springframework
.context
.ApplicationContext
;
24 import org
.springframework
.context
.ApplicationContextAware
;
25 import org
.springframework
.dao
.DataAccessException
;
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
.AbstractPagerImpl
;
30 import eu
.etaxonomy
.cdm
.api
.service
.pager
.impl
.DefaultPagerImpl
;
31 import eu
.etaxonomy
.cdm
.exception
.UnpublishedException
;
32 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
33 import eu
.etaxonomy
.cdm
.model
.common
.IPublishable
;
34 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ICdmEntityDao
;
35 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.Restriction
;
36 import eu
.etaxonomy
.cdm
.persistence
.dao
.hibernate
.common
.DaoBase
;
37 import eu
.etaxonomy
.cdm
.persistence
.dto
.MergeResult
;
38 import eu
.etaxonomy
.cdm
.persistence
.query
.Grouping
;
39 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
40 import eu
.etaxonomy
.cdm
.persistence
.query
.OrderHint
;
42 public abstract class ServiceBase
<T
extends CdmBase
, DAO
extends ICdmEntityDao
<T
>>
43 implements IService
<T
>, ApplicationContextAware
{
45 @SuppressWarnings("unused")
46 private static final Logger logger
= Logger
.getLogger(ServiceBase
.class);
48 protected ApplicationContext appContext
;
50 public final static boolean NO_UNPUBLISHED
= DaoBase
.NO_UNPUBLISHED
; //constant for unpublished
51 public final static boolean INCLUDE_UNPUBLISHED
= DaoBase
.INCLUDE_UNPUBLISHED
; //constant for unpublished
56 @Transactional(readOnly
= true)
57 public void lock(T t
, LockOptions lockOptions
) {
58 dao
.lock(t
, lockOptions
);
62 @Transactional(readOnly
= true)
63 public void refresh(T t
, LockOptions lockOptions
, List
<String
> propertyPaths
) {
64 dao
.refresh(t
, lockOptions
, propertyPaths
);
68 @Transactional(readOnly
= false)
74 @Transactional(readOnly
= true)
75 public int count(Class
<?
extends T
> clazz
) {
76 return Long
.valueOf(dao
.count(clazz
)).intValue();
80 @Transactional(readOnly
= false)
81 public DeleteResult
delete(UUID persistentObjectUUID
) {
82 T persistentObject
= dao
.findByUuid(persistentObjectUUID
);
83 return delete(persistentObject
);
87 @Transactional(readOnly
= false)
88 public DeleteResult
delete(Collection
<UUID
> persistentObjectUUIDs
) {
89 DeleteResult result
= new DeleteResult();
90 for(UUID persistentObjectUUID
: persistentObjectUUIDs
) {
91 T persistentObject
= dao
.findByUuid(persistentObjectUUID
);
92 DeleteResult dr
= delete(persistentObject
);
93 result
.includeResult(dr
);
99 @Transactional(readOnly
= false)
100 public DeleteResult
delete(T persistentObject
) {
101 DeleteResult result
= new DeleteResult();
103 dao
.delete(persistentObject
);
104 result
.addDeletedObject(persistentObject
);
105 } catch(DataAccessException e
){
107 result
.addException(e
);
115 @Transactional(readOnly
= true)
116 public boolean exists(UUID uuid
) {
117 return dao
.exists(uuid
);
121 @Transactional(readOnly
= true)
122 public List
<T
> find(Set
<UUID
> uuidSet
) {
123 return dao
.list(uuidSet
, null, null, null, null);
128 @Transactional(readOnly
= true)
129 public <S
extends T
> List
<S
> find(Class
<S
> clazz
, Set
<UUID
> uuidSet
) {
130 return dao
.list(clazz
, uuidSet
, null, null, null, null);
134 @Transactional(readOnly
= true)
135 public List
<T
> findById(Set
<Integer
> idSet
) { //can't be called find(Set<Integer>) as this conflicts with find(Set<UUID)
136 return dao
.loadList(idSet
, null, null);
140 @Transactional(readOnly
= true)
141 public List
<T
> loadByIds(List
<Integer
> idList
, List
<String
> propertyPaths
){
142 return dao
.loadList(idList
, null, propertyPaths
);
146 @Transactional(readOnly
= true)
147 public List
<T
> loadByIds(List
<Integer
> idList
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
148 return dao
.loadList(idList
, orderHints
, propertyPaths
);
152 @Transactional(readOnly
= true)
153 public T
find(UUID uuid
) {
154 return uuid
== null ?
null : dao
.findByUuid(uuid
);
158 @Transactional(readOnly
= true)
159 public T
findWithoutFlush(UUID uuid
) {
160 return uuid
== null ?
null : dao
.findByUuidWithoutFlush(uuid
);
164 @Transactional(readOnly
= true)
165 public T
find(int id
) {
166 return dao
.findById(id
);
170 @Transactional(readOnly
= true)
171 public Session
getSession() {
172 return dao
.getSession();
176 @Transactional(readOnly
= true)
177 public List
<Object
[]> group(Class
<?
extends T
> clazz
,Integer limit
, Integer start
, List
<Grouping
> groups
, List
<String
> propertyPaths
) {
178 return dao
.group(clazz
, limit
, start
, groups
, propertyPaths
);
182 @Transactional(readOnly
= true)
183 public <S
extends T
> List
<S
> list(Class
<S
> type
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
184 return dao
.list(type
, limit
, start
, orderHints
, propertyPaths
);
188 @Transactional(readOnly
= true)
189 public T
load(UUID uuid
) {
190 return uuid
== null ?
null : dao
.load(uuid
);
194 @Transactional(readOnly
= true)
195 public T
loadWithUpdate(UUID uuid
) {
200 @Transactional(readOnly
= true)
201 public T
load(int id
, List
<String
> propertyPaths
) {
202 return dao
.load(id
, propertyPaths
);
206 @Transactional(readOnly
= true)
207 public T
loadWithoutInitializing(int id
){
208 return dao
.loadWithoutInitializing(id
);
212 @Transactional(readOnly
= true)
213 public T
load(UUID uuid
, List
<String
> propertyPaths
){
214 return uuid
== null ?
null : dao
.load(uuid
, propertyPaths
);
218 @Transactional(readOnly
= true)
219 public List
<T
> load(List
<UUID
> uuids
, List
<String
> propertyPaths
){
224 List
<T
> entities
= new ArrayList
<>();
225 for(UUID uuid
: uuids
) {
226 entities
.add(uuid
== null ?
null : dao
.load(uuid
, propertyPaths
));
232 @Transactional(readOnly
= false)
233 public T
merge(T newInstance
) {
234 return dao
.merge(newInstance
);
238 @Transactional(readOnly
= false)
239 public MergeResult
<T
> merge(T newInstance
, boolean returnTransientEntity
) {
240 return dao
.merge(newInstance
, returnTransientEntity
);
244 @Transactional(readOnly
= false)
245 public List
<T
> merge(List
<T
> detachedObjects
) {
246 List
<T
> mergedObjects
= new ArrayList
<T
>();
247 for(T obj
: detachedObjects
) {
248 mergedObjects
.add(dao
.merge(obj
));
250 return mergedObjects
;
254 @Transactional(readOnly
= false)
255 public List
<MergeResult
<T
>> merge(List
<T
> detachedObjects
, boolean returnTransientEntity
) {
256 List
<MergeResult
<T
>> mergedObjects
= new ArrayList
<MergeResult
<T
>>();
257 for(T obj
: detachedObjects
) {
258 mergedObjects
.add(dao
.merge(obj
, returnTransientEntity
));
260 return mergedObjects
;
264 @Transactional(readOnly
= true)
265 public <S
extends T
> Pager
<S
> page(Class
<S
> type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
266 Long numberOfResults
= dao
.count(type
);
267 List
<S
> results
= new ArrayList
<>();
268 pageNumber
= pageNumber
== null ?
0 : pageNumber
;
269 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
270 Integer start
= pageSize
== null ?
0 : pageSize
* pageNumber
;
271 results
= dao
.list(type
, pageSize
, start
, orderHints
, propertyPaths
);
273 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
277 @Transactional(readOnly
= true)
278 public UUID
refresh(T persistentObject
) {
279 return dao
.refresh(persistentObject
);
283 @Transactional(readOnly
= false)
284 public Map
<UUID
, T
> save(Collection
<?
extends T
> newInstances
) {
285 return dao
.saveAll(newInstances
);
289 @Transactional(readOnly
= false)
290 public <S
extends T
> S
save(S newInstance
) {
291 return dao
.save(newInstance
);
295 @Transactional(readOnly
= false)
296 public UUID
saveOrUpdate(T transientObject
) {
297 return dao
.saveOrUpdate(transientObject
);
301 @Transactional(readOnly
= false)
302 public Map
<UUID
, T
> saveOrUpdate(Collection
<T
> transientInstances
) {
303 return dao
.saveOrUpdateAll(transientInstances
);
307 public void setApplicationContext(ApplicationContext appContext
){
308 this.appContext
= appContext
;
312 protected abstract void setDao(DAO dao
);
315 @Transactional(readOnly
= false)
316 public UUID
update(T transientObject
) {
317 return dao
.update(transientObject
);
321 @Transactional(readOnly
= true)
322 public <S
extends T
> List
<S
> list(S example
, Set
<String
> includeProperties
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
323 return dao
.list(example
, includeProperties
, limit
, start
, orderHints
, propertyPaths
);
327 @Transactional(readOnly
= true)
328 public <S
extends T
> Pager
<S
> page(Class
<S
> clazz
, String param
, String queryString
, MatchMode matchmode
, List
<Criterion
> criteria
, Integer pageSize
, Integer pageIndex
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
331 long resultSize
= dao
.countByParam(clazz
, param
, queryString
, matchmode
, criteria
);
332 if(AbstractPagerImpl
.hasResultsInRange(resultSize
, pageIndex
, pageSize
)){
333 records
= dao
.findByParam(clazz
, param
, queryString
, matchmode
, criteria
, pageSize
, pageIndex
, orderHints
, propertyPaths
);
335 records
= new ArrayList
<>();
337 return new DefaultPagerImpl
<>(pageIndex
, resultSize
, pageSize
, records
);
342 @Transactional(readOnly
= true)
343 public <S
extends T
> Pager
<S
> pageByParamWithRestrictions(Class
<S
> clazz
, String param
, String queryString
, MatchMode matchmode
, List
<Restriction
<?
>> restrictions
, Integer pageSize
, Integer pageIndex
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
346 long resultSize
= dao
.countByParamWithRestrictions(clazz
, param
, queryString
, matchmode
, restrictions
);
347 if(AbstractPagerImpl
.hasResultsInRange(resultSize
, pageIndex
, pageSize
)){
348 records
= dao
.findByParamWithRestrictions(clazz
, param
, queryString
, matchmode
, restrictions
, pageSize
, pageIndex
, orderHints
, propertyPaths
);
350 records
= new ArrayList
<>();
352 Pager
<S
> pager
= new DefaultPagerImpl
<>(pageIndex
, resultSize
, pageSize
, records
);
357 @Transactional(readOnly
= true)
358 public <S
extends T
> Pager
<S
> page(Class
<S
> clazz
, List
<Restriction
<?
>> restrictions
, Integer pageSize
, Integer pageIndex
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
361 long resultSize
= dao
.count(clazz
, restrictions
);
362 if(AbstractPagerImpl
.hasResultsInRange(resultSize
, pageIndex
, pageSize
)){
363 records
= dao
.list(clazz
, restrictions
, pageSize
, pageIndex
, orderHints
, propertyPaths
);
365 records
= new ArrayList
<>();
367 Pager
<S
> pager
= new DefaultPagerImpl
<>(pageIndex
, resultSize
, pageSize
, records
);
373 * Throws an exception if the publishable entity should not be published.
374 * @param publishable the publishable entity
375 * @param includeUnpublished should publish be checked
376 * @param message the error message to include
377 * @throws UnpublishedException thrown if entity is not public and unpublished should not be included
379 protected void checkPublished(IPublishable publishable
, boolean includeUnpublished
, String message
) throws UnpublishedException
{
380 if (!(includeUnpublished
|| publishable
.isPublish())){
381 throw new UnpublishedException("Access denied. "+ message
);