Skip to content

Commit

Permalink
1576 - implementation of ingest relationships API and GET Relationshi…
Browse files Browse the repository at this point in the history
…ps API
  • Loading branch information
Vladysl committed Jan 23, 2024
1 parent b87a118 commit b23b1a4
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ public record RelationshipDto(RelationshipsPojo relationshipPojo,
DataEntityPojo sourceDataEntity,
DataEntityPojo targetDataEntity,
ErdRelationshipDetailsPojo erdRelationshipDetailsPojo,
GraphRelationshipPojo graphRelationshipPojo,
boolean isErd) {
GraphRelationshipPojo graphRelationshipPojo) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.opendatadiscovery.oddplatform.dto;

public enum RelationshipTypeDto {
ERD,
GRAPH
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.opendatadiscovery.oddplatform.api.contract.model.DatasetRelationshipType;
import org.opendatadiscovery.oddplatform.api.contract.model.PageInfo;
import org.opendatadiscovery.oddplatform.dto.RelationshipDto;
import org.opendatadiscovery.oddplatform.dto.RelationshipTypeDto;
import org.opendatadiscovery.oddplatform.utils.Page;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -38,7 +39,9 @@ public DatasetRelationship mapToDatasetRelationship(final RelationshipDto item)
.targetDatasetOddrn(item.relationshipPojo().getTargetDatasetOddrn())
.sourceDataEntityId(item.sourceDataEntity() != null ? item.sourceDataEntity().getId() : null)
.targetDataEntityId(item.targetDataEntity() != null ? item.targetDataEntity().getId() : null)
.type(item.isErd() ? DatasetRelationshipType.ERD : DatasetRelationshipType.GRAPH)
.type(RelationshipTypeDto.ERD.name().equals(item.relationshipPojo().getRelationshipType())
? DatasetRelationshipType.ERD
: DatasetRelationshipType.GRAPH)
.erdRelationship(erdRelationshipMapper.mapPojoToDetails(item.erdRelationshipDetailsPojo()))
.graphRelationship(graphRelationshipMapper.mapPojoToDetails(item.graphRelationshipPojo()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.opendatadiscovery.oddplatform.api.contract.model.RelationshipsType;
import org.opendatadiscovery.oddplatform.dto.RelationshipDto;
import org.opendatadiscovery.oddplatform.dto.RelationshipStatusDto;
import org.opendatadiscovery.oddplatform.dto.RelationshipTypeDto;
import org.opendatadiscovery.oddplatform.model.tables.pojos.DataEntityPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.ErdRelationshipDetailsPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.GraphRelationshipPojo;
Expand Down Expand Up @@ -96,12 +97,8 @@ public Flux<RelationshipDto> getRelationsByDatasetIdAndType(final Long dataEntit
);

final ResultQuery<Record> query = switch (type) {
case ERD -> joinStep.where(DSL.notExists(
select().from(GRAPH_RELATIONSHIP)
.where(GRAPH_RELATIONSHIP.RELATIONSHIP_ID.eq(RELATIONSHIPS.ID))));
case GRAPH -> joinStep.where(DSL.notExists(
select().from(ERD_RELATIONSHIP_DETAILS)
.where(ERD_RELATIONSHIP_DETAILS.RELATIONSHIP_ID.eq(RELATIONSHIPS.ID))));
case ERD -> joinStep.where(RELATIONSHIPS.RELATIONSHIP_TYPE.eq(RelationshipTypeDto.ERD.name()));
case GRAPH -> joinStep.where(RELATIONSHIPS.RELATIONSHIP_TYPE.eq(RelationshipTypeDto.GRAPH.name()));
default -> joinStep;
};

Expand All @@ -110,8 +107,7 @@ public Flux<RelationshipDto> getRelationsByDatasetIdAndType(final Long dataEntit
r.into(srcDataEntity).into(DataEntityPojo.class),
r.into(trgtDataEntity).into(DataEntityPojo.class),
r.into(ERD_RELATIONSHIP_DETAILS).into(ErdRelationshipDetailsPojo.class),
r.into(GRAPH_RELATIONSHIP).into(GraphRelationshipPojo.class),
r.get(IS_ERD_RELATION, Boolean.class))
r.into(GRAPH_RELATIONSHIP).into(GraphRelationshipPojo.class))
);
}

Expand All @@ -130,8 +126,7 @@ public Mono<RelationshipDto> getRelationshipById(final Long relationshipId) {
r.into(srcDataEntity).into(DataEntityPojo.class),
r.into(trgtDataEntity).into(DataEntityPojo.class),
r.into(ERD_RELATIONSHIP_DETAILS).into(ErdRelationshipDetailsPojo.class),
r.into(GRAPH_RELATIONSHIP).into(GraphRelationshipPojo.class),
r.get(IS_ERD_RELATION, Boolean.class))
r.into(GRAPH_RELATIONSHIP).into(GraphRelationshipPojo.class))
);
}

Expand Down Expand Up @@ -163,11 +158,6 @@ public Mono<Page<RelationshipDto>> getRelationships(final Integer page, final In
trgtDataEntity.asterisk(),
ERD_RELATIONSHIP_DETAILS.asterisk(),
GRAPH_RELATIONSHIP.asterisk())
.select(DSL.exists(
DSL.select().from(ERD_RELATIONSHIP_DETAILS)
.where(ERD_RELATIONSHIP_DETAILS.RELATIONSHIP_ID.eq(relationshipCTE.field(RELATIONSHIPS.ID))))
.as(IS_ERD_RELATION)
)
.from(relationshipCTE.getName())
.leftJoin(srcDataEntity)
.on(relationshipCTE.field(RELATIONSHIPS.SOURCE_DATASET_ODDRN).eq(srcDataEntity.field(DATA_ENTITY.ODDRN)))
Expand All @@ -180,14 +170,10 @@ public Mono<Page<RelationshipDto>> getRelationships(final Integer page, final In

final ResultQuery<Record> resultQuery = switch (type) {
case ERD -> generalQuery
.where(DSL.notExists(
select().from(GRAPH_RELATIONSHIP)
.where(GRAPH_RELATIONSHIP.RELATIONSHIP_ID.eq(relationshipCTE.field(RELATIONSHIPS.ID)))))
.where(relationshipCTE.field(RELATIONSHIPS.RELATIONSHIP_TYPE).eq(RelationshipTypeDto.ERD.name()))
.groupBy(groupByFields);
case GRAPH -> generalQuery
.where(DSL.notExists(
select().from(ERD_RELATIONSHIP_DETAILS)
.where(ERD_RELATIONSHIP_DETAILS.RELATIONSHIP_ID.eq(relationshipCTE.field(RELATIONSHIPS.ID)))))
.where(relationshipCTE.field(RELATIONSHIPS.RELATIONSHIP_TYPE).eq(RelationshipTypeDto.GRAPH.name()))
.groupBy(groupByFields);
default -> generalQuery.groupBy(groupByFields);
};
Expand All @@ -202,8 +188,7 @@ public Mono<Page<RelationshipDto>> getRelationships(final Integer page, final In
r.into(srcDataEntity).into(DataEntityPojo.class),
r.into(trgtDataEntity).into(DataEntityPojo.class),
r.into(ERD_RELATIONSHIP_DETAILS).into(ErdRelationshipDetailsPojo.class),
r.into(GRAPH_RELATIONSHIP).into(GraphRelationshipPojo.class),
r.get(IS_ERD_RELATION, Boolean.class)),
r.into(GRAPH_RELATIONSHIP).into(GraphRelationshipPojo.class)),
fetchCount(inputQuery)));
}

Expand All @@ -214,11 +199,6 @@ private SelectOnConditionStep<Record> getRelationshipGenericQuery(final Table<Da
trgtDataEntity.asterisk(),
ERD_RELATIONSHIP_DETAILS.asterisk(),
GRAPH_RELATIONSHIP.asterisk())
.select(DSL.exists(
DSL.select().from(ERD_RELATIONSHIP_DETAILS)
.where(ERD_RELATIONSHIP_DETAILS.RELATIONSHIP_ID.eq(RELATIONSHIPS.ID)))
.as(IS_ERD_RELATION)
)
.from(RELATIONSHIPS)
.leftJoin(srcDataEntity)
.on(RELATIONSHIPS.SOURCE_DATASET_ODDRN.eq(srcDataEntity.field(DATA_ENTITY.ODDRN)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.opendatadiscovery.oddplatform.dto.RelationshipStatusDto;
import org.opendatadiscovery.oddplatform.dto.RelationshipTypeDto;
import org.opendatadiscovery.oddplatform.exception.NotFoundException;
import org.opendatadiscovery.oddplatform.ingestion.contract.model.Relationship;
import org.opendatadiscovery.oddplatform.ingestion.contract.model.RelationshipList;
Expand Down Expand Up @@ -66,7 +67,7 @@ private Mono<Void> handleERDRelations(final List<RelationshipsPojo> existedPojos
final Map<RelationshipsPojo, ErdRelationshipDetailsPojo> erdToCreate = new HashMap<>();
final Map<Long, Pair<RelationshipsPojo, ErdRelationshipDetailsPojo>> erdToUpdateMap = new HashMap<>();

fillUpdateAndCreateMaps(existedPojos, erdMap, erdToUpdateMap, erdToCreate);
fillUpdateAndCreateMaps(existedPojos, erdMap, erdToUpdateMap, erdToCreate, RelationshipTypeDto.ERD);

return updateExistedERDRelations(erdToUpdateMap)
.then(
Expand Down Expand Up @@ -115,7 +116,7 @@ private Mono<Void> handleGraphRelations(final List<RelationshipsPojo> existedPoj
final Map<RelationshipsPojo, GraphRelationshipPojo> graphToCreate = new HashMap<>();
final Map<Long, Pair<RelationshipsPojo, GraphRelationshipPojo>> graphToUpdate = new HashMap<>();

fillUpdateAndCreateMaps(existedPojos, graphMap, graphToUpdate, graphToCreate);
fillUpdateAndCreateMaps(existedPojos, graphMap, graphToUpdate, graphToCreate, RelationshipTypeDto.GRAPH);

return updateExistedGraphRelations(graphToUpdate).then(Flux.fromIterable(graphToCreate.entrySet())
.flatMap(element -> relationshipsRepository.create(element.getKey())
Expand Down Expand Up @@ -164,10 +165,13 @@ private Mono<Void> removeRelationships(final List<RelationshipsPojo> existedPojo
private void fillUpdateAndCreateMaps(final List<RelationshipsPojo> existedPojos,
final Map<RelationshipsPojo, ?> ingestMap,
final Map toUpdateMap,
final Map toCreatteMap) {
final Map toCreatteMap,
final RelationshipTypeDto type) {
ingestMap.forEach((key, value) -> {
final RelationshipsPojo oldPojo = getExistedRelationByOddrn(existedPojos, key);

key.setRelationshipType(type.name());

if (oldPojo != null) {
key.setId(oldPojo.getId());
toUpdateMap.put(key.getId(), Pair.of(key, value));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CREATE TABLE IF NOT EXISTS relationships
target_dataset_oddrn varchar(512) NOT NULL,
is_manualy_created boolean,
relationship_status SMALLINT,
relationship_type varchar(256),
last_ingested_at TIMESTAMP WITHOUT TIME ZONE,
deleted_at TIMESTAMP WITHOUT TIME ZONE,

Expand Down

0 comments on commit b23b1a4

Please sign in to comment.