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 bf4e68d76..68a3970b4 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 @@ -4,6 +4,7 @@ import eu.eudat.commons.enums.DescriptionStatus; import eu.eudat.commons.enums.IsActive; import java.time.Instant; +import java.util.List; import java.util.UUID; public class Description { @@ -72,6 +73,10 @@ public class Description { public static final String _hash = "hash"; + private List descriptionReferences; + + public static final String _descriptionReferences = "descriptionReferences"; + public UUID getId() { return id; } @@ -199,4 +204,13 @@ public class Description { public void setHash(String hash) { this.hash = hash; } + + public List getDescriptionReferences() { + return descriptionReferences; + } + + public void setDescriptionReferences(List descriptionReferences) { + this.descriptionReferences = descriptionReferences; + } + } 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 ec6ef5508..add5e0c10 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 @@ -3,9 +3,14 @@ package eu.eudat.model.builder; import eu.eudat.authorization.AuthorizationFlags; import eu.eudat.convention.ConventionService; import eu.eudat.data.DescriptionEntity; +import eu.eudat.data.DmpEntity; import eu.eudat.model.Description; +import eu.eudat.model.DescriptionReference; import eu.eudat.model.Dmp; +import eu.eudat.model.DmpReference; +import eu.eudat.query.DescriptionReferenceQuery; import eu.eudat.query.DmpQuery; +import eu.eudat.query.DmpReferenceQuery; import gr.cite.tools.data.builder.BuilderFactory; import gr.cite.tools.data.query.QueryFactory; import gr.cite.tools.exception.MyApplicationException; @@ -57,6 +62,9 @@ public class DescriptionBuilder extends BaseBuilder dmpItemsMap = this.collectDmps(dmpFields, data); + FieldSet descriptionReferencesFields = fields.extractPrefixed(this.asPrefix(Description._descriptionReferences)); + Map> descriptionReferencesMap = this.collectDescriptionReferences(descriptionReferencesFields, data); + List models = new ArrayList<>(); for (DescriptionEntity d : data) { Description m = new Description(); @@ -94,6 +102,9 @@ public class DescriptionBuilder extends BaseBuilder> collectDescriptionReferences(FieldSet fields, List data) throws MyApplicationException { + if (fields.isEmpty() || data.isEmpty()) return null; + this.logger.debug("checking related - {}", DescriptionReference.class.getSimpleName()); + + Map> itemMap; + FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(this.asIndexer(DescriptionReference._description, Description._id)); + DescriptionReferenceQuery query = this.queryFactory.query(DescriptionReferenceQuery.class).authorize(this.authorize).descriptionIds(data.stream().map(DescriptionEntity::getId).distinct().collect(Collectors.toList())); + itemMap = this.builderFactory.builder(DescriptionReferenceBuilder.class).authorize(this.authorize).asMasterKey(query, clone, x -> x.getDescription().getId()); + + if (!fields.hasField(this.asIndexer(DmpReference._dmp, Dmp._id))) { + itemMap.values().stream().flatMap(List::stream).filter(x -> x != null && x.getDescription() != null).peek(x -> { + x.getDescription().setId(null); + }); + } + + return itemMap; + } + } diff --git a/dmp-backend/core/src/main/java/eu/eudat/model/builder/DmpBuilder.java b/dmp-backend/core/src/main/java/eu/eudat/model/builder/DmpBuilder.java index e1e6b4725..191d9aa89 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/model/builder/DmpBuilder.java +++ b/dmp-backend/core/src/main/java/eu/eudat/model/builder/DmpBuilder.java @@ -98,10 +98,13 @@ public class DmpBuilder extends BaseBuilder { if (!dmpDescriptionsFields.isEmpty() && dmpDescriptionsMap != null && dmpDescriptionsMap.containsKey(d.getId())) m.setDmpDescriptions(dmpDescriptionsMap.get(d.getId())); - if (dmpReferenceMap != null && !dmpReferenceMap.isEmpty() && dmpReferenceMap.containsKey(d.getId())) m.setDmpReferences(dmpReferenceMap.get(d.getId())); + if (dmpReferenceMap != null && !dmpReferenceMap.isEmpty() && dmpReferenceMap.containsKey(d.getId())) + m.setDmpReferences(dmpReferenceMap.get(d.getId())); + models.add(m); } this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0)); + return models; } @@ -132,15 +135,15 @@ public class DmpBuilder extends BaseBuilder { Map> itemMap = null; FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(this.asIndexer(DmpReference._dmp, Dmp._id)); - DmpReferenceQuery query = this.queryFactory.query(DmpReferenceQuery.class).authorize(this.authorize).dmpIds(datas.stream().map(x -> x.getId()).distinct().collect(Collectors.toList())); + DmpReferenceQuery query = this.queryFactory.query(DmpReferenceQuery.class).authorize(this.authorize).dmpIds(datas.stream().map(DmpEntity::getId).distinct().collect(Collectors.toList())); itemMap = this.builderFactory.builder(DmpReferenceBuilder.class).authorize(this.authorize).authorize(this.authorize).asMasterKey(query, clone, x -> x.getDmp().getId()); if (!fields.hasField(this.asIndexer(DmpReference._dmp, Dmp._id))) { - itemMap.values().stream().flatMap(List::stream).filter(x -> x != null && x.getDmp() != null).map(x -> { + itemMap.values().stream().flatMap(List::stream).filter(x -> x != null && x.getDmp() != null).peek(x -> { x.getDmp().setId(null); - return x; - }).collect(Collectors.toList()); + }); } + return itemMap; } diff --git a/dmp-backend/core/src/main/java/eu/eudat/query/lookup/DescriptionReferenceLookup.java b/dmp-backend/core/src/main/java/eu/eudat/query/lookup/DescriptionReferenceLookup.java new file mode 100644 index 000000000..d79778498 --- /dev/null +++ b/dmp-backend/core/src/main/java/eu/eudat/query/lookup/DescriptionReferenceLookup.java @@ -0,0 +1,76 @@ +package eu.eudat.query.lookup; + +import eu.eudat.commons.enums.IsActive; +import eu.eudat.query.DescriptionReferenceQuery; +import gr.cite.tools.data.query.Lookup; +import gr.cite.tools.data.query.QueryFactory; + +import java.util.Collection; +import java.util.UUID; + +public class DescriptionReferenceLookup extends Lookup { + + private Collection ids; + + private Collection excludedIds; + + private Collection isActives; + + private Collection descriptionIds; + + private Collection referenceIds; + + public Collection getIds() { + return ids; + } + + public void setIds(Collection ids) { + this.ids = ids; + } + + public Collection getExcludedIds() { + return excludedIds; + } + + public void setExcludedIds(Collection excludedIds) { + this.excludedIds = excludedIds; + } + + public Collection getIsActives() { + return isActives; + } + + public void setIsActives(Collection isActives) { + this.isActives = isActives; + } + + public Collection getDescriptionIds() { + return descriptionIds; + } + + public void setDescriptionIds(Collection descriptionIds) { + this.descriptionIds = descriptionIds; + } + + public Collection getReferenceIds() { + return referenceIds; + } + + public void setReferenceIds(Collection referenceIds) { + this.referenceIds = referenceIds; + } + + public DescriptionReferenceQuery enrich(QueryFactory queryFactory) { + DescriptionReferenceQuery query = queryFactory.query(DescriptionReferenceQuery.class); + if (this.ids != null) query.ids(this.ids); + if (this.excludedIds != null) query.excludedIds(this.excludedIds); + if (this.isActives != null) query.isActive(this.isActives); + if (this.descriptionIds != null) query.descriptionIds(this.descriptionIds); + if (this.referenceIds != null) query.referenceIds(this.referenceIds); + + this.enrichCommon(query); + + return query; + } + +}