1
|
/**
|
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
|
package eu.etaxonomy.cdm.persistence.dao.hibernate.common;
|
11
|
|
12
|
import java.util.Collection;
|
13
|
import java.util.HashMap;
|
14
|
import java.util.Iterator;
|
15
|
import java.util.List;
|
16
|
import java.util.Map;
|
17
|
import java.util.UUID;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
import org.hibernate.Criteria;
|
21
|
import org.hibernate.HibernateException;
|
22
|
import org.hibernate.NonUniqueObjectException;
|
23
|
import org.hibernate.Session;
|
24
|
import org.hibernate.criterion.Order;
|
25
|
import org.hibernate.criterion.Projection;
|
26
|
import org.hibernate.criterion.Projections;
|
27
|
import org.hibernate.criterion.Restrictions;
|
28
|
import org.springframework.context.ApplicationContextAware;
|
29
|
import org.springframework.dao.DataAccessException;
|
30
|
import org.springframework.stereotype.Repository;
|
31
|
import org.springframework.transaction.annotation.Transactional;
|
32
|
|
33
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
34
|
import eu.etaxonomy.cdm.persistence.dao.common.ICdmEntityDao;
|
35
|
|
36
|
|
37
|
/**
|
38
|
* @author a.mueller
|
39
|
*
|
40
|
*/
|
41
|
@Repository
|
42
|
public abstract class CdmEntityDaoBase<T extends CdmBase> extends DaoBase implements ICdmEntityDao<T> {
|
43
|
//public abstract class ServiceBase<T extends CdmBase> implements IService<T>, ApplicationContextAware {
|
44
|
|
45
|
static Logger logger = Logger.getLogger(CdmEntityDaoBase.class);
|
46
|
|
47
|
int flushAfterNo = 2000;
|
48
|
protected Class<T> type;
|
49
|
|
50
|
public CdmEntityDaoBase(Class<T> type){
|
51
|
this.type = type;
|
52
|
logger.debug("Creating DAO of type [" + type.getSimpleName() + "]");
|
53
|
}
|
54
|
|
55
|
public UUID saveCdmObj(CdmBase cdmObj) throws DataAccessException {
|
56
|
getSession().saveOrUpdate(cdmObj);
|
57
|
return cdmObj.getUuid();
|
58
|
}
|
59
|
|
60
|
//TODO: Replace saveCdmObj() by saveCdmObject_
|
61
|
private UUID saveCdmObject_(T cdmObj){
|
62
|
getSession().saveOrUpdate(cdmObj);
|
63
|
return cdmObj.getUuid();
|
64
|
}
|
65
|
|
66
|
//TODO: Use everywhere CdmEntityDaoBase.saveAll() instead of ServiceBase.saveCdmObjectAll()?
|
67
|
public Map<UUID, T> saveAll(Collection<T> cdmObjCollection){
|
68
|
int types = cdmObjCollection.getClass().getTypeParameters().length;
|
69
|
if (types > 0){
|
70
|
if (logger.isDebugEnabled()){logger.debug("ClassType: + " + cdmObjCollection.getClass().getTypeParameters()[0]);}
|
71
|
}
|
72
|
|
73
|
Map<UUID, T> resultMap = new HashMap<UUID, T>();
|
74
|
Iterator<T> iterator = cdmObjCollection.iterator();
|
75
|
int i = 0;
|
76
|
while(iterator.hasNext()){
|
77
|
if ( ( (i % 5000) == 0) && (i > 0) ){logger.debug("Saved " + i + " objects" );}
|
78
|
T cdmObj = iterator.next();
|
79
|
UUID uuid = saveCdmObject_(cdmObj);
|
80
|
if (logger.isDebugEnabled()){logger.debug("Save cdmObj: " + (cdmObj == null? null: cdmObj.toString()));}
|
81
|
resultMap.put(uuid, cdmObj);
|
82
|
i++;
|
83
|
if ( (i % flushAfterNo) == 0){
|
84
|
try{
|
85
|
logger.debug("flush");
|
86
|
flush();
|
87
|
}catch(Exception e){
|
88
|
logger.error("UUUIIIII");
|
89
|
e.printStackTrace();
|
90
|
}
|
91
|
}
|
92
|
}
|
93
|
|
94
|
if ( logger.isInfoEnabled() ){logger.info("Saved " + i + " objects" );}
|
95
|
return resultMap;
|
96
|
}
|
97
|
|
98
|
|
99
|
public UUID saveOrUpdate(T transientObject) throws DataAccessException {
|
100
|
try {
|
101
|
if (logger.isDebugEnabled()){logger.debug("dao saveOrUpdate start...");}
|
102
|
if (logger.isDebugEnabled()){logger.debug("transientObject(" + transientObject.getClass().getSimpleName() + ") ID:" + transientObject.getId() + ", UUID: " + transientObject.getUuid()) ;}
|
103
|
Session session = getSession();
|
104
|
session.saveOrUpdate(transientObject);
|
105
|
if (logger.isDebugEnabled()){logger.debug("dao saveOrUpdate end");}
|
106
|
return transientObject.getUuid();
|
107
|
} catch (NonUniqueObjectException e) {
|
108
|
logger.error("Error when in CdmEntityDaoBase saveOrUpdate(obj");
|
109
|
logger.error(e.getIdentifier());
|
110
|
logger.error(e.getEntityName());
|
111
|
logger.error(e.getMessage());
|
112
|
e.printStackTrace();
|
113
|
throw e;
|
114
|
} catch (HibernateException e) {
|
115
|
|
116
|
e.printStackTrace();
|
117
|
throw e;
|
118
|
}
|
119
|
}
|
120
|
|
121
|
public UUID save(T newInstance) throws DataAccessException {
|
122
|
getSession().save(newInstance);
|
123
|
return newInstance.getUuid();
|
124
|
}
|
125
|
|
126
|
public UUID update(T transientObject) throws DataAccessException {
|
127
|
getSession().update(transientObject);
|
128
|
return transientObject.getUuid();
|
129
|
}
|
130
|
|
131
|
public UUID delete(T persistentObject) throws DataAccessException {
|
132
|
getSession().delete(persistentObject);
|
133
|
return persistentObject.getUuid();
|
134
|
}
|
135
|
|
136
|
public T findById(int id) throws DataAccessException {
|
137
|
return (T) getSession().get(type, id);
|
138
|
}
|
139
|
|
140
|
public T findByUuid(UUID uuid) throws DataAccessException{
|
141
|
Session session = getSession();
|
142
|
Criteria crit = session.createCriteria(type);
|
143
|
crit.add(Restrictions.eq("uuid", uuid));
|
144
|
crit.addOrder(Order.desc("created"));
|
145
|
List<T> results = crit.list();
|
146
|
if (results.isEmpty()){
|
147
|
return null;
|
148
|
}else{
|
149
|
return results.get(0);
|
150
|
}
|
151
|
}
|
152
|
|
153
|
public Boolean exists(UUID uuid) {
|
154
|
if (findByUuid(uuid)==null){
|
155
|
return false;
|
156
|
}
|
157
|
return true;
|
158
|
}
|
159
|
|
160
|
public int count() {
|
161
|
return count(type);
|
162
|
}
|
163
|
|
164
|
public int count(Class type) {
|
165
|
Session session = getSession();
|
166
|
Criteria crit = session.createCriteria(type);
|
167
|
crit.setProjection(Projections.projectionList().add(Projections.rowCount()));
|
168
|
Integer nbrRows = (Integer) crit.uniqueResult();
|
169
|
return nbrRows.intValue();
|
170
|
}
|
171
|
|
172
|
public List<T> list(int limit, int start) {
|
173
|
Criteria crit = getSession().createCriteria(type);
|
174
|
crit.setFirstResult(start);
|
175
|
crit.setMaxResults(limit);
|
176
|
return crit.list();
|
177
|
}
|
178
|
|
179
|
}
|