Revision 277e8759
Added by Andreas Müller over 2 years ago
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/common/utils/ImportDeduplicationHelper.java | ||
---|---|---|
88 | 88 |
USE_REPO; |
89 | 89 |
} |
90 | 90 |
|
91 |
private class DedupInfo<S extends IdentifiableEntity>{
|
|
91 |
private class DedupInfo<S extends IMatchable>{
|
|
92 | 92 |
Class<S> clazz; |
93 | 93 |
IMatchStrategyEqual matcher; |
94 | 94 |
Map<String, Set<S>> map = new HashMap<>(); |
... | ... | |
98 | 98 |
private DedupInfo(Class<S> clazz, DedupMap dedupMap){ |
99 | 99 |
this.clazz = clazz; |
100 | 100 |
if (IMatchable.class.isAssignableFrom(clazz)) { |
101 |
matcher = DefaultMatchStrategy.NewInstance((Class<IMatchable>)clazz);
|
|
101 |
matcher = DefaultMatchStrategy.NewInstance(clazz); |
|
102 | 102 |
} |
103 | 103 |
dedupMap.put(clazz, this); |
104 | 104 |
} |
... | ... | |
108 | 108 |
} |
109 | 109 |
} |
110 | 110 |
|
111 |
private class DedupMap<T extends IdentifiableEntity> extends HashMap<Class<T>, DedupInfo<T>>{
|
|
111 |
private class DedupMap<T extends IMatchable> extends HashMap<Class<T>, DedupInfo<T>>{
|
|
112 | 112 |
private static final long serialVersionUID = 3757206594833330646L; |
113 | 113 |
} |
114 | 114 |
private DedupMap<? extends IdentifiableEntity> dedupMap = new DedupMap<>(); |
... | ... | |
119 | 119 |
private DedupInfo<TaxonName> nameDedupInfo = new DedupInfo<>(TaxonName.class, dedupMap); |
120 | 120 |
|
121 | 121 |
|
122 |
@SuppressWarnings("unused") |
|
122 | 123 |
private Status institutionStatus = Status.NOT_INIT; |
123 | 124 |
private Status copyrightStatus = Status.NOT_INIT; |
124 | 125 |
private Status collectionStatus = Status.NOT_INIT; |
... | ... | |
183 | 184 |
|
184 | 185 |
nameDedupInfo.map = refreshSetMap(nameDedupInfo.map, (IService)repository.getNameService(), importResult); |
185 | 186 |
collectionMap = refreshSetMap(collectionMap, (IService)repository.getCollectionService(), importResult); |
186 |
//TODO copyright ?
|
|
187 |
copyrightMap = refreshSetMap(copyrightMap, (IService)repository.getRightsService(), importResult);
|
|
187 | 188 |
} |
188 | 189 |
|
189 | 190 |
//maybe this was used for Institution before |
... | ... | |
213 | 214 |
IService<T> service, ImportResult importResult) { |
214 | 215 |
|
215 | 216 |
Map<String, Set<T>> newMap = new HashMap<>(); |
216 |
logger.debug("Start loading map"); //TODO debug only |
|
217 | 217 |
//create UUID set |
218 | 218 |
Set<UUID> uuidSet = new HashSet<>(); |
219 | 219 |
for (String key : oldMap.keySet()){ |
... | ... | |
264 | 264 |
entitySet.add(CdmBase.deproxy(entity)); |
265 | 265 |
} |
266 | 266 |
|
267 |
private <S extends IdentifiableEntity> Set<S> getEntityByTitle(String title, DedupInfo<S> dedupInfo){
|
|
267 |
private <S extends IMatchable> Set<S> getEntityByTitle(String title, DedupInfo<S> dedupInfo){
|
|
268 | 268 |
return dedupInfo.map.get(title); |
269 | 269 |
} |
270 | 270 |
|
271 |
private <S extends IdentifiableEntity> Optional<S> getMatchingEntity(S entityOrig, DedupInfo<S> dedupInfo){
|
|
271 |
private <S extends IMatchable> Optional<S> getMatchingEntity(S entityOrig, DedupInfo<S> dedupInfo){
|
|
272 | 272 |
S entity = CdmBase.deproxy(entityOrig); |
273 | 273 |
Predicate<S> matchFilter = reference ->{ |
274 | 274 |
try { |
... | ... | |
277 | 277 |
throw new RuntimeException(e); |
278 | 278 |
} |
279 | 279 |
}; |
280 |
Optional<S> result = Optional.ofNullable(getEntityByTitle(entity.getTitleCache(), dedupInfo)) |
|
280 |
//TODO casting |
|
281 |
Optional<S> result = Optional.ofNullable(getEntityByTitle(((IdentifiableEntity<?>)entity).getTitleCache(), dedupInfo)) |
|
281 | 282 |
.orElse(new HashSet<>()) |
282 | 283 |
.stream() |
283 | 284 |
.filter(matchFilter) |
... | ... | |
286 | 287 |
return result; |
287 | 288 |
}else { |
288 | 289 |
try { |
289 |
return (Optional<S>)repository.getCommonService().findMatching((IMatchable)entity, dedupInfo.matcher).stream().findFirst();
|
|
290 |
return (Optional)repository.getCommonService().findMatching((IMatchable)entity, dedupInfo.matcher).stream().findFirst(); |
|
290 | 291 |
} catch (MatchException e) { |
291 | 292 |
throw new RuntimeException(e); |
292 | 293 |
} |
... | ... | |
391 | 392 |
if (author == null){ |
392 | 393 |
return null; |
393 | 394 |
}else{ |
394 |
//TODO |
|
395 | 395 |
init(personDedupInfo); |
396 | 396 |
init(teamDedupInfo); |
397 | 397 |
initAuthorTitleCaches(author); |
... | ... | |
427 | 427 |
|
428 | 428 |
private void initReferenceCaches(Reference ref) { |
429 | 429 |
////TODO better do via matching strategy (newReference might have caches == null) |
430 |
//more or less copy from CdmPreDataChangeListener
|
|
430 |
//the below is more or less a copy from CdmPreDataChangeListener
|
|
431 | 431 |
ref.getAbbrevTitleCache(); |
432 | 432 |
ref.getTitleCache(); |
433 | 433 |
} |
... | ... | |
448 | 448 |
} |
449 | 449 |
} |
450 | 450 |
|
451 |
private <S extends IdentifiableEntity<?>> void init(DedupInfo<S> dedupInfo) {
|
|
451 |
private <S extends IMatchable> void init(DedupInfo<S> dedupInfo) {
|
|
452 | 452 |
dedupInfo.status = init(dedupInfo.clazz, dedupInfo.status, dedupInfo.map); |
453 | 453 |
} |
454 | 454 |
|
455 |
private <S extends IdentifiableEntity<?>> Status init(Class<S> clazz, Status status, Map<String,Set<S>> map) {
|
|
455 |
private <S extends IMatchable> Status init(Class<S> clazz, Status status, Map<String,Set<S>> map) {
|
|
456 | 456 |
|
457 |
Class<IdentifiableEntity> entityClass = (Class<IdentifiableEntity>)clazz; |
|
457 | 458 |
if (status == Status.NOT_INIT && repository != null){ |
458 | 459 |
if (maxCountFullLoad != NEVER_USE_MAP){ |
459 | 460 |
long nExisting = -2; |
460 | 461 |
if (maxCountFullLoad != ALWAYS_USE_MAP){ |
461 |
nExisting = repository.getCommonService().count(clazz);
|
|
462 |
nExisting = repository.getCommonService().count(entityClass);
|
|
462 | 463 |
} |
463 | 464 |
if (nExisting <= maxCountFullLoad ){ |
464 | 465 |
List<String> propertyPaths = Arrays.asList(""); |
465 |
List<S> existingEntities = repository.getCommonService().list(clazz, null, null, null, propertyPaths); |
|
466 |
for (S ref : existingEntities){ |
|
467 |
putEntity(ref.getTitleCache(), ref, map); |
|
466 |
List<IdentifiableEntity> existingEntities = repository.getCommonService().list(entityClass, null, null, null, propertyPaths); |
|
467 |
for (IdentifiableEntity<?> entity : existingEntities){ |
|
468 |
//TODO casting |
|
469 |
putEntity(entity.getTitleCache(), entity, (Map)map); |
|
468 | 470 |
} |
469 | 471 |
return Status.USE_MAP; |
470 | 472 |
}else{ |
... | ... | |
509 | 511 |
} |
510 | 512 |
|
511 | 513 |
private void initCollectionMap() { |
512 |
collectionStatus = init(Collection.class, collectionStatus, collectionMap); |
|
514 |
if (collectionStatus == Status.NOT_INIT && repository != null){ |
|
515 |
List<String> propertyPaths = Arrays.asList(""); |
|
516 |
List<Collection> existingCollections = repository.getCollectionService().list(null, null, null, null, propertyPaths); |
|
517 |
for (Collection collection : existingCollections){ |
|
518 |
putEntity(collection.getTitleCache(), collection, collectionMap); |
|
519 |
} |
|
520 |
} |
|
521 |
collectionStatus = Status.USE_MAP; |
|
522 |
// collectionStatus = init(Collection.class, collectionStatus, collectionMap); //for future, once Collection becomes IMatchable |
|
513 | 523 |
} |
514 | 524 |
|
515 | 525 |
public Reference getExistingReference(Reference ref) { |
Also available in: Unified diff
ref #9889 add direct matching on persistence to deduplication helper and replace for reference.author and .inReference (cont.)