2023-11-03 15:02:47 +01:00
|
|
|
package eu.eudat.model.persist.dmpproperties;
|
|
|
|
|
2024-04-08 12:47:00 +02:00
|
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
|
|
|
import eu.eudat.commons.enums.DmpBlueprintFieldCategory;
|
|
|
|
import eu.eudat.commons.types.dmpblueprint.DefinitionEntity;
|
|
|
|
import eu.eudat.commons.types.dmpblueprint.FieldEntity;
|
|
|
|
import eu.eudat.commons.types.dmpblueprint.ReferenceTypeFieldEntity;
|
2024-01-02 16:39:50 +01:00
|
|
|
import eu.eudat.commons.validation.BaseValidator;
|
2024-04-08 12:47:00 +02:00
|
|
|
import eu.eudat.model.ReferenceType;
|
|
|
|
import eu.eudat.query.ReferenceTypeQuery;
|
|
|
|
import gr.cite.tools.data.query.QueryFactory;
|
|
|
|
import gr.cite.tools.fieldset.BaseFieldSet;
|
2024-01-31 11:30:21 +01:00
|
|
|
import gr.cite.tools.validation.ValidatorFactory;
|
2024-01-17 10:20:02 +01:00
|
|
|
import gr.cite.tools.validation.specification.Specification;
|
2024-01-02 16:39:50 +01:00
|
|
|
import eu.eudat.convention.ConventionService;
|
|
|
|
import eu.eudat.errorcode.ErrorThesaurusProperties;
|
2024-01-12 14:00:34 +01:00
|
|
|
import eu.eudat.model.persist.ReferencePersist;
|
2024-01-02 16:39:50 +01:00
|
|
|
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
|
2024-01-12 14:00:34 +01:00
|
|
|
import org.springframework.context.MessageSource;
|
2024-01-02 16:39:50 +01:00
|
|
|
import org.springframework.context.annotation.Scope;
|
2024-01-12 14:00:34 +01:00
|
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
2024-01-02 16:39:50 +01:00
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
2024-01-12 14:00:34 +01:00
|
|
|
import java.util.Arrays;
|
2024-01-02 16:39:50 +01:00
|
|
|
import java.util.List;
|
2024-01-12 14:00:34 +01:00
|
|
|
import java.util.UUID;
|
2024-01-02 16:39:50 +01:00
|
|
|
|
2023-11-03 15:02:47 +01:00
|
|
|
public class DmpBlueprintValuePersist {
|
2024-01-12 14:00:34 +01:00
|
|
|
private UUID fieldId;
|
2023-11-03 15:02:47 +01:00
|
|
|
|
2024-01-12 14:00:34 +01:00
|
|
|
public static final String _fieldId = "fieldId";
|
2023-11-03 15:02:47 +01:00
|
|
|
private String fieldValue;
|
2024-01-12 14:00:34 +01:00
|
|
|
public static final String _fieldValue = "fieldValue";
|
|
|
|
private List<ReferencePersist> references;
|
|
|
|
public static final String _references = "references";
|
2024-04-19 10:10:51 +02:00
|
|
|
private ReferencePersist reference;
|
|
|
|
public static final String _reference = "reference";
|
2023-11-03 15:02:47 +01:00
|
|
|
|
2024-01-12 14:00:34 +01:00
|
|
|
public UUID getFieldId() {
|
2023-11-03 15:02:47 +01:00
|
|
|
return fieldId;
|
|
|
|
}
|
|
|
|
|
2024-01-12 14:00:34 +01:00
|
|
|
public void setFieldId(UUID fieldId) {
|
2023-11-03 15:02:47 +01:00
|
|
|
this.fieldId = fieldId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getFieldValue() {
|
|
|
|
return fieldValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setFieldValue(String fieldValue) {
|
|
|
|
this.fieldValue = fieldValue;
|
|
|
|
}
|
|
|
|
|
2024-01-12 14:00:34 +01:00
|
|
|
public List<ReferencePersist> getReferences() {
|
|
|
|
return references;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setReferences(List<ReferencePersist> references) {
|
|
|
|
this.references = references;
|
|
|
|
}
|
|
|
|
|
2024-04-19 10:10:51 +02:00
|
|
|
public ReferencePersist getReference() {
|
|
|
|
return reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setReference(ReferencePersist reference) {
|
|
|
|
this.reference = reference;
|
|
|
|
}
|
|
|
|
|
2024-01-02 16:39:50 +01:00
|
|
|
@Component(DmpBlueprintValuePersistValidator.ValidatorName)
|
|
|
|
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
|
|
|
public static class DmpBlueprintValuePersistValidator extends BaseValidator<DmpBlueprintValuePersist> {
|
|
|
|
|
2024-01-31 11:30:21 +01:00
|
|
|
private final ValidatorFactory validatorFactory;
|
2024-04-08 12:47:00 +02:00
|
|
|
private final QueryFactory queryFactory;
|
2024-01-12 14:00:34 +01:00
|
|
|
private final MessageSource messageSource;
|
|
|
|
|
2024-01-02 16:39:50 +01:00
|
|
|
public static final String ValidatorName = "DmpBlueprintValuePersistValidator";
|
|
|
|
|
2024-04-08 12:47:00 +02:00
|
|
|
private DefinitionEntity definition;
|
|
|
|
private FieldEntity fieldEntity;
|
|
|
|
|
|
|
|
protected DmpBlueprintValuePersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, ValidatorFactory validatorFactory, QueryFactory queryFactory, MessageSource messageSource) {
|
2024-01-02 16:39:50 +01:00
|
|
|
super(conventionService, errors);
|
2024-01-31 11:30:21 +01:00
|
|
|
this.validatorFactory = validatorFactory;
|
2024-04-08 12:47:00 +02:00
|
|
|
this.queryFactory = queryFactory;
|
|
|
|
this.messageSource = messageSource;
|
2024-01-02 16:39:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected Class<DmpBlueprintValuePersist> modelClass() {
|
|
|
|
return DmpBlueprintValuePersist.class;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected List<Specification> specifications(DmpBlueprintValuePersist item) {
|
2024-04-08 12:47:00 +02:00
|
|
|
fieldEntity = definition != null && this.isValidGuid(item.getFieldId())? definition.getFieldById(item.getFieldId()).stream().findFirst().orElse(null) : null;
|
|
|
|
boolean required = fieldEntity != null && fieldEntity.isRequired();
|
|
|
|
|
2024-01-12 14:00:34 +01:00
|
|
|
return Arrays.asList(
|
2024-04-08 12:47:00 +02:00
|
|
|
this.spec()
|
|
|
|
.must(() -> this.isValidGuid(item.getFieldId()))
|
|
|
|
.failOn(DmpBlueprintValuePersist._fieldId).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpBlueprintValuePersist._fieldId}, LocaleContextHolder.getLocale())),
|
|
|
|
this.spec()
|
|
|
|
.iff(() -> fieldEntity.getCategory().equals(DmpBlueprintFieldCategory.Extra) && this.isListNullOrEmpty(item.getReferences()) && required)
|
|
|
|
.must(() -> !this.isEmpty(item.getFieldValue()))
|
|
|
|
.failOn(DmpBlueprintValuePersist._fieldValue).failWith(messageSource.getMessage("Validation_Required", new Object[]{fieldEntity.getLabel()}, LocaleContextHolder.getLocale())),
|
|
|
|
this.spec()
|
2024-04-19 10:10:51 +02:00
|
|
|
.iff(() -> fieldEntity.getCategory().equals(DmpBlueprintFieldCategory.ReferenceType) && this.isEmpty(item.getFieldValue()) && ((ReferenceTypeFieldEntity)fieldEntity).getMultipleSelect() && required)
|
2024-04-08 12:47:00 +02:00
|
|
|
.must(() -> !this.isListNullOrEmpty(item.getReferences()))
|
|
|
|
// TODO: Cast Exception
|
2024-04-09 10:48:20 +02:00
|
|
|
// .failOn(DmpBlueprintValuePersist._references).failWith(messageSource.getMessage("Validation_Required", new Object[]{!this.isEmpty(fieldEntity.getLabel()) ? fieldEntity.getLabel() : this.getReferenceTypeName(fieldEntity)}, LocaleContextHolder.getLocale())),
|
2024-04-08 12:47:00 +02:00
|
|
|
.failOn(DmpBlueprintValuePersist._references).failWith(messageSource.getMessage("Validation_Required", new Object[]{!this.isEmpty(fieldEntity.getLabel()) ? fieldEntity.getLabel() : DmpBlueprintValuePersist._references}, LocaleContextHolder.getLocale())),
|
2024-04-19 10:10:51 +02:00
|
|
|
this.spec()
|
|
|
|
.iff(() -> fieldEntity.getCategory().equals(DmpBlueprintFieldCategory.ReferenceType) && this.isEmpty(item.getFieldValue()) && !((ReferenceTypeFieldEntity)fieldEntity).getMultipleSelect() && required)
|
|
|
|
.must(() -> !this.isNull(item.getReference()))
|
|
|
|
// TODO: Cast Exception
|
|
|
|
// .failOn(DmpBlueprintValuePersist._references).failWith(messageSource.getMessage("Validation_Required", new Object[]{!this.isEmpty(fieldEntity.getLabel()) ? fieldEntity.getLabel() : this.getReferenceTypeName(fieldEntity)}, LocaleContextHolder.getLocale())),
|
|
|
|
.failOn(DmpBlueprintValuePersist._reference).failWith(messageSource.getMessage("Validation_Required", new Object[]{!this.isEmpty(fieldEntity.getLabel()) ? fieldEntity.getLabel() : DmpBlueprintValuePersist._reference}, LocaleContextHolder.getLocale())),
|
2024-04-08 12:47:00 +02:00
|
|
|
this.navSpec()
|
2024-01-31 16:07:16 +01:00
|
|
|
.iff(() -> !this.isListNullOrEmpty(item.getReferences()))
|
|
|
|
.on(DmpBlueprintValuePersist._references)
|
|
|
|
.over(item.getReferences())
|
2024-04-19 10:10:51 +02:00
|
|
|
.using((itm) -> this.validatorFactory.validator(ReferencePersist.ReferenceWithoutTypePersistValidator.class)),
|
|
|
|
this.refSpec()
|
|
|
|
.iff(() -> !this.isNull(item.getReference()))
|
|
|
|
.on(DmpBlueprintValuePersist._reference)
|
|
|
|
.over(item.getReferences())
|
|
|
|
.using(() -> this.validatorFactory.validator(ReferencePersist.ReferenceWithoutTypePersistValidator.class))
|
2024-01-12 14:00:34 +01:00
|
|
|
);
|
2024-01-02 16:39:50 +01:00
|
|
|
}
|
2024-04-08 12:47:00 +02:00
|
|
|
|
|
|
|
public DmpBlueprintValuePersistValidator withDefinition(DefinitionEntity definition) {
|
|
|
|
this.definition = definition;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2024-04-09 10:48:20 +02:00
|
|
|
private String getReferenceTypeName(FieldEntity fieldEntity){
|
|
|
|
if (fieldEntity instanceof ReferenceTypeFieldEntity) {
|
|
|
|
return this.queryFactory.query(ReferenceTypeQuery.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).ids(((ReferenceTypeFieldEntity)fieldEntity).getReferenceTypeId()).firstAs(new BaseFieldSet().ensure(ReferenceType._name)).getName();
|
|
|
|
}
|
|
|
|
return "";
|
2024-04-08 12:47:00 +02:00
|
|
|
}
|
2024-01-02 16:39:50 +01:00
|
|
|
}
|
|
|
|
|
2023-11-03 15:02:47 +01:00
|
|
|
}
|