Mercurial > hg > LGDataverses
comparison src/main/java/edu/harvard/iq/dataverse/DatasetFieldValidator.java @ 10:a50cf11e5178
Rewrite LGDataverse completely upgrading to dataverse4.0
| author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
|---|---|
| date | Tue, 08 Sep 2015 17:00:21 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 9:5926d6419569 | 10:a50cf11e5178 |
|---|---|
| 1 /* | |
| 2 * To change this license header, choose License Headers in Project Properties. | |
| 3 * To change this template file, choose Tools | Templates | |
| 4 * and open the template in the editor. | |
| 5 */ | |
| 6 package edu.harvard.iq.dataverse; | |
| 7 | |
| 8 import javax.validation.ConstraintValidator; | |
| 9 import javax.validation.ConstraintValidatorContext; | |
| 10 import org.apache.commons.lang.StringUtils; | |
| 11 | |
| 12 | |
| 13 /** | |
| 14 * | |
| 15 * @author gdurand | |
| 16 */ | |
| 17 public class DatasetFieldValidator implements ConstraintValidator<ValidateDatasetFieldType, DatasetField> { | |
| 18 | |
| 19 @Override | |
| 20 public void initialize(ValidateDatasetFieldType constraintAnnotation) { | |
| 21 } | |
| 22 | |
| 23 @Override | |
| 24 public boolean isValid(DatasetField value, ConstraintValidatorContext context) { | |
| 25 context.disableDefaultConstraintViolation(); // we do this so we can have different messages depending on the different issue | |
| 26 | |
| 27 DatasetFieldType dsfType = value.getDatasetFieldType(); | |
| 28 //SEK Additional logic turns off validation for templates | |
| 29 if (isTemplateDatasetField(value)){ | |
| 30 return true; | |
| 31 } | |
| 32 if (((dsfType.isPrimitive() && dsfType.isRequired()) || (dsfType.isPrimitive() && value.isRequired())) | |
| 33 && StringUtils.isBlank(value.getValue())) { | |
| 34 context.buildConstraintViolationWithTemplate(dsfType.getDisplayName() + " is required.").addConstraintViolation(); | |
| 35 return false; | |
| 36 } | |
| 37 return true; | |
| 38 } | |
| 39 | |
| 40 private boolean isTemplateDatasetField(DatasetField dsf) { | |
| 41 if (dsf.getParentDatasetFieldCompoundValue() != null) { | |
| 42 return isTemplateDatasetField(dsf.getParentDatasetFieldCompoundValue().getParentDatasetField()); | |
| 43 } else { | |
| 44 return dsf.getTemplate() != null; | |
| 45 } | |
| 46 } | |
| 47 } |
