2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.editor
;
12 import java
.util
.ArrayList
;
13 import java
.util
.HashMap
;
14 import java
.util
.List
;
18 import org
.apache
.log4j
.Logger
;
20 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
21 import eu
.etaxonomy
.cdm
.hibernate
.HibernateProxyHelper
;
22 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
23 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
24 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
25 import eu
.etaxonomy
.cdm
.model
.reference
.StrictReferenceBase
;
26 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
27 import eu
.etaxonomy
.cdm
.strategy
.match
.MatchException
;
28 import eu
.etaxonomy
.taxeditor
.editor
.name
.NameComposite
;
29 import eu
.etaxonomy
.taxeditor
.editor
.name
.TaxonNameEditor
;
30 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
33 * This class is supposed to handle possible duplications of cdm entities
34 * for a specific editor
36 * TODO break dependency to editor
43 public class DuplicateArbitrator
{
44 private static final Logger logger
= Logger
.getLogger(DuplicateArbitrator
.class);
47 * The editor this arbitrator should manage possible duplicates for
49 private MultiPageTaxonEditor editor
;
50 private TaxonNameEditor nameEditor
;
51 private Set
<NameComposite
<TaxonBase
>> dirtyNames
;
53 private Map
<TaxonBase
, ReferenceBase
> duplicateReferences
;
55 private Map
<TaxonBase
, TaxonNameBase
> duplicateNames
;
57 private ICommonService commonService
;
60 * @param adaptableObject
62 public DuplicateArbitrator(MultiPageTaxonEditor editor
) {
65 commonService
= CdmStore
.getCommonService();
67 nameEditor
= (TaxonNameEditor
) editor
.getPage(Page
.NAME
);
69 duplicateReferences
= new HashMap
<TaxonBase
, ReferenceBase
>();
70 duplicateNames
= new HashMap
<TaxonBase
, TaxonNameBase
>();
74 * Provide strategies on how to handle possible duplications
76 public void arbitrate() {
77 // iterate over all names that were edited
78 for(NameComposite composite
: nameEditor
.getDirtyNames()){
79 logger
.warn("Found " + composite
+ " with possible duplicates");
81 // since we are dealing with NameComposites getData should always return a TaxonBase
82 // also we do not want to handle viral names at the moment
83 // NonViralName name = (NonViralName) ((TaxonBase) composite.getData()).getName();
85 TaxonBase taxonBase
= HibernateProxyHelper
.deproxy(composite
.getData(), TaxonBase
.class);
87 // TODO decide what entities are candidates for duplicate detection
88 checkDuplicateLatinNames(taxonBase
);
89 checkDuplicateAuthors(taxonBase
);
90 checkDuplicateNomenclaturalReference(taxonBase
);
100 * Provides strategies to solve found duplicates
101 * This can either happen automatically or through user input
103 private void solveDuplicates() {
105 * TODO first idea that comes to mind would be to present the user
106 * a dialog with all possible duplicates and let her mark which entities
109 * If we have sophisticated equals methods that guarantee full equality and
110 * there is only one match of an entity we might want to resolve the duplication
115 for(ReferenceBase reference
: duplicateReferences
.values()){
116 logger
.warn(reference
);
118 for(TaxonNameBase name
: duplicateNames
.values()){
127 private void checkDuplicateNomenclaturalReference(TaxonBase taxonBase
) {
129 NonViralName name
= getName(taxonBase
);
132 StrictReferenceBase referenceBase
= (StrictReferenceBase
) name
.getNomenclaturalReference();
134 List
<StrictReferenceBase
> matches
= commonService
.findMatching(referenceBase
, null);
136 // // query datasource for the reference
137 // List<ReferenceBase> result = CdmStore.getReferenceService().getReferencesByTitle(referenceBase.getTitleCache());
139 // if query delivers results, place possible duplicate in map
140 for(ReferenceBase duplicateReference
: matches
){
141 duplicateReferences
.put(taxonBase
, duplicateReference
);
143 }catch(NullPointerException e
){
144 logger
.warn("Name has no nomenclatural reference");
145 } catch (MatchException e
) {
146 // TODO Auto-generated catch block
155 private void checkDuplicateAuthors(TaxonBase taxonBase
) {
156 // see checkDuplicateNomenclaturalReference for implementation
162 private void checkDuplicateLatinNames(TaxonBase taxonBase
) {
163 NonViralName name
= getName(taxonBase
);
165 List
<NonViralName
> result
= new ArrayList(); //commonService.findMatching(name, null);
167 for(Object object
: result
){
168 TaxonNameBase duplicateName
= HibernateProxyHelper
.deproxy(object
, TaxonNameBase
.class);
169 duplicateNames
.put(taxonBase
, duplicateName
);
174 * Helper method to get the NonViralName of a taxon base.
179 private NonViralName
getName(TaxonBase taxonBase
){
180 return HibernateProxyHelper
.deproxy(taxonBase
.getName(), NonViralName
.class);