1
|
/**
|
2
|
* Copyright (C) 2017 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.name;
|
10
|
|
11
|
import java.util.Collection;
|
12
|
import java.util.Collections;
|
13
|
import java.util.List;
|
14
|
import java.util.Optional;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.hibernate.Query;
|
18
|
import org.springframework.stereotype.Repository;
|
19
|
|
20
|
import eu.etaxonomy.cdm.model.name.Registration;
|
21
|
import eu.etaxonomy.cdm.model.name.RegistrationStatus;
|
22
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
23
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.AnnotatableDaoImpl;
|
24
|
import eu.etaxonomy.cdm.persistence.dao.name.IRegistrationDao;
|
25
|
|
26
|
/**
|
27
|
* @author a.kohlbecker
|
28
|
* @since May 2, 2017
|
29
|
*
|
30
|
*/
|
31
|
@Repository
|
32
|
public class RegistrationDaoHibernateImpl
|
33
|
extends AnnotatableDaoImpl<Registration>
|
34
|
implements IRegistrationDao {
|
35
|
|
36
|
@SuppressWarnings("unused")
|
37
|
private static final Logger logger = Logger.getLogger(RegistrationDaoHibernateImpl.class);
|
38
|
|
39
|
/**
|
40
|
* @param type
|
41
|
*/
|
42
|
public RegistrationDaoHibernateImpl() {
|
43
|
super(Registration.class);
|
44
|
}
|
45
|
|
46
|
|
47
|
/**
|
48
|
* {@inheritDoc}
|
49
|
*/
|
50
|
@Override
|
51
|
public Long count(Optional<Reference> reference, Collection<RegistrationStatus> includedStatus) {
|
52
|
//return 0 for detached volatile references
|
53
|
if (isVolatile(reference)){
|
54
|
return Long.valueOf(0);
|
55
|
}
|
56
|
Query query = makeReferenceQuery(reference, includedStatus, true);
|
57
|
@SuppressWarnings("unchecked")
|
58
|
List<Long> list = query.list();
|
59
|
return list.isEmpty()? Long.valueOf(0) : list.get(0);
|
60
|
}
|
61
|
|
62
|
/**
|
63
|
* {@inheritDoc}
|
64
|
*/
|
65
|
@Override
|
66
|
public List<Registration> list(Optional<Reference> reference, Collection<RegistrationStatus> includedStatus,
|
67
|
Integer limit, Integer start, List<String> propertyPaths) {
|
68
|
|
69
|
if (isVolatile(reference)){
|
70
|
return Collections.emptyList();
|
71
|
}
|
72
|
|
73
|
Query query = makeReferenceQuery(reference, includedStatus, false);
|
74
|
|
75
|
// TODO complete ....
|
76
|
if(limit != null /*&& !doCount*/) {
|
77
|
query.setMaxResults(limit);
|
78
|
if(start != null) {
|
79
|
query.setFirstResult(start);
|
80
|
}
|
81
|
}
|
82
|
|
83
|
//TODO order hints do not work with queries
|
84
|
|
85
|
@SuppressWarnings("unchecked")
|
86
|
List<Registration> results = query.list();
|
87
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
88
|
|
89
|
return results;
|
90
|
}
|
91
|
|
92
|
/**
|
93
|
* @param reference
|
94
|
* @return
|
95
|
*/
|
96
|
private boolean isVolatile(Optional<Reference> reference) {
|
97
|
return reference != null && reference.isPresent() && reference.get().getId() == 0;
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* @param reference
|
102
|
* @param includedStatus
|
103
|
* @param isCount
|
104
|
* @return
|
105
|
*/
|
106
|
private Query makeReferenceQuery(Optional<Reference> reference,
|
107
|
Collection<RegistrationStatus> includedStatus,
|
108
|
boolean isCount) {
|
109
|
|
110
|
String select = "SELECT " + (isCount? " count(DISTINCT r) as cn ": "DISTINCT r ");
|
111
|
String from = " FROM Registration r LEFT JOIN r.typeDesignations desig "
|
112
|
+ " LEFT JOIN r.name n ";
|
113
|
String where = " WHERE (1=1) ";
|
114
|
String orderBy = isCount ? "" : " ORDER BY r.id ";
|
115
|
|
116
|
if (reference == null){
|
117
|
//do nothing
|
118
|
}else if (reference.isPresent()){
|
119
|
where += " AND ((n IS NOT NULL AND n.nomenclaturalReference =:ref)"
|
120
|
+ " OR desig.citation =:ref "
|
121
|
+ ")";
|
122
|
}else{ //ref is null
|
123
|
where += " AND ((r.name IS NULL AND size(r.typeDesignations) = 0 ) "
|
124
|
+ " OR (n IS NOT NULL AND r.name.nomenclaturalReference IS NULL ) "
|
125
|
+ " OR (size(r.typeDesignations) > 0 AND desig.citation IS NULL )"
|
126
|
+ ") "
|
127
|
;
|
128
|
}
|
129
|
boolean hasStatus = includedStatus != null && !includedStatus.isEmpty();
|
130
|
if (hasStatus){
|
131
|
where += " AND r.status IN (:status) ";
|
132
|
}
|
133
|
|
134
|
String hql = select + from + where + orderBy;
|
135
|
Query query = getSession().createQuery(hql);
|
136
|
if (reference != null && reference.isPresent()){
|
137
|
query.setParameter("ref", reference.get());
|
138
|
}
|
139
|
if (hasStatus){
|
140
|
query.setParameterList("status", includedStatus);
|
141
|
}
|
142
|
return query;
|
143
|
}
|
144
|
|
145
|
|
146
|
}
|