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
.context
.ApplicationContext
;
21 import org
.springframework
.context
.ApplicationContextAware
;
22 import org
.springframework
.transaction
.annotation
.Transactional
;
24 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
25 import eu
.etaxonomy
.cdm
.persistence
.dao
.common
.ICdmEntityDao
;
28 public abstract class ServiceBase
<T
extends CdmBase
> implements IService
<T
>, ApplicationContextAware
{
29 static Logger logger
= Logger
.getLogger(ServiceBase
.class);
31 //flush after saving this number of objects
32 int flushAfterNo
= 2000;
33 protected ApplicationContext appContext
;
34 protected ICdmEntityDao
<T
> dao
;
36 protected void setEntityDao(ICdmEntityDao
<T
> dao
){
41 * @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
43 public void setApplicationContext(ApplicationContext appContext
){
44 this.appContext
= appContext
;
47 protected T
getCdmObjectByUuid(UUID uuid
){
48 return dao
.findByUuid(uuid
);
52 public int count(Class
<T
> clazz
) {
53 return dao
.count(clazz
);
56 @Transactional(readOnly
= false)
57 protected UUID
saveCdmObject(T cdmObj
){
58 if (logger
.isDebugEnabled()){logger
.debug("Save cdmObj: " + (cdmObj
== null?
null: cdmObj
.toString()));}
59 return dao
.saveOrUpdate(cdmObj
);
62 @Transactional(readOnly
= false)
63 protected Map
<UUID
, T
> saveCdmObjectAll(Collection
<T
> cdmObjCollection
){
64 int types
= cdmObjCollection
.getClass().getTypeParameters().length
;
66 if (logger
.isDebugEnabled()){logger
.debug("ClassType: + " + cdmObjCollection
.getClass().getTypeParameters()[0]);}
69 Map
<UUID
, T
> resultMap
= new HashMap
<UUID
, T
>();
70 Iterator
<T
> iterator
= cdmObjCollection
.iterator();
72 while(iterator
.hasNext()){
73 if ( ( (i
% 5000) == 0) && (i
> 0) ){logger
.debug("Saved " + i
+ " objects" );}
74 T cdmObj
= iterator
.next();
75 UUID uuid
= saveCdmObject(cdmObj
);
76 if (logger
.isDebugEnabled()){logger
.debug("Save cdmObj: " + (cdmObj
== null?
null: cdmObj
.toString()));}
77 resultMap
.put(uuid
, cdmObj
);
79 if ( (i
% flushAfterNo
) == 0){
81 logger
.debug("flush");
84 logger
.error("UUUIIIII");
90 if ( logger
.isInfoEnabled() ){logger
.info("Saved " + i
+ " objects" );}
94 @Transactional(readOnly
= false)
95 protected UUID
removeCdmObject(T cdmObj
){
96 if (logger
.isDebugEnabled()){logger
.debug("Save cdmObj: " + (cdmObj
== null?
null: cdmObj
.toString()));}
97 return dao
.delete(cdmObj
);
100 protected List
<T
> list(int limit
, int start
) {
101 return dao
.list(limit
, start
);