Project

General

Profile

Download (1.78 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.name.Rank;
16
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
17
import eu.etaxonomy.cdm.validation.annotation.ChildTaxaMustBeLowerRankThanParent;
18

    
19
public class ChildTaxaMustBeLowerRankThanParentValidator implements
20
		ConstraintValidator<ChildTaxaMustBeLowerRankThanParent, TaxonNode> {
21

    
22
	@Override
23
    public void initialize(ChildTaxaMustBeLowerRankThanParent childTaxaMustBeLowerRankThanParent) { }
24

    
25
	@Override
26
    public boolean isValid(TaxonNode taxonNode, ConstraintValidatorContext constraintContext) {
27
		boolean valid = true;
28
        try {
29
            Rank parentRank = taxonNode.getParent() == null ? null : taxonNode.getParent().getNullSafeRank();
30
            Rank childRank = taxonNode.getNullSafeRank();
31
            if (parentRank != null && childRank != null ){
32
                if(parentRank.equals(childRank) || parentRank.isLower(childRank)) {
33
                    valid = false;
34
                    constraintContext.disableDefaultConstraintViolation();
35
                    constraintContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.ChildTaxaMustBeLowerRankThanParent.message}").addNode("fromTaxon").addNode("name").addNode("rank").addConstraintViolation();
36
                }
37
            }
38
        } catch (Exception e) {
39
            throw new RuntimeException(e);
40
        }
41

    
42
		return valid;
43
	}
44

    
45
}
(2-2/20)