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.ReferenceCheck;
|
17
|
|
18
|
/**
|
19
|
* @author k.luther
|
20
|
* @since 2011
|
21
|
*/
|
22
|
public class ReferenceCheckValidator implements ConstraintValidator<ReferenceCheck, Reference>{
|
23
|
|
24
|
@Override
|
25
|
public void initialize(ReferenceCheck constraintAnnotation) {}
|
26
|
|
27
|
@Override
|
28
|
public boolean isValid(Reference value, ConstraintValidatorContext constraintValidatorContext) {
|
29
|
boolean isValid = true;
|
30
|
|
31
|
isValid &= validIsbn(value, constraintValidatorContext);
|
32
|
if (value.getType() == ReferenceType.Journal) {
|
33
|
if (value.getDatePublished() != null){
|
34
|
isValid &= false;
|
35
|
constraintValidatorContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.InReference.JournalShouldNotHaveDatePublished.message}").addConstraintViolation();
|
36
|
}
|
37
|
if (value.getAuthorship() != null){
|
38
|
isValid &= false;
|
39
|
constraintValidatorContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.InReference.JournalShouldNotHaveAnAuthor.message}").addConstraintViolation();
|
40
|
}
|
41
|
}
|
42
|
if (! isValid){
|
43
|
constraintValidatorContext.disableDefaultConstraintViolation();
|
44
|
}
|
45
|
|
46
|
return isValid;
|
47
|
}
|
48
|
|
49
|
private boolean validIsbn(Reference value, ConstraintValidatorContext constraintValidatorContext){
|
50
|
boolean isValid = true;
|
51
|
|
52
|
if ((value.getType() != ReferenceType.Book && value.getType() != ReferenceType.Proceedings && value.getType() != ReferenceType.Generic ) ) {
|
53
|
if (value.getIsbn()!= null){
|
54
|
isValid = false;
|
55
|
constraintValidatorContext.buildConstraintViolationWithTemplate("{eu.etaxonomy.cdm.validation.annotation.InReference.ReferenceShouldNotHaveIsbn.message}").addConstraintViolation();
|
56
|
}
|
57
|
}
|
58
|
return isValid;
|
59
|
}
|
60
|
|
61
|
}
|