138 lines
7.4 KiB
Java
138 lines
7.4 KiB
Java
package org.opencdmp.elastic.elasticbuilder.nested;
|
|
|
|
import org.opencdmp.commons.enums.IsActive;
|
|
import org.opencdmp.convention.ConventionService;
|
|
import org.opencdmp.data.DmpEntity;
|
|
import org.opencdmp.data.DmpReferenceEntity;
|
|
import org.opencdmp.data.DmpUserEntity;
|
|
import org.opencdmp.data.EntityDoiEntity;
|
|
import org.opencdmp.elastic.data.nested.*;
|
|
import org.opencdmp.elastic.elasticbuilder.BaseElasticBuilder;
|
|
import org.opencdmp.model.dmpreference.DmpReference;
|
|
import org.opencdmp.query.DmpReferenceQuery;
|
|
import org.opencdmp.query.DmpUserQuery;
|
|
import org.opencdmp.query.EntityDoiQuery;
|
|
import org.opencdmp.query.ReferenceQuery;
|
|
import gr.cite.tools.data.builder.BuilderFactory;
|
|
import gr.cite.tools.data.query.QueryFactory;
|
|
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.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Component
|
|
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
|
|
public class NestedDmpElasticBuilder extends BaseElasticBuilder<NestedDmpElasticEntity, DmpEntity> {
|
|
|
|
private final QueryFactory queryFactory;
|
|
private final BuilderFactory builderFactory;
|
|
|
|
@Autowired
|
|
public NestedDmpElasticBuilder(
|
|
ConventionService conventionService, QueryFactory queryFactory, BuilderFactory builderFactory) {
|
|
super(conventionService, new LoggerService(LoggerFactory.getLogger(NestedDmpElasticBuilder.class)));
|
|
this.queryFactory = queryFactory;
|
|
this.builderFactory = builderFactory;
|
|
}
|
|
|
|
@Override
|
|
public List<NestedDmpElasticEntity> build(List<DmpEntity> data) throws MyApplicationException {
|
|
if (data == null)
|
|
return new ArrayList<>();
|
|
|
|
Map<UUID, List<NestedReferenceElasticEntity>> referenceElasticEntityMap = this.collectDmpReferences(data);
|
|
Map<UUID, List<NestedCollaboratorElasticEntity>> collaboratorElasticEntityMap = this.collectCollaborators(data);
|
|
Map<UUID, List<NestedDoiElasticEntity>> doiElasticEntityMap = this.collectDois(data);
|
|
|
|
List<NestedDmpElasticEntity> models = new ArrayList<>();
|
|
for (DmpEntity d : data) {
|
|
NestedDmpElasticEntity m = new NestedDmpElasticEntity();
|
|
m.setId(d.getId());
|
|
m.setLabel(d.getLabel());
|
|
m.setDescription(d.getDescription());
|
|
m.setVersion(d.getVersion());
|
|
m.setStatus(d.getStatus());
|
|
m.setAccessType(d.getAccessType());
|
|
m.setLanguage(d.getLanguage());
|
|
m.setBlueprintId(d.getBlueprintId());
|
|
m.setGroupId(d.getGroupId());
|
|
m.setGroupId(d.getGroupId());
|
|
m.setVersionStatus(d.getVersionStatus());
|
|
if (d.getFinalizedAt() != null) {
|
|
m.setFinalizedAt(Date.from(d.getFinalizedAt()));
|
|
}
|
|
if (referenceElasticEntityMap != null) m.setReferences(referenceElasticEntityMap.getOrDefault(d.getId(), null));
|
|
if (collaboratorElasticEntityMap != null) m.setCollaborators(collaboratorElasticEntityMap.getOrDefault(d.getId(), null));
|
|
if (doiElasticEntityMap != null) m.setDois(doiElasticEntityMap.getOrDefault(d.getId(), null));
|
|
models.add(m);
|
|
}
|
|
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
|
|
return models;
|
|
}
|
|
|
|
private Map<UUID, List<NestedReferenceElasticEntity>> collectDmpReferences(List<DmpEntity> data) throws MyApplicationException {
|
|
if (data.isEmpty()) return null;
|
|
this.logger.debug("checking related - {}", DmpReference.class.getSimpleName());
|
|
|
|
DmpReferenceQuery associationQuery = this.queryFactory.query(DmpReferenceQuery.class).dmpIds(data.stream().map(DmpEntity::getId).collect(Collectors.toList())).isActives(IsActive.Active);
|
|
List<DmpReferenceEntity> associationEntities = associationQuery.collect();
|
|
|
|
ReferenceQuery query = this.queryFactory.query(ReferenceQuery.class).isActive(IsActive.Active).ids(associationEntities.stream().map(DmpReferenceEntity::getReferenceId).distinct().collect(Collectors.toList()));
|
|
Map<UUID, NestedReferenceElasticEntity> itemMapById = this.builderFactory.builder(NestedReferenceElasticBuilder.class).asForeignKey(query, NestedReferenceElasticEntity::getId);
|
|
|
|
Map<UUID, List<NestedReferenceElasticEntity>> itemMap = new HashMap<>();
|
|
for (DmpReferenceEntity associationEntity : associationEntities){
|
|
if (!itemMap.containsKey(associationEntity.getDmpId())) itemMap.put(associationEntity.getDmpId(), new ArrayList<>());
|
|
NestedReferenceElasticEntity item = itemMapById.getOrDefault(associationEntity.getReferenceId(), null);
|
|
if (item != null) itemMap.get(associationEntity.getDmpId()).add(item);
|
|
}
|
|
|
|
return itemMap;
|
|
}
|
|
|
|
private Map<UUID, List<NestedCollaboratorElasticEntity>> collectCollaborators(List<DmpEntity> data) throws MyApplicationException {
|
|
if (data.isEmpty())
|
|
return null;
|
|
this.logger.debug("checking related - {}", DmpUserEntity.class.getSimpleName());
|
|
|
|
DmpUserQuery associationQuery = this.queryFactory.query(DmpUserQuery.class).dmpIds(data.stream().map(DmpEntity::getId).collect(Collectors.toList())).isActives(IsActive.Active);
|
|
List<DmpUserEntity> associationEntities = associationQuery.collect();
|
|
|
|
Map<UUID, NestedCollaboratorElasticEntity> itemMapById = this.builderFactory.builder(NestedCollaboratorElasticBuilder.class).asForeignKey(associationEntities, NestedCollaboratorElasticEntity::getId);
|
|
|
|
Map<UUID, List<NestedCollaboratorElasticEntity>> itemMap = new HashMap<>();
|
|
for (DmpUserEntity associationEntity : associationEntities){
|
|
if (!itemMap.containsKey(associationEntity.getId())) itemMap.put(associationEntity.getDmpId(), new ArrayList<>());
|
|
NestedCollaboratorElasticEntity item = itemMapById.getOrDefault(associationEntity.getId(), null);
|
|
if (item != null) itemMap.get(associationEntity.getDmpId()).add(item);
|
|
}
|
|
return itemMap;
|
|
}
|
|
|
|
private Map<UUID, List<NestedDoiElasticEntity>> collectDois(List<DmpEntity> data) throws MyApplicationException {
|
|
if (data.isEmpty())
|
|
return null;
|
|
this.logger.debug("checking related - {}", EntityDoiEntity.class.getSimpleName());
|
|
|
|
EntityDoiQuery associationQuery = this.queryFactory.query(EntityDoiQuery.class).entityIds(data.stream().map(DmpEntity::getId).collect(Collectors.toList())).isActive(IsActive.Active);
|
|
List<EntityDoiEntity> associationEntities = associationQuery.collect();
|
|
|
|
Map<UUID, NestedDoiElasticEntity> itemMapById = this.builderFactory.builder(NestedDoiElasticBuilder.class).asForeignKey(associationEntities, NestedDoiElasticEntity::getId);
|
|
|
|
Map<UUID, List<NestedDoiElasticEntity>> itemMap = new HashMap<>();
|
|
for (EntityDoiEntity associationEntity : associationEntities){
|
|
if (!itemMap.containsKey(associationEntity.getId())) itemMap.put(associationEntity.getEntityId(), new ArrayList<>());
|
|
NestedDoiElasticEntity item = itemMapById.getOrDefault(associationEntity.getId(), null);
|
|
if (item != null) itemMap.get(associationEntity.getEntityId()).add(item);
|
|
}
|
|
return itemMap;
|
|
}
|
|
|
|
}
|