add validate description

This commit is contained in:
amentis 2024-03-19 09:50:29 +02:00
parent 54e194e078
commit 9a99c0c300
7 changed files with 177 additions and 64 deletions

View File

@ -173,50 +173,48 @@ public class DescriptionPersist {
DescriptionTemplateEntity descriptionTemplate = this.isValidGuid(item.getDescriptionTemplateId()) ? this.entityManager.find(DescriptionTemplateEntity.class, item.getDescriptionTemplateId()) : null;
DefinitionEntity definition = descriptionTemplate == null ? null : this.xmlHandlingService.fromXmlSafe(DefinitionEntity.class, descriptionTemplate.getDefinition());
return Arrays.asList(
// this.spec()
// .iff(() -> this.isValidGuid(item.getId()))
// .must(() -> this.isValidHash(item.getHash()))
// .failOn(DescriptionPersist._hash).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionPersist._hash}, LocaleContextHolder.getLocale())),
// this.spec()
// .iff(() -> !this.isValidGuid(item.getId()))
// .must(() -> !this.isValidHash(item.getHash()))
// .failOn(DescriptionPersist._hash).failWith(messageSource.getMessage("Validation_OverPosting", new Object[]{}, LocaleContextHolder.getLocale())),
// this.spec()
// .must(() -> !this.isEmpty(item.getLabel()))
// .failOn(DescriptionPersist._label).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionPersist._label}, LocaleContextHolder.getLocale())),
// this.spec()
// .iff(() -> !this.isEmpty(item.getLabel()))
// .must(() -> this.lessEqualLength(item.getLabel(), DescriptionEntity._labelLength))
// .failOn(DescriptionPersist._label).failWith(messageSource.getMessage("Validation_MaxLength", new Object[]{DescriptionPersist._label}, LocaleContextHolder.getLocale())),
// this.spec()
// .must(() -> this.isValidGuid(item.getDmpId()))
// .failOn(DescriptionPersist._dmpId).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionPersist._dmpId}, LocaleContextHolder.getLocale())),
// this.spec()
// .iff(() -> item.getStatus() == DescriptionStatus.Finalized)
// .must(() -> this.isValidGuid(item.getDescriptionTemplateId()))
// .failOn(DescriptionPersist._descriptionTemplateId).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionPersist._descriptionTemplateId}, LocaleContextHolder.getLocale())),
// this.spec()
// .iff(() -> item.getStatus() == DescriptionStatus.Finalized)
// .must(() -> this.isValidGuid(item.getDmpDescriptionTemplateId()))
// .failOn(DescriptionPersist._dmpDescriptionTemplateId).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionPersist._dmpDescriptionTemplateId}, LocaleContextHolder.getLocale())),
// this.spec()
// .must(() -> !this.isNull(item.getStatus()))
// .failOn(DescriptionPersist._status).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionPersist._status}, LocaleContextHolder.getLocale())),
// this.spec()
// .iff(() -> item.getStatus() == DescriptionStatus.Finalized)
// .must(() -> !this.isListNullOrEmpty(item.getTags()))
// .failOn(DescriptionPersist._tags).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionPersist._tags}, LocaleContextHolder.getLocale())),
//
// this.spec()
// .iff(() -> item.getStatus() == DescriptionStatus.Finalized)
// .must(() -> !this.isNull(item.getProperties()))
// .failOn(DescriptionPersist._properties).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionPersist._properties}, LocaleContextHolder.getLocale())),
// this.refSpec()
// .iff(() -> !this.isNull(item.getProperties()))
// .on(DescriptionPersist._properties)
// .over(item.getProperties())
// .using(() -> this.validatorFactory.validator(PropertyDefinitionPersist.PropertyDefinitionPersistValidator.class).setStatus(item.getStatus()).withDefinition(definition))
);
this.spec()
.iff(() -> this.isValidGuid(item.getId()))
.must(() -> this.isValidHash(item.getHash()))
.failOn(DescriptionPersist._hash).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionPersist._hash}, LocaleContextHolder.getLocale())),
this.spec()
.iff(() -> !this.isValidGuid(item.getId()))
.must(() -> !this.isValidHash(item.getHash()))
.failOn(DescriptionPersist._hash).failWith(messageSource.getMessage("Validation_OverPosting", new Object[]{}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isEmpty(item.getLabel()))
.failOn(DescriptionPersist._label).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionPersist._label}, LocaleContextHolder.getLocale())),
this.spec()
.iff(() -> !this.isEmpty(item.getLabel()))
.must(() -> this.lessEqualLength(item.getLabel(), DescriptionEntity._labelLength))
.failOn(DescriptionPersist._label).failWith(messageSource.getMessage("Validation_MaxLength", new Object[]{DescriptionPersist._label}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> this.isValidGuid(item.getDmpId()))
.failOn(DescriptionPersist._dmpId).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionPersist._dmpId}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> this.isValidGuid(item.getDescriptionTemplateId()))
.failOn(DescriptionPersist._descriptionTemplateId).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionPersist._descriptionTemplateId}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> this.isValidGuid(item.getDmpDescriptionTemplateId()))
.failOn(DescriptionPersist._dmpDescriptionTemplateId).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionPersist._dmpDescriptionTemplateId}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isNull(item.getStatus()))
.failOn(DescriptionPersist._status).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionPersist._status}, LocaleContextHolder.getLocale())),
this.spec()
.iff(() -> item.getStatus() == DescriptionStatus.Finalized)
.must(() -> !this.isNull(item.getProperties()))
.failOn(DescriptionPersist._properties).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionPersist._properties}, LocaleContextHolder.getLocale())),
this.refSpec()
.iff(() -> !this.isNull(item.getProperties()))
.on(DescriptionPersist._properties)
.over(item.getProperties())
.using(() -> this.validatorFactory.validator(PropertyDefinitionPersist.PropertyDefinitionPersistValidator.class).setStatus(item.getStatus()).withDefinition(definition)),
this.navSpec()
.iff(() -> !this.isNull(item.getTags()))
.on(DescriptionPersist._tags)
.over(item.getTags())
.using((itm) -> this.validatorFactory.validator(TagPersist.TagPersistValidator.class))
);
}
}

View File

@ -1,6 +1,8 @@
package eu.eudat.model.persist.descriptionproperties;
import eu.eudat.commons.enums.DescriptionStatus;
import eu.eudat.commons.enums.FieldType;
import eu.eudat.commons.enums.FieldValidationType;
import eu.eudat.commons.types.descriptiontemplate.FieldEntity;
import eu.eudat.commons.validation.BaseValidator;
import eu.eudat.model.persist.ReferencePersist;
@ -81,6 +83,7 @@ public class FieldPersist {
private final MessageSource messageSource;
private FieldEntity fieldEntity;
private DescriptionStatus status;
protected PersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, ValidatorFactory validatorFactory, MessageSource messageSource) {
super(conventionService, errors);
@ -96,29 +99,30 @@ public class FieldPersist {
@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(
boolean required = this.fieldEntity != null && this.fieldEntity.getValidations() != null ? this.fieldEntity.getValidations().contains(FieldValidationType.Required) : false;
return Arrays.asList(
this.spec()
.iff(()-> FieldType.isTextType(fieldType))
.iff(()-> FieldType.isTextType(fieldType) && DescriptionStatus.Finalized.equals(this.status) && required)
.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))
.iff(()-> FieldType.isDateType(fieldType) && DescriptionStatus.Finalized.equals(this.status) && required)
.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))
.iff(()-> FieldType.isExternalIdentifierType(fieldType) && DescriptionStatus.Finalized.equals(this.status) && required)
.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))
.iff(()-> FieldType.isTextListType(fieldType) && DescriptionStatus.Finalized.equals(this.status) && required)
.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))
.iff(()-> FieldType.isReferenceType(fieldType) && DescriptionStatus.Finalized.equals(this.status) && required)
.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))
.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()
@ -146,6 +150,11 @@ public class FieldPersist {
this.fieldEntity = fieldEntity;
return this;
}
public PersistValidator setStatus(DescriptionStatus status) {
this.status = status;
return this;
}
}
}

View File

@ -1,5 +1,6 @@
package eu.eudat.model.persist.descriptionproperties;
import eu.eudat.commons.enums.DescriptionStatus;
import eu.eudat.commons.types.descriptiontemplate.FieldEntity;
import eu.eudat.commons.types.descriptiontemplate.FieldSetEntity;
import eu.eudat.commons.validation.BaseValidator;
@ -59,6 +60,7 @@ public class PropertyDefinitionFieldSetItemPersist {
private final MessageSource messageSource;
private FieldSetEntity fieldSetEntity;
private DescriptionStatus status;
protected PersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, ValidatorFactory validatorFactory, MessageSource messageSource) {
super(conventionService, errors);
this.validatorFactory = validatorFactory;
@ -84,7 +86,7 @@ public class PropertyDefinitionFieldSetItemPersist {
.using((itm) ->
{
FieldEntity fieldEntity = fieldSetEntity != null ? fieldSetEntity.getFieldById((String)itm.getKey()).stream().findFirst().orElse(null) : null;
return this.validatorFactory.validator(FieldPersist.PersistValidator.class).withFieldEntity(fieldEntity);
return this.validatorFactory.validator(FieldPersist.PersistValidator.class).withFieldEntity(fieldEntity).setStatus(this.status);
})
);
@ -94,6 +96,11 @@ public class PropertyDefinitionFieldSetItemPersist {
this.fieldSetEntity = fieldSetEntity;
return this;
}
public PersistValidator setStatus(DescriptionStatus status) {
this.status = status;
return this;
}
}
}

View File

@ -1,5 +1,6 @@
package eu.eudat.model.persist.descriptionproperties;
import eu.eudat.commons.enums.DescriptionStatus;
import eu.eudat.commons.types.descriptiontemplate.FieldSetEntity;
import eu.eudat.commons.validation.BaseValidator;
import eu.eudat.convention.ConventionService;
@ -7,6 +8,7 @@ import eu.eudat.errorcode.ErrorThesaurusProperties;
import gr.cite.tools.validation.ValidatorFactory;
import gr.cite.tools.validation.specification.Specification;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import java.util.Arrays;
@ -30,10 +32,13 @@ public class PropertyDefinitionFieldSetPersist {
public static final String ValidatorName = "Description.PropertyDefinitionFieldSetPersistValidator";
private final ValidatorFactory validatorFactory;
private final MessageSource messageSource;
private FieldSetEntity fieldSetEntity;
protected PersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, ValidatorFactory validatorFactory, MessageSource messageSource) {
private DescriptionStatus status;
protected PersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, ValidatorFactory validatorFactory, MessageSource messageSource, MessageSource messageSource1) {
super(conventionService, errors);
this.validatorFactory = validatorFactory;
this.messageSource = messageSource1;
}
@Override
@ -43,12 +48,23 @@ public class PropertyDefinitionFieldSetPersist {
@Override
protected List<Specification> specifications(PropertyDefinitionFieldSetPersist item) {
Integer min = fieldSetEntity != null && fieldSetEntity.getMultiplicity() != null ? fieldSetEntity.getMultiplicity().getMin() : null;
Integer max = fieldSetEntity != null && fieldSetEntity.getMultiplicity() != null ? fieldSetEntity.getMultiplicity().getMax() : null;
return Arrays.asList(
this.navSpec()
.iff(() -> !this.isNull(item.getItems()))
.on(PropertyDefinitionFieldSetPersist._items)
.over(item.getItems())
.using((itm) -> this.validatorFactory.validator(PropertyDefinitionFieldSetItemPersist.PersistValidator.class).withFieldSetEntity(this.fieldSetEntity))
.using((itm) -> this.validatorFactory.validator(PropertyDefinitionFieldSetItemPersist.PersistValidator.class).withFieldSetEntity(this.fieldSetEntity).setStatus(this.status)),
this.spec()
.iff(() -> DescriptionStatus.Finalized.equals(this.status) && min != null)
.must(() -> !this.isNull(item.getItems()) && min >= item.getItems().size())
.failOn(PropertyDefinitionFieldSetPersist._items).failWith(messageSource.getMessage("Validation.LargerThenEqual", new Object[]{PropertyDefinitionFieldSetPersist._items, min}, LocaleContextHolder.getLocale())),
this.spec()
.iff(() -> DescriptionStatus.Finalized.equals(this.status) && max != null)
.must(() -> !this.isNull(item.getItems()) && max <= item.getItems().size())
.failOn(PropertyDefinitionFieldSetPersist._items).failWith(messageSource.getMessage("Validation.LessThenEqual", new Object[]{PropertyDefinitionFieldSetPersist._items, max}, LocaleContextHolder.getLocale()))
);
}
@ -56,6 +72,12 @@ public class PropertyDefinitionFieldSetPersist {
this.fieldSetEntity = fieldSetEntity;
return this;
}
public PropertyDefinitionFieldSetPersist.PersistValidator setStatus(DescriptionStatus status) {
this.status = status;
return this;
}
}
}

View File

@ -1,7 +1,9 @@
package eu.eudat.model.persist.descriptionproperties;
import eu.eudat.commons.enums.DescriptionStatus;
import eu.eudat.commons.enums.FieldValidationType;
import eu.eudat.commons.types.descriptiontemplate.DefinitionEntity;
import eu.eudat.commons.types.descriptiontemplate.FieldEntity;
import eu.eudat.commons.types.descriptiontemplate.FieldSetEntity;
import eu.eudat.commons.validation.BaseValidator;
import eu.eudat.convention.ConventionService;
@ -15,9 +17,8 @@ import org.springframework.context.annotation.Scope;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
public class PropertyDefinitionPersist {
@ -64,7 +65,12 @@ public class PropertyDefinitionPersist {
.iff(() -> this.status == DescriptionStatus.Finalized)
.must(() -> !this.isNull(item.getFieldSets()) && !item.getFieldSets().isEmpty())
.failOn(PropertyDefinitionPersist._fieldSets).failWith(messageSource.getMessage("Validation_Required", new Object[]{PropertyDefinitionPersist._fieldSets}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> this.isListNullOrEmpty(this.getMissingFieldSetEntity(item)))
.failOn(PropertyDefinitionPersist._fieldSets).failWith(messageSource.getMessage("Validation.MissingFields", new Object[]{this.serializeMissingFieldSets(this.getMissingFieldSetEntity(item))}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> this.isListNullOrEmpty(this.getMissingFieldsEntity(item)))
.failOn(PropertyDefinitionPersist._fieldSets).failWith(messageSource.getMessage("Validation.MissingFields", new Object[]{this.serializeMissingFields(this.getMissingFieldsEntity(item))}, LocaleContextHolder.getLocale())),
this.mapSpec()
.iff(() -> !this.isNull(item.getFieldSets()))
.on(PropertyDefinitionPersist._fieldSets)
@ -72,7 +78,7 @@ public class PropertyDefinitionPersist {
.mapKey((k) -> ((String)k))
.using((itm) -> {
FieldSetEntity fieldSetEntity = definition != null ? definition.getFieldSetById((String)itm.getKey()).stream().findFirst().orElse(null) : null;
return this.validatorFactory.validator(PropertyDefinitionFieldSetPersist.PersistValidator.class).withFieldSetEntity(fieldSetEntity);
return this.validatorFactory.validator(PropertyDefinitionFieldSetPersist.PersistValidator.class).withFieldSetEntity(fieldSetEntity).setStatus(this.status);
})
);
}
@ -87,6 +93,73 @@ public class PropertyDefinitionPersist {
this.definition = definition;
return this;
}
private List<FieldSetEntity> getMissingFieldSetEntity(PropertyDefinitionPersist item){
List<FieldSetEntity> missingMultipleFieldSets = new ArrayList<>();
if (definition == null || definition.getAllFieldSets() == null) return missingMultipleFieldSets;
for (FieldSetEntity fieldSet: definition.getAllFieldSets()) {
boolean requiredAtLeastOneFieldSet = fieldSet.getMultiplicity() != null && fieldSet.getMultiplicity().getMin() > 0;
if (requiredAtLeastOneFieldSet) {
if (item == null || item.getFieldSets() == null) missingMultipleFieldSets.add(fieldSet);
PropertyDefinitionFieldSetPersist fieldSetPersist = item.getFieldSets().getOrDefault(fieldSet.getId(), null);
if (fieldSetPersist == null) missingMultipleFieldSets.add(fieldSet);
}
}
return missingMultipleFieldSets;
}
private List<FieldEntity> getMissingFieldsEntity(PropertyDefinitionPersist item){
List<FieldEntity> missingFields = new ArrayList<>();
if (definition == null || definition.getAllFieldSets() == null) return missingFields;
for (FieldSetEntity fieldSet: definition.getAllFieldSets()) {
if (fieldSet.getFields() == null) continue;
for(FieldEntity field : fieldSet.getFields()){
if (field.getValidations() == null) continue;
if (field.getValidations().contains(FieldValidationType.Required)){
if (item == null || item.getFieldSets() == null) {
missingFields.add(field);
continue;
}
PropertyDefinitionFieldSetPersist propertyDefinitionFieldSetPersist = item.getFieldSets().getOrDefault(fieldSet.getId(), null);
if (propertyDefinitionFieldSetPersist == null || propertyDefinitionFieldSetPersist.getItems() == null) {
missingFields.add(field);
continue;
}
for (PropertyDefinitionFieldSetItemPersist propertyDefinitionFieldSetItemPersist: propertyDefinitionFieldSetPersist.getItems()) {
if (propertyDefinitionFieldSetItemPersist.getFields() == null){
missingFields.add(field);
continue;
}
FieldPersist fieldPersist = propertyDefinitionFieldSetItemPersist.getFields().getOrDefault(field.getId(), null);
if (fieldPersist == null){
missingFields.add(field);
}
}
}
}
}
return missingFields;
}
private String serializeMissingFieldSets (List<FieldSetEntity> missingFieldSets){
if (missingFieldSets == null) return "";
return missingFieldSets.stream().map(x -> x.getId()).collect(Collectors.joining(", "));
}
private String serializeMissingFields (List<FieldEntity> missingFields){
if (missingFields == null) return "";
return missingFields.stream().map(x -> x.getId()).collect(Collectors.joining(", "));
}
}
}

View File

@ -50,6 +50,7 @@ import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.LoggerService;
import gr.cite.tools.logging.MapLogEntry;
import gr.cite.tools.validation.ValidationResult;
import gr.cite.tools.validation.ValidatorFactory;
import jakarta.persistence.EntityManager;
import org.apache.commons.io.FilenameUtils;
@ -379,10 +380,10 @@ public class DescriptionServiceImpl implements DescriptionService {
for (DescriptionEntity description: descriptions) {
DescriptionValidationResult descriptionValidationResult = new DescriptionValidationResult(description.getId(), DescriptionValidationOutput.Invalid);
//TODO description template
if (!this.conventionService.isNullOrEmpty(description.getLabel()) && description.getDmpId() != null && description.getDescriptionTemplateId() != null && description.getStatus() != null && !this.conventionService.isNullOrEmpty(description.getProperties())){
descriptionValidationResult.setResult(DescriptionValidationOutput.Valid);
}
// // TODO Persist
DescriptionPersist.DescriptionPersistValidator validator = this.validatorFactory.validator(DescriptionPersist.DescriptionPersistValidator.class);
validator.validate(new DescriptionPersist());
if (validator.result().isValid()) descriptionValidationResult.setResult(DescriptionValidationOutput.Valid);;
descriptionValidationResults.add(descriptionValidationResult);
}

View File

@ -21,4 +21,7 @@ Validation_OverPosting=Too much info
Validation_MaxLength={0} too long
Validation_UnexpectedValue=Unexpected value in field {0}
Validation_Unique= {0} must be unique
Validation.LowerThanMin= value must be equal or larger than {0}
Validation.LowerThanMin= value must be equal or larger than {0}
Validation.LessThenEqual= value {0} must be equal or less than {1}
Validation.LargerThenEqual= value {0} must be equal or larger than {1}
Validation.MissingFields= missing fields: {0}