Remove not visible fields on persist

This commit is contained in:
Efstratios Giannopoulos 2024-05-17 12:46:20 +03:00
parent 125fd2c994
commit 8a0d02d02e
4 changed files with 53 additions and 46 deletions

View File

@ -1,12 +1,12 @@
package org.opencdmp.model.persist;
import org.opencdmp.commons.validation.BaseValidator;
import org.opencdmp.model.persist.descriptionreference.DescriptionReferenceDataPersist;
import org.opencdmp.model.persist.dmpreference.DmpReferenceDataPersist;
import gr.cite.tools.validation.ValidatorFactory;
import gr.cite.tools.validation.specification.Specification;
import org.opencdmp.commons.validation.BaseValidator;
import org.opencdmp.convention.ConventionService;
import org.opencdmp.errorcode.ErrorThesaurusProperties;
import org.opencdmp.model.persist.descriptionreference.DescriptionReferenceDataPersist;
import org.opencdmp.model.persist.dmpreference.DmpReferenceDataPersist;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Scope;
@ -15,12 +15,11 @@ import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
public class DescriptionReferencePersist {
private ReferencePersist reference = null;
private ReferencePersist reference;
public static final String _reference = "reference";
@ -30,7 +29,7 @@ public class DescriptionReferencePersist {
public static final String _data = "data";
public ReferencePersist getReference() {
return reference;
return this.reference;
}
public void setReference(ReferencePersist reference) {
@ -38,7 +37,7 @@ public class DescriptionReferencePersist {
}
public DescriptionReferenceDataPersist getData() {
return data;
return this.data;
}
public void setData(DescriptionReferenceDataPersist data) {
@ -71,7 +70,7 @@ public class DescriptionReferencePersist {
return Arrays.asList(
this.spec()
.must(() -> !this.isNull(item.getReference()))
.failOn(DescriptionReferencePersist._reference).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionReferencePersist._reference}, LocaleContextHolder.getLocale())),
.failOn(DescriptionReferencePersist._reference).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{DescriptionReferencePersist._reference}, LocaleContextHolder.getLocale())),
this.refSpec()
.iff(() -> !this.isNull(item.getReference()))
.on(DescriptionReferencePersist._reference)
@ -79,7 +78,7 @@ public class DescriptionReferencePersist {
.using(() -> this.validatorFactory.validator(ReferencePersist.ReferencePersistValidator.class)),
this.spec()
.must(() -> !this.isNull(item.getData()))
.failOn(DescriptionReferencePersist._data).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionReferencePersist._data}, LocaleContextHolder.getLocale())),
.failOn(DescriptionReferencePersist._data).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{DescriptionReferencePersist._data}, LocaleContextHolder.getLocale())),
this.refSpec()
.iff(() -> !this.isNull(item.getData()))
.on(DescriptionReferencePersist._data)

View File

@ -1,5 +1,7 @@
package org.opencdmp.model.persist.descriptionproperties;
import gr.cite.tools.validation.ValidatorFactory;
import gr.cite.tools.validation.specification.Specification;
import org.opencdmp.commons.enums.DescriptionStatus;
import org.opencdmp.commons.enums.FieldValidationType;
import org.opencdmp.commons.types.descriptiontemplate.DefinitionEntity;
@ -11,15 +13,16 @@ import org.opencdmp.errorcode.ErrorThesaurusProperties;
import org.opencdmp.model.persist.validation.StatusAware;
import org.opencdmp.service.visibility.VisibilityService;
import org.opencdmp.service.visibility.VisibilityServiceImpl;
import gr.cite.tools.validation.ValidatorFactory;
import gr.cite.tools.validation.specification.Specification;
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.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class PropertyDefinitionPersist {
@ -29,7 +32,7 @@ public class PropertyDefinitionPersist {
private Map<String, PropertyDefinitionFieldSetPersist> fieldSets;
public Map<String, PropertyDefinitionFieldSetPersist> getFieldSets() {
return fieldSets;
return this.fieldSets;
}
public void setFieldSets(Map<String, PropertyDefinitionFieldSetPersist> fieldSets) {
@ -67,21 +70,21 @@ public class PropertyDefinitionPersist {
this.spec()
.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())),
.failOn(PropertyDefinitionPersist._fieldSets).failWith(this.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())),
.failOn(PropertyDefinitionPersist._fieldSets).failWith(this.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())),
.failOn(PropertyDefinitionPersist._fieldSets).failWith(this.messageSource.getMessage("Validation.MissingFields", new Object[]{this.serializeMissingFields(this.getMissingFieldsEntity(item))}, LocaleContextHolder.getLocale())),
this.mapSpec()
.iff(() -> !this.isNull(item.getFieldSets()))
.on(PropertyDefinitionPersist._fieldSets)
.over(item.getFieldSets())
.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).withVisibilityService(visibilityService).setStatus(this.status);
FieldSetEntity fieldSetEntity = this.definition != null ? this.definition.getFieldSetById((String)itm.getKey()).stream().findFirst().orElse(null) : null;
return this.validatorFactory.validator(PropertyDefinitionFieldSetPersist.PersistValidator.class).withFieldSetEntity(fieldSetEntity).withVisibilityService(this.visibilityService).setStatus(this.status);
})
);
}
@ -105,8 +108,8 @@ public class PropertyDefinitionPersist {
private List<FieldSetEntity> getMissingFieldSetEntity(PropertyDefinitionPersist item){
List<FieldSetEntity> missingMultipleFieldSets = new ArrayList<>();
if (definition == null || definition.getAllFieldSets() == null) return missingMultipleFieldSets;
for (FieldSetEntity fieldSet: definition.getAllFieldSets()) {
if (this.definition == null || this.definition.getAllFieldSets() == null) return missingMultipleFieldSets;
for (FieldSetEntity fieldSet: this.definition.getAllFieldSets()) {
boolean requiredAtLeastOneFieldSet = fieldSet.getMultiplicity() != null && fieldSet.getHasMultiplicity() && fieldSet.getMultiplicity().getMin() > 0;
if (requiredAtLeastOneFieldSet) {
@ -124,8 +127,8 @@ public class PropertyDefinitionPersist {
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 (this.definition == null || this.definition.getAllFieldSets() == null) return missingFields;
for (FieldSetEntity fieldSet: this.definition.getAllFieldSets()) {
if (fieldSet.getFields() == null) continue;
@ -163,12 +166,12 @@ public class PropertyDefinitionPersist {
private String serializeMissingFieldSets (List<FieldSetEntity> missingFieldSets){
if (missingFieldSets == null) return "";
return missingFieldSets.stream().map(x -> x.getId()).collect(Collectors.joining(", "));
return missingFieldSets.stream().map(FieldSetEntity::getId).collect(Collectors.joining(", "));
}
private String serializeMissingFields (List<FieldEntity> missingFields){
if (missingFields == null) return "";
return missingFields.stream().map(x -> x.getId()).collect(Collectors.joining(", "));
return missingFields.stream().map(FieldEntity::getId).collect(Collectors.joining(", "));
}
}

View File

@ -1,9 +1,9 @@
package org.opencdmp.model.persist.descriptionreference;
import gr.cite.tools.validation.specification.Specification;
import org.opencdmp.commons.validation.BaseValidator;
import org.opencdmp.convention.ConventionService;
import org.opencdmp.errorcode.ErrorThesaurusProperties;
import gr.cite.tools.validation.specification.Specification;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Scope;
@ -12,7 +12,6 @@ import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
public class DescriptionReferenceDataPersist {
@ -21,7 +20,7 @@ public class DescriptionReferenceDataPersist {
public static final String _fieldId = "fieldId";
public String getFieldId() {
return fieldId;
return this.fieldId;
}
public void setFieldId(String fieldId) {
@ -50,7 +49,7 @@ public class DescriptionReferenceDataPersist {
return Arrays.asList(
this.spec()
.must(() -> !this.isNull(item.getFieldId()))
.failOn(DescriptionReferenceDataPersist._fieldId).failWith(messageSource.getMessage("Validation_Required", new Object[]{DescriptionReferenceDataPersist._fieldId}, LocaleContextHolder.getLocale()))
.failOn(DescriptionReferenceDataPersist._fieldId).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{DescriptionReferenceDataPersist._fieldId}, LocaleContextHolder.getLocale()))
);
}
}

View File

@ -66,6 +66,8 @@ import org.opencdmp.service.filetransformer.FileTransformerService;
import org.opencdmp.service.responseutils.ResponseUtilsService;
import org.opencdmp.service.storage.StorageFileProperties;
import org.opencdmp.service.storage.StorageFileService;
import org.opencdmp.service.visibility.VisibilityService;
import org.opencdmp.service.visibility.VisibilityServiceImpl;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
@ -237,11 +239,12 @@ public class DescriptionServiceImpl implements DescriptionService {
this.entityManager.flush();
org.opencdmp.commons.types.descriptiontemplate.DefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(org.opencdmp.commons.types.descriptiontemplate.DefinitionEntity.class, descriptionTemplateEntity.getDefinition());
Map<String, List<UUID>> fieldToReferenceMap = this.patchAndSaveReferences(this.buildDescriptionReferencePersists(model.getProperties()), data.getId(), definition);
VisibilityService visibilityService = new VisibilityServiceImpl(definition, model.getProperties());
Map<String, List<UUID>> fieldToReferenceMap = this.patchAndSaveReferences(this.buildDescriptionReferencePersists(visibilityService, model.getProperties()), data.getId(), definition);
this.entityManager.flush();
data.setProperties(this.jsonHandlingService.toJson(this.buildPropertyDefinitionEntity(model.getProperties(), definition, fieldToReferenceMap)));
data.setProperties(this.jsonHandlingService.toJson(this.buildPropertyDefinitionEntity(visibilityService, model.getProperties(), definition, fieldToReferenceMap)));
this.entityManager.merge(data);
@ -262,7 +265,7 @@ public class DescriptionServiceImpl implements DescriptionService {
return this.builderFactory.builder(DescriptionBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermission).build(BaseFieldSet.build(fields, Description._id), data);
}
@Override
public void updateDescriptionTemplate(UpdateDescriptionTemplatePersist model) throws InvalidApplicationException, IOException, JAXBException {
public void updateDescriptionTemplate(UpdateDescriptionTemplatePersist model) throws InvalidApplicationException, IOException {
logger.debug(new MapLogEntry("update description template").And("model", model));
this.authorizationService.authorizeAtLeastOneForce(List.of(this.authorizationContentResolver.descriptionAffiliation(model.getId())), Permission.EditDescription);
@ -399,7 +402,7 @@ public class DescriptionServiceImpl implements DescriptionService {
this.eventHandler.handle(event);
}
private NotifyIntegrationEvent applyNotificationType(DescriptionStatus status, NotifyIntegrationEvent event) {
private void applyNotificationType(DescriptionStatus status, NotifyIntegrationEvent event) {
switch (status) {
case Draft:
event.setNotificationType(this.notificationProperties.getDescriptionModifiedType());
@ -410,7 +413,7 @@ public class DescriptionServiceImpl implements DescriptionService {
default:
throw new MyApplicationException("Unsupported Description Status.");
}
return event;
return;
}
@Override
@ -460,38 +463,39 @@ public class DescriptionServiceImpl implements DescriptionService {
DescriptionPersist.DescriptionPersistValidator validator = this.validatorFactory.validator(DescriptionPersist.DescriptionPersistValidator.class);
validator.validate(this.buildDescriptionPersist(description));
if (validator.result().isValid()) descriptionValidationResult.setResult(DescriptionValidationOutput.Valid);
descriptionValidationResults.add(descriptionValidationResult);
descriptionValidationResults.add(descriptionValidationResult);
}
return descriptionValidationResults;
}
private @NotNull PropertyDefinitionEntity buildPropertyDefinitionEntity(PropertyDefinitionPersist persist, org.opencdmp.commons.types.descriptiontemplate.DefinitionEntity definition, Map<String, List<UUID>> fieldToReferenceMap) throws InvalidApplicationException {
private @NotNull PropertyDefinitionEntity buildPropertyDefinitionEntity(VisibilityService visibilityService, PropertyDefinitionPersist persist, org.opencdmp.commons.types.descriptiontemplate.DefinitionEntity definition, Map<String, List<UUID>> fieldToReferenceMap) throws InvalidApplicationException {
PropertyDefinitionEntity data = new PropertyDefinitionEntity();
if (persist == null) return data;
if (persist.getFieldSets() != null && !persist.getFieldSets().isEmpty()){
data.setFieldSets(new HashMap<>());
for (String key: persist.getFieldSets().keySet()) {
FieldSetEntity fieldSetEntity = definition != null ? definition.getFieldSetById(key).stream().findFirst().orElse(null) : null;
data.getFieldSets().put(key, this.buildPropertyDefinitionFieldSetEntity(persist.getFieldSets().get(key), fieldSetEntity, fieldToReferenceMap));
data.getFieldSets().put(key, this.buildPropertyDefinitionFieldSetEntity(persist.getFieldSets().get(key), fieldSetEntity, fieldToReferenceMap, visibilityService));
}
}
return data;
}
private @NotNull PropertyDefinitionFieldSetEntity buildPropertyDefinitionFieldSetEntity(PropertyDefinitionFieldSetPersist persist, FieldSetEntity fieldSetEntity, Map<String, List<UUID>> fieldToReferenceMap) throws InvalidApplicationException {
private @NotNull PropertyDefinitionFieldSetEntity buildPropertyDefinitionFieldSetEntity(PropertyDefinitionFieldSetPersist persist, FieldSetEntity fieldSetEntity, Map<String, List<UUID>> fieldToReferenceMap, VisibilityService visibilityService) throws InvalidApplicationException {
PropertyDefinitionFieldSetEntity data = new PropertyDefinitionFieldSetEntity();
if (persist == null) return data;
if (!this.conventionService.isListNullOrEmpty(persist.getItems())){
data.setItems(new ArrayList<>());
for (PropertyDefinitionFieldSetItemPersist itemsPersist: persist.getItems()) {
data.getItems().add(this.buildPropertyDefinitionFieldSetItemEntity(itemsPersist, fieldSetEntity, fieldToReferenceMap));
data.getItems().add(this.buildPropertyDefinitionFieldSetItemEntity(itemsPersist, fieldSetEntity, fieldToReferenceMap, visibilityService));
}
}
return data;
}
private @NotNull PropertyDefinitionFieldSetItemEntity buildPropertyDefinitionFieldSetItemEntity(PropertyDefinitionFieldSetItemPersist persist, FieldSetEntity fieldSetEntity, Map<String, List<UUID>> fieldToReferenceMap) throws InvalidApplicationException {
private @NotNull PropertyDefinitionFieldSetItemEntity buildPropertyDefinitionFieldSetItemEntity(PropertyDefinitionFieldSetItemPersist persist, FieldSetEntity fieldSetEntity, Map<String, List<UUID>> fieldToReferenceMap, VisibilityService visibilityService) throws InvalidApplicationException {
PropertyDefinitionFieldSetItemEntity data = new PropertyDefinitionFieldSetItemEntity();
if (persist == null) return data;
if (persist.getFields() != null && !persist.getFields().isEmpty()){
@ -500,16 +504,17 @@ public class DescriptionServiceImpl implements DescriptionService {
data.setFields(new HashMap<>());
for (String key: persist.getFields().keySet()) {
org.opencdmp.commons.types.descriptiontemplate.FieldEntity fieldEntity = fieldSetEntity != null ? fieldSetEntity.getFieldById(key).stream().findFirst().orElse(null) : null;
data.getFields().put(key, this.buildFieldEntity(persist.getFields().get(key), fieldEntity, fieldToReferenceMap));
boolean isVisible = fieldEntity != null ? visibilityService.isVisible(fieldEntity.getId(), persist.getOrdinal()) : false;
data.getFields().put(key, this.buildFieldEntity(persist.getFields().get(key), fieldEntity, fieldToReferenceMap, isVisible));
}
}
return data;
}
private @NotNull FieldEntity buildFieldEntity(FieldPersist persist, org.opencdmp.commons.types.descriptiontemplate.FieldEntity fieldEntity, Map<String, List<UUID>> fieldToReferenceMap) throws InvalidApplicationException {
private @NotNull FieldEntity buildFieldEntity(FieldPersist persist, org.opencdmp.commons.types.descriptiontemplate.FieldEntity fieldEntity, Map<String, List<UUID>> fieldToReferenceMap, boolean isVisible) throws InvalidApplicationException {
FieldType fieldType = fieldEntity != null && fieldEntity.getData() != null ? fieldEntity.getData().getFieldType() : FieldType.FREE_TEXT;
FieldEntity data = new FieldEntity();
if (persist == null) return data;
if (persist == null || !isVisible) return data;
if (FieldType.isTextType(fieldType)) {
if (FieldType.UPLOAD.equals(fieldType)){
@ -537,7 +542,7 @@ public class DescriptionServiceImpl implements DescriptionService {
else if (FieldType.isTextListType(fieldType)) {
List<UUID> ids = new ArrayList<>();
if (FieldType.INTERNAL_ENTRIES_DMPS.equals(fieldType)) {
if (!this.conventionService.isListNullOrEmpty(persist.getTextListValue())) persist.getTextListValue().stream().map(UUID::fromString).toList();
if (!this.conventionService.isListNullOrEmpty(persist.getTextListValue())) ids = persist.getTextListValue().stream().map(UUID::fromString).toList();
else if (!this.conventionService.isNullOrEmpty(persist.getTextValue())) ids.add(UUID.fromString(persist.getTextValue()));
if (!ids.isEmpty()){
@ -590,7 +595,7 @@ public class DescriptionServiceImpl implements DescriptionService {
return data;
}
private @NotNull List<DescriptionReferencePersist> buildDescriptionReferencePersists(PropertyDefinitionPersist persist){
private @NotNull List<DescriptionReferencePersist> buildDescriptionReferencePersists(VisibilityService visibilityService, PropertyDefinitionPersist persist){
List<DescriptionReferencePersist> descriptionReferencePersists = new ArrayList<>();
if (persist.getFieldSets() != null && !persist.getFieldSets().isEmpty()){
for (PropertyDefinitionFieldSetPersist propertyDefinitionFieldSetPersist: persist.getFieldSets().values()) {
@ -599,7 +604,8 @@ public class DescriptionServiceImpl implements DescriptionService {
if (definitionFieldSetItemPersist.getFields() != null && !definitionFieldSetItemPersist.getFields().isEmpty()) {
for (String key : definitionFieldSetItemPersist.getFields().keySet()) {
FieldPersist fieldPersist = definitionFieldSetItemPersist.getFields().get(key);
this.BuildDescriptionReferencePersist(key, fieldPersist, descriptionReferencePersists);
boolean isVisible = visibilityService.isVisible(key, definitionFieldSetItemPersist.getOrdinal());
if (isVisible) this.BuildDescriptionReferencePersist(key, fieldPersist, descriptionReferencePersists);
}
}
}
@ -649,7 +655,7 @@ public class DescriptionServiceImpl implements DescriptionService {
List<UUID> updatedCreatedIds = new ArrayList<>();
for (DescriptionReferencePersist model : models) {
ReferencePersist referencePersist = model.getReference();
ReferenceEntity referenceEntity = null;
ReferenceEntity referenceEntity;
if (this.conventionService.isValidGuid(referencePersist.getId())){
referenceEntity = this.entityManager.find(ReferenceEntity.class, referencePersist.getId());
if (referenceEntity == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{referencePersist.getId(), Reference.class.getSimpleName()}, LocaleContextHolder.getLocale()));