Project

General

Profile

Download (2.16 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
package eu.etaxonomy.cdm.validation.constraint;
10

    
11
import javax.validation.ConstraintValidator;
12
import javax.validation.ConstraintValidatorContext;
13

    
14
import eu.etaxonomy.cdm.model.reference.Reference;
15
import eu.etaxonomy.cdm.model.reference.ReferenceType;
16
import eu.etaxonomy.cdm.validation.annotation.InReference;
17

    
18
public class InReferenceValidator implements ConstraintValidator<InReference, Reference> {
19

    
20
	@Override
21
    public void initialize(InReference constraintAnnotation) {}
22

    
23
	@Override
24
    public boolean isValid(Reference value, ConstraintValidatorContext constraintValidatorContext) {
25
		boolean isValid = true;
26
		try {
27
    		if (value.getInReference() != null){
28
    			if (value.getType() == ReferenceType.Article ){
29
					if (value.getInReference().getType() != ReferenceType.Journal) {
30
                        isValid = false;
31
                    }
32
    			}
33
    			if (value.getType() == ReferenceType.BookSection){
34
    				if (value.getInReference().getType() != ReferenceType.Book) {
35
                        isValid = false;
36
                    }
37
    			}
38
    			if (value.getType() == ReferenceType.InProceedings){
39
    				if (value.getInReference().getType() != ReferenceType.Proceedings) {
40
                        isValid = false;
41
                    }
42
    			}
43
    			if (value.getType() == ReferenceType.Book){
44
    				if (value.getInReference().getType() != ReferenceType.PrintSeries) {
45
                        isValid = false;
46
                    }
47
    			}
48

    
49
    		}
50
    		if (!isValid){
51
    			constraintValidatorContext.disableDefaultConstraintViolation();
52
    			constraintValidatorContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.InReference.wrongInReferenceForReferenceType.message}").addNode("inReference").addConstraintViolation();
53
    		}
54
		}catch(NullPointerException e){
55
			return isValid;
56
		}
57

    
58
		return isValid;
59
	}
60

    
61
}
(7-7/18)