Connecting 'dmp' entity with 'description' entity (description builder populating dmp data)

This commit is contained in:
Thomas Georgios Giannos 2023-10-24 17:07:28 +03:00
parent 3e7b9ca90b
commit 1e733cf704
2 changed files with 55 additions and 6 deletions

View File

@ -16,7 +16,7 @@ public class Description {
public static final String _label = "label"; public static final String _label = "label";
private UUID dmp; private Dmp dmp;
public static final String _dmp = "dmp"; public static final String _dmp = "dmp";
@ -88,11 +88,11 @@ public class Description {
this.label = label; this.label = label;
} }
public UUID getDmp() { public Dmp getDmp() {
return dmp; return dmp;
} }
public void setDmp(UUID dmp) { public void setDmp(Dmp dmp) {
this.dmp = dmp; this.dmp = dmp;
} }

View File

@ -4,7 +4,12 @@ import eu.eudat.authorization.AuthorizationFlags;
import eu.eudat.convention.ConventionService; import eu.eudat.convention.ConventionService;
import eu.eudat.data.DescriptionEntity; import eu.eudat.data.DescriptionEntity;
import eu.eudat.model.Description; 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.exception.MyApplicationException;
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.fieldset.FieldSet; import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.DataLogEntry; import gr.cite.tools.logging.DataLogEntry;
import gr.cite.tools.logging.LoggerService; import gr.cite.tools.logging.LoggerService;
@ -15,17 +20,26 @@ import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
@Component @Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class DescriptionBuilder extends BaseBuilder<Description, DescriptionEntity> { public class DescriptionBuilder extends BaseBuilder<Description, DescriptionEntity> {
private final QueryFactory queryFactory;
private final BuilderFactory builderFactory;
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None); private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
@Autowired @Autowired
public DescriptionBuilder( public DescriptionBuilder(
ConventionService conventionService) { ConventionService conventionService,
QueryFactory queryFactory,
BuilderFactory builderFactory) {
super(conventionService, new LoggerService(LoggerFactory.getLogger(DescriptionBuilder.class))); super(conventionService, new LoggerService(LoggerFactory.getLogger(DescriptionBuilder.class)));
this.queryFactory = queryFactory;
this.builderFactory = builderFactory;
} }
public DescriptionBuilder authorize(EnumSet<AuthorizationFlags> values) { public DescriptionBuilder authorize(EnumSet<AuthorizationFlags> values) {
@ -40,6 +54,9 @@ public class DescriptionBuilder extends BaseBuilder<Description, DescriptionEnti
if (fields == null || data == null || fields.isEmpty()) if (fields == null || data == null || fields.isEmpty())
return new ArrayList<>(); return new ArrayList<>();
FieldSet dmpFields = fields.extractPrefixed(this.asPrefix(Description._dmp));
Map<UUID, Dmp> dmpItemsMap = this.collectDmps(dmpFields, data);
List<Description> models = new ArrayList<>(); List<Description> models = new ArrayList<>();
for (DescriptionEntity d : data) { for (DescriptionEntity d : data) {
Description m = new Description(); Description m = new Description();
@ -47,8 +64,6 @@ public class DescriptionBuilder extends BaseBuilder<Description, DescriptionEnti
m.setId(d.getId()); m.setId(d.getId());
if (fields.hasField(this.asIndexer(Description._label))) if (fields.hasField(this.asIndexer(Description._label)))
m.setLabel(d.getLabel()); m.setLabel(d.getLabel());
if (fields.hasField(this.asIndexer(Description._dmp)))
m.setDmp(d.getDmp());
if (fields.hasField(this.asIndexer(Description._uri))) if (fields.hasField(this.asIndexer(Description._uri)))
m.setUri(d.getUri()); m.setUri(d.getUri());
if (fields.hasField(this.asIndexer(Description._properties))) if (fields.hasField(this.asIndexer(Description._properties)))
@ -75,10 +90,44 @@ public class DescriptionBuilder extends BaseBuilder<Description, DescriptionEnti
m.setFinalizedAt(d.getFinalizedAt()); m.setFinalizedAt(d.getFinalizedAt());
if (fields.hasField(this.asIndexer(Description._hash))) if (fields.hasField(this.asIndexer(Description._hash)))
m.setHash(this.hashValue(d.getUpdatedAt())); m.setHash(this.hashValue(d.getUpdatedAt()));
if (!dmpFields.isEmpty() && dmpItemsMap != null && dmpItemsMap.containsKey(d.getDmp()))
m.setDmp(dmpItemsMap.get(d.getDmp()));
models.add(m); models.add(m);
} }
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0)); this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
return models; return models;
} }
private Map<UUID, Dmp> collectDmps(FieldSet fields, List<DescriptionEntity> data) throws MyApplicationException {
if (fields.isEmpty() || data.isEmpty())
return null;
this.logger.debug("checking related - {}", Dmp.class.getSimpleName());
Map<UUID, Dmp> 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;
}
} }