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 javax.validation.ConstraintValidator;
|
13
|
import javax.validation.ConstraintValidatorContext;
|
14
|
|
15
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
16
|
import eu.etaxonomy.cdm.model.name.INonViralName;
|
17
|
import eu.etaxonomy.cdm.model.name.NameRelationship;
|
18
|
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
|
19
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
20
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
21
|
import eu.etaxonomy.cdm.model.name.ZoologicalName;
|
22
|
import eu.etaxonomy.cdm.validation.annotation.BasionymsMustShareEpithetsAndAuthors;
|
23
|
|
24
|
|
25
|
public class BasionymsMustShareEpithetsAndAuthorsValidator implements
|
26
|
ConstraintValidator<BasionymsMustShareEpithetsAndAuthors, NameRelationship> {
|
27
|
|
28
|
@Override
|
29
|
public void initialize(BasionymsMustShareEpithetsAndAuthors basionymsMustShareEpithetsAndAuthors) { }
|
30
|
|
31
|
@Override
|
32
|
public boolean isValid(NameRelationship nameRelationship, ConstraintValidatorContext constraintContext) {
|
33
|
boolean valid = true;
|
34
|
if(nameRelationship.getType() != null && nameRelationship.getType().equals(NameRelationshipType.BASIONYM())) {
|
35
|
TaxonNameBase<?,?> from = CdmBase.deproxy(nameRelationship.getFromName(), TaxonNameBase.class);
|
36
|
TaxonNameBase<?,?> to = CdmBase.deproxy(nameRelationship.getToName(), TaxonNameBase.class);
|
37
|
|
38
|
if(from instanceof NonViralName && to instanceof NonViralName) {
|
39
|
INonViralName fromName = from;
|
40
|
INonViralName toName = to;
|
41
|
|
42
|
//compare author teams
|
43
|
if(fromName.getCombinationAuthorship() == null || !fromName.getCombinationAuthorship().equals(toName.getBasionymAuthorship())) {
|
44
|
valid = false;
|
45
|
constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.BasionymsMustShareEpithetsAndAuthors.differentAuthors.message}").addNode("fromName").addNode("basionymAuthorship").addConstraintViolation();
|
46
|
//remove duplicate violation as it does not give more information
|
47
|
// constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.BasionymsMustShareEpithetsAndAuthors.differentAuthors.message}").addNode("toName").addNode("basionymAuthorship").addConstraintViolation();
|
48
|
}
|
49
|
|
50
|
//compare last epithet
|
51
|
String fromNameLastEpithet = fromName.getInfraSpecificEpithet() != null ? fromName.getInfraSpecificEpithet() : fromName.getSpecificEpithet();
|
52
|
String toNameLastEpithet = toName.getInfraSpecificEpithet() != null ? toName.getInfraSpecificEpithet() : toName.getSpecificEpithet();
|
53
|
if( fromNameLastEpithet != null && ! fromNameLastEpithet.equals(toNameLastEpithet)) {
|
54
|
valid = false;
|
55
|
constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.BasionymsMustShareEpithetsAndAuthors.differentEpithets.message}").addNode("fromName").addNode("nameCache").addConstraintViolation();
|
56
|
//remove duplicate violation as it does not give more information
|
57
|
// constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.BasionymsMustShareEpithetsAndAuthors.differentEpithets.message}").addNode("toName").addNode("nameCache").addConstraintViolation();
|
58
|
}
|
59
|
|
60
|
|
61
|
//compare nomRefs and details for zoological names
|
62
|
//why only for zooNames?
|
63
|
if(fromName instanceof ZoologicalName && toName instanceof ZoologicalName) {
|
64
|
if(fromName.getNomenclaturalReference() != null && !fromName.getNomenclaturalReference().equals(toName.getNomenclaturalReference())) {
|
65
|
valid = false;
|
66
|
constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.BasionymsMustShareEpithetsAndAuthors.differentNomenclaturalReference.message}").addNode("fromName").addNode("nomenclaturalReference").addConstraintViolation();
|
67
|
//remove duplicate violation as it does not give more information
|
68
|
constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.BasionymsMustShareEpithetsAndAuthors.differentNomenclaturalReference.message}").addNode("toName").addNode("nomenclaturalReference").addConstraintViolation();
|
69
|
}
|
70
|
|
71
|
if(fromName.getNomenclaturalMicroReference() != null && !fromName.getNomenclaturalMicroReference().equals(toName.getNomenclaturalMicroReference())) {
|
72
|
valid = false;
|
73
|
constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.BasionymsMustShareEpithetsAndAuthors.differentNomenclaturalReference.message}").addNode("fromName").addNode("nomenclaturalMicroReference").addConstraintViolation();
|
74
|
//remove duplicate violation as it does not give more information
|
75
|
constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.BasionymsMustShareEpithetsAndAuthors.differentNomenclaturalReference.message}").addNode("toName").addNode("nomenclaturalMicroReference").addConstraintViolation();
|
76
|
}
|
77
|
}
|
78
|
}
|
79
|
|
80
|
}
|
81
|
if(!valid){
|
82
|
constraintContext.disableDefaultConstraintViolation();
|
83
|
}
|
84
|
|
85
|
return valid;
|
86
|
}
|
87
|
}
|