From 1e733cf704235a5637b198da8a2254bb116b7f90 Mon Sep 17 00:00:00 2001 From: Thomas Georgios Giannos Date: Tue, 24 Oct 2023 17:07:28 +0300 Subject: [PATCH] Connecting 'dmp' entity with 'description' entity (description builder populating dmp data) --- .../main/java/eu/eudat/model/Description.java | 6 +- .../model/builder/DescriptionBuilder.java | 55 ++++++++++++++++++- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/dmp-backend/core/src/main/java/eu/eudat/model/Description.java b/dmp-backend/core/src/main/java/eu/eudat/model/Description.java index ea9d73a56..bf4e68d76 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/model/Description.java +++ b/dmp-backend/core/src/main/java/eu/eudat/model/Description.java @@ -16,7 +16,7 @@ public class Description { public static final String _label = "label"; - private UUID dmp; + private Dmp dmp; public static final String _dmp = "dmp"; @@ -88,11 +88,11 @@ public class Description { this.label = label; } - public UUID getDmp() { + public Dmp getDmp() { return dmp; } - public void setDmp(UUID dmp) { + public void setDmp(Dmp dmp) { this.dmp = dmp; } diff --git a/dmp-backend/core/src/main/java/eu/eudat/model/builder/DescriptionBuilder.java b/dmp-backend/core/src/main/java/eu/eudat/model/builder/DescriptionBuilder.java index 0d015d357..9ef64a834 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/model/builder/DescriptionBuilder.java +++ b/dmp-backend/core/src/main/java/eu/eudat/model/builder/DescriptionBuilder.java @@ -4,7 +4,12 @@ import eu.eudat.authorization.AuthorizationFlags; import eu.eudat.convention.ConventionService; import eu.eudat.data.DescriptionEntity; import eu.eudat.model.Description; +import eu.eudat.model.Dmp; +import eu.eudat.query.DmpQuery; +import gr.cite.tools.data.builder.BuilderFactory; +import gr.cite.tools.data.query.QueryFactory; import gr.cite.tools.exception.MyApplicationException; +import gr.cite.tools.fieldset.BaseFieldSet; import gr.cite.tools.fieldset.FieldSet; import gr.cite.tools.logging.DataLogEntry; import gr.cite.tools.logging.LoggerService; @@ -15,17 +20,26 @@ import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Component; import java.util.*; +import java.util.stream.Collectors; @Component @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) public class DescriptionBuilder extends BaseBuilder { + private final QueryFactory queryFactory; + + private final BuilderFactory builderFactory; + private EnumSet authorize = EnumSet.of(AuthorizationFlags.None); @Autowired public DescriptionBuilder( - ConventionService conventionService) { + ConventionService conventionService, + QueryFactory queryFactory, + BuilderFactory builderFactory) { super(conventionService, new LoggerService(LoggerFactory.getLogger(DescriptionBuilder.class))); + this.queryFactory = queryFactory; + this.builderFactory = builderFactory; } public DescriptionBuilder authorize(EnumSet values) { @@ -40,6 +54,9 @@ public class DescriptionBuilder extends BaseBuilder(); + FieldSet dmpFields = fields.extractPrefixed(this.asPrefix(Description._dmp)); + Map dmpItemsMap = this.collectDmps(dmpFields, data); + List models = new ArrayList<>(); for (DescriptionEntity d : data) { Description m = new Description(); @@ -47,8 +64,6 @@ public class DescriptionBuilder extends BaseBuilder collectDmps(FieldSet fields, List data) throws MyApplicationException { + if (fields.isEmpty() || data.isEmpty()) + return null; + this.logger.debug("checking related - {}", Dmp.class.getSimpleName()); + + Map itemMap; + if (!fields.hasOtherField(this.asIndexer(Dmp._id))) { + itemMap = this.asEmpty( + data.stream().map(DescriptionEntity::getDmp).distinct().collect(Collectors.toList()), + x -> { + Dmp item = new Dmp(); + item.setId(x); + return item; + }, + Dmp::getId); + } else { + FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(Dmp._id); + DmpQuery q = this.queryFactory.query(DmpQuery.class).authorize(this.authorize).ids(data.stream().map(DescriptionEntity::getDmp).distinct().collect(Collectors.toList())); + itemMap = this.builderFactory.builder(DmpBuilder.class).authorize(this.authorize).asForeignKey(q, clone, Dmp::getId); + } + if (!fields.hasField(Dmp._id)) { + itemMap.forEach((id, item) -> { + if (item != null) + item.setId(null); + }); + } + + return itemMap; + } + }