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.Autowired;
|
17
|
import org.springframework.beans.factory.annotation.Qualifier;
|
18
|
import org.springframework.stereotype.Repository;
|
19
|
|
20
|
import eu.etaxonomy.cdm.model.description.CategoricalData;
|
21
|
import eu.etaxonomy.cdm.model.description.DescriptionBase;
|
22
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
23
|
import eu.etaxonomy.cdm.model.description.DescriptiveDataSet;
|
24
|
import eu.etaxonomy.cdm.model.description.DescriptiveSystemRole;
|
25
|
import eu.etaxonomy.cdm.model.description.Feature;
|
26
|
import eu.etaxonomy.cdm.model.description.QuantitativeData;
|
27
|
import eu.etaxonomy.cdm.persistence.dao.description.IDescriptionDao;
|
28
|
import eu.etaxonomy.cdm.persistence.dao.description.IDescriptiveDataSetDao;
|
29
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.common.IdentifiableDaoBase;
|
30
|
import eu.etaxonomy.cdm.persistence.dao.taxon.ITaxonNodeDao;
|
31
|
import eu.etaxonomy.cdm.persistence.dao.term.IDefinedTermDao;
|
32
|
import eu.etaxonomy.cdm.persistence.dao.term.ITermTreeDao;
|
33
|
import eu.etaxonomy.cdm.persistence.dto.DescriptiveDataSetBaseDto;
|
34
|
import eu.etaxonomy.cdm.persistence.dto.TaxonNodeDto;
|
35
|
import eu.etaxonomy.cdm.persistence.dto.TermDto;
|
36
|
import eu.etaxonomy.cdm.persistence.dto.TermTreeDto;
|
37
|
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
|
38
|
|
39
|
@Repository
|
40
|
@Qualifier("descriptiveDataSetDaoImpl")
|
41
|
public class DescriptiveDataSetDao
|
42
|
extends IdentifiableDaoBase<DescriptiveDataSet>
|
43
|
implements IDescriptiveDataSetDao {
|
44
|
private static final Logger logger = Logger.getLogger(DescriptiveDataSetDao.class);
|
45
|
|
46
|
@Autowired
|
47
|
private ITermTreeDao termTreeDao;
|
48
|
|
49
|
@Autowired
|
50
|
private IDefinedTermDao termDao;
|
51
|
|
52
|
@Autowired
|
53
|
private ITaxonNodeDao nodeDao;
|
54
|
|
55
|
@Autowired
|
56
|
private IDescriptionDao descriptionDao;
|
57
|
|
58
|
public DescriptiveDataSetDao() {
|
59
|
super(DescriptiveDataSet.class);
|
60
|
}
|
61
|
|
62
|
@Override
|
63
|
public Map<DescriptionBase, Set<DescriptionElementBase>> getDescriptionElements(DescriptiveDataSet descriptiveDataSet, Set<Feature> features, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
64
|
checkNotInPriorView("DescriptiveDataSetDao.getDescriptionElements(DescriptiveDataSet descriptiveDataSet, Set<Feature> features, Integer pageSize,Integer pageNumber, List<String> propertyPaths)");
|
65
|
Query query = getSession().createQuery("SELECT description FROM DescriptiveDataSet descriptiveDataSet JOIN DescriptiveDataSet.descriptions description ORDER BY description.titleCache ASC");
|
66
|
|
67
|
if(pageSize != null) {
|
68
|
query.setMaxResults(pageSize);
|
69
|
if(pageNumber != null) {
|
70
|
query.setFirstResult(pageNumber * pageSize);
|
71
|
} else {
|
72
|
query.setFirstResult(0);
|
73
|
}
|
74
|
}
|
75
|
@SuppressWarnings("unchecked")
|
76
|
List<DescriptionBase> descriptions = query.list();
|
77
|
Map<DescriptionBase, Set<DescriptionElementBase>> result = new HashMap<>();
|
78
|
for(DescriptionBase description : descriptions) {
|
79
|
Criteria criteria = getSession().createCriteria(DescriptionElementBase.class);
|
80
|
criteria.add(Restrictions.eq("inDescription", description));
|
81
|
if(features != null && !features.isEmpty()) {
|
82
|
criteria.add(Restrictions.in("feature", features));
|
83
|
}
|
84
|
|
85
|
@SuppressWarnings("unchecked")
|
86
|
List<DescriptionElementBase> r = criteria.list();
|
87
|
defaultBeanInitializer.initializeAll(r, propertyPaths);
|
88
|
result.put(description, new HashSet<>(r));
|
89
|
}
|
90
|
return result;
|
91
|
}
|
92
|
|
93
|
@Override
|
94
|
public <T extends DescriptionElementBase> Map<UuidAndTitleCache, Map<UUID, Set<T>>> getTaxonFeatureDescriptionElementMap(
|
95
|
Class<T> clazz, UUID descriptiveDataSetUuid, DescriptiveSystemRole role) {
|
96
|
checkNotInPriorView("DescriptiveDataSetDao.getTaxonFeatureDescriptionElementMap(DescriptiveDataSet descriptiveDataSet, Set<Feature> features, Integer pageSize,Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
|
97
|
|
98
|
Map<UuidAndTitleCache, Map<UUID, Set<T>>> result = new HashMap<>();
|
99
|
try {
|
100
|
|
101
|
//for maps see
|
102
|
// http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html
|
103
|
// Elements of indexed collections (arrays, lists, and maps) can be referred to by index in a where clause only:
|
104
|
//
|
105
|
// Example: from Order order where order.items[0].id = 1234
|
106
|
|
107
|
|
108
|
//feature
|
109
|
String strQueryTreeId = "SELECT ws.descriptiveSystem.id FROM DescriptiveDataSet dds join dds.descriptiveSystem tree WHERE dds.uuid = :descriptiveDataSetUuid ";
|
110
|
Query queryTree = getSession().createQuery(strQueryTreeId);
|
111
|
queryTree.setParameter("descriptiveDataSetUuid", descriptiveDataSetUuid);
|
112
|
List<?> trees = queryTree.list();
|
113
|
|
114
|
|
115
|
String ftSelect = "SELECT feature.id FROM TermNode node join node.feature as feature " +
|
116
|
" WHERE node.featureTree.id in (:trees) ";
|
117
|
Query ftQuery = getSession().createQuery(ftSelect);
|
118
|
ftQuery.setParameterList("trees", trees);
|
119
|
List<?> features = ftQuery.list();
|
120
|
|
121
|
String strClass = (clazz == null )? "DescriptionElementBase" : clazz.getSimpleName();
|
122
|
|
123
|
String fetch = "";
|
124
|
if (clazz.equals(CategoricalData.class)){
|
125
|
fetch = " left join fetch el.states stateList join fetch stateList.state ";
|
126
|
}else if (clazz.equals(QuantitativeData.class)){
|
127
|
fetch = " left join fetch el.statisticalValues valueList join fetch valueList.type ";
|
128
|
}
|
129
|
|
130
|
String strQuery = " select taxon.uuid, taxon.id, taxon.titleCache, feature.uuid, el "
|
131
|
+ " from " + strClass + " el "
|
132
|
+ " join el.feature feature "
|
133
|
+ " join el.inDescription d "
|
134
|
+ " join d.taxon taxon "
|
135
|
+ " join d.descriptiveDataSets ws "
|
136
|
+ " join ws.descriptiveSystem ftree "
|
137
|
+ fetch
|
138
|
+ " where ws.uuid = :descriptiveDataSetUuid "
|
139
|
+ " and el.class = :clazz "
|
140
|
+ " and feature.id in (:features) "
|
141
|
+ " order by taxon.uuid asc, feature.uuid asc"
|
142
|
;
|
143
|
Query query = getSession().createQuery(strQuery);
|
144
|
|
145
|
query.setParameter("descriptiveDataSetUuid", descriptiveDataSetUuid);
|
146
|
query.setParameter("clazz", clazz.getSimpleName());
|
147
|
query.setParameterList("features", features);
|
148
|
|
149
|
//NOTE: Paging does not work with fetch
|
150
|
|
151
|
// fill result
|
152
|
@SuppressWarnings("unchecked")
|
153
|
List<Object[]> list = query.list();
|
154
|
for (Object[] listEntry : list){
|
155
|
UUID taxonUuid = (UUID)listEntry[0];
|
156
|
Integer id = (Integer)listEntry[1];
|
157
|
String titleCache = (String)listEntry[2];
|
158
|
UuidAndTitleCache taxon = new UuidAndTitleCache(taxonUuid, id, titleCache);
|
159
|
UUID featureUuid = (UUID)listEntry[3];
|
160
|
T data = (T)listEntry[4];
|
161
|
Map<UUID, Set<T>> taxonMap = result.get(taxon);
|
162
|
if (taxonMap == null){
|
163
|
taxonMap = new HashMap<>();
|
164
|
result.put(taxon, taxonMap);
|
165
|
}
|
166
|
Set<T> featureSet = taxonMap.get(featureUuid);
|
167
|
if (featureSet == null){
|
168
|
featureSet = new HashSet<>();
|
169
|
taxonMap.put(featureUuid, featureSet);
|
170
|
}else{
|
171
|
if (logger.isDebugEnabled()){logger.debug("feature set already exists");}
|
172
|
}
|
173
|
featureSet.add(data);
|
174
|
|
175
|
}
|
176
|
|
177
|
// defaultBeanInitializer.initialize(
|
178
|
|
179
|
return result;
|
180
|
} catch (Exception e) {
|
181
|
e.printStackTrace();
|
182
|
throw new RuntimeException(e);
|
183
|
}
|
184
|
}
|
185
|
|
186
|
/**
|
187
|
* {@inheritDoc}
|
188
|
*/
|
189
|
@Override
|
190
|
public List<UuidAndTitleCache<DescriptiveDataSet>> getDescriptiveDataSetUuidAndTitleCache(Integer limitOfInitialElements,
|
191
|
String pattern) {
|
192
|
Session session = getSession();
|
193
|
|
194
|
String queryString = "SELECT uuid, id, label FROM DescriptiveDataSet ";
|
195
|
|
196
|
if ( pattern != null){
|
197
|
queryString += " WHERE ";
|
198
|
queryString += " label LIKE :pattern";
|
199
|
|
200
|
}
|
201
|
|
202
|
Query query;
|
203
|
query = session.createQuery(queryString);
|
204
|
|
205
|
|
206
|
if (limitOfInitialElements != null){
|
207
|
query.setMaxResults(limitOfInitialElements);
|
208
|
}
|
209
|
if (pattern != null){
|
210
|
pattern = pattern.replace("*", "%");
|
211
|
pattern = pattern.replace("?", "_");
|
212
|
pattern = pattern + "%";
|
213
|
query.setParameter("pattern", pattern);
|
214
|
}
|
215
|
|
216
|
@SuppressWarnings("unchecked")
|
217
|
List<Object[]> result = query.list();
|
218
|
List<UuidAndTitleCache<DescriptiveDataSet>> list = new ArrayList<>();
|
219
|
for(Object[] object : result){
|
220
|
list.add(new UuidAndTitleCache<DescriptiveDataSet>(DescriptiveDataSet.class, (UUID) object[0],(Integer)object[1], (String)object[2]));
|
221
|
}
|
222
|
|
223
|
return list;
|
224
|
}
|
225
|
|
226
|
private List<UUID> getNodeUuidsForDescriptiveDataSet(UUID uuid) {
|
227
|
Session session = getSession();
|
228
|
|
229
|
String queryString = "SELECT t.uuid FROM DescriptiveDataSet a JOIN a.taxonSubtreeFilter as t WHERE a.uuid = :uuid";
|
230
|
|
231
|
|
232
|
Query query;
|
233
|
query = session.createQuery(queryString);
|
234
|
query.setParameter("uuid", uuid);
|
235
|
|
236
|
|
237
|
@SuppressWarnings("unchecked")
|
238
|
List<UUID> result = query.list();
|
239
|
List<UUID> list = new ArrayList<>();
|
240
|
for(UUID object : result){
|
241
|
list.add(object);
|
242
|
}
|
243
|
|
244
|
return list;
|
245
|
}
|
246
|
|
247
|
private List<UUID> getDescriptionUuidsForDescriptiveDataSet(UUID uuid) {
|
248
|
Session session = getSession();
|
249
|
|
250
|
String queryString = "SELECT t.uuid FROM DescriptiveDataSet a JOIN a.descriptions as t WHERE a.uuid = :uuid";
|
251
|
|
252
|
|
253
|
Query query;
|
254
|
query = session.createQuery(queryString);
|
255
|
query.setParameter("uuid", uuid);
|
256
|
|
257
|
|
258
|
@SuppressWarnings("unchecked")
|
259
|
List<UUID> result = query.list();
|
260
|
List<UUID> list = new ArrayList<>();
|
261
|
for(UUID object : result){
|
262
|
list.add(object);
|
263
|
}
|
264
|
|
265
|
return list;
|
266
|
}
|
267
|
|
268
|
|
269
|
|
270
|
|
271
|
@Override
|
272
|
public DescriptiveDataSetBaseDto getDescriptiveDataSetDtoByUuid(UUID uuid) {
|
273
|
String queryString = DescriptiveDataSetBaseDto.getDescriptiveDataSetDtoSelect()
|
274
|
+ " WHERE a.uuid = :uuid"
|
275
|
+ " ORDER BY a.titleCache";
|
276
|
Query query = getSession().createQuery(queryString);
|
277
|
query.setParameter("uuid", uuid);
|
278
|
|
279
|
@SuppressWarnings("unchecked")
|
280
|
List<Object[]> result = query.list();
|
281
|
|
282
|
List<DescriptiveDataSetBaseDto> list = DescriptiveDataSetBaseDto.descriptiveDataSetBaseDtoListFrom(result);
|
283
|
UUID descriptiveSystemUuid = null;
|
284
|
UUID minRankUuid = null;
|
285
|
UUID maxRankUuid = null;
|
286
|
if (result != null && !result.isEmpty()){
|
287
|
Object[] descriptiveDataSetResult = result.get(0);
|
288
|
descriptiveSystemUuid = (UUID)descriptiveDataSetResult[4];
|
289
|
minRankUuid = (UUID)descriptiveDataSetResult[5];
|
290
|
maxRankUuid = (UUID)descriptiveDataSetResult[6];
|
291
|
}else{
|
292
|
return null;
|
293
|
}
|
294
|
//get descriptiveSystem
|
295
|
DescriptiveDataSetBaseDto dto = list.get(0);
|
296
|
if (descriptiveSystemUuid != null){
|
297
|
TermTreeDto treeDto = termTreeDao.getTermTreeDtosByUuid(descriptiveSystemUuid);
|
298
|
dto.setDescriptiveSystem(treeDto);
|
299
|
}
|
300
|
//get taxon nodes
|
301
|
List<UUID> nodeUuids = getNodeUuidsForDescriptiveDataSet(uuid);
|
302
|
List<TaxonNodeDto> nodeDtos = nodeDao.getTaxonNodeDtos(nodeUuids);
|
303
|
Set<TaxonNodeDto> nodeSet = new HashSet<>(nodeDtos);
|
304
|
dto.setSubTreeFilter(nodeSet);
|
305
|
|
306
|
List<UUID> descriptionUuidList = getDescriptionUuidsForDescriptiveDataSet(uuid);
|
307
|
Set<UUID> descriptionUuids = new HashSet<>(descriptionUuidList);
|
308
|
dto.setDescriptionUuids(descriptionUuids);
|
309
|
|
310
|
TermDto minRank = termDao.getTermDto(minRankUuid);
|
311
|
TermDto maxRank = termDao.getTermDto(maxRankUuid);
|
312
|
dto.setMaxRank(maxRank);
|
313
|
dto.setMinRank(minRank);
|
314
|
return dto;
|
315
|
}
|
316
|
}
|