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
.Collection
;
13 import java
.util
.HashMap
;
14 import java
.util
.Iterator
;
15 import java
.util
.List
;
17 import java
.util
.UUID
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.springframework
.beans
.factory
.annotation
.Autowired
;
21 import org
.springframework
.beans
.factory
.annotation
.Qualifier
;
22 import org
.springframework
.context
.ApplicationContext
;
23 import org
.springframework
.context
.ApplicationContextAware
;
24 import org
.springframework
.transaction
.TransactionStatus
;
25 import org
.springframework
.transaction
.annotation
.Transactional
;
27 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
28 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ICdmEntityDao
;
31 public abstract class ServiceBase
<T
extends CdmBase
> implements IService
<T
>, ApplicationContextAware
{
32 private static final Logger logger
= Logger
.getLogger(ServiceBase
.class);
34 //flush after saving this number of objects
35 int flushAfterNo
= 2000;
36 protected ApplicationContext appContext
;
40 protected ICdmEntityDao
<T
> dao
;
42 protected void setEntityDao(ICdmEntityDao
<T
> dao
){
47 * @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
49 public void setApplicationContext(ApplicationContext appContext
){
50 this.appContext
= appContext
;
53 protected T
getCdmObjectByUuid(UUID uuid
){
54 return dao
.findByUuid(uuid
);
58 public int count(Class
<T
> clazz
) {
59 return dao
.count(clazz
);
62 @Transactional(readOnly
= false)
63 protected UUID
saveCdmObject(T cdmObj
){
64 if (logger
.isDebugEnabled()){logger
.debug("Save cdmObj: " + (cdmObj
== null?
null: cdmObj
.toString()));}
65 return dao
.saveOrUpdate(cdmObj
);
68 @Transactional(readOnly
= false)
69 protected UUID
saveCdmObject(T cdmObj
, TransactionStatus txStatus
){
70 // TODO: Implement with considering txStatus
71 if (logger
.isDebugEnabled()){logger
.debug("Save cdmObj: " + (cdmObj
== null?
null: cdmObj
.toString()));}
72 return dao
.saveOrUpdate(cdmObj
);
75 @Transactional(readOnly
= false)
76 protected <S
extends T
> Map
<UUID
, S
> saveCdmObjectAll(Collection
<S
> cdmObjCollection
){
77 int types
= cdmObjCollection
.getClass().getTypeParameters().length
;
79 if (logger
.isDebugEnabled()){logger
.debug("ClassType: + " + cdmObjCollection
.getClass().getTypeParameters()[0]);}
82 Map
<UUID
, S
> resultMap
= new HashMap
<UUID
, S
>();
83 Iterator
<S
> iterator
= cdmObjCollection
.iterator();
85 while(iterator
.hasNext()){
86 if ( ( (i
% 5000) == 0) && (i
> 0) ){logger
.debug("Saved " + i
+ " objects" );}
87 S cdmObj
= iterator
.next();
88 UUID uuid
= saveCdmObject(cdmObj
);
89 // if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
90 resultMap
.put(uuid
, cdmObj
);
92 if ( (i
% flushAfterNo
) == 0){
94 logger
.debug("flush");
97 logger
.error("UUUIIIII");
103 if ( logger
.isInfoEnabled() ){logger
.info("Saved " + i
+ " objects" );}
107 @Transactional(readOnly
= false)
108 protected UUID
removeCdmObject(T cdmObj
){
109 if (logger
.isDebugEnabled()){logger
.debug("Save cdmObj: " + (cdmObj
== null?
null: cdmObj
.toString()));}
110 return dao
.delete(cdmObj
);
113 public List
<T
> list(int limit
, int start
) {
114 return dao
.list(limit
, start
);
117 public List
<T
> rows(String tableName
, int limit
, int start
) {
118 return dao
.rows(tableName
, limit
, start
);