1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.api.service;
|
12
|
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.Arrays;
|
15
|
import java.util.HashMap;
|
16
|
import java.util.HashSet;
|
17
|
import java.util.List;
|
18
|
import java.util.Map;
|
19
|
import java.util.Set;
|
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.pager.Pager;
|
27
|
import eu.etaxonomy.cdm.api.service.pager.impl.DefaultPagerImpl;
|
28
|
import eu.etaxonomy.cdm.common.monitor.DefaultProgressMonitor;
|
29
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
30
|
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
|
31
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
32
|
import eu.etaxonomy.cdm.model.common.DefinedTerm;
|
33
|
import eu.etaxonomy.cdm.model.common.ISourceable;
|
34
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
35
|
import eu.etaxonomy.cdm.model.common.IdentifiableSource;
|
36
|
import eu.etaxonomy.cdm.model.common.LSID;
|
37
|
import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
|
38
|
import eu.etaxonomy.cdm.model.media.Rights;
|
39
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
40
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
41
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
42
|
import eu.etaxonomy.cdm.persistence.dao.common.IIdentifiableDao;
|
43
|
import eu.etaxonomy.cdm.persistence.dao.hibernate.HibernateBeanInitializer;
|
44
|
import eu.etaxonomy.cdm.persistence.dao.initializer.AutoPropertyInitializer;
|
45
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
46
|
import eu.etaxonomy.cdm.persistence.query.OrderHint;
|
47
|
import eu.etaxonomy.cdm.persistence.query.OrderHint.SortOrder;
|
48
|
import eu.etaxonomy.cdm.strategy.cache.common.IIdentifiableEntityCacheStrategy;
|
49
|
import eu.etaxonomy.cdm.strategy.match.DefaultMatchStrategy;
|
50
|
import eu.etaxonomy.cdm.strategy.match.IMatchStrategy;
|
51
|
import eu.etaxonomy.cdm.strategy.match.IMatchable;
|
52
|
import eu.etaxonomy.cdm.strategy.match.MatchException;
|
53
|
import eu.etaxonomy.cdm.strategy.merge.IMergable;
|
54
|
import eu.etaxonomy.cdm.strategy.merge.IMergeStrategy;
|
55
|
import eu.etaxonomy.cdm.strategy.merge.MergeException;
|
56
|
|
57
|
public abstract class IdentifiableServiceBase<T extends IdentifiableEntity,DAO extends IIdentifiableDao<T>> extends AnnotatableServiceBase<T,DAO>
|
58
|
implements IIdentifiableEntityService<T>{
|
59
|
|
60
|
|
61
|
protected static final int UPDATE_TITLE_CACHE_DEFAULT_STEP_SIZE = 1000;
|
62
|
protected static final Logger logger = Logger.getLogger(IdentifiableServiceBase.class);
|
63
|
|
64
|
@Override
|
65
|
@Transactional(readOnly = true)
|
66
|
public Pager<Rights> getRights(T t, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
67
|
Integer numberOfResults = dao.countRights(t);
|
68
|
|
69
|
List<Rights> results = new ArrayList<Rights>();
|
70
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
71
|
results = dao.getRights(t, pageSize, pageNumber,propertyPaths);
|
72
|
}
|
73
|
|
74
|
return new DefaultPagerImpl<Rights>(pageNumber, numberOfResults, pageSize, results);
|
75
|
}
|
76
|
|
77
|
@Override
|
78
|
@Transactional(readOnly = true)
|
79
|
public Pager<IdentifiableSource> getSources(T t, Integer pageSize, Integer pageNumber, List<String> propertyPaths) {
|
80
|
Integer numberOfResults = dao.countSources(t);
|
81
|
|
82
|
List<IdentifiableSource> results = new ArrayList<IdentifiableSource>();
|
83
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
84
|
results = dao.getSources(t, pageSize, pageNumber,propertyPaths);
|
85
|
}
|
86
|
|
87
|
return new DefaultPagerImpl<IdentifiableSource>(pageNumber, numberOfResults, pageSize, results);
|
88
|
}
|
89
|
|
90
|
|
91
|
@Transactional(readOnly = false)
|
92
|
@Override
|
93
|
public T replace(T x, T y) {
|
94
|
return dao.replace(x, y);
|
95
|
}
|
96
|
/**
|
97
|
* FIXME Candidate for harmonization
|
98
|
* Given that this method is strongly typed, and generic, could we not simply expose it as
|
99
|
* List<T> findByTitle(String title) as it is somewhat less cumbersome. Admittedly, I don't
|
100
|
* understand what is going on with the configurators etc. so maybe there is a good reason for
|
101
|
* the design of this method.
|
102
|
* @param title
|
103
|
* @return
|
104
|
*/
|
105
|
@Transactional(readOnly = true)
|
106
|
protected List<T> findCdmObjectsByTitle(String title){
|
107
|
return ((IIdentifiableDao)dao).findByTitle(title);
|
108
|
}
|
109
|
|
110
|
@Transactional(readOnly = true)
|
111
|
protected List<T> findCdmObjectsByTitle(String title, Class<T> clazz){
|
112
|
return ((IIdentifiableDao)dao).findByTitleAndClass(title, clazz);
|
113
|
}
|
114
|
@Transactional(readOnly = true)
|
115
|
protected List<T> findCdmObjectsByTitle(String title, CdmBase sessionObject){
|
116
|
return ((IIdentifiableDao)dao).findByTitle(title, sessionObject);
|
117
|
}
|
118
|
|
119
|
/*
|
120
|
* TODO - Migrated from CommonServiceBase
|
121
|
* (non-Javadoc)
|
122
|
* @see eu.etaxonomy.cdm.api.service.ICommonService#getSourcedObjectById(java.lang.String, java.lang.String)
|
123
|
*/
|
124
|
@Transactional(readOnly = true)
|
125
|
@Override
|
126
|
public ISourceable getSourcedObjectByIdInSource(Class clazz, String idInSource, String idNamespace) {
|
127
|
ISourceable result = null;
|
128
|
|
129
|
List<T> list = dao.findOriginalSourceByIdInSource(idInSource, idNamespace);
|
130
|
if (! list.isEmpty()){
|
131
|
result = list.get(0);
|
132
|
}
|
133
|
return result;
|
134
|
}
|
135
|
|
136
|
@Transactional(readOnly = true)
|
137
|
@Override
|
138
|
public List<UuidAndTitleCache<T>> getUuidAndTitleCache() {
|
139
|
return dao.getUuidAndTitleCache();
|
140
|
}
|
141
|
|
142
|
@Transactional(readOnly = true)
|
143
|
@Override
|
144
|
public Pager<T> findByTitle(Class<? extends T> clazz, String queryString,MatchMode matchmode, List<Criterion> criteria, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
145
|
Integer numberOfResults = dao.countByTitle(clazz, queryString, matchmode, criteria);
|
146
|
|
147
|
List<T> results = new ArrayList<T>();
|
148
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
149
|
results = dao.findByTitle(clazz, queryString, matchmode, criteria, pageSize, pageNumber, orderHints, propertyPaths);
|
150
|
}
|
151
|
|
152
|
return new DefaultPagerImpl<T>(pageNumber, numberOfResults, pageSize, results);
|
153
|
}
|
154
|
|
155
|
@Transactional(readOnly = true)
|
156
|
@Override
|
157
|
public Pager<T> findByTitle(IIdentifiableEntityServiceConfigurator<T> config){
|
158
|
return findByTitle(config.getClazz(), config.getTitleSearchStringSqlized(), config.getMatchMode(), config.getCriteria(), config.getPageSize(), config.getPageNumber(), config.getOrderHints(), config.getPropertyPaths());
|
159
|
}
|
160
|
|
161
|
@Transactional(readOnly = true)
|
162
|
@Override
|
163
|
public List<T> listByTitle(Class<? extends T> clazz, String queryString,MatchMode matchmode, List<Criterion> criteria, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
164
|
Integer numberOfResults = dao.countByTitle(clazz, queryString, matchmode, criteria);
|
165
|
|
166
|
List<T> results = new ArrayList<T>();
|
167
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
168
|
results = dao.findByTitle(clazz, queryString, matchmode, criteria, pageSize, pageNumber, orderHints, propertyPaths);
|
169
|
}
|
170
|
return results;
|
171
|
}
|
172
|
|
173
|
@Transactional(readOnly = true)
|
174
|
@Override
|
175
|
public Pager<T> findTitleCache(Class<? extends T> clazz, String queryString, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, MatchMode matchMode){
|
176
|
long numberOfResults = dao.countTitleCache(clazz, queryString, matchMode);
|
177
|
|
178
|
List<T> results = new ArrayList<T>();
|
179
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
180
|
results = dao.findTitleCache(clazz, queryString, pageSize, pageNumber, orderHints, matchMode);
|
181
|
}
|
182
|
int r = 0;
|
183
|
r += numberOfResults;
|
184
|
|
185
|
return new DefaultPagerImpl<T>(pageNumber, r , pageSize, results);
|
186
|
}
|
187
|
|
188
|
@Transactional(readOnly = true)
|
189
|
@Override
|
190
|
public List<T> listByReferenceTitle(Class<? extends T> clazz, String queryString,MatchMode matchmode, List<Criterion> criteria, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
191
|
Integer numberOfResults = dao.countByReferenceTitle(clazz, queryString, matchmode, criteria);
|
192
|
|
193
|
List<T> results = new ArrayList<T>();
|
194
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
195
|
results = dao.findByReferenceTitle(clazz, queryString, matchmode, criteria, pageSize, pageNumber, orderHints, propertyPaths);
|
196
|
}
|
197
|
return results;
|
198
|
}
|
199
|
|
200
|
@Transactional(readOnly = true)
|
201
|
@Override
|
202
|
public T find(LSID lsid) {
|
203
|
return dao.find(lsid);
|
204
|
}
|
205
|
|
206
|
@Transactional(readOnly = true)
|
207
|
@Override
|
208
|
public Pager<T> search(Class<? extends T> clazz, String queryString, Integer pageSize, Integer pageNumber, List<OrderHint> orderHints, List<String> propertyPaths) {
|
209
|
Integer numberOfResults = dao.count(clazz,queryString);
|
210
|
|
211
|
List<T> results = new ArrayList<T>();
|
212
|
if(numberOfResults > 0) { // no point checking again //TODO use AbstractPagerImpl.hasResultsInRange(numberOfResults, pageNumber, pageSize)
|
213
|
results = dao.search(clazz,queryString, pageSize, pageNumber, orderHints, propertyPaths);
|
214
|
}
|
215
|
|
216
|
return new DefaultPagerImpl<T>(pageNumber, numberOfResults, pageSize, results);
|
217
|
}
|
218
|
|
219
|
@Override
|
220
|
@Transactional(readOnly = false)
|
221
|
public void updateTitleCache() {
|
222
|
updateTitleCache(null, null, null, null);
|
223
|
}
|
224
|
|
225
|
@Transactional(readOnly = false) //TODO check transactional behaviour, e.g. what happens with the session if count is very large
|
226
|
protected <S extends T > void updateTitleCacheImpl(Class<S> clazz, Integer stepSize, IIdentifiableEntityCacheStrategy<T> cacheStrategy, IProgressMonitor monitor) {
|
227
|
if (stepSize == null){
|
228
|
stepSize = UPDATE_TITLE_CACHE_DEFAULT_STEP_SIZE;
|
229
|
}
|
230
|
if (monitor == null){
|
231
|
monitor = DefaultProgressMonitor.NewInstance();
|
232
|
}
|
233
|
|
234
|
int count = dao.count(clazz);
|
235
|
monitor.beginTask("update titles", count);
|
236
|
int worked = 0;
|
237
|
for(int i = 0 ; i < count ; i = i + stepSize){
|
238
|
// not sure if such strict ordering is necessary here, but for safety reasons I do it
|
239
|
ArrayList<OrderHint> orderHints = new ArrayList<OrderHint>();
|
240
|
orderHints.add( new OrderHint("id", OrderHint.SortOrder.ASCENDING));
|
241
|
|
242
|
|
243
|
Map<Class<? extends CdmBase>, AutoPropertyInitializer<CdmBase>> oldAutoInit = switchOfAutoinitializer();
|
244
|
List<S> list = this.list(clazz, stepSize, i, orderHints, null);
|
245
|
switchOnOldAutoInitializer(oldAutoInit);
|
246
|
|
247
|
List<T> entitiesToUpdate = new ArrayList<T>();
|
248
|
for (T entity : list){
|
249
|
HibernateProxyHelper.deproxy(entity, clazz);
|
250
|
if (entity.isProtectedTitleCache() == false){
|
251
|
updateTitleCacheForSingleEntity(cacheStrategy, entitiesToUpdate, entity);
|
252
|
}
|
253
|
worked++;
|
254
|
}
|
255
|
for (T entity: entitiesToUpdate){
|
256
|
if (entity.getTitleCache() != null){
|
257
|
//System.err.println(entity.getTitleCache());
|
258
|
}else{
|
259
|
//System.err.println("no titleCache" + ((NonViralName)entity).getNameCache());
|
260
|
}
|
261
|
}
|
262
|
saveOrUpdate(entitiesToUpdate);
|
263
|
monitor.worked(list.size());
|
264
|
if (monitor.isCanceled()){
|
265
|
monitor.done();
|
266
|
return;
|
267
|
}
|
268
|
}
|
269
|
monitor.done();
|
270
|
}
|
271
|
|
272
|
/**
|
273
|
* Brings back all auto initializers to the bean initializer
|
274
|
* @see #switchOfAutoinitializer()
|
275
|
* @param oldAutoInit
|
276
|
*/
|
277
|
protected void switchOnOldAutoInitializer(
|
278
|
Map<Class<? extends CdmBase>, AutoPropertyInitializer<CdmBase>> oldAutoInit) {
|
279
|
HibernateBeanInitializer initializer = (HibernateBeanInitializer)this.appContext.getBean("defaultBeanInitializer");
|
280
|
initializer.setBeanAutoInitializers(oldAutoInit);
|
281
|
}
|
282
|
|
283
|
/**
|
284
|
* Removes all auto initializers from the bean initializer
|
285
|
*
|
286
|
* @see #switchOnOldAutoInitializer(Map)
|
287
|
* @return
|
288
|
*/
|
289
|
protected Map<Class<? extends CdmBase>, AutoPropertyInitializer<CdmBase>> switchOfAutoinitializer() {
|
290
|
HibernateBeanInitializer initializer = (HibernateBeanInitializer)this.appContext.getBean("defaultBeanInitializer");
|
291
|
Map<Class<? extends CdmBase>, AutoPropertyInitializer<CdmBase>> oldAutoInitializers = initializer.getBeanAutoInitializers();
|
292
|
Map<Class<? extends CdmBase>, AutoPropertyInitializer<CdmBase>> map = new HashMap<Class<? extends CdmBase>, AutoPropertyInitializer<CdmBase>>();
|
293
|
initializer.setBeanAutoInitializers(map);
|
294
|
return oldAutoInitializers;
|
295
|
}
|
296
|
|
297
|
/**
|
298
|
* @param cacheStrategy
|
299
|
* @param entitiesToUpdate
|
300
|
* @param entity
|
301
|
*/
|
302
|
/**
|
303
|
* @param cacheStrategy
|
304
|
* @param entitiesToUpdate
|
305
|
* @param entity
|
306
|
*/
|
307
|
private void updateTitleCacheForSingleEntity(
|
308
|
IIdentifiableEntityCacheStrategy<T> cacheStrategy,
|
309
|
List<T> entitiesToUpdate, T entity) {
|
310
|
|
311
|
assert (entity.isProtectedTitleCache() == false );
|
312
|
|
313
|
//exclude recursive inreferences
|
314
|
if (entity.isInstanceOf(Reference.class)){
|
315
|
Reference<?> ref = CdmBase.deproxy(entity, Reference.class);
|
316
|
if (ref.getInReference() != null && ref.getInReference().equals(ref)){
|
317
|
return;
|
318
|
}
|
319
|
}
|
320
|
|
321
|
|
322
|
//define the correct cache strategy
|
323
|
IIdentifiableEntityCacheStrategy entityCacheStrategy = cacheStrategy;
|
324
|
if (entityCacheStrategy == null){
|
325
|
entityCacheStrategy = entity.getCacheStrategy();
|
326
|
//FIXME find out why the wrong cache strategy is loaded here, see #1876
|
327
|
if (entity instanceof Reference){
|
328
|
entityCacheStrategy = ReferenceFactory.newReference(((Reference<?>)entity).getType()).getCacheStrategy();
|
329
|
}
|
330
|
}
|
331
|
entity.setCacheStrategy(entityCacheStrategy);
|
332
|
|
333
|
|
334
|
//old titleCache
|
335
|
entity.setProtectedTitleCache(true);
|
336
|
String oldTitleCache = entity.getTitleCache();
|
337
|
entity.setTitleCache(oldTitleCache, false); //before we had entity.setProtectedTitleCache(false) but this deleted the titleCache itself
|
338
|
|
339
|
//NonViralNames and Reference have more caches //TODO handle in NameService
|
340
|
String oldNameCache = null;
|
341
|
String oldFullTitleCache = null;
|
342
|
String oldAbbrevTitleCache = null;
|
343
|
if (entity instanceof NonViralName ){
|
344
|
|
345
|
try{
|
346
|
NonViralName<?> nvn = (NonViralName) entity;
|
347
|
if (!nvn.isProtectedNameCache()){
|
348
|
nvn.setProtectedNameCache(true);
|
349
|
oldNameCache = nvn.getNameCache();
|
350
|
nvn.setProtectedNameCache(false);
|
351
|
}
|
352
|
if (!nvn.isProtectedFullTitleCache()){
|
353
|
nvn.setProtectedFullTitleCache(true);
|
354
|
oldFullTitleCache = nvn.getFullTitleCache();
|
355
|
nvn.setProtectedFullTitleCache(false);
|
356
|
}
|
357
|
}catch(ClassCastException e){
|
358
|
System.out.println("entity: " + entity.getTitleCache());
|
359
|
}
|
360
|
|
361
|
}else if (entity instanceof Reference){
|
362
|
Reference<?> ref = (Reference<?>) entity;
|
363
|
if (!ref.isProtectedAbbrevTitleCache()){
|
364
|
ref.setProtectedAbbrevTitleCache(true);
|
365
|
oldAbbrevTitleCache = ref.getAbbrevTitleCache();
|
366
|
ref.setProtectedAbbrevTitleCache(false);
|
367
|
}
|
368
|
}
|
369
|
setOtherCachesNull(entity);
|
370
|
String newTitleCache= null;
|
371
|
NonViralName<?> nvn = null;//TODO find better solution
|
372
|
try{
|
373
|
if (entity instanceof NonViralName){
|
374
|
nvn = (NonViralName) entity;
|
375
|
newTitleCache = entityCacheStrategy.getTitleCache(nvn);
|
376
|
} else{
|
377
|
newTitleCache = entityCacheStrategy.getTitleCache(entity);
|
378
|
}
|
379
|
}catch (ClassCastException e){
|
380
|
nvn = HibernateProxyHelper.deproxy(entity, NonViralName.class);
|
381
|
newTitleCache = entityCacheStrategy.getTitleCache(nvn);
|
382
|
//System.out.println("titleCache: " +entity.getTitleCache());
|
383
|
}
|
384
|
|
385
|
if ( oldTitleCache == null || oldTitleCache != null && ! oldTitleCache.equals(newTitleCache) ){
|
386
|
entity.setTitleCache(null, false);
|
387
|
String newCache = entity.getTitleCache();
|
388
|
if (newCache == null){
|
389
|
logger.warn("newCache should never be null");
|
390
|
}
|
391
|
if (oldTitleCache == null){
|
392
|
logger.info("oldTitleCache should never be null");
|
393
|
}
|
394
|
if (nvn != null){
|
395
|
//NonViralName<?> nvn = (NonViralName) entity;
|
396
|
nvn.getNameCache();
|
397
|
nvn.getFullTitleCache();
|
398
|
}
|
399
|
if (entity instanceof Reference){
|
400
|
Reference<?> ref = (Reference<?>) entity;
|
401
|
ref.getAbbrevTitleCache();
|
402
|
}
|
403
|
entitiesToUpdate.add(entity);
|
404
|
}else if (nvn != null){
|
405
|
//NonViralName<?> nvn = (NonViralName) entity;
|
406
|
String newNameCache = nvn.getNameCache();
|
407
|
String newFullTitleCache = nvn.getFullTitleCache();
|
408
|
if ((oldNameCache == null && !nvn.isProtectedNameCache()) || (oldNameCache != null && !oldNameCache.equals(newNameCache))){
|
409
|
entitiesToUpdate.add(entity);
|
410
|
}else if ((oldFullTitleCache == null && !nvn.isProtectedFullTitleCache()) || (oldFullTitleCache != null && !oldFullTitleCache.equals(newFullTitleCache))){
|
411
|
entitiesToUpdate.add(entity);
|
412
|
}
|
413
|
}else if (entity instanceof Reference){
|
414
|
Reference<?> ref = (Reference<?>) entity;
|
415
|
String newAbbrevTitleCache = ref.getAbbrevTitleCache();
|
416
|
if ( (oldAbbrevTitleCache == null && !ref.isProtectedAbbrevTitleCache() ) || (oldAbbrevTitleCache != null && !oldAbbrevTitleCache.equals(newAbbrevTitleCache))){
|
417
|
entitiesToUpdate.add(entity);
|
418
|
}
|
419
|
}
|
420
|
|
421
|
|
422
|
}
|
423
|
|
424
|
|
425
|
|
426
|
/**
|
427
|
* Needs override if not only the title cache should be set to null to
|
428
|
* generate the correct new title cache
|
429
|
*/
|
430
|
protected void setOtherCachesNull(T entity) {
|
431
|
return;
|
432
|
}
|
433
|
|
434
|
|
435
|
|
436
|
private class DeduplicateState{
|
437
|
String lastTitleCache;
|
438
|
Integer pageSize = 50;
|
439
|
int nPages = 3;
|
440
|
int startPage = 0;
|
441
|
boolean isCompleted = false;
|
442
|
int result;
|
443
|
}
|
444
|
|
445
|
@Override
|
446
|
@Transactional(readOnly = false)
|
447
|
public int deduplicate(Class<? extends T> clazz, IMatchStrategy matchStrategy, IMergeStrategy mergeStrategy) {
|
448
|
DeduplicateState dedupState = new DeduplicateState();
|
449
|
|
450
|
if (clazz == null){
|
451
|
logger.warn("Deduplication clazz must not be null!");
|
452
|
return 0;
|
453
|
}
|
454
|
if (! ( IMatchable.class.isAssignableFrom(clazz) && IMergable.class.isAssignableFrom(clazz) ) ){
|
455
|
logger.warn("Deduplication implemented only for classes implementing IMatchable and IMergeable. No deduplication performed!");
|
456
|
return 0;
|
457
|
}
|
458
|
Class matchableClass = clazz;
|
459
|
if (matchStrategy == null){
|
460
|
matchStrategy = DefaultMatchStrategy.NewInstance(matchableClass);
|
461
|
}
|
462
|
List<T> nextGroup = new ArrayList<T>();
|
463
|
|
464
|
int result = 0;
|
465
|
// double countTotal = count(clazz);
|
466
|
//
|
467
|
// Number countPagesN = Math.ceil(countTotal/dedupState.pageSize.doubleValue()) ;
|
468
|
// int countPages = countPagesN.intValue();
|
469
|
//
|
470
|
|
471
|
List<OrderHint> orderHints = Arrays.asList(new OrderHint[]{new OrderHint("titleCache", SortOrder.ASCENDING)});
|
472
|
|
473
|
while (! dedupState.isCompleted){
|
474
|
//get x page sizes
|
475
|
List<T> objectList = getPages(clazz, dedupState, orderHints);
|
476
|
//after each page check if any changes took place
|
477
|
int nUnEqualPages = handleAllPages(objectList, dedupState, nextGroup, matchStrategy, mergeStrategy);
|
478
|
nUnEqualPages = nUnEqualPages + dedupState.pageSize * dedupState.startPage;
|
479
|
//refresh start page counter
|
480
|
int finishedPages = nUnEqualPages / dedupState.pageSize;
|
481
|
dedupState.startPage = finishedPages;
|
482
|
}
|
483
|
|
484
|
result += handleLastGroup(nextGroup, matchStrategy, mergeStrategy);
|
485
|
return result;
|
486
|
}
|
487
|
|
488
|
|
489
|
private int handleAllPages(List<T> objectList, DeduplicateState dedupState, List<T> nextGroup, IMatchStrategy matchStrategy, IMergeStrategy mergeStrategy) {
|
490
|
int nUnEqual = 0;
|
491
|
for (T object : objectList){
|
492
|
String currentTitleCache = object.getTitleCache();
|
493
|
if (currentTitleCache != null && currentTitleCache.equals(dedupState.lastTitleCache)){
|
494
|
//=titleCache
|
495
|
nextGroup.add(object);
|
496
|
}else{
|
497
|
//<> titleCache
|
498
|
dedupState.result += handleLastGroup(nextGroup, matchStrategy, mergeStrategy);
|
499
|
nextGroup = new ArrayList<T>();
|
500
|
nextGroup.add(object);
|
501
|
nUnEqual++;
|
502
|
}
|
503
|
dedupState.lastTitleCache = currentTitleCache;
|
504
|
}
|
505
|
handleLastGroup(nextGroup, matchStrategy, mergeStrategy);
|
506
|
return nUnEqual;
|
507
|
}
|
508
|
|
509
|
private List<T> getPages(Class<? extends T> clazz, DeduplicateState dedupState, List<OrderHint> orderHints) {
|
510
|
List<T> result = new ArrayList<T>();
|
511
|
for (int pageNo = dedupState.startPage; pageNo < dedupState.startPage + dedupState.nPages; pageNo++){
|
512
|
List<T> objectList = listByTitle(clazz, null, null, null, dedupState.pageSize, pageNo, orderHints, null);
|
513
|
result.addAll(objectList);
|
514
|
}
|
515
|
if (result.size()< dedupState.nPages * dedupState.pageSize ){
|
516
|
dedupState.isCompleted = true;
|
517
|
}
|
518
|
return result;
|
519
|
}
|
520
|
|
521
|
private int handleLastGroup(List<T> group, IMatchStrategy matchStrategy, IMergeStrategy mergeStrategy) {
|
522
|
int result = 0;
|
523
|
int size = group.size();
|
524
|
Set<Integer> exclude = new HashSet<Integer>(); //set to collect all objects, that have been merged already
|
525
|
for (int i = 0; i < size - 1; i++){
|
526
|
if (exclude.contains(i)){
|
527
|
continue;
|
528
|
}
|
529
|
for (int j = i + 1; j < size; j++){
|
530
|
if (exclude.contains(j)){
|
531
|
continue;
|
532
|
}
|
533
|
T firstObject = group.get(i);
|
534
|
T secondObject = group.get(j);
|
535
|
|
536
|
try {
|
537
|
if (matchStrategy.invoke((IMatchable)firstObject, (IMatchable)secondObject)){
|
538
|
commonService.merge((IMergable)firstObject, (IMergable)secondObject, mergeStrategy);
|
539
|
exclude.add(j);
|
540
|
result++;
|
541
|
}
|
542
|
} catch (MatchException e) {
|
543
|
logger.warn("MatchException when trying to match " + firstObject.getTitleCache());
|
544
|
e.printStackTrace();
|
545
|
} catch (MergeException e) {
|
546
|
logger.warn("MergeException when trying to merge " + firstObject.getTitleCache());
|
547
|
e.printStackTrace();
|
548
|
}
|
549
|
}
|
550
|
}
|
551
|
return result;
|
552
|
}
|
553
|
|
554
|
@Transactional(readOnly = true)
|
555
|
@Override
|
556
|
public Integer countByTitle(Class<? extends T> clazz, String queryString,MatchMode matchmode, List<Criterion> criteria){
|
557
|
Integer numberOfResults = dao.countByTitle(clazz, queryString, matchmode, criteria);
|
558
|
|
559
|
return numberOfResults;
|
560
|
}
|
561
|
|
562
|
@Transactional(readOnly = true)
|
563
|
@Override
|
564
|
public Integer countByTitle(IIdentifiableEntityServiceConfigurator<T> config){
|
565
|
return countByTitle(config.getClazz(), config.getTitleSearchStringSqlized(),
|
566
|
config.getMatchMode(), config.getCriteria());
|
567
|
|
568
|
}
|
569
|
|
570
|
@Override
|
571
|
public <S extends IdentifiableEntity> List<S> listByIdentifier(
|
572
|
S clazz, String identifier, DefinedTerm identifierType,
|
573
|
MatchMode matchmode, Integer pageSize,
|
574
|
Integer pageNumber, List<OrderHint> orderHints,
|
575
|
List<String> propertyPaths) {
|
576
|
return dao.findByIdentifier(clazz, identifier, identifierType,
|
577
|
matchmode, pageSize, pageNumber, orderHints,
|
578
|
propertyPaths);
|
579
|
}
|
580
|
|
581
|
|
582
|
}
|
583
|
|