argos/dmp-backend/core/src/main/java/eu/eudat/model/builder/PublicDmpBuilder.java

97 lines
4.5 KiB
Java
Raw Normal View History

2023-11-07 16:07:24 +01:00
package eu.eudat.model.builder;
import eu.eudat.authorization.AuthorizationFlags;
import eu.eudat.convention.ConventionService;
import eu.eudat.data.DmpEntity;
import eu.eudat.model.PublicDmp;
import eu.eudat.model.PublicDmpReference;
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;
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.DataLogEntry;
import gr.cite.tools.logging.LoggerService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
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 PublicDmpBuilder extends BaseBuilder<PublicDmp, DmpEntity> {
private final QueryFactory queryFactory;
private final BuilderFactory builderFactory;
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
@Autowired
public PublicDmpBuilder(ConventionService conventionService,
QueryFactory queryFactory,
BuilderFactory builderFactory) {
super(conventionService, new LoggerService(LoggerFactory.getLogger(PublicDmpBuilder.class)));
this.queryFactory = queryFactory;
this.builderFactory = builderFactory;
}
public PublicDmpBuilder authorize(EnumSet<AuthorizationFlags> values) {
this.authorize = values;
return this;
}
@Override
public List<PublicDmp> build(FieldSet fields, List<DmpEntity> data) throws MyApplicationException {
this.logger.debug("building for {} items requesting {} fields", Optional.ofNullable(data).map(List::size).orElse(0), Optional.ofNullable(fields).map(FieldSet::getFields).map(Set::size).orElse(0));
this.logger.trace(new DataLogEntry("requested fields", fields));
if (fields == null || data == null || fields.isEmpty())
return new ArrayList<>();
List<PublicDmp> models = new ArrayList<>();
FieldSet dmpReferencesFields = fields.extractPrefixed(this.asPrefix(PublicDmp._dmpReferences));
Map<UUID, List<PublicDmpReference>> dmpReferenceMap = this.collectDmpReferences(dmpReferencesFields, data);
for (DmpEntity d : data) {
PublicDmp m = new PublicDmp();
if (fields.hasField(this.asIndexer(PublicDmp._id))) m.setId(d.getId());
if (fields.hasField(this.asIndexer(PublicDmp._label))) m.setLabel(d.getLabel());
if (fields.hasField(this.asIndexer(PublicDmp._version))) m.setVersion(d.getVersion());
if (fields.hasField(this.asIndexer(PublicDmp._description))) m.setDescription(d.getDescription());
if (fields.hasField(this.asIndexer(PublicDmp._finalizedAt))) m.setFinalizedAt(d.getFinalizedAt());
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;
}
2023-11-13 08:39:31 +01:00
private Map<UUID, List<PublicDmpReference>> collectDmpReferences(FieldSet fields, List<DmpEntity> data) throws MyApplicationException {
if (fields.isEmpty() || data.isEmpty()) return null;
2023-11-07 16:07:24 +01:00
this.logger.debug("checking related - {}", PublicDmpReference.class.getSimpleName());
Map<UUID, List<PublicDmpReference>> itemMap = null;
FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(this.asIndexer(PublicDmpReference._dmp, PublicDmp._id));
2023-11-13 08:39:31 +01:00
DmpReferenceQuery query = this.queryFactory.query(DmpReferenceQuery.class).authorize(this.authorize).dmpIds(data.stream().map(DmpEntity::getId).distinct().collect(Collectors.toList()));
2023-11-07 16:07:24 +01:00
itemMap = this.builderFactory.builder(PublicDmpReferenceBuilder.class).authorize(this.authorize).authorize(this.authorize).asMasterKey(query, clone, x -> x.getDmp().getId());
if (!fields.hasField(this.asIndexer(PublicDmpReference._dmp, PublicDmp._id))) {
itemMap.values().stream().flatMap(List::stream).filter(x -> x != null && x.getDmp() != null).peek(x -> {
x.getDmp().setId(null);
});
}
return itemMap;
}
}