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
.HashMap
;
16 import java
.util
.Iterator
;
17 import java
.util
.List
;
19 import java
.util
.UUID
;
21 import org
.apache
.log4j
.Logger
;
22 import org
.springframework
.beans
.factory
.annotation
.Qualifier
;
23 import org
.springframework
.context
.ApplicationContext
;
24 import org
.springframework
.context
.ApplicationContextAware
;
25 import org
.springframework
.transaction
.TransactionStatus
;
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
.model
.reference
.ReferenceBase
;
32 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ICdmEntityDao
;
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 private static final Logger logger
= Logger
.getLogger(ServiceBase
.class);
38 //flush after saving this number of objects
39 int flushAfterNo
= 2000;
40 protected ApplicationContext appContext
;
45 protected abstract void setDao(DAO dao
);
48 * @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
50 public void setApplicationContext(ApplicationContext appContext
){
51 this.appContext
= appContext
;
54 public T
getCdmObjectByUuid(UUID uuid
) {
55 return dao
.findByUuid(uuid
);
58 @Transactional(readOnly
= true)
59 public <TYPE
extends T
> int count(Class
<TYPE
> clazz
) {
60 return dao
.count(clazz
);
63 @Transactional(readOnly
= true)
69 * FIXME harmonise with saveOrUpdate
73 @Transactional(readOnly
= false)
74 protected UUID
saveCdmObject(T cdmObj
){
75 if (logger
.isDebugEnabled()){logger
.debug("Save cdmObj: " + (cdmObj
== null?
null: cdmObj
.toString()));}
76 return dao
.saveOrUpdate(cdmObj
);
81 * FIXME harmonise with saveOrUpdate
85 @Transactional(readOnly
= false)
86 protected UUID
saveCdmObject(T cdmObj
, TransactionStatus txStatus
){
87 // TODO: Implement with considering txStatus
88 if (logger
.isDebugEnabled()){logger
.debug("Save cdmObj: " + (cdmObj
== null?
null: cdmObj
.toString()));}
89 return dao
.saveOrUpdate(cdmObj
);
93 * FIXME harmonise with saveAll
95 * @param cdmObjCollection
98 @Transactional(readOnly
= false)
99 protected <S
extends T
> Map
<UUID
, S
> saveCdmObjectAll(Collection
<?
extends S
> cdmObjCollection
){
100 int types
= cdmObjCollection
.getClass().getTypeParameters().length
;
102 if (logger
.isDebugEnabled()){logger
.debug("ClassType: + " + cdmObjCollection
.getClass().getTypeParameters()[0]);}
105 Map
<UUID
, S
> resultMap
= new HashMap
<UUID
, S
>();
106 Iterator
<?
extends S
> iterator
= cdmObjCollection
.iterator();
108 while(iterator
.hasNext()){
109 if ( ( (i
% 5000) == 0) && (i
> 0) ){logger
.debug("Saved " + i
+ " objects" );}
110 S cdmObj
= iterator
.next();
111 UUID uuid
= saveCdmObject(cdmObj
);
112 // if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
113 resultMap
.put(uuid
, cdmObj
);
115 if ( (i
% flushAfterNo
) == 0){
117 logger
.debug("flush");
120 logger
.error("UUUIIIII");
126 if ( logger
.isInfoEnabled() ){logger
.info("Saved " + i
+ " objects" );}
130 @Transactional(readOnly
= false)
131 public UUID
delete(T persistentObject
) {
132 return dao
.delete(persistentObject
);
135 @Transactional(readOnly
= true)
136 public boolean exists(UUID uuid
) {
137 return dao
.exists(uuid
);
140 @Transactional(readOnly
= true)
141 public T
findByUuid(UUID uuid
) {
142 return dao
.findByUuid(uuid
);
145 @Transactional(readOnly
= true)
146 public <TYPE
extends T
> List
<TYPE
> list(Class
<TYPE
> type
, int limit
,int start
) {
147 return dao
.list(type
, limit
, start
);
150 @Transactional(readOnly
= true)
151 public Pager
<T
> list(Integer pageSize
, Integer pageNumber
){
152 return list(pageSize
, pageNumber
, null);
155 @Transactional (readOnly
= true)
156 public Pager
<T
> list(Integer pageSize
, Integer pageNumber
, List
<OrderHint
> orderHints
){
157 Integer numberOfResults
= dao
.count();
158 List
<T
> results
= new ArrayList
<T
>();
159 if(numberOfResults
> 0) { // no point checking again
160 Integer start
= pageSize
== null ?
0 : pageSize
* (pageNumber
- 1);
161 results
= dao
.list(pageSize
, start
, orderHints
);
163 return new DefaultPagerImpl
<T
>(pageNumber
, numberOfResults
, pageSize
, results
);
166 @Transactional(readOnly
= false)
167 public UUID
save(T newInstance
) {
168 return dao
.save(newInstance
);
171 @Transactional(readOnly
= false)
172 public Map
<UUID
, T
> saveAll(Collection
<T
> newInstances
) {
173 return dao
.saveAll(newInstances
);
176 @Transactional(readOnly
= false)
177 public UUID
saveOrUpdate(T transientObject
) {
178 return dao
.saveOrUpdate(transientObject
);
181 @Transactional(readOnly
= false)
182 public UUID
update(T transientObject
) {
183 return dao
.update(transientObject
);
186 @Transactional(readOnly
= true)
187 public UUID
refresh(T persistentObject
) {
188 return dao
.refresh(persistentObject
);
192 * FIXME harmonise with delete()
196 @Transactional(readOnly
= false)
197 protected UUID
removeCdmObject(T cdmObj
){
198 if (logger
.isDebugEnabled()){logger
.debug("Save cdmObj: " + (cdmObj
== null?
null: cdmObj
.toString()));}
199 return dao
.delete(cdmObj
);
202 @Transactional(readOnly
= true)
203 public List
<T
> list(int limit
, int start
) {
204 return dao
.list(limit
, start
);
207 @Transactional(readOnly
= true)
208 public List
<T
> rows(String tableName
, int limit
, int start
) {
209 return dao
.rows(tableName
, limit
, start
);