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.api.service;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.Arrays;
|
14
|
import java.util.HashMap;
|
15
|
import java.util.HashSet;
|
16
|
import java.util.List;
|
17
|
import java.util.Map;
|
18
|
import java.util.Set;
|
19
|
import java.util.UUID;
|
20
|
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.hibernate.criterion.Criterion;
|
23
|
import org.springframework.transaction.annotation.Transactional;
|
24
|
|
25
|
import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
|
26
|
import eu.etaxonomy.cdm.api.service.dto.CdmEntityIdentifier;
|
27
|
import eu.etaxonomy.cdm.api.service.dto.IdentifiedEntityDTO;
|
28
|
import eu.etaxonomy.cdm.api.service.dto.MarkedEntityDTO;
|
29
|
import eu.etaxonomy.cdm.api.service.pager.Pager;
|
30
|
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
|
31
|
import eu.etaxonomy.cdm.common.monitor.DefaultProgressMonitor;
|
32
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
33
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
34
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
35
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
36
|
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
|
37
|
import eu.etaxonomy.cdm.model.common.LSID;
|
38
|
import eu.etaxonomy.cdm.model.common.MarkerType;
|
39
|
import eu.etaxonomy.cdm.model.media.Rights;
|
40
|
import eu.etaxonomy.cdm.model.reference.ISourceable;
|
41
|
import eu.etaxonomy.cdm.model.term.DefinedTerm;
|
42
|
import eu.etaxonomy.cdm.persistence.dao.common.IIdentifiableDao;
|
43
|
import eu.etaxonomy.cdm.persistence.dao.common.Restriction;
|
44
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.HibernateBeanInitializer;
|
45
|
import eu.etaxonomy.cdm.persistence.dao.initializer.AutoPropertyInitializer;
|
46
|
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
|
47
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
48
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
49
|
import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
|
50
|
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
|
51
|
import eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy;
|
52
|
import eu.etaxonomy.cdm.strategy.match.IMatchStrategyEqual;
|
53
|
import eu.etaxonomy.cdm.strategy.match.IMatchable;
|
54
|
import eu.etaxonomy.cdm.strategy.match.MatchException;
|
55
|
import eu.etaxonomy.cdm.strategy.merge.IMergable;
|
56
|
import eu.etaxonomy.cdm.strategy.merge.IMergeStrategy;
|
57
|
import eu.etaxonomy.cdm.strategy.merge.MergeException;
|
58
|
|
59
|
public abstract class IdentifiableServiceBase<T extends IdentifiableEntity, DAO extends IIdentifiableDao<T>> extends AnnotatableServiceBase<T,DAO>
|
60
|
implements IIdentifiableEntityService<T>{
|
61
|
|
62
|
|
63
|
protected static final int UPDATE_TITLE_CACHE_DEFAULT_STEP_SIZE = 1000;
|
64
|
protected static final Logger logger = Logger.getLogger(IdentifiableServiceBase.class);
|
65
|
|
66
|
@Override
|
67
|
@Transactional(readOnly = true)
|
68
|
public Pager<Rights> getRights(T t, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
69
|
long numberOfResults = dao.countRights(t);
|
70
|
|
71
|
List<Rights> results = new ArrayList<>();
|
72
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
73
|
results = dao.getRights(t, pageSize, pageNumber,propertyPaths);
|
74
|
}
|
75
|
|
76
|
return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
|
77
|
}
|
78
|
|
79
|
@Override
|
80
|
@Transactional(readOnly = true)
|
81
|
public Pager<IdentifiableSource> getSources(T t, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
82
|
long numberOfResults = dao.countSources(t);
|
83
|
|
84
|
List<IdentifiableSource> results = new ArrayList<>();
|
85
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
86
|
results = dao.getSources(t, pageSize, pageNumber,propertyPaths);
|
87
|
}
|
88
|
|
89
|
return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
|
90
|
}
|
91
|
|
92
|
|
93
|
@Transactional(readOnly = false)
|
94
|
@Override
|
95
|
public T replace(T x, T y) {
|
96
|
return dao.replace(x, y);
|
97
|
}
|
98
|
|
99
|
/*
|
100
|
* TODO - Migrated from CommonServiceBase
|
101
|
*/
|
102
|
@Transactional(readOnly = true)
|
103
|
@Override
|
104
|
public ISourceable getSourcedObjectByIdInSource(Class clazz, String idInSource, String idNamespace) {
|
105
|
ISourceable<?> result = null;
|
106
|
|
107
|
List<T> list = dao.findOriginalSourceByIdInSource(idInSource, idNamespace);
|
108
|
if (! list.isEmpty()){
|
109
|
result = list.get(0);
|
110
|
}
|
111
|
return result;
|
112
|
}
|
113
|
|
114
|
@Transactional(readOnly = true)
|
115
|
@Override
|
116
|
public List<UuidAndTitleCache<T>> getUuidAndTitleCache(Integer limit, String pattern) {
|
117
|
return dao.getUuidAndTitleCache(limit, pattern);
|
118
|
}
|
119
|
|
120
|
@Transactional(readOnly = true)
|
121
|
@Override
|
122
|
public <S extends T> List<UuidAndTitleCache<S>> getUuidAndTitleCache(Class<S> clazz,Integer limit, String pattern) {
|
123
|
return dao.getUuidAndTitleCache(clazz, limit, pattern);
|
124
|
}
|
125
|
|
126
|
@Transactional(readOnly = true)
|
127
|
@Override
|
128
|
public String getTitleCache(UUID uuid, boolean refresh){
|
129
|
return dao.getTitleCache(uuid, refresh);
|
130
|
}
|
131
|
|
132
|
@Transactional(readOnly = true)
|
133
|
@Override
|
134
|
public <S extends T> Pager<S> findByTitle(Class<S> clazz, String queryString,MatchMode matchmode, List<Criterion> criteria, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
135
|
long numberOfResults = dao.countByTitle(clazz, queryString, matchmode, criteria);
|
136
|
|
137
|
List<S> results = new ArrayList<>();
|
138
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
139
|
results = dao.findByTitle(clazz, queryString, matchmode, criteria, pageSize, pageNumber, orderHints, propertyPaths);
|
140
|
}
|
141
|
|
142
|
return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
|
143
|
}
|
144
|
|
145
|
@Transactional(readOnly = true)
|
146
|
@Override
|
147
|
public <S extends T> Pager<S> findByTitleWithRestrictions(Class<S> clazz, String queryString, MatchMode matchmode, List<Restriction<?>> restrictions, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
148
|
long numberOfResults = dao.countByTitleWithRestrictions(clazz, queryString, matchmode, restrictions);
|
149
|
|
150
|
List<S> results = new ArrayList<>();
|
151
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
152
|
results = dao.findByTitleWithRestrictions(clazz, queryString, matchmode, restrictions, pageSize, pageNumber, orderHints, propertyPaths);
|
153
|
}
|
154
|
|
155
|
return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
|
156
|
}
|
157
|
|
158
|
|
159
|
@Transactional(readOnly = true)
|
160
|
@Override
|
161
|
public <S extends T> Pager<S> findByTitle(IIdentifiableEntityServiceConfigurator<S> config){
|
162
|
|
163
|
boolean withRestrictions = config.getRestrictions() != null && !config.getRestrictions().isEmpty();
|
164
|
boolean withCriteria = config.getCriteria() != null && !config.getCriteria().isEmpty();
|
165
|
|
166
|
if(withCriteria && withRestrictions){
|
167
|
throw new RuntimeException("Restrictions and Criteria can not be used at the same time");
|
168
|
} else if(withRestrictions){
|
169
|
return findByTitleWithRestrictions((Class<S>)config.getClazz(), config.getTitleSearchStringSqlized(), config.getMatchMode(), config.getRestrictions(), config.getPageSize(), config.getPageNumber(), config.getOrderHints(), config.getPropertyPaths());
|
170
|
} else {
|
171
|
return findByTitle((Class<S>) config.getClazz(), config.getTitleSearchStringSqlized(), config.getMatchMode(), config.getCriteria(), config.getPageSize(), config.getPageNumber(), config.getOrderHints(), config.getPropertyPaths());
|
172
|
}
|
173
|
}
|
174
|
|
175
|
@Transactional(readOnly = true)
|
176
|
@Override
|
177
|
public <S extends T> List<S> listByTitle(Class<S> clazz, String queryString,MatchMode matchmode, List<Criterion> criteria, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
178
|
long numberOfResults = dao.countByTitle(clazz, queryString, matchmode, criteria);
|
179
|
|
180
|
List<S> results = new ArrayList<>();
|
181
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
182
|
results = dao.findByTitle(clazz, queryString, matchmode, criteria, pageSize, pageNumber, orderHints, propertyPaths);
|
183
|
}
|
184
|
return results;
|
185
|
}
|
186
|
|
187
|
@Transactional(readOnly = true)
|
188
|
@Override
|
189
|
public <S extends T> List<S> listByTitleWithRestrictions(Class<S> clazz, String queryString,MatchMode matchmode, List<Restriction<?>> restrictions, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
190
|
long numberOfResults = dao.countByTitleWithRestrictions(clazz, queryString, matchmode, restrictions);
|
191
|
|
192
|
List<S> results = new ArrayList<>();
|
193
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
194
|
results = dao.findByTitleWithRestrictions(clazz, queryString, matchmode, restrictions, pageSize, pageNumber, orderHints, propertyPaths);
|
195
|
}
|
196
|
return results;
|
197
|
}
|
198
|
|
199
|
@Transactional(readOnly = true)
|
200
|
@Override
|
201
|
public <S extends T> Pager<S> findTitleCache(Class<S> clazz, String queryString, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, MatchMode matchMode){
|
202
|
long numberOfResults = dao.countTitleCache(clazz, queryString, matchMode);
|
203
|
|
204
|
List<S> results = new ArrayList<>();
|
205
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
206
|
results = dao.findTitleCache(clazz, queryString, pageSize, pageNumber, orderHints, matchMode);
|
207
|
}
|
208
|
long r = 0;
|
209
|
r += numberOfResults;
|
210
|
|
211
|
return new DefaultPagerImpl<>(pageNumber, r , pageSize, results);
|
212
|
}
|
213
|
|
214
|
@Transactional(readOnly = true)
|
215
|
@Override
|
216
|
public <S extends T> List<S> listByReferenceTitle(Class<S> clazz, String queryString,MatchMode matchmode, List<Criterion> criteria, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
217
|
long numberOfResults = dao.countByReferenceTitle(clazz, queryString, matchmode, criteria);
|
218
|
|
219
|
List<S> results = new ArrayList<>();
|
220
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
221
|
results = dao.findByReferenceTitle(clazz, queryString, matchmode, criteria, pageSize, pageNumber, orderHints, propertyPaths);
|
222
|
}
|
223
|
return results;
|
224
|
}
|
225
|
|
226
|
@Transactional(readOnly = true)
|
227
|
@Override
|
228
|
public <S extends T> List<S> listByReferenceTitleWithRestrictions(Class<S> clazz, String queryString,MatchMode matchmode, List<Restriction<?>> restrictions, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
229
|
long numberOfResults = dao.countByReferenceTitleWithRestrictions(clazz, queryString, matchmode, restrictions);
|
230
|
|
231
|
List<S> results = new ArrayList<>();
|
232
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
233
|
results = dao.findByReferenceTitleWithRestrictions(clazz, queryString, matchmode, restrictions, pageSize, pageNumber, orderHints, propertyPaths);
|
234
|
}
|
235
|
return results;
|
236
|
}
|
237
|
|
238
|
@Transactional(readOnly = true)
|
239
|
@Override
|
240
|
public T find(LSID lsid) {
|
241
|
return dao.find(lsid);
|
242
|
}
|
243
|
|
244
|
@Transactional(readOnly = true)
|
245
|
@Override
|
246
|
public Pager<T> search(Class<? extends T> clazz, String queryString, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
247
|
// public <S extends T> Pager<S> search(Class<S> clazz, String queryString, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
248
|
long numberOfResults = dao.count(clazz,queryString);
|
249
|
|
250
|
List<T> results = new ArrayList<>();
|
251
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
252
|
results = dao.search(clazz,queryString, pageSize, pageNumber, orderHints, propertyPaths);
|
253
|
}
|
254
|
|
255
|
return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, results);
|
256
|
}
|
257
|
|
258
|
@Override
|
259
|
@Transactional(readOnly = false)
|
260
|
public UpdateResult updateCaches() {
|
261
|
return updateCaches(null, null, null, null);
|
262
|
}
|
263
|
|
264
|
@Transactional(readOnly = false) //TODO check transactional behavior, e.g. what happens with the session if count is very large
|
265
|
protected <S extends T > UpdateResult updateCachesImpl(Class<S> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<T> cacheStrategy, IProgressMonitor monitor) {
|
266
|
if (stepSize == null){
|
267
|
stepSize = UPDATE_TITLE_CACHE_DEFAULT_STEP_SIZE;
|
268
|
}
|
269
|
if (monitor == null){
|
270
|
monitor = DefaultProgressMonitor.NewInstance();
|
271
|
}
|
272
|
UpdateResult result = new UpdateResult();
|
273
|
long count = dao.count(clazz);
|
274
|
long countUpdated = 0;
|
275
|
monitor.beginTask("update titles for " + clazz.getSimpleName(), Long.valueOf(count).intValue());
|
276
|
int worked = 0;
|
277
|
Set<CdmEntityIdentifier> updatedCdmIds = new HashSet();
|
278
|
for(int i = 0 ; i < count ; i = i + stepSize){
|
279
|
// not sure if such strict ordering is necessary here, but for safety reasons I do it
|
280
|
ArrayList<OrderHint> orderHints = new ArrayList<>();
|
281
|
orderHints.add( new OrderHint("id", OrderHint.SortOrder.ASCENDING));
|
282
|
|
283
|
|
284
|
Map<Class<? extends CdmBase>, AutoPropertyInitializer<CdmBase>> oldAutoInit = switchOfAutoinitializer();
|
285
|
List<S> list = this.list(clazz, stepSize, i, orderHints, null);
|
286
|
switchOnOldAutoInitializer(oldAutoInit);
|
287
|
|
288
|
List<T> entitiesToUpdate = new ArrayList<>();
|
289
|
for (T entity : list){
|
290
|
entity = HibernateProxyHelper.deproxy(entity);
|
291
|
if (entity.updateCaches(cacheStrategy)){
|
292
|
countUpdated++;
|
293
|
updatedCdmIds.add(new CdmEntityIdentifier(entity.getId(), clazz));
|
294
|
}
|
295
|
worked++;
|
296
|
monitor.internalWorked(1);
|
297
|
}
|
298
|
|
299
|
|
300
|
if (monitor.isCanceled()){
|
301
|
break;
|
302
|
}
|
303
|
}
|
304
|
monitor.done();
|
305
|
result.addUpdatedCdmIds(updatedCdmIds);
|
306
|
return result;
|
307
|
}
|
308
|
|
309
|
/**
|
310
|
* Brings back all auto initializers to the bean initializer
|
311
|
* @see #switchOfAutoinitializer()
|
312
|
* @param oldAutoInit
|
313
|
*/
|
314
|
protected void switchOnOldAutoInitializer(
|
315
|
Map<Class<? extends CdmBase>, AutoPropertyInitializer<CdmBase>> oldAutoInit) {
|
316
|
HibernateBeanInitializer initializer = (HibernateBeanInitializer)this.appContext.getBean("defaultBeanInitializer");
|
317
|
initializer.setBeanAutoInitializers(oldAutoInit);
|
318
|
}
|
319
|
|
320
|
/**
|
321
|
* Removes all auto initializers from the bean initializer
|
322
|
*
|
323
|
* @see #switchOnOldAutoInitializer(Map)
|
324
|
* @return
|
325
|
*/
|
326
|
protected Map<Class<? extends CdmBase>, AutoPropertyInitializer<CdmBase>> switchOfAutoinitializer() {
|
327
|
HibernateBeanInitializer initializer = (HibernateBeanInitializer)this.appContext.getBean("defaultBeanInitializer");
|
328
|
Map<Class<? extends CdmBase>, AutoPropertyInitializer<CdmBase>> oldAutoInitializers = initializer.getBeanAutoInitializers();
|
329
|
Map<Class<? extends CdmBase>, AutoPropertyInitializer<CdmBase>> map = new HashMap<>();
|
330
|
initializer.setBeanAutoInitializers(map);
|
331
|
return oldAutoInitializers;
|
332
|
}
|
333
|
|
334
|
private class DeduplicateState{
|
335
|
String lastTitleCache;
|
336
|
Integer pageSize = 50;
|
337
|
int nPages = 3;
|
338
|
int startPage = 0;
|
339
|
boolean isCompleted = false;
|
340
|
int result;
|
341
|
}
|
342
|
|
343
|
@Override
|
344
|
@Transactional(readOnly = false)
|
345
|
public int deduplicate(Class<? extends T> clazz, IMatchStrategyEqual matchStrategy, IMergeStrategy mergeStrategy) {
|
346
|
DeduplicateState dedupState = new DeduplicateState();
|
347
|
|
348
|
if (clazz == null){
|
349
|
logger.warn("Deduplication clazz must not be null!");
|
350
|
return 0;
|
351
|
}
|
352
|
if (! ( IMatchable.class.isAssignableFrom(clazz) && IMergable.class.isAssignableFrom(clazz) ) ){
|
353
|
logger.warn("Deduplication implemented only for classes implementing IMatchable and IMergeable. No deduplication performed!");
|
354
|
return 0;
|
355
|
}
|
356
|
Class matchableClass = clazz;
|
357
|
if (matchStrategy == null){
|
358
|
matchStrategy = DefaultMatchStrategy.NewInstance(matchableClass);
|
359
|
}
|
360
|
List<T> nextGroup = new ArrayList<>();
|
361
|
|
362
|
int result = 0;
|
363
|
// double countTotal = count(clazz);
|
364
|
//
|
365
|
// Number countPagesN = Math.ceil(countTotal/dedupState.pageSize.doubleValue()) ;
|
366
|
// int countPages = countPagesN.intValue();
|
367
|
//
|
368
|
|
369
|
List<OrderHint> orderHints = Arrays.asList(new OrderHint[]{new OrderHint("titleCache", SortOrder.ASCENDING)});
|
370
|
|
371
|
while (! dedupState.isCompleted){
|
372
|
//get x page sizes
|
373
|
List<? extends T> objectList = getPages(clazz, dedupState, orderHints);
|
374
|
//after each page check if any changes took place
|
375
|
int nUnEqualPages = handleAllPages(objectList, dedupState, nextGroup, matchStrategy, mergeStrategy);
|
376
|
nUnEqualPages = nUnEqualPages + dedupState.pageSize * dedupState.startPage;
|
377
|
//refresh start page counter
|
378
|
int finishedPages = nUnEqualPages / dedupState.pageSize;
|
379
|
dedupState.startPage = finishedPages;
|
380
|
}
|
381
|
|
382
|
result += handleLastGroup(nextGroup, matchStrategy, mergeStrategy);
|
383
|
return result;
|
384
|
}
|
385
|
|
386
|
|
387
|
private int handleAllPages(List<? extends T> objectList, DeduplicateState dedupState, List<T> nextGroup, IMatchStrategyEqual matchStrategy, IMergeStrategy mergeStrategy) {
|
388
|
int nUnEqual = 0;
|
389
|
for (T object : objectList){
|
390
|
String currentTitleCache = object.getTitleCache();
|
391
|
if (currentTitleCache != null && currentTitleCache.equals(dedupState.lastTitleCache)){
|
392
|
//=titleCache
|
393
|
nextGroup.add(object);
|
394
|
}else{
|
395
|
//<> titleCache
|
396
|
dedupState.result += handleLastGroup(nextGroup, matchStrategy, mergeStrategy);
|
397
|
nextGroup = new ArrayList<>();
|
398
|
nextGroup.add(object);
|
399
|
nUnEqual++;
|
400
|
}
|
401
|
dedupState.lastTitleCache = currentTitleCache;
|
402
|
}
|
403
|
handleLastGroup(nextGroup, matchStrategy, mergeStrategy);
|
404
|
return nUnEqual;
|
405
|
}
|
406
|
|
407
|
private <S extends T> List<S> getPages(Class<S> clazz, DeduplicateState dedupState, List<OrderHint> orderHints) {
|
408
|
List<S> result = new ArrayList<>();
|
409
|
for (int pageNo = dedupState.startPage; pageNo < dedupState.startPage + dedupState.nPages; pageNo++){
|
410
|
List<S> objectList = listByTitleWithRestrictions(clazz, null, null, null, dedupState.pageSize, pageNo, orderHints, null);
|
411
|
result.addAll(objectList);
|
412
|
}
|
413
|
if (result.size()< dedupState.nPages * dedupState.pageSize ){
|
414
|
dedupState.isCompleted = true;
|
415
|
}
|
416
|
return result;
|
417
|
}
|
418
|
|
419
|
private int handleLastGroup(List<T> group, IMatchStrategyEqual matchStrategy, IMergeStrategy mergeStrategy) {
|
420
|
int result = 0;
|
421
|
int size = group.size();
|
422
|
Set<Integer> exclude = new HashSet<>(); //set to collect all objects, that have been merged already
|
423
|
for (int i = 0; i < size - 1; i++){
|
424
|
if (exclude.contains(i)){
|
425
|
continue;
|
426
|
}
|
427
|
for (int j = i + 1; j < size; j++){
|
428
|
if (exclude.contains(j)){
|
429
|
continue;
|
430
|
}
|
431
|
T firstObject = group.get(i);
|
432
|
T secondObject = group.get(j);
|
433
|
|
434
|
try {
|
435
|
if (matchStrategy.invoke((IMatchable)firstObject, (IMatchable)secondObject).isSuccessful()){
|
436
|
commonService.merge((IMergable)firstObject, (IMergable)secondObject, mergeStrategy);
|
437
|
exclude.add(j);
|
438
|
result++;
|
439
|
}
|
440
|
} catch (MatchException e) {
|
441
|
logger.warn("MatchException when trying to match " + firstObject.getTitleCache());
|
442
|
e.printStackTrace();
|
443
|
} catch (MergeException e) {
|
444
|
logger.warn("MergeException when trying to merge " + firstObject.getTitleCache());
|
445
|
e.printStackTrace();
|
446
|
}
|
447
|
}
|
448
|
}
|
449
|
return result;
|
450
|
}
|
451
|
|
452
|
@Transactional(readOnly = true)
|
453
|
@Override
|
454
|
public long countByTitle(Class<? extends T> clazz, String queryString,MatchMode matchmode, List<Criterion> criteria){
|
455
|
long numberOfResults = dao.countByTitle(clazz, queryString, matchmode, criteria);
|
456
|
|
457
|
return numberOfResults;
|
458
|
}
|
459
|
|
460
|
@Transactional(readOnly = true)
|
461
|
@Override
|
462
|
public long countByTitleWithRestrictions(Class<? extends T> clazz, String queryString, MatchMode matchmode, List<Restriction<?>> restrictions){
|
463
|
long numberOfResults = dao.countByTitleWithRestrictions(clazz, queryString, matchmode, restrictions);
|
464
|
|
465
|
return numberOfResults;
|
466
|
}
|
467
|
|
468
|
@Transactional(readOnly = true)
|
469
|
@Override
|
470
|
public long countByTitle(IIdentifiableEntityServiceConfigurator<T> config){
|
471
|
|
472
|
boolean withRestrictions = config.getRestrictions() != null && !config.getRestrictions().isEmpty();
|
473
|
boolean withCriteria = config.getCriteria() != null && !config.getCriteria().isEmpty();
|
474
|
|
475
|
if(withCriteria && withRestrictions){
|
476
|
throw new RuntimeException("Restrictions and Criteria can not be used at the same time");
|
477
|
} else if(withRestrictions){
|
478
|
return countByTitleWithRestrictions(config.getClazz(), config.getTitleSearchStringSqlized(), config.getMatchMode(), config.getRestrictions());
|
479
|
} else {
|
480
|
return countByTitle(config.getClazz(), config.getTitleSearchStringSqlized(), config.getMatchMode(), config.getCriteria());
|
481
|
}
|
482
|
|
483
|
}
|
484
|
|
485
|
@Override
|
486
|
@Transactional(readOnly = true)
|
487
|
public <S extends T> Pager<IdentifiedEntityDTO<S>> findByIdentifier(
|
488
|
Class<S> clazz, String identifier, DefinedTerm identifierType, MatchMode matchmode,
|
489
|
boolean includeEntity, Integer pageSize,
|
490
|
Integer pageNumber, List<String> propertyPaths) {
|
491
|
|
492
|
long numberOfResults = dao.countByIdentifier(clazz, identifier, identifierType, matchmode);
|
493
|
List<Object[]> daoResults = new ArrayList<>();
|
494
|
if(numberOfResults > 0) { // no point checking again
|
495
|
daoResults = dao.findByIdentifier(clazz, identifier, identifierType,
|
496
|
matchmode, includeEntity, pageSize, pageNumber, propertyPaths);
|
497
|
}
|
498
|
|
499
|
List<IdentifiedEntityDTO<S>> result = new ArrayList<>();
|
500
|
for (Object[] daoObj : daoResults){
|
501
|
if (includeEntity){
|
502
|
result.add(new IdentifiedEntityDTO<>((DefinedTerm)daoObj[0], (String)daoObj[1], (S)daoObj[2]));
|
503
|
}else{
|
504
|
result.add(new IdentifiedEntityDTO<>((DefinedTerm)daoObj[0], (String)daoObj[1], (UUID)daoObj[2], (String)daoObj[3], null));
|
505
|
}
|
506
|
}
|
507
|
return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, result);
|
508
|
}
|
509
|
|
510
|
@Override
|
511
|
@Transactional(readOnly = true)
|
512
|
public <S extends T> List<IdentifiedEntityDTO<S>> listByIdentifier(
|
513
|
Class<S> clazz, String identifier, DefinedTerm identifierType, MatchMode matchmode,
|
514
|
boolean includeEntity, List<String> propertyPaths, Integer limit) {
|
515
|
|
516
|
long numberOfResults = dao.countByIdentifier(clazz, identifier, identifierType, matchmode);
|
517
|
List<Object[]> daoResults = new ArrayList<>();
|
518
|
if(numberOfResults > 0) { // no point checking again
|
519
|
daoResults = dao.findByIdentifier(clazz, identifier, identifierType,
|
520
|
matchmode, includeEntity, limit, 0, propertyPaths);
|
521
|
}
|
522
|
|
523
|
List<IdentifiedEntityDTO<S>> result = new ArrayList<>();
|
524
|
for (Object[] daoObj : daoResults){
|
525
|
if (includeEntity){
|
526
|
result.add(new IdentifiedEntityDTO<>((DefinedTerm)daoObj[0], (String)daoObj[1], (S)daoObj[2]));
|
527
|
}else{
|
528
|
result.add(new IdentifiedEntityDTO<>((DefinedTerm)daoObj[0], (String)daoObj[1], (UUID)daoObj[2], (String)daoObj[3], null));
|
529
|
}
|
530
|
}
|
531
|
return result;
|
532
|
}
|
533
|
|
534
|
|
535
|
@Override
|
536
|
@Transactional(readOnly = true)
|
537
|
public <S extends T> Pager<MarkedEntityDTO<S>> findByMarker(
|
538
|
Class<S> clazz, MarkerType markerType, Boolean markerValue,
|
539
|
boolean includeEntity, Integer pageSize,
|
540
|
Integer pageNumber, List<String> propertyPaths) {
|
541
|
|
542
|
Long numberOfResults = dao.countByMarker(clazz, markerType, markerValue);
|
543
|
List<Object[]> daoResults = new ArrayList<>();
|
544
|
if(numberOfResults > 0) { // no point checking again
|
545
|
daoResults = dao.findByMarker(clazz, markerType, markerValue, includeEntity,
|
546
|
pageSize, pageNumber, propertyPaths);
|
547
|
}
|
548
|
|
549
|
List<MarkedEntityDTO<S>> result = new ArrayList<>();
|
550
|
for (Object[] daoObj : daoResults){
|
551
|
if (includeEntity){
|
552
|
result.add(new MarkedEntityDTO<>((MarkerType)daoObj[0], (Boolean)daoObj[1], (S)daoObj[2]));
|
553
|
}else{
|
554
|
result.add(new MarkedEntityDTO<>((MarkerType)daoObj[0], (Boolean)daoObj[1], (UUID)daoObj[2], (String)daoObj[3]));
|
555
|
}
|
556
|
}
|
557
|
return new DefaultPagerImpl<>(pageNumber, numberOfResults, pageSize, result);
|
558
|
}
|
559
|
|
560
|
@Override
|
561
|
@Transactional(readOnly = true)
|
562
|
public List<UuidAndTitleCache<T>> findUuidAndTitleCacheByMarker(Integer limit, String pattern,
|
563
|
MarkerType markerType){
|
564
|
|
565
|
Long numberOfResults = dao.countByMarker(null, markerType, null);
|
566
|
List<UuidAndTitleCache<T>> daoResults = new ArrayList<>();
|
567
|
if(numberOfResults > 0) { // no point checking again
|
568
|
daoResults = dao.getUuidAndTitleCacheByMarker(limit, pattern, markerType);
|
569
|
}
|
570
|
|
571
|
|
572
|
return daoResults;
|
573
|
}
|
574
|
|
575
|
|
576
|
|
577
|
}
|
578
|
|