argos/dmp-backend/core/src/main/java/eu/eudat/model/persist/descriptiontemplatedefinition/RulePersist.java

171 lines
8.3 KiB
Java

package eu.eudat.model.persist.descriptiontemplatedefinition;
import eu.eudat.commons.enums.FieldType;
import eu.eudat.commons.validation.BaseValidator;
import eu.eudat.model.persist.ReferencePersist;
import eu.eudat.model.persist.descriptionproperties.FieldPersist;
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.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Scope;
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 RulePersist {
private String target = null;
public static final String _target = "target";
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 getTarget() {
return target;
}
public void setTarget(String target) {
this.target = target;
}
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(RulePersistValidator.ValidatorName)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public static class RulePersistValidator extends BaseValidator<RulePersist> {
public static final String ValidatorName = "DescriptionTemplate.RulePersistValidator";
private final MessageSource messageSource;
private final ValidatorFactory validatorFactory;
private eu.eudat.model.persist.descriptiontemplatedefinition.FieldPersist fieldEntity;
protected RulePersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) {
super(conventionService, errors);
this.messageSource = messageSource;
this.validatorFactory = validatorFactory;
}
public RulePersistValidator withFieldPersist(eu.eudat.model.persist.descriptiontemplatedefinition.FieldPersist fieldEntity) {
this.fieldEntity = fieldEntity;
return this;
}
@Override
protected Class<RulePersist> modelClass() {
return RulePersist.class;
}
@Override
protected List<Specification> specifications(RulePersist item) {
FieldType fieldType = this.fieldEntity != null && this.fieldEntity.getData() != null ? this.fieldEntity.getData().getFieldType() : FieldType.FREE_TEXT;
return Arrays.asList(
this.spec()
.must(() -> !this.isEmpty(item.getTarget()))
.failOn(RulePersist._target).failWith(messageSource.getMessage("Validation_Required", new Object[]{RulePersist._target}, LocaleContextHolder.getLocale())),
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_ENTRIES_DMPS) || fieldType.equals(FieldType.INTERNAL_ENTRIES_DESCRIPTIONS)))
.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))
);
}
}
}