Project

General

Profile

Download (4.1 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 org.apache.commons.lang3.StringUtils;
16

    
17
import eu.etaxonomy.cdm.model.name.INonViralName;
18
import eu.etaxonomy.cdm.validation.annotation.CorrectEpithetsForRank;
19

    
20
public class CorrectEpithetsForRankValidator implements ConstraintValidator<CorrectEpithetsForRank, INonViralName> {
21

    
22
	@Override
23
    public void initialize(CorrectEpithetsForRank correctEpithetsForRank) { }
24

    
25
	@Override
26
	public boolean isValid(INonViralName name, ConstraintValidatorContext constraintContext) {
27
		boolean valid = true;
28
		if(name.isSupraGeneric() || name.isGenus()) {
29
			if(isNotBlank(name.getInfraGenericEpithet())) {
30
				valid = false;
31
				constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.CorrectEpithetsForRank.epithetNotNull}").addNode("infraGenericEpithet").addConstraintViolation();
32
			}
33

    
34
			if(isNotBlank(name.getSpecificEpithet())) {
35
				valid = false;
36
				constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.CorrectEpithetsForRank.epithetNotNull}").addNode("specificEpithet").addConstraintViolation();
37
			}
38
			if(isNotBlank(name.getInfraSpecificEpithet())) {
39
				valid = false;
40
				constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.CorrectEpithetsForRank.epithetNotNull}").addNode("infraSpecificEpithet").addConstraintViolation();
41
			}
42
		} else if(name.isInfraGeneric()) {
43
			if(isBlank(name.getInfraGenericEpithet())) {
44
				valid = false;
45
				constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.CorrectEpithetsForRank.epithetNull}").addNode("infraGenericEpithet").addConstraintViolation();
46
			}
47

    
48
			if(isNotBlank(name.getSpecificEpithet())) {
49
				valid = false;
50
				constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.CorrectEpithetsForRank.epithetNotNull}").addNode("specificEpithet").addConstraintViolation();
51
			}
52
			if(isNotBlank(name.getInfraSpecificEpithet())) {
53
				valid = false;
54
				constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.CorrectEpithetsForRank.epithetNotNull}").addNode("infraSpecificEpithet").addConstraintViolation();
55
			}
56
		} else if(name.isSpecies()) {
57
			if(isBlank(name.getSpecificEpithet())) {
58
				valid = false;
59
				constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.CorrectEpithetsForRank.epithetNull}").addNode("specificEpithet").addConstraintViolation();
60
			}
61

    
62
			if(isNotBlank(name.getInfraSpecificEpithet())) {
63
				valid = false;
64
				constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.CorrectEpithetsForRank.epithetNotNull}").addNode("infraSpecificEpithet").addConstraintViolation();
65
			}
66
		} else if(name.isInfraSpecific()) {
67
			if(isBlank(name.getSpecificEpithet())) {
68
				valid = false;
69
				constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.CorrectEpithetsForRank.epithetNull}").addNode("specificEpithet").addConstraintViolation();
70
			}
71
			if(isBlank(name.getInfraSpecificEpithet())) {
72
				valid = false;
73
				constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.CorrectEpithetsForRank.epithetNull}").addNode("infraSpecificEpithet").addConstraintViolation();
74
			}
75
		}
76
		if (!valid){
77
		    constraintContext.disableDefaultConstraintViolation();
78
		}
79
		return valid;
80
	}
81

    
82
    /**
83
     * @param specificEpithet
84
     * @return
85
     */
86
    private boolean isNotBlank(String str) {
87
        return StringUtils.isNotBlank(str);
88
    }
89

    
90
    private boolean isBlank(String str) {
91
        return StringUtils.isBlank(str);
92
    }
93
}
(5-5/18)