Project

General

Profile

Download (4.22 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.isCultivar()){
29
		    return valid;  //there are no strict rules for cultivar ranks so far
30
		}
31
		if(name.isSupraGeneric() || name.isGenus()) {
32
			if(isNotBlank(name.getInfraGenericEpithet())) {
33
				valid = false;
34
				constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.CorrectEpithetsForRank.epithetNotNull}").addPropertyNode("infraGenericEpithet").addConstraintViolation();
35
			}
36

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

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

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

    
85
    private boolean isNotBlank(String str) {
86
        return StringUtils.isNotBlank(str);
87
    }
88

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