Finalize File Transformer Builder

This commit is contained in:
George Kalampokis 2024-01-16 18:07:13 +02:00
parent 3d5be3a00a
commit 7b963858f2
4 changed files with 141 additions and 16 deletions

View File

@ -6,16 +6,24 @@ import eu.eudat.commons.XmlHandlingService;
import eu.eudat.commons.enums.IsActive;
import eu.eudat.commons.types.description.PropertyDefinitionEntity;
import eu.eudat.convention.ConventionService;
import eu.eudat.data.DescriptionEntity;
import eu.eudat.data.DescriptionTemplateEntity;
import eu.eudat.data.*;
import eu.eudat.depositinterface.models.DescriptionDepositModel;
import eu.eudat.file.transformer.enums.DescriptionStatus;
import eu.eudat.file.transformer.models.description.DescriptionFileTransformerModel;
import eu.eudat.file.transformer.models.description.DescriptionReferenceFileTransformerModel;
import eu.eudat.file.transformer.models.description.DescriptionTagFileTransformerModel;
import eu.eudat.file.transformer.models.descriptiontemplate.DescriptionTemplateFileTransformerModel;
import eu.eudat.file.transformer.models.dmp.DmpFileTransformerModel;
import eu.eudat.file.transformer.models.dmp.DmpReferenceFileTransformerModel;
import eu.eudat.file.transformer.models.reference.ReferenceFileTransformerModel;
import eu.eudat.file.transformer.models.tag.TagFileTransformerModel;
import eu.eudat.file.transformer.models.user.UserFileTransformerModel;
import eu.eudat.model.DescriptionReference;
import eu.eudat.model.DescriptionTemplate;
import eu.eudat.query.DescriptionTemplateQuery;
import eu.eudat.query.DmpQuery;
import eu.eudat.model.DmpReference;
import eu.eudat.model.Tag;
import eu.eudat.model.builder.TagBuilder;
import eu.eudat.query.*;
import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.MyApplicationException;
@ -65,6 +73,9 @@ public class DescriptionFileTransformerBuilder extends BaseFileTransformerBuilde
if (data == null || data.isEmpty()) return new ArrayList<>();
List<DescriptionTemplateFileTransformerModel> definitionMap = this.collectDescriptionTemplates(data);
Map<UUID, List<DescriptionReferenceFileTransformerModel>> referenceMap = collectReferences(data);
Map<UUID, List<DescriptionTagFileTransformerModel>> tagMap = collectTags(data);
DmpFileTransformerModel dmp = this.getDmp(data.get(0).getDmpId());
@ -84,6 +95,9 @@ public class DescriptionFileTransformerBuilder extends BaseFileTransformerBuilde
if (definitionMap != null && definitionMap.stream().anyMatch(dt -> dt.getId().equals(d.getDescriptionTemplateId())) && propertyDefinition != null && !this.conventionService.isListNullOrEmpty(propertyDefinition.getFields())) m.setDescriptionTemplate(this.builderFactory.builder(DescriptionFieldFileTransformerBuilder.class).authorize(this.authorize).setDefinition(definitionMap.stream().filter(dm -> dm.getId().equals(d.getDescriptionTemplateId())).findFirst().get()).build(propertyDefinition.getFields()).get(0));
}
if (referenceMap != null && !referenceMap.isEmpty() && referenceMap.containsKey(d.getId())) m.setDescriptionReferenceFileTransformerModels(referenceMap.get(d.getId()));
if (tagMap != null && !tagMap.isEmpty() && tagMap.containsKey(d.getId())) m.setDescriptionTagFileTransformerModels(tagMap.get(d.getId()));
m.setDmp(dmp);
models.add(new FileTransformerBuilderItemResponse<>(m, d));
}
@ -108,4 +122,49 @@ public class DescriptionFileTransformerBuilder extends BaseFileTransformerBuilde
return this.builderFactory.builder(DescriptionTemplateFileTransformerBuilder.class).authorize(authorize).build(descriptionTemplateEntities);
}
private Map<UUID, List<DescriptionReferenceFileTransformerModel>> collectReferences(List<DescriptionEntity> data) throws MyApplicationException {
if (data.isEmpty()) return null;
this.logger.debug("checking related - {}", DmpReference.class.getSimpleName());
List<DescriptionReferenceEntity> descriptionReferences = this.queryFactory.query(DescriptionReferenceQuery.class).authorize(this.authorize).descriptionIds(data.stream().map(DescriptionEntity::getId).distinct().collect(Collectors.toList())).collectAs(new BaseFieldSet().ensure(DescriptionReference._description).ensure(DescriptionReference._reference));
Map<UUID, List<DescriptionReferenceFileTransformerModel>> itemMap = new HashMap<>();
ReferenceQuery query = this.queryFactory.query(ReferenceQuery.class).authorize(this.authorize).isActive(IsActive.Active).ids(descriptionReferences.stream().map(DescriptionReferenceEntity::getReferenceId).distinct().collect(Collectors.toList()));
Map<UUID, ReferenceFileTransformerModel> referenceDepositModelMap = this.builderFactory.builder(ReferenceFileTransformerBuilder.class).authorize(this.authorize).asForeignKey(query, ReferenceEntity::getId);
if (referenceDepositModelMap == null) return null;
for (DescriptionReferenceEntity descriptionReference : descriptionReferences) {
DescriptionReferenceFileTransformerModel descriptionReferenceFileTransformerModel = new DescriptionReferenceFileTransformerModel();
descriptionReferenceFileTransformerModel.setId(descriptionReference.getId());
descriptionReferenceFileTransformerModel.setReference(referenceDepositModelMap.getOrDefault(descriptionReference.getReferenceId(), null));
DescriptionReferenceFileTransformerModel model = descriptionReferenceFileTransformerModel;
if (model == null) continue;
UUID key = descriptionReference.getDescriptionId();
if (!itemMap.containsKey(key)) itemMap.put(key, new ArrayList<>());
itemMap.get(key).add(model);
}
return itemMap;
}
private Map<UUID, List<DescriptionTagFileTransformerModel>> collectTags(List<DescriptionEntity> data) {
if (data.isEmpty()) return null;
List<DescriptionTagEntity> descriptionTags = this.queryFactory.query(DescriptionTagQuery.class).authorize(this.authorize).descriptionIds(data.stream().map(DescriptionEntity::getId).distinct().toList()).collect();
Map<UUID, List<DescriptionTagFileTransformerModel>> itemMap = new HashMap<>();
TagQuery query = this.queryFactory.query(TagQuery.class).authorize(this.authorize).isActive(IsActive.Active).ids(descriptionTags.stream().map(DescriptionTagEntity::getTagId).distinct().toList());
Map<UUID, TagFileTransformerModel> tagMap = this.builderFactory.builder(TagFileTransformerBuilder.class).asForeignKey(query, TagEntity::getId);
for (DescriptionTagEntity descriptionTag : descriptionTags) {
DescriptionTagFileTransformerModel descriptionTagFileTransformerModel = new DescriptionTagFileTransformerModel();
descriptionTagFileTransformerModel.setId(descriptionTag.getId());
descriptionTagFileTransformerModel.setTagFileTransformerModel(tagMap.getOrDefault(descriptionTag.getTagId(), null));
DescriptionTagFileTransformerModel model = descriptionTagFileTransformerModel;
if (model == null) continue;
UUID key = descriptionTag.getDescriptionId();
if (!itemMap.containsKey(key)) itemMap.put(key, new ArrayList<>());
itemMap.get(key).add(model);
}
return itemMap;
}
}

View File

@ -14,8 +14,10 @@ import eu.eudat.file.transformer.models.dmp.DmpUserFileTransformerModel;
import eu.eudat.file.transformer.models.dmpblueprint.DmpBlueprintFileTransformerModel;
import eu.eudat.file.transformer.models.dmpblueprint.definition.SectionFileTransformerModel;
import eu.eudat.file.transformer.models.entitydoi.EntityDoiFileTransformerModel;
import eu.eudat.file.transformer.models.reference.ReferenceFileTransformerModel;
import eu.eudat.file.transformer.models.user.UserFileTransformerModel;
import eu.eudat.model.*;
import eu.eudat.model.builder.DmpReferenceBuilder;
import eu.eudat.query.*;
import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.data.query.Ordering;
@ -114,7 +116,10 @@ public class DmpFileTransformerBuilder extends BaseFileTransformerBuilder<DmpFil
SectionFileTransformerModel templateSection = m.getBlueprint().getDefinitionFileTransformerModel().getSections().stream().filter(SectionFileTransformerModel::getHasTemplates).findFirst().orElse(null);
if (templateSection != null && m.getDescriptions() != null && !m.getDescriptions().isEmpty()) {
m.getDescriptions().forEach(description -> description.setSectionId(templateSection.getId()));
m.getDescriptions().forEach(description -> {
description.setSectionId(templateSection.getId());
description.setCreatedBy(m.getCreator());
});
}
models.add(new FileTransformerBuilderItemResponse<>(m, d));
@ -132,11 +137,15 @@ public class DmpFileTransformerBuilder extends BaseFileTransformerBuilder<DmpFil
Map<UUID, List<DmpReferenceFileTransformerModel>> itemMap = new HashMap<>();
ReferenceQuery query = this.queryFactory.query(ReferenceQuery.class).authorize(this.authorize).isActive(IsActive.Active).ids(dmpReferences.stream().map(DmpReferenceEntity::getReferenceId).distinct().collect(Collectors.toList()));
Map<UUID, DmpReferenceFileTransformerModel> referenceDepositModelMap = this.builderFactory.builder(ReferenceFileTransformerBuilder.class).authorize(this.authorize).asForeignKey(query, ReferenceEntity::getId);
Map<UUID, ReferenceFileTransformerModel> referenceDepositModelMap = this.builderFactory.builder(ReferenceFileTransformerBuilder.class).authorize(this.authorize).asForeignKey(query, ReferenceEntity::getId);
if (referenceDepositModelMap == null) return null;
for (DmpReferenceEntity dmpReference : dmpReferences) {
DmpReferenceFileTransformerModel model =referenceDepositModelMap.getOrDefault(dmpReference.getReferenceId(), null);
if (model == null) continue;;
DmpReferenceFileTransformerModel dmpReferenceFileTransformerModel = new DmpReferenceFileTransformerModel();
dmpReferenceFileTransformerModel.setId(dmpReference.getId());
dmpReferenceFileTransformerModel.setData(dmpReference.getData());
dmpReferenceFileTransformerModel.setReference(referenceDepositModelMap.getOrDefault(dmpReference.getReferenceId(), null));
DmpReferenceFileTransformerModel model = dmpReferenceFileTransformerModel;
if (model == null) continue;
UUID key = dmpReference.getDmpId();
if (!itemMap.containsKey(key)) itemMap.put(key, new ArrayList<>());
itemMap.get(key).add(model);

View File

@ -7,6 +7,7 @@ import eu.eudat.convention.ConventionService;
import eu.eudat.data.ReferenceEntity;
import eu.eudat.file.transformer.enums.ReferenceSourceType;
import eu.eudat.file.transformer.enums.ReferenceType;
import eu.eudat.file.transformer.models.description.DescriptionReferenceFileTransformerModel;
import eu.eudat.file.transformer.models.dmp.DmpReferenceFileTransformerModel;
import eu.eudat.file.transformer.models.reference.ReferenceFileTransformerModel;
import gr.cite.tools.data.builder.BuilderFactory;
@ -25,7 +26,7 @@ import java.util.Optional;
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class ReferenceFileTransformerBuilder extends BaseFileTransformerBuilder<DmpReferenceFileTransformerModel, ReferenceEntity> {
public class ReferenceFileTransformerBuilder extends BaseFileTransformerBuilder<ReferenceFileTransformerModel, ReferenceEntity> {
private final BuilderFactory builderFactory;
private final XmlHandlingService xmlHandlingService;
@ -46,11 +47,11 @@ public class ReferenceFileTransformerBuilder extends BaseFileTransformerBuilder<
}
@Override
protected List<FileTransformerBuilderItemResponse<DmpReferenceFileTransformerModel, ReferenceEntity>> buildInternal(List<ReferenceEntity> data) throws MyApplicationException {
protected List<FileTransformerBuilderItemResponse<ReferenceFileTransformerModel, ReferenceEntity>> buildInternal(List<ReferenceEntity> data) throws MyApplicationException {
this.logger.debug("building for {}", Optional.ofNullable(data).map(List::size).orElse(0));
if (data == null || data.isEmpty()) return new ArrayList<>();
List<FileTransformerBuilderItemResponse<DmpReferenceFileTransformerModel, ReferenceEntity>> models = new ArrayList<>();
List<FileTransformerBuilderItemResponse<ReferenceFileTransformerModel, ReferenceEntity>> models = new ArrayList<>();
for (ReferenceEntity d : data) {
ReferenceFileTransformerModel m = new ReferenceFileTransformerModel();
m.setId(d.getId());
@ -85,10 +86,7 @@ public class ReferenceFileTransformerBuilder extends BaseFileTransformerBuilder<
case Researcher -> m.setType(ReferenceType.Researcher);
default -> throw new MyApplicationException("unrecognized type " + d.getType().getValue());
}
DmpReferenceFileTransformerModel dmpReferenceFileTransformerModel = new DmpReferenceFileTransformerModel();
dmpReferenceFileTransformerModel.setReference(m);
models.add(new FileTransformerBuilderItemResponse<>(dmpReferenceFileTransformerModel, d));
models.add(new FileTransformerBuilderItemResponse<>(m, d));
}
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
return models;

View File

@ -0,0 +1,59 @@
package eu.eudat.model.builder.filetransformer;
import eu.eudat.authorization.AuthorizationFlags;
import eu.eudat.commons.XmlHandlingService;
import eu.eudat.convention.ConventionService;
import eu.eudat.data.TagEntity;
import eu.eudat.file.transformer.models.tag.TagFileTransformerModel;
import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.exception.MyApplicationException;
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.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class TagFileTransformerBuilder extends BaseFileTransformerBuilder<TagFileTransformerModel, TagEntity> {
private final BuilderFactory builderFactory;
private final XmlHandlingService xmlHandlingService;
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
@Autowired
public TagFileTransformerBuilder(
ConventionService conventionService,
BuilderFactory builderFactory, XmlHandlingService xmlHandlingService) {
super(conventionService, new LoggerService(LoggerFactory.getLogger(TagFileTransformerBuilder.class)));
this.builderFactory = builderFactory;
this.xmlHandlingService = xmlHandlingService;
}
public TagFileTransformerBuilder authorize(EnumSet<AuthorizationFlags> values) {
this.authorize = values;
return this;
}
@Override
protected List<FileTransformerBuilderItemResponse<TagFileTransformerModel, TagEntity>> buildInternal(List<TagEntity> data) throws MyApplicationException {
this.logger.debug("building for {}", Optional.ofNullable(data).map(List::size).orElse(0));
if (data == null || data.isEmpty()) return new ArrayList<>();
List<FileTransformerBuilderItemResponse<TagFileTransformerModel, TagEntity>> models = new ArrayList<>();
for (TagEntity d : data) {
TagFileTransformerModel m = new TagFileTransformerModel();
m.setId(d.getId());
m.setLabel(d.getLabel());
models.add(new FileTransformerBuilderItemResponse<>(m, d));
}
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
return models;
}
}