argos/backend/core/src/main/java/org/opencdmp/model/persist/descriptiontemplatedefinition/RulePersist.java

143 lines
6.4 KiB
Java
Raw Normal View History

2024-04-26 12:50:56 +02:00
package org.opencdmp.model.persist.descriptiontemplatedefinition;
2023-10-30 14:07:59 +01:00
2024-03-20 17:40:34 +01:00
import gr.cite.tools.validation.ValidatorFactory;
2024-01-17 10:20:02 +01:00
import gr.cite.tools.validation.specification.Specification;
2024-05-02 09:46:36 +02:00
import org.opencdmp.commons.enums.FieldType;
import org.opencdmp.commons.validation.BaseValidator;
2024-04-26 12:50:56 +02:00
import org.opencdmp.convention.ConventionService;
import org.opencdmp.errorcode.ErrorThesaurusProperties;
2024-05-02 09:46:36 +02:00
import org.opencdmp.model.persist.ReferencePersist;
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;
2024-03-20 17:40:34 +01:00
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
2023-10-30 14:07:59 +01:00
public class RulePersist {
2024-05-02 09:46:36 +02:00
private String target;
public static final String _target = "target";
2024-03-20 17:40:34 +01:00
private String textValue;
public static final String _textValue = "textValue";
2024-03-20 17:40:34 +01:00
private Instant dateValue;
public static final String _dateValue = "dateValue";
2024-05-02 09:46:36 +02:00
private Boolean booleanValue;
public static final String _booleanValue = "booleanValue";
2024-03-20 17:40:34 +01:00
private List<ReferencePersist> references;
public static final String _references = "references";
public String getTarget() {
2024-05-02 09:46:36 +02:00
return this.target;
}
public void setTarget(String target) {
this.target = target;
}
2024-03-20 17:40:34 +01:00
public String getTextValue() {
2024-05-02 09:46:36 +02:00
return this.textValue;
2024-03-20 17:40:34 +01:00
}
public void setTextValue(String textValue) {
this.textValue = textValue;
}
public Instant getDateValue() {
2024-05-02 09:46:36 +02:00
return this.dateValue;
2024-03-20 17:40:34 +01:00
}
public void setDateValue(Instant dateValue) {
this.dateValue = dateValue;
}
2024-05-02 09:46:36 +02:00
public Boolean getBooleanValue() {
return this.booleanValue;
}
public void setBooleanValue(Boolean booleanValue) {
this.booleanValue = booleanValue;
}
2024-03-20 17:40:34 +01:00
public List<ReferencePersist> getReferences() {
2024-05-02 09:46:36 +02:00
return this.references;
2024-03-20 17:40:34 +01:00
}
public void setReferences(List<ReferencePersist> references) {
this.references = references;
}
@Component(RulePersistValidator.ValidatorName)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public static class RulePersistValidator extends BaseValidator<RulePersist> {
2023-10-30 14:07:59 +01:00
public static final String ValidatorName = "DescriptionTemplate.RulePersistValidator";
2023-10-30 14:07:59 +01:00
private final MessageSource messageSource;
2024-03-20 17:40:34 +01:00
private final ValidatorFactory validatorFactory;
2024-04-26 12:50:56 +02:00
private org.opencdmp.model.persist.descriptiontemplatedefinition.FieldPersist fieldEntity;
2023-10-30 14:07:59 +01:00
2024-03-20 17:40:34 +01:00
protected RulePersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) {
super(conventionService, errors);
this.messageSource = messageSource;
2024-03-20 17:40:34 +01:00
this.validatorFactory = validatorFactory;
}
2024-04-26 12:50:56 +02:00
public RulePersistValidator withFieldPersist(org.opencdmp.model.persist.descriptiontemplatedefinition.FieldPersist fieldEntity) {
2024-03-20 17:40:34 +01:00
this.fieldEntity = fieldEntity;
return this;
}
2023-10-30 14:07:59 +01:00
@Override
protected Class<RulePersist> modelClass() {
return RulePersist.class;
}
2023-10-30 14:07:59 +01:00
@Override
protected List<Specification> specifications(RulePersist item) {
2024-03-20 17:40:34 +01:00
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()))
2024-05-02 09:46:36 +02:00
.failOn(RulePersist._target).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{RulePersist._target}, LocaleContextHolder.getLocale())),
this.spec()
.iff(() -> this.isNull(fieldType))
.must(() -> !FieldType.TAGS.equals(fieldType) && !FieldType.INTERNAL_ENTRIES_DESCRIPTIONS.equals(fieldType) && !FieldType.INTERNAL_ENTRIES_DMPS.equals(fieldType) &&
!FieldType.UPLOAD.equals(fieldType) && !FieldType.REFERENCE_TYPES.equals(fieldType) && !FieldType.DATASET_IDENTIFIER.equals(fieldType)
&& !FieldType.VALIDATION.equals(fieldType))
.failOn(RulePersist._target).failWith(this.messageSource.getMessage("Validation_UnexpectedValue", new Object[]{RulePersist._target}, LocaleContextHolder.getLocale())),
this.spec()
2024-03-20 17:40:34 +01:00
.iff(()-> FieldType.isTextType(fieldType))
.must(() -> !this.isEmpty(item.getTextValue()))
2024-05-02 09:46:36 +02:00
.failOn(RulePersist._textValue).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{RulePersist._textValue}, LocaleContextHolder.getLocale())),
this.spec()
.iff(()-> FieldType.isTextListType(fieldType))
.must(() -> !this.isEmpty(item.getTextValue()))
.failOn(RulePersist._textValue).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{RulePersist._textValue}, LocaleContextHolder.getLocale())),
2024-03-20 17:40:34 +01:00
this.spec()
.iff(()-> FieldType.isDateType(fieldType))
.must(() -> !this.isNull(item.getDateValue()))
2024-05-02 09:46:36 +02:00
.failOn(RulePersist._dateValue).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{RulePersist._dateValue}, LocaleContextHolder.getLocale())),
this.spec()
.iff(()-> FieldType.isBooleanType(fieldType))
.must(() -> !this.isNull(item.getBooleanValue()))
.failOn(RulePersist._booleanValue).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{RulePersist._booleanValue}, LocaleContextHolder.getLocale())),
2024-03-20 17:40:34 +01:00
this.navSpec()
.iff(() -> FieldType.isReferenceType(fieldType) && !this.isListNullOrEmpty(item.getReferences()))
2024-05-02 09:46:36 +02:00
.on(RulePersist._references)
2024-03-20 17:40:34 +01:00
.over(item.getReferences())
2024-05-02 15:29:50 +02:00
.using((itm) -> this.validatorFactory.validator(ReferencePersist.ReferencePersistValidator.class))
);
}
}
2023-10-30 14:07:59 +01:00
}