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