2023-11-06 12:31:01 +01:00
|
|
|
package eu.eudat.model.builder;
|
|
|
|
|
|
|
|
import eu.eudat.authorization.AuthorizationFlags;
|
2023-11-24 11:39:26 +01:00
|
|
|
import eu.eudat.commons.enums.DescriptionTemplateVersionStatus;
|
2023-11-06 12:31:01 +01:00
|
|
|
import eu.eudat.convention.ConventionService;
|
|
|
|
import eu.eudat.data.DmpDescriptionTemplateEntity;
|
2023-11-24 11:39:26 +01:00
|
|
|
import eu.eudat.data.DmpEntity;
|
2023-11-06 12:31:01 +01:00
|
|
|
import eu.eudat.model.*;
|
|
|
|
import eu.eudat.query.DescriptionTemplateQuery;
|
|
|
|
import eu.eudat.query.DmpQuery;
|
2023-11-24 11:39:26 +01:00
|
|
|
import eu.eudat.query.DmpUserQuery;
|
2023-11-06 12:31:01 +01:00
|
|
|
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 DmpDescriptionTemplateBuilder extends BaseBuilder<DmpDescriptionTemplate, DmpDescriptionTemplateEntity> {
|
|
|
|
|
|
|
|
private final BuilderFactory builderFactory;
|
|
|
|
|
|
|
|
private final QueryFactory queryFactory;
|
|
|
|
|
|
|
|
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
public DmpDescriptionTemplateBuilder(
|
|
|
|
ConventionService conventionService,
|
|
|
|
BuilderFactory builderFactory, QueryFactory queryFactory) {
|
|
|
|
super(conventionService, new LoggerService(LoggerFactory.getLogger(DmpDescriptionTemplateBuilder.class)));
|
|
|
|
this.builderFactory = builderFactory;
|
|
|
|
this.queryFactory = queryFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
public DmpDescriptionTemplateBuilder authorize(EnumSet<AuthorizationFlags> values) {
|
|
|
|
this.authorize = values;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<DmpDescriptionTemplate> build(FieldSet fields, List<DmpDescriptionTemplateEntity> 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<>();
|
|
|
|
|
2023-11-24 11:39:26 +01:00
|
|
|
FieldSet templateFields = fields.extractPrefixed(this.asPrefix(DmpDescriptionTemplate._descriptionTemplates));
|
|
|
|
Map<UUID, List<DescriptionTemplate>> templateItemsMap = this.collectDescriptionTemplates(templateFields, data);
|
|
|
|
|
|
|
|
FieldSet currentDescriptionTemplateFields = fields.extractPrefixed(this.asPrefix(DmpDescriptionTemplate._currentDescriptionTemplate));
|
|
|
|
Map<UUID, DescriptionTemplate> currentDescriptionTemplateItemsMap = this.collectCurrentDescriptionTemplates(currentDescriptionTemplateFields, data);
|
2023-11-06 12:31:01 +01:00
|
|
|
|
|
|
|
FieldSet dmpFields = fields.extractPrefixed(this.asPrefix(DmpDescriptionTemplate._dmp));
|
|
|
|
Map<UUID, Dmp> dmpItemsMap = this.collectDmps(dmpFields, data);
|
|
|
|
|
|
|
|
List<DmpDescriptionTemplate> models = new ArrayList<>();
|
|
|
|
for (DmpDescriptionTemplateEntity d : data) {
|
|
|
|
DmpDescriptionTemplate m = new DmpDescriptionTemplate();
|
2023-11-20 16:09:24 +01:00
|
|
|
if (fields.hasField(this.asIndexer(DmpDescriptionTemplate._id))) m.setId(d.getId());
|
|
|
|
if (fields.hasField(this.asIndexer(DmpDescriptionTemplate._sectionId))) m.setSectionId(d.getSectionId());
|
2024-01-15 17:47:34 +01:00
|
|
|
if (fields.hasField(this.asIndexer(DmpDescriptionTemplate._descriptionTemplateGroupId))) m.setDescriptionTemplateGroupId(d.getDescriptionTemplateGroupId());
|
2023-11-20 16:09:24 +01:00
|
|
|
if (fields.hasField(this.asIndexer(DmpDescriptionTemplate._createdAt))) m.setCreatedAt(d.getCreatedAt());
|
|
|
|
if (fields.hasField(this.asIndexer(DmpDescriptionTemplate._updatedAt))) m.setUpdatedAt(d.getUpdatedAt());
|
2024-03-19 17:27:30 +01:00
|
|
|
if (fields.hasField(this.asIndexer(DmpDescriptionTemplate._isActive))) m.setIsActive(d.getIsActive());
|
2023-11-20 16:09:24 +01:00
|
|
|
if (fields.hasField(this.asIndexer(DmpDescriptionTemplate._hash))) m.setHash(this.hashValue(d.getUpdatedAt()));
|
2023-11-24 11:39:26 +01:00
|
|
|
if (!templateFields.isEmpty() && templateItemsMap != null && templateItemsMap.containsKey(d.getDescriptionTemplateGroupId())) m.setDescriptionTemplates(templateItemsMap.get(d.getDescriptionTemplateGroupId()));
|
|
|
|
if (!currentDescriptionTemplateFields.isEmpty() && currentDescriptionTemplateItemsMap != null && currentDescriptionTemplateItemsMap.containsKey(d.getDescriptionTemplateGroupId())) m.setCurrentDescriptionTemplate(currentDescriptionTemplateItemsMap.get(d.getDescriptionTemplateGroupId()));
|
2023-11-20 16:09:24 +01:00
|
|
|
if (!dmpFields.isEmpty() && dmpItemsMap != null && dmpItemsMap.containsKey(d.getDmpId())) m.setDmp(dmpItemsMap.get(d.getDmpId()));
|
2023-11-06 12:31:01 +01:00
|
|
|
models.add(m);
|
|
|
|
}
|
|
|
|
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
|
|
|
|
return models;
|
|
|
|
}
|
|
|
|
|
2023-11-24 11:39:26 +01:00
|
|
|
|
|
|
|
private Map<UUID, Dmp> collectDmps(FieldSet fields, List<DmpDescriptionTemplateEntity> data) throws MyApplicationException {
|
2023-11-06 12:31:01 +01:00
|
|
|
if (fields.isEmpty() || data.isEmpty())
|
|
|
|
return null;
|
2023-11-24 11:39:26 +01:00
|
|
|
this.logger.debug("checking related - {}", Dmp.class.getSimpleName());
|
2023-11-06 12:31:01 +01:00
|
|
|
|
2023-11-24 11:39:26 +01:00
|
|
|
Map<UUID, Dmp> itemMap;
|
|
|
|
if (!fields.hasOtherField(this.asIndexer(Dmp._id))) {
|
2023-11-06 12:31:01 +01:00
|
|
|
itemMap = this.asEmpty(
|
2023-11-24 11:39:26 +01:00
|
|
|
data.stream().map(DmpDescriptionTemplateEntity::getDmpId).distinct().collect(Collectors.toList()),
|
2023-11-06 12:31:01 +01:00
|
|
|
x -> {
|
2023-11-24 11:39:26 +01:00
|
|
|
Dmp item = new Dmp();
|
2023-11-06 12:31:01 +01:00
|
|
|
item.setId(x);
|
|
|
|
return item;
|
|
|
|
},
|
2023-11-24 11:39:26 +01:00
|
|
|
Dmp::getId);
|
2023-11-06 12:31:01 +01:00
|
|
|
} else {
|
2023-11-24 11:39:26 +01:00
|
|
|
FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(Dmp._id);
|
|
|
|
DmpQuery q = this.queryFactory.query(DmpQuery.class).authorize(this.authorize).ids(data.stream().map(DmpDescriptionTemplateEntity::getDmpId).distinct().collect(Collectors.toList()));
|
|
|
|
itemMap = this.builderFactory.builder(DmpBuilder.class).authorize(this.authorize).asForeignKey(q, clone, Dmp::getId);
|
2023-11-06 12:31:01 +01:00
|
|
|
}
|
2023-11-24 11:39:26 +01:00
|
|
|
if (!fields.hasField(Dmp._id)) {
|
2023-11-06 12:31:01 +01:00
|
|
|
itemMap.values().stream().filter(Objects::nonNull).peek(x -> x.setId(null)).collect(Collectors.toList());
|
|
|
|
}
|
|
|
|
|
|
|
|
return itemMap;
|
|
|
|
}
|
|
|
|
|
2023-11-24 11:39:26 +01:00
|
|
|
private Map<UUID, List<DescriptionTemplate>> collectDescriptionTemplates(FieldSet fields, List<DmpDescriptionTemplateEntity> data) throws MyApplicationException {
|
2023-11-06 12:31:01 +01:00
|
|
|
if (fields.isEmpty() || data.isEmpty())
|
|
|
|
return null;
|
2023-11-24 11:39:26 +01:00
|
|
|
this.logger.debug("checking related - {}", DescriptionTemplate.class.getSimpleName());
|
2023-11-06 12:31:01 +01:00
|
|
|
|
2023-11-24 11:39:26 +01:00
|
|
|
Map<UUID, List<DescriptionTemplate>> itemMap;
|
|
|
|
FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(DescriptionTemplate._groupId);
|
|
|
|
DescriptionTemplateQuery query = this.queryFactory.query(DescriptionTemplateQuery.class).authorize(this.authorize).groupIds(data.stream().map(DmpDescriptionTemplateEntity::getDescriptionTemplateGroupId).distinct().collect(Collectors.toList()));
|
|
|
|
itemMap = this.builderFactory.builder(DescriptionTemplateBuilder.class).authorize(this.authorize).asMasterKey(query, clone, DescriptionTemplate::getGroupId);
|
|
|
|
|
|
|
|
if (!fields.hasField(DescriptionTemplate._groupId)) {
|
|
|
|
itemMap.values().stream().flatMap(List::stream).filter(x -> x != null && x.getGroupId() != null).peek(x -> {
|
|
|
|
x.setGroupId(null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return itemMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Map<UUID, DescriptionTemplate> collectCurrentDescriptionTemplates(FieldSet fields, List<DmpDescriptionTemplateEntity> data) throws MyApplicationException {
|
|
|
|
if (fields.isEmpty() || data.isEmpty())
|
|
|
|
return null;
|
|
|
|
this.logger.debug("checking related - {}", DescriptionTemplate.class.getSimpleName());
|
|
|
|
|
|
|
|
Map<UUID, DescriptionTemplate> itemMap;
|
|
|
|
if (!fields.hasOtherField(this.asIndexer(DescriptionTemplate._groupId))) {
|
2023-11-06 12:31:01 +01:00
|
|
|
itemMap = this.asEmpty(
|
2023-11-24 11:39:26 +01:00
|
|
|
data.stream().map(DmpDescriptionTemplateEntity::getDescriptionTemplateGroupId).distinct().collect(Collectors.toList()),
|
2023-11-06 12:31:01 +01:00
|
|
|
x -> {
|
2023-11-24 11:39:26 +01:00
|
|
|
DescriptionTemplate item = new DescriptionTemplate();
|
|
|
|
item.setGroupId(x);
|
2023-11-06 12:31:01 +01:00
|
|
|
return item;
|
|
|
|
},
|
2023-11-24 11:39:26 +01:00
|
|
|
DescriptionTemplate::getGroupId);
|
2023-11-06 12:31:01 +01:00
|
|
|
} else {
|
2024-01-19 13:28:31 +01:00
|
|
|
FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(DescriptionTemplate._id, DescriptionTemplate._groupId);
|
2023-11-24 11:39:26 +01:00
|
|
|
DescriptionTemplateQuery q = this.queryFactory.query(DescriptionTemplateQuery.class).authorize(this.authorize).versionStatuses(DescriptionTemplateVersionStatus.Current).groupIds(data.stream().map(DmpDescriptionTemplateEntity::getDescriptionTemplateGroupId).distinct().collect(Collectors.toList()));
|
|
|
|
itemMap = this.builderFactory.builder(DescriptionTemplateBuilder.class).authorize(this.authorize).asForeignKey(q, clone, DescriptionTemplate::getGroupId);
|
2023-11-06 12:31:01 +01:00
|
|
|
}
|
2023-11-24 11:39:26 +01:00
|
|
|
if (!fields.hasField(DescriptionTemplate._groupId)) {
|
|
|
|
itemMap.values().stream().filter(Objects::nonNull).peek(x -> x.setGroupId(null)).collect(Collectors.toList());
|
2023-11-06 12:31:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return itemMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|