Implementing validators for Dmp persist models (completed)

This commit is contained in:
Thomas Georgios Giannos 2024-01-02 17:39:50 +02:00
parent 3f554554fd
commit 8d5b3d0426
8 changed files with 335 additions and 38 deletions

View File

@ -1,22 +1,29 @@
package eu.eudat.model.persist;
import eu.eudat.commons.validation.old.FieldNotNullIfOtherSet;
import eu.eudat.commons.validation.old.ValidId;
import jakarta.validation.constraints.NotNull;
import eu.eudat.commons.validation.BaseValidator;
import eu.eudat.commons.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.util.Arrays;
import java.util.List;
import java.util.UUID;
@FieldNotNullIfOtherSet(message = "{validation.hashempty}")
public class DmpDescriptionTemplatePersist {
@ValidId(message = "{validation.invalidid}")
@NotNull(message = "{validation.empty}")
private UUID descriptionTemplateGroupId;
@ValidId(message = "{validation.invalidid}")
@NotNull(message = "{validation.empty}")
public static final String _descriptionTemplateGroupId = "descriptionTemplateGroupId";
private UUID sectionId;
public static final String _sectionId = "sectionId";
public UUID getDescriptionTemplateGroupId() {
return descriptionTemplateGroupId;
}
@ -33,4 +40,35 @@ public class DmpDescriptionTemplatePersist {
this.sectionId = sectionId;
}
@Component(DmpDescriptionTemplatePersistValidator.ValidatorName)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public static class DmpDescriptionTemplatePersistValidator extends BaseValidator<DmpDescriptionTemplatePersist> {
public static final String ValidatorName = "DmpDescriptionTemplatePersistValidator";
private final MessageSource messageSource;
protected DmpDescriptionTemplatePersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource) {
super(conventionService, errors);
this.messageSource = messageSource;
}
@Override
protected Class<DmpDescriptionTemplatePersist> modelClass() {
return DmpDescriptionTemplatePersist.class;
}
@Override
protected List<Specification> specifications(DmpDescriptionTemplatePersist item) {
return Arrays.asList(
this.spec()
.must(() -> this.isValidGuid(item.getDescriptionTemplateGroupId()))
.failOn(DmpDescriptionTemplatePersist._descriptionTemplateGroupId).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpDescriptionTemplatePersist._descriptionTemplateGroupId}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> this.isValidGuid(item.getSectionId()))
.failOn(DmpDescriptionTemplatePersist._sectionId).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpDescriptionTemplatePersist._sectionId}, LocaleContextHolder.getLocale()))
);
}
}
}

View File

@ -2,57 +2,70 @@ package eu.eudat.model.persist;
import eu.eudat.commons.enums.DmpAccessType;
import eu.eudat.commons.enums.DmpStatus;
import eu.eudat.commons.validation.BaseValidator;
import eu.eudat.commons.validation.ValidatorFactory;
import eu.eudat.commons.validation.old.FieldNotNullIfOtherSet;
import eu.eudat.commons.validation.old.ValidEnum;
import eu.eudat.commons.validation.old.ValidId;
import eu.eudat.commons.validation.specification.Specification;
import eu.eudat.convention.ConventionService;
import eu.eudat.data.DescriptionEntity;
import eu.eudat.data.DmpEntity;
import eu.eudat.errorcode.ErrorThesaurusProperties;
import eu.eudat.model.persist.descriptionproperties.PropertyDefinitionPersist;
import eu.eudat.model.persist.dmpproperties.DmpPropertiesPersist;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
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.util.Arrays;
import java.util.List;
import java.util.UUID;
@FieldNotNullIfOtherSet(message = "{validation.hashempty}")
public class DmpPersist {
@ValidId(message = "{validation.invalidid}")
private UUID id;
@NotNull(message = "{validation.empty}")
@NotEmpty(message = "{validation.empty}")
@Size(max = DmpEntity._labelLength, message = "{validation.largerthanmax}")
private String label;
@ValidEnum(message = "{validation.empty}")
public static final String _label = "label";
private DmpStatus status;
@NotNull(message = "{validation.empty}")
public static final String _status = "status";
private DmpPropertiesPersist properties;
public static final String _properties = "properties";
private String description;
private String language;
@NotNull(message = "{validation.empty}")
@ValidId(message = "{validation.invalidid}")
private UUID blueprint;
public static final String _blueprint = "blueprint";
private DmpAccessType accessType;
@NotNull(message = "{validation.empty}")
@Valid
private List<DmpReferencePersist> references;
@NotNull(message = "{validation.empty}")
@Valid
public static final String _references = "references";
private List<DmpDescriptionTemplatePersist> descriptionTemplates;
public static final String _descriptionTemplates = "descriptionTemplates";
private String hash;
public static final String _hash = "hash";
public UUID getId() {
return id;
}
@ -141,4 +154,78 @@ public class DmpPersist {
this.hash = hash;
}
@Component(DmpPersistValidator.ValidatorName)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public static class DmpPersistValidator extends BaseValidator<DmpPersist> {
public static final String ValidatorName = "DmpPersistValidator";
private final MessageSource messageSource;
private final ValidatorFactory validatorFactory;
protected DmpPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) {
super(conventionService, errors);
this.messageSource = messageSource;
this.validatorFactory = validatorFactory;
}
@Override
protected Class<DmpPersist> modelClass() {
return DmpPersist.class;
}
@Override
protected List<Specification> specifications(DmpPersist item) {
return Arrays.asList(
this.spec()
.iff(() -> this.isValidGuid(item.getId()))
.must(() -> this.isValidHash(item.getHash()))
.failOn(DmpPersist._hash).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpPersist._hash}, LocaleContextHolder.getLocale())),
this.spec()
.iff(() -> !this.isValidGuid(item.getId()))
.must(() -> !this.isValidHash(item.getHash()))
.failOn(DmpPersist._hash).failWith(messageSource.getMessage("Validation_OverPosting", new Object[]{}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isEmpty(item.getLabel()))
.failOn(DmpPersist._label).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpPersist._label}, LocaleContextHolder.getLocale())),
this.spec()
.iff(() -> !this.isEmpty(item.getLabel()))
.must(() -> this.lessEqualLength(item.getLabel(), DmpEntity._labelLength))
.failOn(DmpPersist._label).failWith(messageSource.getMessage("Validation_MaxLength", new Object[]{DmpPersist._label}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isNull(item.getStatus()))
.failOn(DmpPersist._status).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpPersist._status}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> this.isValidGuid(item.getBlueprint()))
.failOn(DmpPersist._blueprint).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpPersist._blueprint}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isNull(item.getProperties()))
.failOn(DmpPersist._properties).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpPersist._properties}, LocaleContextHolder.getLocale())),
this.refSpec()
.iff(() -> !this.isNull(item.getProperties()))
.on(DmpPersist._properties)
.over(item.getProperties())
.using(() -> this.validatorFactory.validator(DmpPropertiesPersist.DmpPropertiesPersistValidator.class)),
this.spec()
.must(() -> !this.isNull(item.getReferences()))
.failOn(DmpPersist._references).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpPersist._references}, LocaleContextHolder.getLocale())),
this.navSpec()
.iff(() -> !this.isNull(item.getReferences()))
.on(DmpPersist._references)
.over(item.getReferences())
.using(() -> this.validatorFactory.validator(DmpReferencePersist.DmpReferencePersistValidator.class)),
this.spec()
.must(() -> !this.isNull(item.getDescriptionTemplates()))
.failOn(DmpPersist._descriptionTemplates).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpPersist._descriptionTemplates}, LocaleContextHolder.getLocale())),
this.navSpec()
.iff(() -> !this.isNull(item.getDescriptionTemplates()))
.on(DmpPersist._descriptionTemplates)
.over(item.getDescriptionTemplates())
.using(() -> this.validatorFactory.validator(DmpDescriptionTemplatePersist.DmpDescriptionTemplatePersistValidator.class))
);
}
}
}

View File

@ -1,28 +1,36 @@
package eu.eudat.model.persist;
import eu.eudat.commons.validation.old.FieldNotNullIfOtherSet;
import eu.eudat.commons.validation.old.ValidId;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import eu.eudat.commons.validation.BaseValidator;
import eu.eudat.commons.validation.ValidatorFactory;
import eu.eudat.commons.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.util.Arrays;
import java.util.List;
import java.util.UUID;
@FieldNotNullIfOtherSet(message = "{validation.hashempty}")
public class DmpReferencePersist {
@ValidId(message = "{validation.invalidid}")
private UUID id;
@NotNull(message = "{validation.empty}")
@NotEmpty(message = "{validation.empty}")
private ReferencePersist reference;
@NotNull(message = "{validation.empty}")
@NotEmpty(message = "{validation.empty}")
public static final String _reference = "reference";
private String data;
public static final String _data = "data";
private String hash;
public static final String _hash = "hash";
public UUID getId() {
return id;
}
@ -55,4 +63,52 @@ public class DmpReferencePersist {
this.hash = hash;
}
@Component(DmpReferencePersistValidator.ValidatorName)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public static class DmpReferencePersistValidator extends BaseValidator<DmpReferencePersist> {
public static final String ValidatorName = "DmpReferencePersistValidator";
private final MessageSource messageSource;
private final ValidatorFactory validatorFactory;
protected DmpReferencePersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, MessageSource messageSource, ValidatorFactory validatorFactory) {
super(conventionService, errors);
this.messageSource = messageSource;
this.validatorFactory = validatorFactory;
}
@Override
protected Class<DmpReferencePersist> modelClass() {
return DmpReferencePersist.class;
}
@Override
protected List<Specification> specifications(DmpReferencePersist item) {
return Arrays.asList(
this.spec()
.iff(() -> this.isValidGuid(item.getId()))
.must(() -> this.isValidHash(item.getHash()))
.failOn(DmpReferencePersist._hash).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpReferencePersist._hash}, LocaleContextHolder.getLocale())),
this.spec()
.iff(() -> !this.isValidGuid(item.getId()))
.must(() -> !this.isValidHash(item.getHash()))
.failOn(DmpReferencePersist._hash).failWith(messageSource.getMessage("Validation_OverPosting", new Object[]{}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isNull(item.getData()))
.failOn(DmpReferencePersist._data).failWith(messageSource.getMessage("Validation_Required", new Object[]{DmpReferencePersist._data}, LocaleContextHolder.getLocale())),
this.spec()
.must(() -> !this.isNull(item.getReference()))
.failOn(DmpReferencePersist._reference).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionPersist._properties}, LocaleContextHolder.getLocale())),
this.refSpec()
.iff(() -> !this.isNull(item.getReference()))
.on(DmpReferencePersist._reference)
.over(item.getReference())
.using(() -> this.validatorFactory.validator(ReferencePersist.ReferencePersistValidator.class))
);
}
}
}

View File

@ -1,5 +1,16 @@
package eu.eudat.model.persist.dmpproperties;
import eu.eudat.commons.validation.BaseValidator;
import eu.eudat.commons.validation.specification.Specification;
import eu.eudat.convention.ConventionService;
import eu.eudat.errorcode.ErrorThesaurusProperties;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
public class DmpBlueprintValuePersist {
private String fieldId;
@ -32,4 +43,25 @@ public class DmpBlueprintValuePersist {
this.fieldValue = fieldValue;
}
@Component(DmpBlueprintValuePersistValidator.ValidatorName)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public static class DmpBlueprintValuePersistValidator extends BaseValidator<DmpBlueprintValuePersist> {
public static final String ValidatorName = "DmpBlueprintValuePersistValidator";
protected DmpBlueprintValuePersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors) {
super(conventionService, errors);
}
@Override
protected Class<DmpBlueprintValuePersist> modelClass() {
return DmpBlueprintValuePersist.class;
}
@Override
protected List<Specification> specifications(DmpBlueprintValuePersist item) {
return new ArrayList<>();
}
}
}

View File

@ -1,5 +1,16 @@
package eu.eudat.model.persist.dmpproperties;
import eu.eudat.commons.validation.BaseValidator;
import eu.eudat.commons.validation.specification.Specification;
import eu.eudat.convention.ConventionService;
import eu.eudat.errorcode.ErrorThesaurusProperties;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
public class DmpContactPersist {
String userId;
@ -42,4 +53,25 @@ public class DmpContactPersist {
this.email = email;
}
@Component(DmpContactPersistValidator.ValidatorName)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public static class DmpContactPersistValidator extends BaseValidator<DmpContactPersist> {
public static final String ValidatorName = "DmpContactPersistValidator";
protected DmpContactPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors) {
super(conventionService, errors);
}
@Override
protected Class<DmpContactPersist> modelClass() {
return DmpContactPersist.class;
}
@Override
protected List<Specification> specifications(DmpContactPersist item) {
return new ArrayList<>();
}
}
}

View File

@ -1,13 +1,27 @@
package eu.eudat.model.persist.dmpproperties;
import eu.eudat.commons.validation.BaseValidator;
import eu.eudat.commons.validation.ValidatorFactory;
import eu.eudat.commons.validation.specification.Specification;
import eu.eudat.convention.ConventionService;
import eu.eudat.errorcode.ErrorThesaurusProperties;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
public class DmpPropertiesPersist {
private List<DmpBlueprintValuePersist> dmpBlueprintValues;
public static final String _dmpBlueprintValues = "dmpBlueprintValues";
private List<DmpContactPersist> contacts;
public static final String _contacts = "contacts";
public List<DmpBlueprintValuePersist> getDmpBlueprintValues() {
return dmpBlueprintValues;
}
@ -24,4 +38,39 @@ public class DmpPropertiesPersist {
this.contacts = contacts;
}
@Component(DmpPropertiesPersistValidator.ValidatorName)
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public static class DmpPropertiesPersistValidator extends BaseValidator<DmpPropertiesPersist> {
public static final String ValidatorName = "DmpPropertiesPersistValidator";
private final ValidatorFactory validatorFactory;
protected DmpPropertiesPersistValidator(ConventionService conventionService, ErrorThesaurusProperties errors, ValidatorFactory validatorFactory) {
super(conventionService, errors);
this.validatorFactory = validatorFactory;
}
@Override
protected Class<DmpPropertiesPersist> modelClass() {
return DmpPropertiesPersist.class;
}
@Override
protected List<Specification> specifications(DmpPropertiesPersist item) {
return Arrays.asList(
this.refSpec()
.iff(() -> !this.isNull(item.getDmpBlueprintValues()))
.on(DmpPropertiesPersist._dmpBlueprintValues)
.over(item.getDmpBlueprintValues())
.using(() -> this.validatorFactory.validator(DmpBlueprintValuePersist.DmpBlueprintValuePersistValidator.class)),
this.refSpec()
.iff(() -> !this.isNull(item.getContacts()))
.on(DmpPropertiesPersist._contacts)
.over(item.getContacts())
.using(() -> this.validatorFactory.validator(DmpContactPersist.DmpContactPersistValidator.class))
);
}
}
}

View File

@ -3,13 +3,14 @@ package eu.eudat.controllers.v2;
import com.fasterxml.jackson.core.JsonProcessingException;
import eu.eudat.audit.AuditableAction;
import eu.eudat.authorization.AuthorizationFlags;
import eu.eudat.commons.validation.ValidationFilterAnnotation;
import eu.eudat.data.DmpBlueprintEntity;
import eu.eudat.model.DmpBlueprint;
import eu.eudat.model.builder.DmpBlueprintBuilder;
import eu.eudat.model.censorship.DmpBlueprintCensor;
import eu.eudat.model.persist.DmpBlueprintPersist;
import eu.eudat.model.result.QueryResult;
import eu.eudat.query.*;
import eu.eudat.query.DmpBlueprintQuery;
import eu.eudat.query.lookup.DmpBlueprintLookup;
import eu.eudat.service.dmpblueprint.DmpBlueprintService;
import gr.cite.tools.auditing.AuditService;
@ -114,6 +115,7 @@ public class DmpBlueprintController {
@PostMapping("persist")
@Transactional
@ValidationFilterAnnotation(validator = DmpBlueprintPersist.DmpBlueprintPersistValidator.ValidatorName, argumentName = "model")
public DmpBlueprint persist(@MyValidate @RequestBody DmpBlueprintPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JAXBException, ParserConfigurationException, JsonProcessingException, TransformerException {
logger.debug(new MapLogEntry("persisting" + DmpBlueprint.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet));
this.censorFactory.censor(DmpBlueprintCensor.class).censor(fieldSet, null);
@ -157,10 +159,10 @@ public class DmpBlueprintController {
}
@RequestMapping(method = RequestMethod.GET, value = {"/xml/export/{id}"}, produces = "application/xml")
public @ResponseBody ResponseEntity getXml(@PathVariable UUID id) throws JAXBException, ParserConfigurationException, IOException, TransformerException, InstantiationException, IllegalAccessException, SAXException, InvalidApplicationException {
public @ResponseBody ResponseEntity<byte[]> getXml(@PathVariable UUID id) throws JAXBException, ParserConfigurationException, IOException, TransformerException, InstantiationException, IllegalAccessException, SAXException, InvalidApplicationException {
logger.debug(new MapLogEntry("export" + DmpBlueprint.class.getSimpleName()).And("id", id));
ResponseEntity response = this.dmpBlueprintService.exportXml(id);
ResponseEntity<byte[]> response = this.dmpBlueprintService.exportXml(id);
this.auditService.track(AuditableAction.DmpBlueprint_GetXml, Map.ofEntries(
new AbstractMap.SimpleEntry<String, Object>("id", id)

View File

@ -120,6 +120,7 @@ public class DmpController {
@PostMapping("persist")
@Transactional
@ValidationFilterAnnotation(validator = DmpPersist.DmpPersistValidator.ValidatorName, argumentName = "model")
public Dmp Persist(@MyValidate @RequestBody DmpPersist model, FieldSet fieldSet) throws MyApplicationException, MyForbiddenException, MyNotFoundException, InvalidApplicationException, JsonProcessingException {
logger.debug(new MapLogEntry("persisting" + Dmp.class.getSimpleName()).And("model", model).And("fieldSet", fieldSet));