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
load(UUID uuid
, List
<String
> propertyPaths
){
208 return uuid
== null ?
null : dao
.load(uuid
, propertyPaths
);
212 @Transactional(readOnly
= true)
213 public List
<T
> load(List
<UUID
> uuids
, List
<String
> propertyPaths
){
218 List
<T
> entities
= new ArrayList
<>();
219 for(UUID uuid
: uuids
) {
220 entities
.add(uuid
== null ?
null : dao
.load(uuid
, propertyPaths
));
226 @Transactional(readOnly
= false)
227 public T
merge(T newInstance
) {
228 return dao
.merge(newInstance
);
232 @Transactional(readOnly
= false)
233 public MergeResult
<T
> merge(T newInstance
, boolean returnTransientEntity
) {
234 return dao
.merge(newInstance
, returnTransientEntity
);
238 @Transactional(readOnly
= false)
239 public List
<T
> merge(List
<T
> detachedObjects
) {
240 List
<T
> mergedObjects
= new ArrayList
<T
>();
241 for(T obj
: detachedObjects
) {
242 mergedObjects
.add(dao
.merge(obj
));
244 return mergedObjects
;
248 @Transactional(readOnly
= false)
249 public List
<MergeResult
<T
>> merge(List
<T
> detachedObjects
, boolean returnTransientEntity
) {
250 List
<MergeResult
<T
>> mergedObjects
= new ArrayList
<MergeResult
<T
>>();
251 for(T obj
: detachedObjects
) {
252 mergedObjects
.add(dao
.merge(obj
, returnTransientEntity
));
254 return mergedObjects
;
258 @Transactional(readOnly
= true)
259 public <S
extends T
> Pager
<S
> page(Class
<S
> type
, Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
260 Long numberOfResults
= dao
.count(type
);
261 List
<S
> results
= new ArrayList
<>();
262 pageNumber
= pageNumber
== null ?
0 : pageNumber
;
263 if(numberOfResults
> 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
264 Integer start
= pageSize
== null ?
0 : pageSize
* pageNumber
;
265 results
= dao
.list(type
, pageSize
, start
, orderHints
, propertyPaths
);
267 return new DefaultPagerImpl
<>(pageNumber
, numberOfResults
, pageSize
, results
);
271 @Transactional(readOnly
= true)
272 public UUID
refresh(T persistentObject
) {
273 return dao
.refresh(persistentObject
);
277 @Transactional(readOnly
= false)
278 public Map
<UUID
, T
> save(Collection
<T
> newInstances
) {
279 return dao
.saveAll(newInstances
);
283 @Transactional(readOnly
= false)
284 public T
save(T newInstance
) {
285 return dao
.save(newInstance
);
289 @Transactional(readOnly
= false)
290 public UUID
saveOrUpdate(T transientObject
) {
291 return dao
.saveOrUpdate(transientObject
);
295 @Transactional(readOnly
= false)
296 public Map
<UUID
, T
> saveOrUpdate(Collection
<T
> transientInstances
) {
297 return dao
.saveOrUpdateAll(transientInstances
);
301 public void setApplicationContext(ApplicationContext appContext
){
302 this.appContext
= appContext
;
306 protected abstract void setDao(DAO dao
);
309 @Transactional(readOnly
= false)
310 public UUID
update(T transientObject
) {
311 return dao
.update(transientObject
);
315 @Transactional(readOnly
= true)
316 public List
<T
> list(T example
, Set
<String
> includeProperties
, Integer limit
, Integer start
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
) {
317 return dao
.list(example
, includeProperties
, limit
, start
, orderHints
, propertyPaths
);
321 @Transactional(readOnly
= true)
322 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
){
325 long resultSize
= dao
.countByParam(clazz
, param
, queryString
, matchmode
, criteria
);
326 if(AbstractPagerImpl
.hasResultsInRange(resultSize
, pageIndex
, pageSize
)){
327 records
= dao
.findByParam(clazz
, param
, queryString
, matchmode
, criteria
, pageSize
, pageIndex
, orderHints
, propertyPaths
);
329 records
= new ArrayList
<>();
331 return new DefaultPagerImpl
<>(pageIndex
, resultSize
, pageSize
, records
);
336 @Transactional(readOnly
= true)
337 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
){
340 long resultSize
= dao
.countByParamWithRestrictions(clazz
, param
, queryString
, matchmode
, restrictions
);
341 if(AbstractPagerImpl
.hasResultsInRange(resultSize
, pageIndex
, pageSize
)){
342 records
= dao
.findByParamWithRestrictions(clazz
, param
, queryString
, matchmode
, restrictions
, pageSize
, pageIndex
, orderHints
, propertyPaths
);
344 records
= new ArrayList
<>();
346 Pager
<S
> pager
= new DefaultPagerImpl
<>(pageIndex
, resultSize
, pageSize
, records
);
351 @Transactional(readOnly
= true)
352 public <S
extends T
> Pager
<S
> page(Class
<S
> clazz
, List
<Restriction
<?
>> restrictions
, Integer pageSize
, Integer pageIndex
, List
<OrderHint
> orderHints
, List
<String
> propertyPaths
){
355 long resultSize
= dao
.count(clazz
, restrictions
);
356 if(AbstractPagerImpl
.hasResultsInRange(resultSize
, pageIndex
, pageSize
)){
357 records
= dao
.list(clazz
, restrictions
, pageSize
, pageIndex
, orderHints
, propertyPaths
);
359 records
= new ArrayList
<>();
361 Pager
<S
> pager
= new DefaultPagerImpl
<>(pageIndex
, resultSize
, pageSize
, records
);
367 * Throws an exception if the publishable entity should not be published.
368 * @param publishable the publishable entity
369 * @param includeUnpublished should publish be checked
370 * @param message the error message to include
371 * @throws UnpublishedException thrown if entity is not public and unpublished should not be included
373 protected void checkPublished(IPublishable publishable
, boolean includeUnpublished
, String message
) throws UnpublishedException
{
374 if (!(includeUnpublished
|| publishable
.isPublish())){
375 throw new UnpublishedException("Access denied. "+ message
);