1
|
package eu.etaxonomy.cdm.persistence.dao.hibernate.description;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.HashMap;
|
5
|
import java.util.HashSet;
|
6
|
import java.util.List;
|
7
|
import java.util.Map;
|
8
|
import java.util.Set;
|
9
|
import java.util.UUID;
|
10
|
|
11
|
import org.apache.log4j.Logger;
|
12
|
import org.hibernate.Criteria;
|
13
|
import org.hibernate.Query;
|
14
|
import org.hibernate.Session;
|
15
|
import org.hibernate.criterion.Restrictions;
|
16
|
import org.springframework.beans.factory.annotation.Qualifier;
|
17
|
import org.springframework.stereotype.Repository;
|
18
|
|
19
|
import eu.etaxonomy.cdm.model.description.CategoricalData;
|
20
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
21
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
22
|
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
|
23
|
import eu.etaxonomy.cdm.model.description.DescriptiveSystemRole;
|
24
|
import eu.etaxonomy.cdm.model.description.Feature;
|
25
|
import eu.etaxonomy.cdm.model.description.QuantitativeData;
|
26
|
import eu.etaxonomy.cdm.persistence.dao.description.IDescriptiveDataSetDao;
|
27
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.IdentifiableDaoBase;
|
28
|
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
|
29
|
|
30
|
@Repository
|
31
|
@Qualifier("descriptiveDataSetDaoImpl")
|
32
|
public class DescriptiveDataSetDao
|
33
|
extends IdentifiableDaoBase<DescriptiveDataSet>
|
34
|
implements IDescriptiveDataSetDao {
|
35
|
private static final Logger logger = Logger.getLogger(DescriptiveDataSetDao.class);
|
36
|
|
37
|
public DescriptiveDataSetDao() {
|
38
|
super(DescriptiveDataSet.class);
|
39
|
}
|
40
|
|
41
|
@Override
|
42
|
public Map<DescriptionBase, Set<DescriptionElementBase>> getDescriptionElements(DescriptiveDataSet descriptiveDataSet, Set<Feature> features, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
43
|
checkNotInPriorView("DescriptiveDataSetDao.getDescriptionElements(DescriptiveDataSet descriptiveDataSet, Set<Feature> features, Integer pageSize,Integer pageNumber, List<String> propertyPaths)");
|
44
|
Query query = getSession().createQuery("SELECT description FROM DescriptiveDataSet descriptiveDataSet JOIN DescriptiveDataSet.descriptions description ORDER BY description.titleCache ASC");
|
45
|
|
46
|
if(pageSize != null) {
|
47
|
query.setMaxResults(pageSize);
|
48
|
if(pageNumber != null) {
|
49
|
query.setFirstResult(pageNumber * pageSize);
|
50
|
} else {
|
51
|
query.setFirstResult(0);
|
52
|
}
|
53
|
}
|
54
|
List<DescriptionBase> descriptions = query.list();
|
55
|
Map<DescriptionBase, Set<DescriptionElementBase>> result = new HashMap<>();
|
56
|
for(DescriptionBase description : descriptions) {
|
57
|
Criteria criteria = getSession().createCriteria(DescriptionElementBase.class);
|
58
|
criteria.add(Restrictions.eq("inDescription", description));
|
59
|
if(features != null && !features.isEmpty()) {
|
60
|
criteria.add(Restrictions.in("feature", features));
|
61
|
}
|
62
|
|
63
|
List<DescriptionElementBase> r = criteria.list();
|
64
|
defaultBeanInitializer.initializeAll(r, propertyPaths);
|
65
|
result.put(description, new HashSet<>(r));
|
66
|
}
|
67
|
return result;
|
68
|
}
|
69
|
|
70
|
@Override
|
71
|
public <T extends DescriptionElementBase> Map<UuidAndTitleCache, Map<UUID, Set<T>>> getTaxonFeatureDescriptionElementMap(
|
72
|
Class<T> clazz, UUID descriptiveDataSetUuid, DescriptiveSystemRole role) {
|
73
|
checkNotInPriorView("DescriptiveDataSetDao.getTaxonFeatureDescriptionElementMap(DescriptiveDataSet descriptiveDataSet, Set<Feature> features, Integer pageSize,Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
|
74
|
|
75
|
Map<UuidAndTitleCache, Map<UUID, Set<T>>> result = new HashMap<>();
|
76
|
try {
|
77
|
|
78
|
//for maps see
|
79
|
// http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html
|
80
|
// Elements of indexed collections (arrays, lists, and maps) can be referred to by index in a where clause only:
|
81
|
//
|
82
|
// Example: from Order order where order.items[0].id = 1234
|
83
|
|
84
|
|
85
|
//feature
|
86
|
String strQueryTreeId = "SELECT ws.descriptiveSystem.id FROM DescriptiveDataSet dds join dds.descriptiveSystem tree WHERE dds.uuid = :descriptiveDataSetUuid ";
|
87
|
Query queryTree = getSession().createQuery(strQueryTreeId);
|
88
|
queryTree.setParameter("descriptiveDataSetUuid", descriptiveDataSetUuid);
|
89
|
List<?> trees = queryTree.list();
|
90
|
|
91
|
|
92
|
String ftSelect = "SELECT feature.id FROM FeatureNode node join node.feature as feature " +
|
93
|
" WHERE node.featureTree.id in (:trees) ";
|
94
|
Query ftQuery = getSession().createQuery(ftSelect);
|
95
|
ftQuery.setParameterList("trees", trees);
|
96
|
List<?> features = ftQuery.list();
|
97
|
|
98
|
String strClass = (clazz == null )? "DescriptionElementBase" : clazz.getSimpleName();
|
99
|
|
100
|
String fetch = "";
|
101
|
if (clazz.equals(CategoricalData.class)){
|
102
|
fetch = " left join fetch el.states stateList join fetch stateList.state ";
|
103
|
}else if (clazz.equals(QuantitativeData.class)){
|
104
|
fetch = " left join fetch el.statisticalValues valueList join fetch valueList.type ";
|
105
|
}
|
106
|
|
107
|
String strQuery = " select taxon.uuid, taxon.id, taxon.titleCache, feature.uuid, el "
|
108
|
+ " from " + strClass + " el "
|
109
|
+ " join el.feature feature "
|
110
|
+ " join el.inDescription d "
|
111
|
+ " join d.taxon taxon "
|
112
|
+ " join d.descriptiveDataSets ws "
|
113
|
+ " join ws.descriptiveSystem ftree "
|
114
|
+ fetch
|
115
|
+ " where ws.uuid = :descriptiveDataSetUuid "
|
116
|
+ " and el.class = :clazz "
|
117
|
+ " and feature.id in (:features) "
|
118
|
+ " order by taxon.uuid asc, feature.uuid asc"
|
119
|
;
|
120
|
Query query = getSession().createQuery(strQuery);
|
121
|
|
122
|
query.setParameter("descriptiveDataSetUuid", descriptiveDataSetUuid);
|
123
|
query.setParameter("clazz", clazz.getSimpleName());
|
124
|
query.setParameterList("features", features);
|
125
|
|
126
|
//NOTE: Paging does not work with fetch
|
127
|
|
128
|
// fill result
|
129
|
@SuppressWarnings("unchecked")
|
130
|
List<Object[]> list = query.list();
|
131
|
for (Object[] listEntry : list){
|
132
|
UUID taxonUuid = (UUID)listEntry[0];
|
133
|
Integer id = (Integer)listEntry[1];
|
134
|
String titleCache = (String)listEntry[2];
|
135
|
UuidAndTitleCache taxon = new UuidAndTitleCache(taxonUuid, id, titleCache);
|
136
|
UUID featureUuid = (UUID)listEntry[3];
|
137
|
T data = (T)listEntry[4];
|
138
|
Map<UUID, Set<T>> taxonMap = result.get(taxon);
|
139
|
if (taxonMap == null){
|
140
|
taxonMap = new HashMap<>();
|
141
|
result.put(taxon, taxonMap);
|
142
|
}
|
143
|
Set<T> featureSet = taxonMap.get(featureUuid);
|
144
|
if (featureSet == null){
|
145
|
featureSet = new HashSet<>();
|
146
|
taxonMap.put(featureUuid, featureSet);
|
147
|
}else{
|
148
|
if (logger.isDebugEnabled()){logger.debug("feature set already exists");}
|
149
|
}
|
150
|
featureSet.add(data);
|
151
|
|
152
|
}
|
153
|
|
154
|
// defaultBeanInitializer.initialize(
|
155
|
|
156
|
return result;
|
157
|
} catch (Exception e) {
|
158
|
e.printStackTrace();
|
159
|
throw new RuntimeException(e);
|
160
|
}
|
161
|
}
|
162
|
|
163
|
/**
|
164
|
* {@inheritDoc}
|
165
|
*/
|
166
|
@Override
|
167
|
public List<UuidAndTitleCache<DescriptiveDataSet>> getDescriptiveDataSetUuidAndTitleCache(Integer limitOfInitialElements,
|
168
|
String pattern) {
|
169
|
Session session = getSession();
|
170
|
|
171
|
String queryString = "SELECT uuid, id, label FROM DescriptiveDataSet ";
|
172
|
|
173
|
if ( pattern != null){
|
174
|
queryString += " WHERE ";
|
175
|
queryString += " label LIKE :pattern";
|
176
|
|
177
|
}
|
178
|
|
179
|
Query query;
|
180
|
query = session.createQuery(queryString);
|
181
|
|
182
|
|
183
|
if (limitOfInitialElements != null){
|
184
|
query.setMaxResults(limitOfInitialElements);
|
185
|
}
|
186
|
if (pattern != null){
|
187
|
pattern = pattern.replace("*", "%");
|
188
|
pattern = pattern.replace("?", "_");
|
189
|
pattern = pattern + "%";
|
190
|
query.setParameter("pattern", pattern);
|
191
|
}
|
192
|
|
193
|
@SuppressWarnings("unchecked")
|
194
|
List<Object[]> result = query.list();
|
195
|
List<UuidAndTitleCache<DescriptiveDataSet>> list = new ArrayList<>();
|
196
|
for(Object[] object : result){
|
197
|
list.add(new UuidAndTitleCache<DescriptiveDataSet>(DescriptiveDataSet.class, (UUID) object[0],(Integer)object[1], (String)object[2]));
|
198
|
}
|
199
|
|
200
|
return list;
|
201
|
}
|
202
|
}
|