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
|
|
10
|
package eu.etaxonomy.cdm.persistence.dao.hibernate.common;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.List;
|
14
|
import java.util.UUID;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.apache.lucene.analysis.standard.StandardAnalyzer;
|
18
|
import org.apache.lucene.queryparser.classic.ParseException;
|
19
|
import org.apache.lucene.queryparser.classic.QueryParser;
|
20
|
import org.hibernate.Criteria;
|
21
|
import org.hibernate.Query;
|
22
|
import org.hibernate.Session;
|
23
|
import org.hibernate.criterion.Criterion;
|
24
|
import org.hibernate.criterion.Order;
|
25
|
import org.hibernate.criterion.Projections;
|
26
|
import org.hibernate.criterion.Restrictions;
|
27
|
import org.hibernate.envers.query.AuditEntity;
|
28
|
import org.hibernate.envers.query.AuditQuery;
|
29
|
import org.hibernate.search.FullTextSession;
|
30
|
import org.hibernate.search.Search;
|
31
|
import org.hibernate.search.SearchFactory;
|
32
|
|
33
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
34
|
import eu.etaxonomy.cdm.model.common.Credit;
|
35
|
import eu.etaxonomy.cdm.model.common.DefinedTerm;
|
36
|
import eu.etaxonomy.cdm.model.common.IIdentifiableEntity;
|
37
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
38
|
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
|
39
|
import eu.etaxonomy.cdm.model.common.LSID;
|
40
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
41
|
import eu.etaxonomy.cdm.model.media.Rights;
|
42
|
import eu.etaxonomy.cdm.persistence.dao.QueryParseException;
|
43
|
import eu.etaxonomy.cdm.persistence.dao.common.IIdentifiableDao;
|
44
|
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
|
45
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
46
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
47
|
|
48
|
|
49
|
public class IdentifiableDaoBase<T extends IdentifiableEntity> extends AnnotatableDaoImpl<T> implements IIdentifiableDao<T>{
|
50
|
@SuppressWarnings("unused")
|
51
|
private static final Logger logger = Logger.getLogger(IdentifiableDaoBase.class);
|
52
|
|
53
|
protected String defaultField = "titleCache_tokenized";
|
54
|
protected Class<? extends T> indexedClasses[];
|
55
|
|
56
|
|
57
|
|
58
|
public IdentifiableDaoBase(Class<T> type) {
|
59
|
super(type);
|
60
|
}
|
61
|
|
62
|
|
63
|
@Override
|
64
|
public List<T> findByTitle(String queryString) {
|
65
|
return findByTitle(queryString, null);
|
66
|
}
|
67
|
|
68
|
@Override
|
69
|
public List<T> findByTitle(String queryString, CdmBase sessionObject) {
|
70
|
/**
|
71
|
* FIXME why do we need to call update in a find* method? I don't know for sure
|
72
|
* that this is a good idea . . .
|
73
|
*/
|
74
|
Session session = getSession();
|
75
|
if ( sessionObject != null ) {
|
76
|
session.update(sessionObject);
|
77
|
}
|
78
|
checkNotInPriorView("IdentifiableDaoBase.findByTitle(String queryString, CdmBase sessionObject)");
|
79
|
Criteria crit = session.createCriteria(type);
|
80
|
crit.add(Restrictions.ilike("titleCache", queryString));
|
81
|
List<T> results = crit.list();
|
82
|
List<String> propertyPaths = null;
|
83
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
84
|
return results;
|
85
|
}
|
86
|
|
87
|
@Override
|
88
|
public List<T> findByTitleAndClass(String queryString, Class<T> clazz) {
|
89
|
checkNotInPriorView("IdentifiableDaoBase.findByTitleAndClass(String queryString, Class<T> clazz)");
|
90
|
Criteria crit = getSession().createCriteria(clazz);
|
91
|
crit.add(Restrictions.ilike("titleCache", queryString));
|
92
|
List<T> results = crit.list();
|
93
|
return results;
|
94
|
}
|
95
|
|
96
|
@Override
|
97
|
public List<T> findTitleCache(Class<? extends T> clazz, String queryString, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, MatchMode matchMode){
|
98
|
|
99
|
Query query = prepareFindTitleCache(clazz, queryString, pageSize,
|
100
|
pageNumber, matchMode, false);
|
101
|
List<T> result = query.list();
|
102
|
return result;
|
103
|
}
|
104
|
|
105
|
@Override
|
106
|
public Long countTitleCache(Class<? extends T> clazz, String queryString, MatchMode matchMode){
|
107
|
|
108
|
Query query = prepareFindTitleCache(clazz, queryString, null,
|
109
|
null, matchMode, true);
|
110
|
Long result = (Long)query.uniqueResult();
|
111
|
return result;
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* @param clazz filter by class - can be null to include all instances of type T
|
116
|
* @param queryString the query string to filter by
|
117
|
* @param pageSize
|
118
|
* @param pageNumber
|
119
|
* @param matchmode use a particular type of matching (can be null - defaults to exact matching)
|
120
|
* @return
|
121
|
*/
|
122
|
private Query prepareFindTitleCache(Class<? extends T> clazz,
|
123
|
String queryString, Integer pageSize, Integer pageNumber,
|
124
|
MatchMode matchMode, boolean doCount) {
|
125
|
if(clazz == null) {
|
126
|
clazz = type;
|
127
|
}
|
128
|
|
129
|
String what = (doCount ? "count(distinct e.titleCache)": "distinct e.titleCache");
|
130
|
|
131
|
if(matchMode != null){
|
132
|
queryString = matchMode.queryStringFrom(queryString);
|
133
|
}
|
134
|
String hql = "select " + what + " from " + clazz.getName() + " e where e.titleCache like '" + queryString + "'";
|
135
|
|
136
|
Query query = getSession().createQuery(hql);
|
137
|
|
138
|
if(pageSize != null && !doCount) {
|
139
|
query.setMaxResults(pageSize);
|
140
|
if(pageNumber != null) {
|
141
|
query.setFirstResult(pageNumber * pageSize);
|
142
|
}
|
143
|
}
|
144
|
return query;
|
145
|
}
|
146
|
|
147
|
|
148
|
@Override
|
149
|
public List<T> findByTitle(Class<? extends T> clazz, String queryString, MatchMode matchmode, List<Criterion> criterion, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
150
|
return findByParam(clazz, "titleCache", queryString, matchmode, criterion, pageSize, pageNumber, orderHints, propertyPaths);
|
151
|
}
|
152
|
|
153
|
@Override
|
154
|
public List<T> findByReferenceTitle(Class<? extends T> clazz, String queryString, MatchMode matchmode, List<Criterion> criterion, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
155
|
return findByParam(clazz, "title", queryString, matchmode, criterion, pageSize, pageNumber, orderHints, propertyPaths);
|
156
|
}
|
157
|
|
158
|
@Override
|
159
|
public List<T> findByTitle(String queryString, MatchMode matchmode, int page, int pagesize, List<Criterion> criteria) {
|
160
|
checkNotInPriorView("IdentifiableDaoBase.findByTitle(String queryString, MATCH_MODE matchmode, int page, int pagesize, List<Criterion> criteria)");
|
161
|
Criteria crit = getSession().createCriteria(type);
|
162
|
if (matchmode == MatchMode.EXACT) {
|
163
|
crit.add(Restrictions.eq("titleCache", matchmode.queryStringFrom(queryString)));
|
164
|
} else {
|
165
|
// crit.add(Restrictions.ilike("titleCache", matchmode.queryStringFrom(queryString)));
|
166
|
crit.add(Restrictions.like("titleCache", matchmode.queryStringFrom(queryString)));
|
167
|
}
|
168
|
if (pagesize >= 0) {
|
169
|
crit.setMaxResults(pagesize);
|
170
|
}
|
171
|
if(criteria != null){
|
172
|
for (Criterion criterion : criteria) {
|
173
|
crit.add(criterion);
|
174
|
}
|
175
|
}
|
176
|
crit.addOrder(Order.asc("titleCache"));
|
177
|
int firstItem = (page - 1) * pagesize;
|
178
|
crit.setFirstResult(firstItem);
|
179
|
List<T> results = crit.list();
|
180
|
List<String> propertyPaths = null;
|
181
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
182
|
return results;
|
183
|
}
|
184
|
|
185
|
@Override
|
186
|
public int countRights(T identifiableEntity) {
|
187
|
checkNotInPriorView("IdentifiableDaoBase.countRights(T identifiableEntity)");
|
188
|
Query query = getSession().createQuery("select count(rights) from " + type.getSimpleName() + " identifiableEntity join identifiableEntity.rights rights where identifiableEntity = :identifiableEntity");
|
189
|
query.setParameter("identifiableEntity",identifiableEntity);
|
190
|
return ((Long)query.uniqueResult()).intValue();
|
191
|
}
|
192
|
|
193
|
@Override
|
194
|
public int countSources(T identifiableEntity) {
|
195
|
checkNotInPriorView("IdentifiableDaoBase.countSources(T identifiableEntity)");
|
196
|
Query query = getSession().createQuery("SELECT COUNT(source) FROM "+identifiableEntity.getClass().getName() + " ie JOIN ie.sources source WHERE ie = :identifiableEntity");
|
197
|
query.setParameter("identifiableEntity", identifiableEntity);
|
198
|
return ((Long)query.uniqueResult()).intValue();
|
199
|
}
|
200
|
|
201
|
@Override
|
202
|
public List<Rights> getRights(T identifiableEntity, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
203
|
checkNotInPriorView("IdentifiableDaoBase.getRights(T identifiableEntity, Integer pageSize, Integer pageNumber, List<String> propertyPaths)");
|
204
|
Query query = getSession().createQuery("select rights from " + type.getSimpleName() + " identifiableEntity join identifiableEntity.rights rights where identifiableEntity = :identifiableEntity");
|
205
|
query.setParameter("identifiableEntity",identifiableEntity);
|
206
|
setPagingParameter(query, pageSize, pageNumber);
|
207
|
List<Rights> results = query.list();
|
208
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
209
|
return results;
|
210
|
}
|
211
|
|
212
|
// @Override //TODO add to interface, maybe add property path
|
213
|
public List<Credit> getCredits(T identifiableEntity, Integer pageSize, Integer pageNumber) {
|
214
|
checkNotInPriorView("IdentifiableDaoBase.getCredits(T identifiableEntity, Integer pageSize, Integer pageNumber)");
|
215
|
Query query = getSession().createQuery("select credits from " + type.getSimpleName() + " identifiableEntity join identifiableEntity.credits credits where identifiableEntity = :identifiableEntity");
|
216
|
query.setParameter("identifiableEntity",identifiableEntity);
|
217
|
setPagingParameter(query, pageSize, pageNumber);
|
218
|
return query.list();
|
219
|
}
|
220
|
|
221
|
@Override
|
222
|
public List<IdentifiableSource> getSources(T identifiableEntity, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
223
|
checkNotInPriorView("IdentifiableDaoBase.getSources(T identifiableEntity, Integer pageSize, Integer pageNumber)");
|
224
|
Query query = getSession().createQuery("SELECT source FROM "+ identifiableEntity.getClass().getName()+ " ie JOIN ie.sources source WHERE ie.id = :id");
|
225
|
query.setParameter("id",identifiableEntity.getId());
|
226
|
setPagingParameter(query, pageSize, pageNumber);
|
227
|
@SuppressWarnings("unchecked")
|
228
|
List<IdentifiableSource> results = query.list();
|
229
|
defaultBeanInitializer.initializeAll(results, propertyPaths);
|
230
|
return results;
|
231
|
}
|
232
|
|
233
|
@Override
|
234
|
public List<T> findOriginalSourceByIdInSource(String idInSource, String idNamespace) {
|
235
|
checkNotInPriorView("IdentifiableDaoBase.findOriginalSourceByIdInSource(String idInSource, String idNamespace)");
|
236
|
Query query = getSession().createQuery(
|
237
|
"Select c from " + type.getSimpleName() + " as c " +
|
238
|
"inner join c.sources as source " +
|
239
|
"where source.idInSource = :idInSource " +
|
240
|
" AND source.idNamespace = :idNamespace"
|
241
|
);
|
242
|
query.setString("idInSource", idInSource);
|
243
|
query.setString("idNamespace", idNamespace);
|
244
|
//TODO integrate reference in where
|
245
|
return query.list();
|
246
|
}
|
247
|
|
248
|
@Override
|
249
|
public T find(LSID lsid) {
|
250
|
checkNotInPriorView("IdentifiableDaoBase.find(LSID lsid)");
|
251
|
Criteria criteria = getSession().createCriteria(type);
|
252
|
criteria.add(Restrictions.eq("lsid.authority", lsid.getAuthority()));
|
253
|
criteria.add(Restrictions.eq("lsid.namespace", lsid.getNamespace()));
|
254
|
criteria.add(Restrictions.eq("lsid.object", lsid.getObject()));
|
255
|
|
256
|
if(lsid.getRevision() != null) {
|
257
|
criteria.add(Restrictions.eq("lsid.revision", lsid.getRevision()));
|
258
|
}
|
259
|
|
260
|
T object = (T)criteria.uniqueResult();
|
261
|
if(object != null) {
|
262
|
return object;
|
263
|
} else {
|
264
|
AuditQuery query = getAuditReader().createQuery().forRevisionsOfEntity(type, false, true);
|
265
|
query.add(AuditEntity.property("lsid_authority").eq(lsid.getAuthority()));
|
266
|
query.add(AuditEntity.property("lsid_namespace").eq(lsid.getNamespace()));
|
267
|
query.add(AuditEntity.property("lsid_object").eq(lsid.getObject()));
|
268
|
|
269
|
if(lsid.getRevision() != null) {
|
270
|
query.add(AuditEntity.property("lsid_revision").eq(lsid.getRevision()));
|
271
|
}
|
272
|
|
273
|
query.addOrder(AuditEntity.revisionNumber().asc());
|
274
|
query.setMaxResults(1);
|
275
|
query.setFirstResult(0);
|
276
|
List<Object[]> objs = query.getResultList();
|
277
|
if(objs.isEmpty()) {
|
278
|
return null;
|
279
|
} else {
|
280
|
return (T)objs.get(0)[0];
|
281
|
}
|
282
|
}
|
283
|
}
|
284
|
|
285
|
@Override
|
286
|
public List<UuidAndTitleCache<T>> getUuidAndTitleCache(Integer limit, String pattern){
|
287
|
Session session = getSession();
|
288
|
Query query = null;
|
289
|
if (pattern != null){
|
290
|
query = session.createQuery("select uuid, id, titleCache from " + type.getSimpleName() +" where titleCache like :pattern");
|
291
|
pattern = pattern.replace("*", "%");
|
292
|
pattern = pattern.replace("?", "_");
|
293
|
pattern = pattern + "%";
|
294
|
query.setParameter("pattern", pattern);
|
295
|
} else {
|
296
|
query = session.createQuery("select uuid, id, titleCache from " + type.getSimpleName() );
|
297
|
}
|
298
|
if (limit != null){
|
299
|
query.setMaxResults(limit);
|
300
|
}
|
301
|
return getUuidAndTitleCache(query);
|
302
|
}
|
303
|
|
304
|
|
305
|
@Override
|
306
|
public List<UuidAndTitleCache<T>> getUuidAndTitleCache(){
|
307
|
return getUuidAndTitleCache(null, null);
|
308
|
}
|
309
|
|
310
|
protected <E extends IIdentifiableEntity> List<UuidAndTitleCache<E>> getUuidAndAbbrevTitleCache(Query query){
|
311
|
List<UuidAndTitleCache<E>> list = new ArrayList<UuidAndTitleCache<E>>();
|
312
|
|
313
|
List<Object[]> result = query.list();
|
314
|
|
315
|
for(Object[] object : result){
|
316
|
list.add(new UuidAndTitleCache<E>((UUID) object[0],(Integer) object[1], (String) object[3], (String) object[2]));
|
317
|
}
|
318
|
return list;
|
319
|
}
|
320
|
|
321
|
protected <E extends IIdentifiableEntity> List<UuidAndTitleCache<E>> getUuidAndTitleCache(Query query){
|
322
|
List<UuidAndTitleCache<E>> list = new ArrayList<UuidAndTitleCache<E>>();
|
323
|
|
324
|
List<Object[]> result = query.list();
|
325
|
|
326
|
for(Object[] object : result){
|
327
|
list.add(new UuidAndTitleCache<E>((UUID) object[0],(Integer) object[1], (String) object[2]));
|
328
|
}
|
329
|
return list;
|
330
|
}
|
331
|
|
332
|
|
333
|
@Override
|
334
|
public int countByTitle(Class<? extends T> clazz, String queryString, MatchMode matchmode, List<Criterion> criterion) {
|
335
|
return countByParam(clazz, "titleCache",queryString,matchmode,criterion);
|
336
|
}
|
337
|
|
338
|
@Override
|
339
|
public int countByReferenceTitle(Class<? extends T> clazz, String queryString, MatchMode matchmode, List<Criterion> criterion) {
|
340
|
return countByParam(clazz, "title",queryString,matchmode,criterion);
|
341
|
}
|
342
|
|
343
|
@Override
|
344
|
public int count(Class<? extends T> clazz, String queryString) {
|
345
|
checkNotInPriorView("IdentifiableDaoBase.count(Class<? extends T> clazz, String queryString)");
|
346
|
QueryParser queryParser = new QueryParser(defaultField , new StandardAnalyzer());
|
347
|
|
348
|
try {
|
349
|
org.apache.lucene.search.Query query = queryParser.parse(queryString);
|
350
|
|
351
|
FullTextSession fullTextSession = Search.getFullTextSession(this.getSession());
|
352
|
org.hibernate.search.FullTextQuery fullTextQuery = null;
|
353
|
|
354
|
if(clazz == null) {
|
355
|
fullTextQuery = fullTextSession.createFullTextQuery(query, type);
|
356
|
} else {
|
357
|
fullTextQuery = fullTextSession.createFullTextQuery(query, clazz);
|
358
|
}
|
359
|
|
360
|
Integer result = fullTextQuery.getResultSize();
|
361
|
return result;
|
362
|
|
363
|
} catch (ParseException e) {
|
364
|
throw new QueryParseException(e, queryString);
|
365
|
}
|
366
|
}
|
367
|
|
368
|
@Override
|
369
|
public void optimizeIndex() {
|
370
|
FullTextSession fullTextSession = Search.getFullTextSession(getSession());
|
371
|
SearchFactory searchFactory = fullTextSession.getSearchFactory();
|
372
|
for(Class clazz : indexedClasses) {
|
373
|
searchFactory.optimize(clazz); // optimize the indices ()
|
374
|
}
|
375
|
fullTextSession.flushToIndexes();
|
376
|
}
|
377
|
|
378
|
@Override
|
379
|
public void purgeIndex() {
|
380
|
FullTextSession fullTextSession = Search.getFullTextSession(getSession());
|
381
|
for(Class clazz : indexedClasses) {
|
382
|
fullTextSession.purgeAll(clazz); // remove all objects of type t from indexes
|
383
|
}
|
384
|
fullTextSession.flushToIndexes();
|
385
|
}
|
386
|
|
387
|
@Override
|
388
|
public void rebuildIndex() {
|
389
|
FullTextSession fullTextSession = Search.getFullTextSession(getSession());
|
390
|
|
391
|
for(T t : list(null,null)) { // re-index all objects
|
392
|
fullTextSession.index(t);
|
393
|
}
|
394
|
fullTextSession.flushToIndexes();
|
395
|
}
|
396
|
|
397
|
@Override
|
398
|
public List<T> search(Class<? extends T> clazz, String queryString, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,List<String> propertyPaths) {
|
399
|
checkNotInPriorView("IdentifiableDaoBase.search(Class<? extends T> clazz, String queryString, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints,List<String> propertyPaths)");
|
400
|
QueryParser queryParser = new QueryParser(defaultField, new StandardAnalyzer());
|
401
|
List<T> results = new ArrayList<T>();
|
402
|
|
403
|
try {
|
404
|
org.apache.lucene.search.Query query = queryParser.parse(queryString);
|
405
|
|
406
|
FullTextSession fullTextSession = Search.getFullTextSession(getSession());
|
407
|
org.hibernate.search.FullTextQuery fullTextQuery = null;
|
408
|
|
409
|
if(clazz == null) {
|
410
|
fullTextQuery = fullTextSession.createFullTextQuery(query, type);
|
411
|
} else {
|
412
|
fullTextQuery = fullTextSession.createFullTextQuery(query, clazz);
|
413
|
}
|
414
|
|
415
|
addOrder(fullTextQuery,orderHints);
|
416
|
|
417
|
if(pageSize != null) {
|
418
|
fullTextQuery.setMaxResults(pageSize);
|
419
|
if(pageNumber != null) {
|
420
|
fullTextQuery.setFirstResult(pageNumber * pageSize);
|
421
|
} else {
|
422
|
fullTextQuery.setFirstResult(0);
|
423
|
}
|
424
|
}
|
425
|
|
426
|
List<T> result = fullTextQuery.list();
|
427
|
defaultBeanInitializer.initializeAll(result, propertyPaths);
|
428
|
return result;
|
429
|
|
430
|
} catch (ParseException e) {
|
431
|
throw new QueryParseException(e, queryString);
|
432
|
}
|
433
|
}
|
434
|
|
435
|
@Override
|
436
|
public String suggestQuery(String string) {
|
437
|
throw new UnsupportedOperationException("suggestQuery is not supported for objects of class " + type.getName());
|
438
|
}
|
439
|
|
440
|
@Override
|
441
|
public Integer countByTitle(String queryString) {
|
442
|
return countByTitle(queryString, null);
|
443
|
}
|
444
|
|
445
|
@Override
|
446
|
public Integer countByTitle(String queryString, CdmBase sessionObject) {
|
447
|
Session session = getSession();
|
448
|
if ( sessionObject != null ) {
|
449
|
session.update(sessionObject);
|
450
|
}
|
451
|
checkNotInPriorView("IdentifiableDaoBase.countByTitle(String queryString, CdmBase sessionObject)");
|
452
|
Criteria crit = session.createCriteria(type);
|
453
|
crit.add(Restrictions.ilike("titleCache", queryString));
|
454
|
Integer result = ((Number)crit.setProjection(Projections.rowCount()).uniqueResult()).intValue();
|
455
|
return result;
|
456
|
}
|
457
|
|
458
|
@Override
|
459
|
public Integer countByTitle(String queryString, MatchMode matchMode, List<Criterion> criteria) {
|
460
|
checkNotInPriorView("IdentifiableDaoBase.findByTitle(String queryString, MATCH_MODE matchmode, int page, int pagesize, List<Criterion> criteria)");
|
461
|
Criteria crit = getSession().createCriteria(type);
|
462
|
if (matchMode == MatchMode.EXACT) {
|
463
|
crit.add(Restrictions.eq("titleCache", matchMode.queryStringFrom(queryString)));
|
464
|
} else {
|
465
|
// crit.add(Restrictions.ilike("titleCache", matchmode.queryStringFrom(queryString)));
|
466
|
crit.add(Restrictions.like("titleCache", matchMode.queryStringFrom(queryString)));
|
467
|
}
|
468
|
|
469
|
if(criteria != null){
|
470
|
for (Criterion criterion : criteria) {
|
471
|
crit.add(criterion);
|
472
|
}
|
473
|
}
|
474
|
|
475
|
|
476
|
Integer result = ((Number)crit.setProjection(Projections.rowCount()).uniqueResult()).intValue();
|
477
|
return result;
|
478
|
}
|
479
|
|
480
|
|
481
|
@Override
|
482
|
public <S extends T> int countByIdentifier(Class<S> clazz,
|
483
|
String identifier, DefinedTerm identifierType, MatchMode matchmode) {
|
484
|
checkNotInPriorView("IdentifiableDaoBase.countByIdentifier(T clazz, String identifier, DefinedTerm identifierType, MatchMode matchmode)");
|
485
|
|
486
|
Class<?> clazzParam = clazz == null ? type : clazz;
|
487
|
String queryString = "SELECT count(*) FROM " + clazzParam.getSimpleName() + " as c " +
|
488
|
"INNER JOIN c.identifiers as ids " +
|
489
|
"WHERE (1=1) ";
|
490
|
if (identifier != null){
|
491
|
if (matchmode == null || matchmode == MatchMode.EXACT){
|
492
|
queryString += " AND ids.identifier = '" + identifier + "'";
|
493
|
}else {
|
494
|
queryString += " AND ids.identifier LIKE '" + matchmode.queryStringFrom(identifier) + "'";
|
495
|
}
|
496
|
}
|
497
|
if (identifierType != null){
|
498
|
queryString += " AND ids.type = :type";
|
499
|
}
|
500
|
|
501
|
Query query = getSession().createQuery(queryString);
|
502
|
if (identifierType != null){
|
503
|
query.setEntity("type", identifierType);
|
504
|
}
|
505
|
|
506
|
Long c = (Long)query.uniqueResult();
|
507
|
return c.intValue();
|
508
|
}
|
509
|
|
510
|
@Override
|
511
|
public <S extends T> List<Object[]> findByIdentifier(
|
512
|
Class<S> clazz, String identifier, DefinedTerm identifierType,
|
513
|
MatchMode matchmode, boolean includeEntity,
|
514
|
Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
515
|
|
516
|
checkNotInPriorView("IdentifiableDaoBase.findByIdentifier(T clazz, String identifier, DefinedTerm identifierType, MatchMode matchmode, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
|
517
|
|
518
|
Class<?> clazzParam = clazz == null ? type : clazz;
|
519
|
String queryString = "SELECT ids.type, ids.identifier, %s FROM %s as c " +
|
520
|
" INNER JOIN c.identifiers as ids " +
|
521
|
" WHERE (1=1) ";
|
522
|
queryString = String.format(queryString, (includeEntity ? "c":"c.uuid, c.titleCache") , clazzParam.getSimpleName());
|
523
|
|
524
|
//Matchmode and identifier
|
525
|
if (identifier != null){
|
526
|
if (matchmode == null || matchmode == MatchMode.EXACT){
|
527
|
queryString += " AND ids.identifier = '" + identifier + "'";
|
528
|
}else {
|
529
|
queryString += " AND ids.identifier LIKE '" + matchmode.queryStringFrom(identifier) + "'";
|
530
|
}
|
531
|
}
|
532
|
if (identifierType != null){
|
533
|
queryString += " AND ids.type = :type";
|
534
|
}
|
535
|
//order
|
536
|
queryString +=" ORDER BY ids.type.uuid, ids.identifier, c.uuid ";
|
537
|
|
538
|
Query query = getSession().createQuery(queryString);
|
539
|
|
540
|
//parameters
|
541
|
if (identifierType != null){
|
542
|
query.setEntity("type", identifierType);
|
543
|
}
|
544
|
|
545
|
//paging
|
546
|
setPagingParameter(query, pageSize, pageNumber);
|
547
|
|
548
|
List<Object[]> results = query.list();
|
549
|
//initialize
|
550
|
if (includeEntity){
|
551
|
List<S> entities = new ArrayList<S>();
|
552
|
for (Object[] result : results){
|
553
|
entities.add((S)result[2]);
|
554
|
}
|
555
|
defaultBeanInitializer.initializeAll(entities, propertyPaths);
|
556
|
}
|
557
|
return results;
|
558
|
}
|
559
|
|
560
|
@Override
|
561
|
public <S extends T> long countByMarker(Class<S> clazz, MarkerType markerType,
|
562
|
Boolean markerValue) {
|
563
|
checkNotInPriorView("IdentifiableDaoBase.countByMarker(T clazz, MarkerType markerType, Boolean markerValue)");
|
564
|
|
565
|
if (markerType == null){
|
566
|
return 0;
|
567
|
}
|
568
|
Class<?> clazzParam = clazz == null ? type : clazz;
|
569
|
String queryString = "SELECT count(*) FROM " + clazzParam.getSimpleName() + " as c " +
|
570
|
"INNER JOIN c.markers as mks " +
|
571
|
"WHERE (1=1) ";
|
572
|
|
573
|
if (markerValue != null){
|
574
|
queryString += " AND mks.flag = :flag";
|
575
|
}
|
576
|
queryString += " AND mks.markerType = :type";
|
577
|
|
578
|
Query query = getSession().createQuery(queryString);
|
579
|
query.setEntity("type", markerType);
|
580
|
if (markerValue != null){
|
581
|
query.setBoolean("flag", markerValue);
|
582
|
}
|
583
|
|
584
|
Long c = (Long)query.uniqueResult();
|
585
|
return c;
|
586
|
}
|
587
|
|
588
|
@Override
|
589
|
public <S extends T> List<Object[]> findByMarker(
|
590
|
Class<S> clazz, MarkerType markerType,
|
591
|
Boolean markerValue, boolean includeEntity, Integer pageSize, Integer pageNumber,
|
592
|
List<String> propertyPaths) {
|
593
|
|
594
|
checkNotInPriorView("IdentifiableDaoBase.findByMarker(T clazz, String identifier, DefinedTerm identifierType, MatchMode matchmode, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths)");
|
595
|
if (markerType == null){
|
596
|
return new ArrayList<Object[]>();
|
597
|
}
|
598
|
|
599
|
Class<?> clazzParam = clazz == null ? type : clazz;
|
600
|
String queryString = "SELECT mks.markerType, mks.flag, %s FROM %s as c " +
|
601
|
" INNER JOIN c.markers as mks " +
|
602
|
" WHERE (1=1) ";
|
603
|
queryString = String.format(queryString, (includeEntity ? "c":"c.uuid, c.titleCache") , clazzParam.getSimpleName());
|
604
|
|
605
|
//Matchmode and identifier
|
606
|
if (markerValue != null){
|
607
|
queryString += " AND mks.flag = :flag";
|
608
|
}
|
609
|
queryString += " AND mks.markerType = :type";
|
610
|
|
611
|
//order
|
612
|
queryString +=" ORDER BY mks.markerType.uuid, mks.flag, c.uuid ";
|
613
|
|
614
|
Query query = getSession().createQuery(queryString);
|
615
|
|
616
|
//parameters
|
617
|
query.setEntity("type", markerType);
|
618
|
if (markerValue != null){
|
619
|
query.setBoolean("flag", markerValue);
|
620
|
}
|
621
|
|
622
|
//paging
|
623
|
setPagingParameter(query, pageSize, pageNumber);
|
624
|
|
625
|
@SuppressWarnings("unchecked")
|
626
|
List<Object[]> results = query.list();
|
627
|
//initialize
|
628
|
if (includeEntity){
|
629
|
List<S> entities = new ArrayList<S>();
|
630
|
for (Object[] result : results){
|
631
|
entities.add((S)result[2]);
|
632
|
}
|
633
|
defaultBeanInitializer.initializeAll(entities, propertyPaths);
|
634
|
}
|
635
|
return results;
|
636
|
}
|
637
|
|
638
|
}
|