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
|
package eu.etaxonomy.cdm.persistence.dao.hibernate.reference;
|
10
|
|
11
|
import java.util.HashMap;
|
12
|
import java.util.List;
|
13
|
import java.util.Map;
|
14
|
import java.util.Set;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.hibernate.Criteria;
|
18
|
import org.hibernate.Query;
|
19
|
import org.hibernate.Session;
|
20
|
import org.hibernate.criterion.Order;
|
21
|
import org.hibernate.criterion.Projections;
|
22
|
import org.hibernate.criterion.Restrictions;
|
23
|
import org.springframework.stereotype.Repository;
|
24
|
|
25
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
26
|
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
|
27
|
import eu.etaxonomy.cdm.model.name.HybridRelationship;
|
28
|
import eu.etaxonomy.cdm.model.reference.ISourceable;
|
29
|
import eu.etaxonomy.cdm.model.reference.OriginalSourceBase;
|
30
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.CdmEntityDaoBase;
|
31
|
import eu.etaxonomy.cdm.persistence.dao.reference.IOriginalSourceDao;
|
32
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
33
|
|
34
|
/**
|
35
|
* @author a.mueller
|
36
|
* @since 17.07.2008
|
37
|
*/
|
38
|
@Repository
|
39
|
public class OriginalSourceDaoImpl
|
40
|
extends CdmEntityDaoBase<OriginalSourceBase>
|
41
|
implements IOriginalSourceDao {
|
42
|
|
43
|
@SuppressWarnings("unused")
|
44
|
private static final Logger logger = Logger.getLogger(OriginalSourceDaoImpl.class);
|
45
|
|
46
|
public OriginalSourceDaoImpl() {
|
47
|
super(OriginalSourceBase.class);
|
48
|
}
|
49
|
|
50
|
@Override
|
51
|
public <S extends ISourceable> Map<String, S> findOriginalSourcesByIdInSource(Class<S> clazz, Set<String> idInSourceSet, String idNamespace) {
|
52
|
|
53
|
Session session = getSession();
|
54
|
String idInSourceString = "";
|
55
|
for (String idInSource : idInSourceSet){
|
56
|
idInSourceString = CdmUtils.concat("','", idInSourceString, idInSource);
|
57
|
}
|
58
|
idInSourceString = "'"+ idInSourceString + "'";
|
59
|
|
60
|
Query q = session.createQuery(
|
61
|
"SELECT source.idInSource, c " +
|
62
|
"FROM " + clazz.getSimpleName() + " AS c " +
|
63
|
"INNER JOIN c.sources AS source " +
|
64
|
"WHERE source.idInSource IN ( " + idInSourceString + " )" +
|
65
|
" AND source.idNamespace = :idNamespace"
|
66
|
);
|
67
|
q.setString("idNamespace", idNamespace);
|
68
|
//TODO integrate reference in where
|
69
|
|
70
|
Map<String, S> result = new HashMap<>();
|
71
|
|
72
|
@SuppressWarnings("unchecked")
|
73
|
List<Object[]> list = q.list();
|
74
|
for (Object[] pair : list){
|
75
|
result.put((String)pair[0], (S)pair[1]);
|
76
|
}
|
77
|
|
78
|
return result;
|
79
|
}
|
80
|
|
81
|
@Override
|
82
|
public <S extends ISourceable> List<S> findOriginalSourceByIdInSource(Class<S> clazz, String idInSource, String idNamespace) {
|
83
|
Session session = getSession();
|
84
|
Query q = session.createQuery(
|
85
|
"SELECT c FROM " + clazz.getSimpleName() + " as c " +
|
86
|
" INNER JOIN c.sources as source " +
|
87
|
"WHERE source.idInSource = :idInSource " +
|
88
|
" AND source.idNamespace = :idNamespace"
|
89
|
);
|
90
|
q.setString("idInSource", idInSource);
|
91
|
q.setString("idNamespace", idNamespace);
|
92
|
//TODO integrate reference in where
|
93
|
@SuppressWarnings("unchecked")
|
94
|
List<S> results = q.list();
|
95
|
|
96
|
return results;
|
97
|
}
|
98
|
|
99
|
@Override
|
100
|
public List<OriginalSourceBase> findOriginalSourceByIdInSource(String idInSource, String idNamespace) {
|
101
|
Session session = getSession();
|
102
|
Criteria crit = session.createCriteria(type);
|
103
|
crit.add(Restrictions.eq("idInSource", idInSource));
|
104
|
if (idNamespace == null){
|
105
|
crit.add(Restrictions.isNull("idNamespace"));
|
106
|
}else{
|
107
|
crit.add(Restrictions.eq("idNamespace", idNamespace));
|
108
|
}
|
109
|
crit.addOrder(Order.desc("created"));
|
110
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
111
|
List<OriginalSourceBase> results = crit.list();
|
112
|
|
113
|
return results;
|
114
|
}
|
115
|
|
116
|
@Override
|
117
|
public <T extends DescriptionElementSource> Long countWithNameUsedInSource(Class<T> clazz){
|
118
|
Criteria criteria = getSession().createCriteria(HybridRelationship.class);
|
119
|
|
120
|
clazz = clazz != null? clazz : (Class<T>) DescriptionElementSource.class;
|
121
|
Criteria crit = getSession().createCriteria(clazz);
|
122
|
//count
|
123
|
criteria.setProjection(Projections.rowCount());
|
124
|
long result = (Long)criteria.uniqueResult();
|
125
|
|
126
|
return result;
|
127
|
}
|
128
|
|
129
|
|
130
|
@Override
|
131
|
public <T extends DescriptionElementSource> List<T> listWithNameUsedInSource(Class<T> clazz,
|
132
|
Integer pageSize, Integer pageNumber,List<OrderHint> orderHints, List<String> propertyPaths){
|
133
|
clazz = clazz != null? clazz : (Class<T>) DescriptionElementSource.class;
|
134
|
Criteria crit = getSession().createCriteria(clazz);
|
135
|
crit.add(Restrictions.isNotNull("nameUsedInSource"));
|
136
|
|
137
|
crit.addOrder(Order.desc("created"));
|
138
|
@SuppressWarnings({ "unchecked" })
|
139
|
List<T> results = crit.list();
|
140
|
|
141
|
return results;
|
142
|
}
|
143
|
}
|