From b9c4648d8f53a8c4e7a9db6ae5f52eab7bff6d88 Mon Sep 17 00:00:00 2001 From: amentis Date: Fri, 24 May 2024 13:58:30 +0300 Subject: [PATCH] handle common models with date, number blueprint values --- backend/core/pom.xml | 6 ++-- .../DmpBlueprintValueCommonModelBuilder.java | 31 ++++++++++++++++--- .../dmp/DmpCommonModelBuilder.java | 26 ++++++++++++++-- .../dmp/DmpPropertiesCommonModelBuilder.java | 10 +++++- .../opencdmp/service/dmp/DmpServiceImpl.java | 18 ++++++++--- 5 files changed, 76 insertions(+), 15 deletions(-) diff --git a/backend/core/pom.xml b/backend/core/pom.xml index 556b07ead..5ffac96a2 100644 --- a/backend/core/pom.xml +++ b/backend/core/pom.xml @@ -51,12 +51,12 @@ org.opencdmp repositorydepositbase - 2.0.13 + 2.0.14 org.opencdmp common-models - 0.0.16 + 0.0.17 gr.cite @@ -66,7 +66,7 @@ org.opencdmp file-transformer-base - 0.0.20 + 0.0.21 gr.cite diff --git a/backend/core/src/main/java/org/opencdmp/model/builder/commonmodels/dmp/DmpBlueprintValueCommonModelBuilder.java b/backend/core/src/main/java/org/opencdmp/model/builder/commonmodels/dmp/DmpBlueprintValueCommonModelBuilder.java index 568e114b8..e21ccae22 100644 --- a/backend/core/src/main/java/org/opencdmp/model/builder/commonmodels/dmp/DmpBlueprintValueCommonModelBuilder.java +++ b/backend/core/src/main/java/org/opencdmp/model/builder/commonmodels/dmp/DmpBlueprintValueCommonModelBuilder.java @@ -2,7 +2,12 @@ package org.opencdmp.model.builder.commonmodels.dmp; import org.opencdmp.authorization.AuthorizationFlags; import org.opencdmp.commonmodels.models.dmp.DmpBlueprintValueModel; +import org.opencdmp.commons.enums.DmpBlueprintExtraFieldDataType; +import org.opencdmp.commons.enums.DmpBlueprintFieldCategory; import org.opencdmp.commons.types.dmp.DmpBlueprintValueEntity; +import org.opencdmp.commons.types.dmpblueprint.DefinitionEntity; +import org.opencdmp.commons.types.dmpblueprint.ExtraFieldEntity; +import org.opencdmp.commons.types.dmpblueprint.FieldEntity; import org.opencdmp.convention.ConventionService; import org.opencdmp.model.builder.commonmodels.BaseCommonModelBuilder; import org.opencdmp.model.builder.commonmodels.CommonModelBuilderItemResponse; @@ -23,6 +28,8 @@ import java.util.Optional; @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) public class DmpBlueprintValueCommonModelBuilder extends BaseCommonModelBuilder { private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); + private DefinitionEntity definition; + @Autowired public DmpBlueprintValueCommonModelBuilder( ConventionService conventionService @@ -35,6 +42,11 @@ public class DmpBlueprintValueCommonModelBuilder extends BaseCommonModelBuilder< return this; } + public DmpBlueprintValueCommonModelBuilder withDefinition(DefinitionEntity definition) { + this.definition = definition; + return this; + } + @Override protected List> buildInternal(List data) throws MyApplicationException { this.logger.debug("building for {}", Optional.ofNullable(data).map(List::size).orElse(0)); @@ -42,10 +54,21 @@ public class DmpBlueprintValueCommonModelBuilder extends BaseCommonModelBuilder< List> models = new ArrayList<>(); for (DmpBlueprintValueEntity d : data) { - DmpBlueprintValueModel m = new DmpBlueprintValueModel(); - m.setValue(d.getValue()); - m.setFieldId(d.getFieldId()); - models.add(new CommonModelBuilderItemResponse<>(m, d)); + FieldEntity fieldEntity = this.definition != null ? this.definition.getFieldById(d.getFieldId()).stream().findFirst().orElse(null) : null; + + if (fieldEntity != null && fieldEntity.getCategory().equals(DmpBlueprintFieldCategory.Extra)) { + ExtraFieldEntity extraFieldEntity = (ExtraFieldEntity) fieldEntity; + DmpBlueprintValueModel m = new DmpBlueprintValueModel(); + m.setFieldId(d.getFieldId()); + if (extraFieldEntity != null && DmpBlueprintExtraFieldDataType.isDateType(extraFieldEntity.getType())){ + m.setDateValue(d.getDateValue()); + } else if (extraFieldEntity != null && DmpBlueprintExtraFieldDataType.isNumberType(extraFieldEntity.getType())){ + m.setNumberValue(d.getNumberValue()); + } else { + m.setValue(d.getValue()); + } + models.add(new CommonModelBuilderItemResponse<>(m, d)); + } } this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0)); diff --git a/backend/core/src/main/java/org/opencdmp/model/builder/commonmodels/dmp/DmpCommonModelBuilder.java b/backend/core/src/main/java/org/opencdmp/model/builder/commonmodels/dmp/DmpCommonModelBuilder.java index 66f647a7c..dcc08b3b8 100644 --- a/backend/core/src/main/java/org/opencdmp/model/builder/commonmodels/dmp/DmpCommonModelBuilder.java +++ b/backend/core/src/main/java/org/opencdmp/model/builder/commonmodels/dmp/DmpCommonModelBuilder.java @@ -18,8 +18,10 @@ import org.opencdmp.commonmodels.models.dmp.DmpModel; import org.opencdmp.commonmodels.models.dmpblueprint.DmpBlueprintModel; import org.opencdmp.commonmodels.models.dmpreference.DmpReferenceModel; import org.opencdmp.commons.JsonHandlingService; +import org.opencdmp.commons.XmlHandlingService; import org.opencdmp.commons.enums.IsActive; import org.opencdmp.commons.types.dmp.DmpPropertiesEntity; +import org.opencdmp.commons.types.dmpblueprint.DefinitionEntity; import org.opencdmp.convention.ConventionService; import org.opencdmp.data.*; import org.opencdmp.model.DmpUser; @@ -30,6 +32,7 @@ import org.opencdmp.model.builder.commonmodels.dmpblueprint.DmpBlueprintCommonMo import org.opencdmp.model.builder.commonmodels.dmpreference.DmpReferenceCommonModelBuilder; import org.opencdmp.model.description.Description; import org.opencdmp.model.dmp.Dmp; +import org.opencdmp.model.dmpblueprint.DmpBlueprint; import org.opencdmp.model.dmpreference.DmpReference; import org.opencdmp.query.*; import org.slf4j.LoggerFactory; @@ -49,6 +52,7 @@ public class DmpCommonModelBuilder extends BaseCommonModelBuilder values) { @@ -110,6 +115,7 @@ public class DmpCommonModelBuilder extends BaseCommonModelBuilder> entityDois = this.collectDmpEntityDois(data); Map creators = this.collectCreators(data); Map dmpBlueprints = this.collectDmpBlueprints(data); + Map definitionEntityMap = this.collectDmpBlueprintDefinitions(data); for (DmpEntity d : data) { DmpModel m = new DmpModel(); @@ -131,7 +137,7 @@ public class DmpCommonModelBuilder extends BaseCommonModelBuilder collectDmpBlueprintDefinitions(List data) throws MyApplicationException { + if (data.isEmpty()) + return null; + this.logger.debug("checking related - {}", DefinitionEntity.class.getSimpleName()); + + Map itemMap = new HashMap<>(); + DmpBlueprintQuery q = this.queryFactory.query(DmpBlueprintQuery.class).disableTracking().authorize(this.authorize).ids(data.stream().map(DmpEntity::getBlueprintId).distinct().collect(Collectors.toList())); + List items = q.collectAs(new BaseFieldSet().ensure(DmpBlueprint._id).ensure(DmpBlueprint._definition)); + for (DmpBlueprintEntity item : items){ + DefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(DefinitionEntity.class, item.getDefinition()); + itemMap.put(item.getId(), definition); + } + + return itemMap; + } + } diff --git a/backend/core/src/main/java/org/opencdmp/model/builder/commonmodels/dmp/DmpPropertiesCommonModelBuilder.java b/backend/core/src/main/java/org/opencdmp/model/builder/commonmodels/dmp/DmpPropertiesCommonModelBuilder.java index 904052850..1a5d397d1 100644 --- a/backend/core/src/main/java/org/opencdmp/model/builder/commonmodels/dmp/DmpPropertiesCommonModelBuilder.java +++ b/backend/core/src/main/java/org/opencdmp/model/builder/commonmodels/dmp/DmpPropertiesCommonModelBuilder.java @@ -3,6 +3,7 @@ package org.opencdmp.model.builder.commonmodels.dmp; import org.opencdmp.authorization.AuthorizationFlags; import org.opencdmp.commonmodels.models.dmp.DmpPropertiesModel; import org.opencdmp.commons.types.dmp.DmpPropertiesEntity; +import org.opencdmp.commons.types.dmpblueprint.DefinitionEntity; import org.opencdmp.convention.ConventionService; import org.opencdmp.model.builder.commonmodels.BaseCommonModelBuilder; import org.opencdmp.model.builder.commonmodels.CommonModelBuilderItemResponse; @@ -10,6 +11,7 @@ import org.opencdmp.model.builder.commonmodels.descriptiontemplate.PageCommonMod import gr.cite.tools.data.builder.BuilderFactory; import gr.cite.tools.exception.MyApplicationException; import gr.cite.tools.logging.LoggerService; +import org.opencdmp.model.builder.dmp.DmpPropertiesBuilder; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.ConfigurableBeanFactory; @@ -26,6 +28,7 @@ import java.util.Optional; public class DmpPropertiesCommonModelBuilder extends BaseCommonModelBuilder { private final BuilderFactory builderFactory; private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); + private DefinitionEntity definition; @Autowired public DmpPropertiesCommonModelBuilder( ConventionService conventionService, BuilderFactory builderFactory @@ -34,6 +37,11 @@ public class DmpPropertiesCommonModelBuilder extends BaseCommonModelBuilder values) { this.authorize = values; return this; @@ -47,7 +55,7 @@ public class DmpPropertiesCommonModelBuilder extends BaseCommonModelBuilder> models = new ArrayList<>(); for (DmpPropertiesEntity d : data) { DmpPropertiesModel m = new DmpPropertiesModel(); - if (d.getDmpBlueprintValues() != null) m.setDmpBlueprintValues(this.builderFactory.builder(DmpBlueprintValueCommonModelBuilder.class).authorize(this.authorize).build(d.getDmpBlueprintValues())); + if (d.getDmpBlueprintValues() != null) m.setDmpBlueprintValues(this.builderFactory.builder(DmpBlueprintValueCommonModelBuilder.class).withDefinition(definition).authorize(this.authorize).build(d.getDmpBlueprintValues())); if (d.getContacts() != null) m.setContacts(this.builderFactory.builder(DmpContactCommonModelBuilder.class).authorize(this.authorize).build(d.getContacts())); models.add(new CommonModelBuilderItemResponse<>(m, d)); diff --git a/backend/core/src/main/java/org/opencdmp/service/dmp/DmpServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/dmp/DmpServiceImpl.java index d52d41da6..9193e2ac4 100644 --- a/backend/core/src/main/java/org/opencdmp/service/dmp/DmpServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/dmp/DmpServiceImpl.java @@ -27,6 +27,7 @@ import org.opencdmp.commonmodels.models.description.DescriptionModel; import org.opencdmp.commonmodels.models.dmp.DmpBlueprintValueModel; import org.opencdmp.commonmodels.models.dmp.DmpContactModel; import org.opencdmp.commonmodels.models.dmp.DmpModel; +import org.opencdmp.commonmodels.models.dmpblueprint.ExtraFieldModel; import org.opencdmp.commonmodels.models.dmpblueprint.FieldModel; import org.opencdmp.commonmodels.models.dmpblueprint.ReferenceTypeFieldModel; import org.opencdmp.commonmodels.models.dmpblueprint.SectionModel; @@ -1992,9 +1993,10 @@ public class DmpServiceImpl implements DmpService { } } else { // custom fields - if (model.getProperties() != null && this.conventionService.isListNullOrEmpty(model.getProperties().getDmpBlueprintValues())){ + if (field.getCategory().equals(DmpBlueprintFieldCategory.Extra) && model.getProperties() != null && this.conventionService.isListNullOrEmpty(model.getProperties().getDmpBlueprintValues())){ DmpBlueprintValueModel dmpBlueprintValueModel = model.getProperties().getDmpBlueprintValues().stream().filter(x -> x.getFieldId().equals(field.getId())).findFirst().orElse(null); - if (dmpBlueprintValueModel != null) dmpBlueprintValues.put(dmpBlueprintValueModel.getFieldId(), this.commonDmpBlueprintValueToPersist(dmpBlueprintValueModel)); + ExtraFieldModel extraFieldModel = (ExtraFieldModel) field; + if (dmpBlueprintValueModel != null && extraFieldModel != null) dmpBlueprintValues.put(dmpBlueprintValueModel.getFieldId(), this.commonDmpBlueprintValueToPersist(dmpBlueprintValueModel, extraFieldModel)); } } } @@ -2051,14 +2053,20 @@ public class DmpServiceImpl implements DmpService { return persist; } - private DmpBlueprintValuePersist commonDmpBlueprintValueToPersist(DmpBlueprintValueModel model) { - if (model == null) + private DmpBlueprintValuePersist commonDmpBlueprintValueToPersist(DmpBlueprintValueModel model, ExtraFieldModel extraFieldModel) { + if (model == null || extraFieldModel == null) return null; DmpBlueprintValuePersist persist = new DmpBlueprintValuePersist(); persist.setFieldId(model.getFieldId()); - persist.setFieldValue(model.getValue()); + if (extraFieldModel.getDataType().equals(org.opencdmp.commonmodels.enums.DmpBlueprintExtraFieldDataType.Date)){ + persist.setDateValue(model.getDateValue()); + } else if (extraFieldModel.getDataType().equals(org.opencdmp.commonmodels.enums.DmpBlueprintExtraFieldDataType.Number)){ + persist.setNumberValue(model.getNumberValue()); + } else { + persist.setFieldValue(model.getValue()); + } return persist; }