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