diff --git a/dmp-backend/core/src/main/java/eu/eudat/model/persist/DescriptionPersist.java b/dmp-backend/core/src/main/java/eu/eudat/model/persist/DescriptionPersist.java index 5fceccdf4..da053e457 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/model/persist/DescriptionPersist.java +++ b/dmp-backend/core/src/main/java/eu/eudat/model/persist/DescriptionPersist.java @@ -228,7 +228,7 @@ public class DescriptionPersist { .iff(() -> item.getStatus() == DescriptionStatus.Finalized) .on(DescriptionPersist._properties) .over(item.getProperties()) - .using(() -> this.validatorFactory.validator(PropertyDefinitionPersist.PropertyDefinitionPersistValidator.class).setStatus(item.getStatus()).withDefinition(definition)) + .using(() -> this.validatorFactory.validator(PropertyDefinitionPersist.PropertyDefinitionPersistValidator.class).setStatus(item.getStatus()).withDefinition(definition).withRules(definition)) // this.navSpec() // .iff(() -> !this.isNull(item.getTags())) // .on(DescriptionPersist._tags) diff --git a/dmp-backend/core/src/main/java/eu/eudat/model/persist/descriptionproperties/FieldPersist.java b/dmp-backend/core/src/main/java/eu/eudat/model/persist/descriptionproperties/FieldPersist.java index bf5506efa..ebcd1727b 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/model/persist/descriptionproperties/FieldPersist.java +++ b/dmp-backend/core/src/main/java/eu/eudat/model/persist/descriptionproperties/FieldPersist.java @@ -4,6 +4,7 @@ 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.types.descriptiontemplate.RuleEntity; import eu.eudat.commons.validation.BaseValidator; import eu.eudat.model.persist.ReferencePersist; import gr.cite.tools.validation.ValidatorFactory; @@ -95,6 +96,7 @@ public class FieldPersist { private final MessageSource messageSource; private FieldEntity fieldEntity; private DescriptionStatus status; + private List> rules; protected PersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, ValidatorFactory validatorFactory, MessageSource messageSource) { super(conventionService, errors); @@ -111,33 +113,34 @@ public class FieldPersist { protected List specifications(FieldPersist item) { FieldType fieldType = this.fieldEntity != null && this.fieldEntity.getData() != null ? this.fieldEntity.getData().getFieldType() : FieldType.FREE_TEXT; boolean required = this.fieldEntity != null && this.fieldEntity.getValidations() != null ? this.fieldEntity.getValidations().contains(FieldValidationType.Required) : false; + boolean isRuleTarget = this.checkIfFieldIsRuleTarget(); return Arrays.asList( this.spec() - .iff(()-> FieldType.isTextType(fieldType) && !fieldType.equals(FieldType.CHECK_BOX) && DescriptionStatus.Finalized.equals(this.status) && required) + .iff(()-> FieldType.isTextType(fieldType) && !fieldType.equals(FieldType.CHECK_BOX) && DescriptionStatus.Finalized.equals(this.status) && this.isListNullOrEmpty(fieldEntity.getVisibilityRules()) && !isRuleTarget && 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) && DescriptionStatus.Finalized.equals(this.status) && required) + .iff(()-> FieldType.isDateType(fieldType) && DescriptionStatus.Finalized.equals(this.status) && this.isListNullOrEmpty(fieldEntity.getVisibilityRules()) && !isRuleTarget && 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) && DescriptionStatus.Finalized.equals(this.status) && required) + .iff(()-> FieldType.isExternalIdentifierType(fieldType) && DescriptionStatus.Finalized.equals(this.status) && this.isListNullOrEmpty(fieldEntity.getVisibilityRules()) && !isRuleTarget && 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) && DescriptionStatus.Finalized.equals(this.status) && required && !fieldType.equals(FieldType.TAGS)) + .iff(()-> FieldType.isTextListType(fieldType) && DescriptionStatus.Finalized.equals(this.status) && this.isListNullOrEmpty(fieldEntity.getVisibilityRules()) && !isRuleTarget && required && !fieldType.equals(FieldType.TAGS)) .must(() -> !this.isListNullOrEmpty(item.getTextListValue()) || !this.isEmpty(item.getTextValue())) .failOn(FieldPersist._textListValue).failWith(messageSource.getMessage("Validation_Required", new Object[]{FieldPersist._textListValue}, LocaleContextHolder.getLocale())), this.spec() - .iff(()-> fieldType.equals(FieldType.TAGS) && DescriptionStatus.Finalized.equals(this.status) && required) + .iff(()-> fieldType.equals(FieldType.TAGS) && DescriptionStatus.Finalized.equals(this.status) && this.isListNullOrEmpty(fieldEntity.getVisibilityRules()) && !isRuleTarget && required) .must(() -> !this.isListNullOrEmpty(item.getTextListValue())) .failOn(FieldPersist._textListValue).failWith(messageSource.getMessage("Validation_Required", new Object[]{FieldPersist._textListValue}, LocaleContextHolder.getLocale())), this.spec() - .iff(()-> FieldType.isReferenceType(fieldType) && DescriptionStatus.Finalized.equals(this.status) && required) + .iff(()-> FieldType.isReferenceType(fieldType) && DescriptionStatus.Finalized.equals(this.status) && this.isListNullOrEmpty(fieldEntity.getVisibilityRules()) && !isRuleTarget && required) .must(() -> !this.isListNullOrEmpty(item.getReferences()) || !this.isNull(item.getReference())) .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)) && this.isListNullOrEmpty(fieldEntity.getVisibilityRules()) && !isRuleTarget) .must(() -> this.isBoolean(item.getTextValue())) .failOn(FieldPersist._textValue).failWith(messageSource.getMessage("Validation_UnexpectedValue", new Object[]{FieldPersist._textValue}, LocaleContextHolder.getLocale())), this.spec() @@ -167,10 +170,27 @@ public class FieldPersist { return this; } + public PersistValidator withRules(List> rules) { + this.rules = rules; + return this; + } + public PersistValidator setStatus(DescriptionStatus status) { this.status = status; return this; } + + private boolean checkIfFieldIsRuleTarget(){ + if (this.isListNullOrEmpty(rules)) return false; + for (List rulesBySection: rules) { + if (!this.isListNullOrEmpty(rulesBySection)){ + for (RuleEntity rule :rulesBySection) { + if (rule.getTarget().equals(this.fieldEntity.getId())) return true; + } + } + } + return false; + } } } diff --git a/dmp-backend/core/src/main/java/eu/eudat/model/persist/descriptionproperties/PropertyDefinitionFieldSetItemPersist.java b/dmp-backend/core/src/main/java/eu/eudat/model/persist/descriptionproperties/PropertyDefinitionFieldSetItemPersist.java index 804348fdb..823373e42 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/model/persist/descriptionproperties/PropertyDefinitionFieldSetItemPersist.java +++ b/dmp-backend/core/src/main/java/eu/eudat/model/persist/descriptionproperties/PropertyDefinitionFieldSetItemPersist.java @@ -3,6 +3,7 @@ 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.types.descriptiontemplate.RuleEntity; import eu.eudat.commons.validation.BaseValidator; import eu.eudat.convention.ConventionService; import eu.eudat.errorcode.ErrorThesaurusProperties; @@ -60,6 +61,7 @@ public class PropertyDefinitionFieldSetItemPersist { private final MessageSource messageSource; private FieldSetEntity fieldSetEntity; + private List> rules; private DescriptionStatus status; protected PersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, ValidatorFactory validatorFactory, MessageSource messageSource) { super(conventionService, errors); @@ -86,7 +88,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).setStatus(this.status); + return this.validatorFactory.validator(FieldPersist.PersistValidator.class).withFieldEntity(fieldEntity).withRules(rules).setStatus(this.status); }) ); @@ -97,6 +99,11 @@ public class PropertyDefinitionFieldSetItemPersist { return this; } + public PersistValidator withRules(List> rules) { + this.rules = rules; + return this; + } + public PersistValidator setStatus(DescriptionStatus status) { this.status = status; return this; diff --git a/dmp-backend/core/src/main/java/eu/eudat/model/persist/descriptionproperties/PropertyDefinitionFieldSetPersist.java b/dmp-backend/core/src/main/java/eu/eudat/model/persist/descriptionproperties/PropertyDefinitionFieldSetPersist.java index 73469b1f0..0ee158626 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/model/persist/descriptionproperties/PropertyDefinitionFieldSetPersist.java +++ b/dmp-backend/core/src/main/java/eu/eudat/model/persist/descriptionproperties/PropertyDefinitionFieldSetPersist.java @@ -2,6 +2,7 @@ package eu.eudat.model.persist.descriptionproperties; import eu.eudat.commons.enums.DescriptionStatus; import eu.eudat.commons.types.descriptiontemplate.FieldSetEntity; +import eu.eudat.commons.types.descriptiontemplate.RuleEntity; import eu.eudat.commons.validation.BaseValidator; import eu.eudat.convention.ConventionService; import eu.eudat.errorcode.ErrorThesaurusProperties; @@ -35,6 +36,7 @@ public class PropertyDefinitionFieldSetPersist { private final MessageSource messageSource; private FieldSetEntity fieldSetEntity; private DescriptionStatus status; + private List> rules; protected PersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, ValidatorFactory validatorFactory, MessageSource messageSource, MessageSource messageSource1) { super(conventionService, errors); this.validatorFactory = validatorFactory; @@ -50,7 +52,7 @@ public class PropertyDefinitionFieldSetPersist { protected List 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; - if(min == 0 && max == 0) max = null; + if(min != null && min == 0 && max != null && max == 0) max = null; Integer finalMax = max; return Arrays.asList( @@ -58,7 +60,7 @@ public class PropertyDefinitionFieldSetPersist { .iff(() -> !this.isNull(item.getItems())) .on(PropertyDefinitionFieldSetPersist._items) .over(item.getItems()) - .using((itm) -> this.validatorFactory.validator(PropertyDefinitionFieldSetItemPersist.PersistValidator.class).withFieldSetEntity(this.fieldSetEntity).setStatus(this.status)), + .using((itm) -> this.validatorFactory.validator(PropertyDefinitionFieldSetItemPersist.PersistValidator.class).withFieldSetEntity(this.fieldSetEntity).withRules(this.rules).setStatus(this.status)), this.spec() .iff(() -> DescriptionStatus.Finalized.equals(this.status) && min != null) .must(() -> !this.isListNullOrEmpty(item.getItems()) && min <= item.getItems().size()) @@ -75,6 +77,11 @@ public class PropertyDefinitionFieldSetPersist { return this; } + public PersistValidator withRules(List> rules) { + this.rules = rules; + return this; + } + public PropertyDefinitionFieldSetPersist.PersistValidator setStatus(DescriptionStatus status) { this.status = status; return this; diff --git a/dmp-backend/core/src/main/java/eu/eudat/model/persist/descriptionproperties/PropertyDefinitionPersist.java b/dmp-backend/core/src/main/java/eu/eudat/model/persist/descriptionproperties/PropertyDefinitionPersist.java index 0dc4fab0e..5e3537950 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/model/persist/descriptionproperties/PropertyDefinitionPersist.java +++ b/dmp-backend/core/src/main/java/eu/eudat/model/persist/descriptionproperties/PropertyDefinitionPersist.java @@ -5,6 +5,7 @@ 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.types.descriptiontemplate.RuleEntity; import eu.eudat.commons.validation.BaseValidator; import eu.eudat.convention.ConventionService; import eu.eudat.errorcode.ErrorThesaurusProperties; @@ -47,6 +48,8 @@ public class PropertyDefinitionPersist { private DescriptionStatus status; private DefinitionEntity definition; + private List> rules; + protected PropertyDefinitionPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) { super(conventionService, errors); this.messageSource = messageSource; @@ -78,7 +81,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).setStatus(this.status); + return this.validatorFactory.validator(PropertyDefinitionFieldSetPersist.PersistValidator.class).withFieldSetEntity(fieldSetEntity).withRules(rules).setStatus(this.status); }) ); } @@ -94,6 +97,12 @@ public class PropertyDefinitionPersist { return this; } + public PropertyDefinitionPersistValidator withRules(DefinitionEntity definition) { + List fields = definition.getAllField(); + this.rules = fields.stream().filter(x -> x.getVisibilityRules() != null).map(FieldEntity::getVisibilityRules).collect(Collectors.toList()); + return this; + } + private List getMissingFieldSetEntity(PropertyDefinitionPersist item){ List missingMultipleFieldSets = new ArrayList<>();