Project

General

Profile

Download (4.39 KB) Statistics
| Branch: | Tag: | Revision:
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.NameRelationship;
17
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
18
import eu.etaxonomy.cdm.model.name.NonViralName;
19
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
20
import eu.etaxonomy.cdm.model.name.ZoologicalName;
21
import eu.etaxonomy.cdm.validation.annotation.BasionymsMustShareEpithetsAndAuthors;
22

    
23

    
24
public class BasionymsMustShareEpithetsAndAuthorsValidator implements
25
		ConstraintValidator<BasionymsMustShareEpithetsAndAuthors, NameRelationship> {
26

    
27
	public void initialize(BasionymsMustShareEpithetsAndAuthors basionymsMustShareEpithetsAndAuthors) { }
28

    
29
	public boolean isValid(NameRelationship nameRelationship, ConstraintValidatorContext constraintContext) {
30
		boolean valid = true;
31
		if(nameRelationship.getType().equals(NameRelationshipType.BASIONYM())) {
32
			TaxonNameBase from = CdmBase.deproxy(nameRelationship.getFromName(), TaxonNameBase.class);
33
			TaxonNameBase to = CdmBase.deproxy(nameRelationship.getToName(), TaxonNameBase.class);
34
			
35
			if(from instanceof NonViralName && to instanceof NonViralName) {
36
				NonViralName fromName = (NonViralName) from;
37
				NonViralName toName = (NonViralName) to;
38
				if(fromName.getBasionymAuthorTeam() == null || !fromName.getBasionymAuthorTeam().equals(toName.getBasionymAuthorTeam())) {
39
					valid = false;
40
					constraintContext.buildErrorWithMessageTemplate("{eu.etaxonomy.cdm.validation.annotation.BasionymsMustShareEpithetsAndAuthors.differentAuthors.message}").addSubNode("fromName").addSubNode("basionymAuthorTeam").addError();				
41
					constraintContext.buildErrorWithMessageTemplate("{eu.etaxonomy.cdm.validation.annotation.BasionymsMustShareEpithetsAndAuthors.differentAuthors.message}").addSubNode("toName").addSubNode("basionymAuthorTeam").addError();
42
				}
43

    
44
				String fromNameLastEpithet = fromName.getInfraSpecificEpithet() == null ? fromName.getInfraSpecificEpithet() : fromName.getSpecificEpithet();
45
				String toNameLastEpithet = toName.getInfraSpecificEpithet() == null ? toName.getInfraSpecificEpithet() : toName.getSpecificEpithet();
46
				if(!fromNameLastEpithet.equals(toNameLastEpithet)) {
47
					valid = false;
48
					constraintContext.buildErrorWithMessageTemplate("{eu.etaxonomy.cdm.validation.annotation.BasionymsMustShareEpithetsAndAuthors.differentEpithets.message}").addSubNode("fromName").addSubNode("nameCache").addError();				
49
					constraintContext.buildErrorWithMessageTemplate("{eu.etaxonomy.cdm.validation.annotation.BasionymsMustShareEpithetsAndAuthors.differentEpithets.message}").addSubNode("toName").addSubNode("nameCache").addError();
50
				}
51
				
52
				if(fromName instanceof ZoologicalName && toName instanceof ZoologicalName) {
53
					if(!fromName.getNomenclaturalReference().equals(toName.getNomenclaturalReference())) {
54
						valid = false;
55
						constraintContext.buildErrorWithMessageTemplate("{eu.etaxonomy.cdm.validation.annotation.BasionymsMustShareEpithetsAndAuthors.differentNomenclaturalReference.message}").addSubNode("fromName").addSubNode("nomenclaturalReference").addError();				
56
						constraintContext.buildErrorWithMessageTemplate("{eu.etaxonomy.cdm.validation.annotation.BasionymsMustShareEpithetsAndAuthors.differentNomenclaturalReference.message}").addSubNode("toName").addSubNode("nomenclaturalReference").addError();
57
					}
58
					
59
					if(!fromName.getNomenclaturalMicroReference().equals(toName.getNomenclaturalMicroReference())) {
60
						valid = false;
61
						constraintContext.buildErrorWithMessageTemplate("{eu.etaxonomy.cdm.validation.annotation.BasionymsMustShareEpithetsAndAuthors.differentNomenclaturalReference.message}").addSubNode("fromName").addSubNode("nomenclaturalMicroReference").addError();				
62
						constraintContext.buildErrorWithMessageTemplate("{eu.etaxonomy.cdm.validation.annotation.BasionymsMustShareEpithetsAndAuthors.differentNomenclaturalReference.message}").addSubNode("toName").addSubNode("nomenclaturalMicroReference").addError();
63
					}
64
				}
65
			}
66
				
67
		}
68
		
69
		return valid;		
70
	}
71
}
(1-1/10)