1
|
/**
|
2
|
* Copyright (C) 2009 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.validation.constraint;
|
11
|
|
12
|
import java.util.Set;
|
13
|
|
14
|
import javax.validation.ConstraintValidator;
|
15
|
import javax.validation.ConstraintValidatorContext;
|
16
|
|
17
|
import org.hibernate.Hibernate;
|
18
|
|
19
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
20
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
21
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
22
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
23
|
import eu.etaxonomy.cdm.validation.annotation.CorrectEpithetsForRank;
|
24
|
import eu.etaxonomy.cdm.validation.annotation.TaxonNameCannotBeAcceptedAndSynonym;
|
25
|
|
26
|
public class TaxonNameCannotBeAcceptedAndSynonymValidator implements
|
27
|
ConstraintValidator<TaxonNameCannotBeAcceptedAndSynonym, TaxonBase> {
|
28
|
|
29
|
public void initialize(TaxonNameCannotBeAcceptedAndSynonym taxonNameCannotBeAcceptedAndSynonym) { }
|
30
|
|
31
|
public boolean isValid(TaxonBase taxon, ConstraintValidatorContext constraintContext) {
|
32
|
boolean valid = true;
|
33
|
if(Hibernate.isInitialized(taxon.getName()) && Hibernate.isInitialized(taxon.getName().getTaxonBases())) {
|
34
|
Set<TaxonBase> taxa = taxon.getName().getTaxonBases();
|
35
|
|
36
|
for(TaxonBase t1 : taxa) {
|
37
|
for(TaxonBase t2 : taxa) { // pairwise comparison of all taxa sharing the same name
|
38
|
if(!t1.equals(t2)) { // exclude self comparison
|
39
|
if(Hibernate.isInitialized(t1.getSec()) && Hibernate.isInitialized(t2.getSec())) { // Check that the sec property is initialized
|
40
|
if(t1.getSec().equals(t2.getSec())) { // only compare concepts belonging to the same source
|
41
|
TaxonBase taxonBase1 = TaxonBase.deproxy(t1, TaxonBase.class);
|
42
|
TaxonBase taxonBase2 = TaxonBase.deproxy(t2, TaxonBase.class);
|
43
|
|
44
|
if(taxonBase1 instanceof Taxon && taxonBase2 instanceof Taxon) {
|
45
|
valid = false;
|
46
|
constraintContext.buildErrorWithMessageTemplate("{eu.etaxonomy.cdm.validation.annotation.TaxonNameCannotBeAcceptedAndSynonym.twoAcceptedTaxaNotAllowed.message}").addSubNode("name").addError();
|
47
|
}
|
48
|
if((taxonBase1 instanceof Taxon && taxonBase2 instanceof Synonym) || (taxonBase1 instanceof Synonym && taxonBase2 instanceof Taxon)) {
|
49
|
valid = false;
|
50
|
constraintContext.buildErrorWithMessageTemplate("{eu.etaxonomy.cdm.validation.annotation.TaxonNameCannotBeAcceptedAndSynonym.synonymAndTaxonNotAllowed.message}").addSubNode("name").addError();
|
51
|
}
|
52
|
}
|
53
|
}
|
54
|
}
|
55
|
}
|
56
|
}
|
57
|
}
|
58
|
return valid;
|
59
|
}
|
60
|
}
|