argos/dmp-backend/core/src/main/java/eu/eudat/model/persist/descriptionproperties/FieldPersist.java

154 lines
7.5 KiB
Java

package eu.eudat.model.persist.descriptionproperties;
import eu.eudat.commons.enums.FieldType;
import eu.eudat.commons.types.descriptiontemplate.FieldEntity;
import eu.eudat.commons.validation.BaseValidator;
import eu.eudat.model.persist.ReferencePersist;
import gr.cite.tools.validation.ValidatorFactory;
import gr.cite.tools.validation.specification.Specification;
import eu.eudat.convention.ConventionService;
import eu.eudat.errorcode.ErrorThesaurusProperties;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
public class FieldPersist {
private String textValue;
public static final String _textValue = "textValue";
private List<String> textListValue;
public static final String _textListValue = "textListValue";
private Instant dateValue;
public static final String _dateValue = "dateValue";
private List<ReferencePersist> references;
public static final String _references = "references";
private ExternalIdentifierPersist externalIdentifier;
public static final String _externalIdentifier = "externalIdentifier";
public String getTextValue() {
return textValue;
}
public void setTextValue(String textValue) {
this.textValue = textValue;
}
public List<String> getTextListValue() {
return textListValue;
}
public void setTextListValue(List<String> textListValue) {
this.textListValue = textListValue;
}
public Instant getDateValue() {
return dateValue;
}
public void setDateValue(Instant dateValue) {
this.dateValue = dateValue;
}
public List<ReferencePersist> getReferences() {
return references;
}
public void setReferences(List<ReferencePersist> references) {
this.references = references;
}
public ExternalIdentifierPersist getExternalIdentifier() {
return externalIdentifier;
}
public void setExternalIdentifier(ExternalIdentifierPersist externalIdentifier) {
this.externalIdentifier = externalIdentifier;
}
@Component(PersistValidator.ValidatorName)
public static class PersistValidator extends BaseValidator<FieldPersist> {
public static final String ValidatorName = "Description.FieldPersistValidator";
private final ValidatorFactory validatorFactory;
private final MessageSource messageSource;
private FieldEntity fieldEntity;
protected PersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, ValidatorFactory validatorFactory, MessageSource messageSource) {
super(conventionService, errors);
this.validatorFactory = validatorFactory;
this.messageSource = messageSource;
}
@Override
protected Class<FieldPersist> modelClass() {
return FieldPersist.class;
}
@Override
protected List<Specification> specifications(FieldPersist item) {
FieldType fieldType = this.fieldEntity != null && this.fieldEntity.getData() != null ? this.fieldEntity.getData().getFieldType() : FieldType.FREE_TEXT;
return Arrays.asList(
this.spec()
.iff(()-> FieldType.isTextType(fieldType))
.must(() -> !this.isEmpty(item.getTextValue()))
.failOn(FieldPersist._textValue).failWith(messageSource.getMessage("Validation_Required", new Object[]{FieldPersist._textValue}, LocaleContextHolder.getLocale())),
this.spec()
.iff(()-> FieldType.isDateType(fieldType))
.must(() -> !this.isNull(item.getDateValue()))
.failOn(FieldPersist._dateValue).failWith(messageSource.getMessage("Validation_Required", new Object[]{FieldPersist._dateValue}, LocaleContextHolder.getLocale())),
this.spec()
.iff(()-> FieldType.isExternalIdentifierType(fieldType))
.must(() -> !this.isNull(item.getExternalIdentifier()))
.failOn(FieldPersist._externalIdentifier).failWith(messageSource.getMessage("Validation_Required", new Object[]{FieldPersist._externalIdentifier}, LocaleContextHolder.getLocale())),
this.spec()
.iff(()-> FieldType.isTextListType(fieldType))
.must(() -> !this.isNull(item.getTextListValue()))
.failOn(FieldPersist._textListValue).failWith(messageSource.getMessage("Validation_Required", new Object[]{FieldPersist._textListValue}, LocaleContextHolder.getLocale())),
this.spec()
.iff(()-> FieldType.isReferenceType(fieldType))
.must(() -> !this.isNull(item.getTextListValue()))
.failOn(FieldPersist._textListValue).failWith(messageSource.getMessage("Validation_Required", new Object[]{FieldPersist._textListValue}, LocaleContextHolder.getLocale())),
this.spec()
.iff(()-> !this.isEmpty(item.getTextValue()) && fieldType.equals(FieldType.CHECK_BOX) || fieldType.equals(FieldType.BOOLEAN_DECISION))
.must(() -> this.isBoolean(item.getTextValue()))
.failOn(FieldPersist._textValue).failWith(messageSource.getMessage("Validation_UnexpectedValue", new Object[]{FieldPersist._textValue}, LocaleContextHolder.getLocale())),
this.spec()
.iff(()-> !this.isEmpty(item.getTextValue()) && fieldType.equals(FieldType.CURRENCY))
.must(() -> this.isUUID(item.getTextValue()))
.failOn(FieldPersist._textValue).failWith(messageSource.getMessage("Validation_UnexpectedValue", new Object[]{FieldPersist._textValue}, LocaleContextHolder.getLocale())),
this.spec()
.iff(()-> !this.isNull(item.getTextListValue()) && (fieldType.equals(FieldType.INTERNAL_DMP_ENTRIES_DMPS) || fieldType.equals(FieldType.INTERNAL_DMP_ENTRIES_DATASETS)))
.must(() -> item.getTextListValue().stream().allMatch(this::isUUID))
.failOn(FieldPersist._textListValue).failWith(messageSource.getMessage("Validation_UnexpectedValue", new Object[]{FieldPersist._textListValue}, LocaleContextHolder.getLocale())),
this.navSpec()
.iff(() -> FieldType.isReferenceType(fieldType) && !this.isListNullOrEmpty(item.getReferences()))
.on(FieldPersist._references)
.over(item.getReferences())
.using((itm) -> this.validatorFactory.validator(ReferencePersist.ReferencePersistValidator.class)),
this.refSpec()
.iff(() -> FieldType.isExternalIdentifierType(fieldType) && !this.isNull(item.getExternalIdentifier()))
.on(FieldPersist._externalIdentifier)
.over(item.getExternalIdentifier())
.using(() -> this.validatorFactory.validator(ExternalIdentifierPersist.PersistValidator.class))
);
}
public PersistValidator withFieldEntity(FieldEntity fieldEntity) {
this.fieldEntity = fieldEntity;
return this;
}
}
}