Project

General

Profile

Download (2.01 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.validation.constraint;
2

    
3
import javax.validation.ConstraintValidator;
4
import javax.validation.ConstraintValidatorContext;
5

    
6
import eu.etaxonomy.cdm.model.reference.Reference;
7
import eu.etaxonomy.cdm.model.reference.ReferenceType;
8
import eu.etaxonomy.cdm.validation.annotation.ReferenceCheck;
9

    
10
/**
11
 * @author k.luther
12
 * @since 2011
13
 *
14
 */
15
public class ReferenceCheckValidator implements ConstraintValidator<ReferenceCheck, Reference>{
16

    
17
    @Override
18
    public void initialize(ReferenceCheck constraintAnnotation) {}
19

    
20
	@Override
21
    public boolean isValid(Reference value, ConstraintValidatorContext constraintValidatorContext) {
22
		boolean isValid = true;
23

    
24
		isValid &= validIsbn(value, constraintValidatorContext);
25
		if (value.getType() == ReferenceType.Journal) {
26
			if (value.getDatePublished() != null){
27
			    isValid &= false;
28
			    constraintValidatorContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.InReference.JournalShouldNotHaveDatePublished.message}").addConstraintViolation();
29
			}
30
	         if (value.getAuthorship() != null){
31
	             isValid &= false;
32
	             constraintValidatorContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.InReference.JournalShouldNotHaveAnAuthor.message}").addConstraintViolation();
33
	         }
34
		}
35
		if (! isValid){
36
		    constraintValidatorContext.disableDefaultConstraintViolation();
37
		}
38

    
39
		return isValid;
40
	}
41

    
42

    
43

    
44
	private boolean validIsbn(Reference value, ConstraintValidatorContext constraintValidatorContext){
45
		boolean isValid = true;
46

    
47
		if ((value.getType() != ReferenceType.Book && value.getType() != ReferenceType.Proceedings && value.getType() != ReferenceType.Generic ) ) {
48
			if (value.getIsbn()!= null){
49
				isValid = false;
50
				constraintValidatorContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.InReference.ReferenceShouldNotHaveIsbn.message}").addConstraintViolation();
51
			}
52
		}
53
		return isValid;
54
	}
55

    
56
}
(13-13/17)