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