Revision 96b017d6
Added by Andreas Müller almost 7 years ago
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/common/utils/ImportDeduplicationHelper.java | ||
---|---|---|
21 | 21 |
|
22 | 22 |
import eu.etaxonomy.cdm.api.application.ICdmRepository; |
23 | 23 |
import eu.etaxonomy.cdm.io.common.ImportStateBase; |
24 |
import eu.etaxonomy.cdm.model.agent.AgentBase; |
|
25 |
import eu.etaxonomy.cdm.model.agent.Institution; |
|
24 | 26 |
import eu.etaxonomy.cdm.model.agent.Person; |
25 | 27 |
import eu.etaxonomy.cdm.model.agent.Team; |
26 | 28 |
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase; |
27 | 29 |
import eu.etaxonomy.cdm.model.common.CdmBase; |
30 |
import eu.etaxonomy.cdm.model.media.Rights; |
|
31 |
import eu.etaxonomy.cdm.model.media.RightsType; |
|
28 | 32 |
import eu.etaxonomy.cdm.model.name.HybridRelationship; |
29 | 33 |
import eu.etaxonomy.cdm.model.name.INonViralName; |
30 | 34 |
import eu.etaxonomy.cdm.model.name.NonViralName; |
... | ... | |
50 | 54 |
boolean referenceMapIsInitialized = false; |
51 | 55 |
boolean nameMapIsInitialized = false; |
52 | 56 |
boolean agentMapIsInitialized = false; |
57 |
boolean copyrightMapIsInitialized = false; |
|
53 | 58 |
|
54 | 59 |
private Map<String, Set<Reference>> refMap = new HashMap<>(); |
55 | 60 |
private Map<String, Team> teamMap = new HashMap<>(); |
56 | 61 |
private Map<String, Person> personMap = new HashMap<>(); |
62 |
private Map<String, Institution> institutionMap = new HashMap<>(); |
|
57 | 63 |
//using titleCache |
58 | 64 |
private Map<String, Set<INonViralName>> nameMap = new HashMap<>(); |
65 |
private static Map<String, Set<Rights>> copyrightMap = new HashMap<>(); |
|
66 |
|
|
59 | 67 |
|
60 | 68 |
private IMatchStrategy referenceMatcher = DefaultMatchStrategy.NewInstance(Reference.class); |
61 | 69 |
private IMatchStrategy nameMatcher = DefaultMatchStrategy.NewInstance(TaxonNameBase.class); |
... | ... | |
120 | 128 |
} |
121 | 129 |
|
122 | 130 |
// AGENTS |
123 |
private void putAgentBase(String title, TeamOrPersonBase<?> agent){
|
|
131 |
private void putAgentBase(String title, AgentBase<?> agent){
|
|
124 | 132 |
if (agent.isInstanceOf(Person.class) ){ |
125 | 133 |
personMap.put(title, CdmBase.deproxy(agent, Person.class)); |
126 |
}else{ |
|
134 |
}else if (agent.isInstanceOf(Team.class)){
|
|
127 | 135 |
teamMap.put(title, CdmBase.deproxy(agent, Team.class)); |
136 |
}else{ |
|
137 |
institutionMap.put(title, CdmBase.deproxy(agent, Institution.class)); |
|
128 | 138 |
} |
129 | 139 |
} |
130 | 140 |
|
... | ... | |
228 | 238 |
} |
229 | 239 |
} |
230 | 240 |
|
241 |
public AgentBase<?> getExistingAgent(STATE state, |
|
242 |
AgentBase<?> agent) { |
|
243 |
if (agent == null){ |
|
244 |
return null; |
|
245 |
} else if (agent.isInstanceOf(TeamOrPersonBase.class)){ |
|
246 |
return getExistingAuthor(state, CdmBase.deproxy(agent, TeamOrPersonBase.class)); |
|
247 |
}else{ |
|
248 |
initAgentMap(state); |
|
249 |
Institution result = institutionMap.get(agent.getTitleCache()); |
|
250 |
if (result == null){ |
|
251 |
putAgentBase(agent.getTitleCache(), agent); |
|
252 |
result = CdmBase.deproxy(agent, Institution.class); |
|
253 |
} |
|
254 |
return result; |
|
255 |
} |
|
256 |
} |
|
257 |
|
|
231 | 258 |
|
232 | 259 |
/** |
233 | 260 |
* @param state |
... | ... | |
237 | 264 |
private void initAgentMap(STATE state) { |
238 | 265 |
if (!agentMapIsInitialized && repository != null){ |
239 | 266 |
List<String> propertyPaths = Arrays.asList(""); |
240 |
List<TeamOrPersonBase> existingAgents = repository.getAgentService().list(null, null, null, null, propertyPaths);
|
|
241 |
for (TeamOrPersonBase agent : existingAgents){
|
|
267 |
List<AgentBase> existingAgents = repository.getAgentService().list(null, null, null, null, propertyPaths);
|
|
268 |
for (AgentBase agent : existingAgents){
|
|
242 | 269 |
putAgentBase(agent.getTitleCache(), agent); |
243 | 270 |
} |
244 | 271 |
agentMapIsInitialized = true; |
... | ... | |
346 | 373 |
} |
347 | 374 |
} |
348 | 375 |
|
376 |
public Rights getExistingCopyright(STATE state, |
|
377 |
Rights right) { |
|
378 |
if (right == null || !RightsType.COPYRIGHT().equals(right.getType())){ |
|
379 |
return null; |
|
380 |
}else{ |
|
381 |
initCopyrightMap(state); |
|
382 |
String key = makeCopyrightKey(right); |
|
383 |
Set<Rights> set = copyrightMap.get(key); |
|
384 |
if (set == null || set.isEmpty()){ |
|
385 |
putCopyright(key, right); |
|
386 |
return right; |
|
387 |
}else if (set.size()>1){ |
|
388 |
//TODO |
|
389 |
logger.warn("More than 1 matching copyright not yet handled"); |
|
390 |
} |
|
391 |
return set.iterator().next(); |
|
392 |
} |
|
393 |
} |
|
394 |
|
|
395 |
/** |
|
396 |
* @param state |
|
397 |
*/ |
|
398 |
private void initCopyrightMap(STATE state) { |
|
399 |
if (!copyrightMapIsInitialized && repository != null){ |
|
400 |
List<String> propertyPaths = Arrays.asList(""); |
|
401 |
List<Rights> existingRights = repository.getRightsService().list(null, null, null, null, propertyPaths); |
|
402 |
for (Rights right : existingRights){ |
|
403 |
if (RightsType.COPYRIGHT().equals(right.getType())){ |
|
404 |
putCopyright(makeCopyrightKey(right), right); |
|
405 |
} |
|
406 |
} |
|
407 |
copyrightMapIsInitialized = true; |
|
408 |
} |
|
409 |
|
|
410 |
} |
|
411 |
|
|
412 |
/** |
|
413 |
* @param makeCopyrightKey |
|
414 |
* @param right |
|
415 |
*/ |
|
416 |
private void putCopyright(String key, Rights right) { |
|
417 |
Set<Rights> rights = copyrightMap.get(key); |
|
418 |
if (rights == null){ |
|
419 |
rights = new HashSet<>(); |
|
420 |
copyrightMap.put(key, rights); |
|
421 |
} |
|
422 |
rights.add(right); |
|
423 |
|
|
424 |
} |
|
425 |
|
|
426 |
/** |
|
427 |
* @param right |
|
428 |
* @return |
|
429 |
*/ |
|
430 |
private String makeCopyrightKey(Rights right) { |
|
431 |
if (right.getAgent() != null){ |
|
432 |
return right.getAgent().getTitleCache(); |
|
433 |
}else if (right.getText() != null){ |
|
434 |
return right.getText(); |
|
435 |
}else { |
|
436 |
logger.warn("Key for copyright could not be created: " + right); |
|
437 |
return right.getUuid().toString(); |
|
438 |
} |
|
439 |
} |
|
440 |
|
|
349 | 441 |
} |
Also available in: Unified diff
ref #5744 update deduplication helper for Institutions and Rights