1 |
25663b56
|
Andreas Müller
|
/**
|
2 |
|
|
* Copyright (C) 2007 EDIT
|
3 |
|
|
* European Distributed Institute of Taxonomy
|
4 |
|
|
* http://www.e-taxonomy.eu
|
5 |
|
|
*
|
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.
|
8 |
|
|
*/
|
9 |
|
|
|
10 |
2d993c6e
|
Andreas Müller
|
package eu.etaxonomy.cdm.api.service;
|
11 |
|
|
|
12 |
78f4027b
|
Andreas Müller
|
import java.util.Collection;
|
13 |
|
|
import java.util.HashMap;
|
14 |
|
|
import java.util.Iterator;
|
15 |
2d993c6e
|
Andreas Müller
|
import java.util.List;
|
16 |
78f4027b
|
Andreas Müller
|
import java.util.Map;
|
17 |
2d993c6e
|
Andreas Müller
|
import java.util.UUID;
|
18 |
|
|
|
19 |
|
|
import org.apache.log4j.Logger;
|
20 |
7926676f
|
a.babadshanjan
|
import org.springframework.beans.factory.annotation.Autowired;
|
21 |
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
22 |
2d993c6e
|
Andreas Müller
|
import org.springframework.context.ApplicationContext;
|
23 |
|
|
import org.springframework.context.ApplicationContextAware;
|
24 |
|
|
import org.springframework.transaction.annotation.Transactional;
|
25 |
|
|
|
26 |
|
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
27 |
|
|
import eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao;
|
28 |
|
|
|
29 |
|
|
|
30 |
|
|
public abstract class ServiceBase<T extends CdmBase> implements IService<T>, ApplicationContextAware {
|
31 |
|
|
static Logger logger = Logger.getLogger(ServiceBase.class);
|
32 |
|
|
|
33 |
a9b3dbd9
|
Andreas Müller
|
//flush after saving this number of objects
|
34 |
684b3daf
|
Andreas Müller
|
int flushAfterNo = 2000;
|
35 |
2d993c6e
|
Andreas Müller
|
protected ApplicationContext appContext;
|
36 |
7926676f
|
a.babadshanjan
|
|
37 |
|
|
@Autowired
|
38 |
|
|
@Qualifier("cdmDao")
|
39 |
2d993c6e
|
Andreas Müller
|
protected ICdmEntityDao<T> dao;
|
40 |
|
|
|
41 |
|
|
protected void setEntityDao(ICdmEntityDao<T> dao){
|
42 |
|
|
this.dao=dao;
|
43 |
|
|
}
|
44 |
|
|
|
45 |
|
|
/* (non-Javadoc)
|
46 |
|
|
* @see eu.etaxonomy.cdm.api.service.Iyyy#setApplicationContext(org.springframework.context.ApplicationContext)
|
47 |
|
|
*/
|
48 |
|
|
public void setApplicationContext(ApplicationContext appContext){
|
49 |
|
|
this.appContext = appContext;
|
50 |
|
|
}
|
51 |
|
|
|
52 |
|
|
protected T getCdmObjectByUuid(UUID uuid){
|
53 |
|
|
return dao.findByUuid(uuid);
|
54 |
|
|
}
|
55 |
264906f2
|
a.babadshanjan
|
|
56 |
|
|
|
57 |
|
|
public int count(Class<T> clazz) {
|
58 |
|
|
return dao.count(clazz);
|
59 |
|
|
}
|
60 |
2d993c6e
|
Andreas Müller
|
|
61 |
|
|
@Transactional(readOnly = false)
|
62 |
|
|
protected UUID saveCdmObject(T cdmObj){
|
63 |
|
|
if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
|
64 |
|
|
return dao.saveOrUpdate(cdmObj);
|
65 |
|
|
}
|
66 |
|
|
|
67 |
78f4027b
|
Andreas Müller
|
@Transactional(readOnly = false)
|
68 |
069095b0
|
a.babadshanjan
|
protected <S extends T> Map<UUID, S> saveCdmObjectAll(Collection<S> cdmObjCollection){
|
69 |
3903cac8
|
Andreas Müller
|
int types = cdmObjCollection.getClass().getTypeParameters().length;
|
70 |
|
|
if (types > 0){
|
71 |
a131f971
|
Andreas Müller
|
if (logger.isDebugEnabled()){logger.debug("ClassType: + " + cdmObjCollection.getClass().getTypeParameters()[0]);}
|
72 |
3903cac8
|
Andreas Müller
|
}
|
73 |
|
|
|
74 |
069095b0
|
a.babadshanjan
|
Map<UUID, S> resultMap = new HashMap<UUID, S>();
|
75 |
|
|
Iterator<S> iterator = cdmObjCollection.iterator();
|
76 |
4e41264b
|
Andreas Müller
|
int i = 0;
|
77 |
33abfde1
|
Andreas Müller
|
while(iterator.hasNext()){
|
78 |
|
|
if ( ( (i % 5000) == 0) && (i > 0) ){logger.debug("Saved " + i + " objects" );}
|
79 |
069095b0
|
a.babadshanjan
|
S cdmObj = iterator.next();
|
80 |
33abfde1
|
Andreas Müller
|
UUID uuid = saveCdmObject(cdmObj);
|
81 |
|
|
if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
|
82 |
|
|
resultMap.put(uuid, cdmObj);
|
83 |
|
|
i++;
|
84 |
|
|
if ( (i % flushAfterNo) == 0){
|
85 |
|
|
try{
|
86 |
|
|
logger.debug("flush");
|
87 |
|
|
dao.flush();
|
88 |
|
|
}catch(Exception e){
|
89 |
|
|
logger.error("UUUIIIII");
|
90 |
|
|
e.printStackTrace();
|
91 |
|
|
}
|
92 |
|
|
}
|
93 |
ac6fdf4c
|
Andreas Müller
|
}
|
94 |
33abfde1
|
Andreas Müller
|
|
95 |
3903cac8
|
Andreas Müller
|
if ( logger.isInfoEnabled() ){logger.info("Saved " + i + " objects" );}
|
96 |
78f4027b
|
Andreas Müller
|
return resultMap;
|
97 |
|
|
}
|
98 |
|
|
|
99 |
2d993c6e
|
Andreas Müller
|
@Transactional(readOnly = false)
|
100 |
|
|
protected UUID removeCdmObject(T cdmObj){
|
101 |
|
|
if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
|
102 |
|
|
return dao.delete(cdmObj);
|
103 |
|
|
}
|
104 |
|
|
|
105 |
7926676f
|
a.babadshanjan
|
public List<T> list(int limit, int start) {
|
106 |
2d993c6e
|
Andreas Müller
|
return dao.list(limit, start);
|
107 |
|
|
}
|
108 |
|
|
|
109 |
7926676f
|
a.babadshanjan
|
public List<T> rows(String tableName, int limit, int start) {
|
110 |
|
|
return dao.rows(tableName, limit, start);
|
111 |
|
|
}
|
112 |
2d993c6e
|
Andreas Müller
|
}
|