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 48611e3 commit b87a118
Show file tree
Hide file tree
Showing 27 changed files with 481 additions and 441 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
import org.opendatadiscovery.oddplatform.api.contract.model.DataSetStructure;
import org.opendatadiscovery.oddplatform.api.contract.model.DataSetVersionDiffList;
import org.opendatadiscovery.oddplatform.api.contract.model.DatasetRelationshipList;
import org.opendatadiscovery.oddplatform.api.contract.model.ERDRelationshipDetailsList;
import org.opendatadiscovery.oddplatform.api.contract.model.GraphRelationshipDetailsList;
import org.opendatadiscovery.oddplatform.api.contract.model.RelationshipsType;
import org.opendatadiscovery.oddplatform.service.DatasetVersionService;
import org.opendatadiscovery.oddplatform.service.ERDRelationshipsService;
import org.opendatadiscovery.oddplatform.service.GraphRelationshipsService;
import org.opendatadiscovery.oddplatform.service.RelationshipsService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
Expand All @@ -21,8 +18,6 @@
public class DatasetController implements DataSetApi {
private final DatasetVersionService datasetVersionService;
private final RelationshipsService relationshipsService;
private final ERDRelationshipsService erdRelationshipsService;
private final GraphRelationshipsService graphRelationshipsService;

@Override
public Mono<ResponseEntity<DataSetStructure>> getDataSetStructureByVersionId(
Expand Down Expand Up @@ -56,26 +51,9 @@ public Mono<ResponseEntity<DataSetVersionDiffList>> getDataSetStructureDiff(fina

@Override
public Mono<ResponseEntity<DatasetRelationshipList>> getDataSetRelationships(final Long dataEntityId,
final RelationshipsType type,
final ServerWebExchange exchange) {
return relationshipsService.getRelationsByDatasetId(dataEntityId)
.map(ResponseEntity::ok);
}

@Override
public Mono<ResponseEntity<ERDRelationshipDetailsList>>
getDataSetErdRelationshipsById(final Long dataEntityId,
final Long relationshipsId,
final ServerWebExchange exchange) {
return erdRelationshipsService.getDataSetErdRelationshipsById(dataEntityId, relationshipsId)
.map(ResponseEntity::ok);
}

@Override
public Mono<ResponseEntity<GraphRelationshipDetailsList>>
getDataSetGraphRelationshipsById(final Long dataEntityId,
final Long relationshipsId,
final ServerWebExchange exchange) {
return graphRelationshipsService.getDataSetGraphRelationshipsById(relationshipsId)
return relationshipsService.getRelationsByDatasetId(dataEntityId, type)
.map(ResponseEntity::ok);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.opendatadiscovery.oddplatform.controller;

import lombok.RequiredArgsConstructor;
import org.opendatadiscovery.oddplatform.api.contract.api.RelationshipApi;
import org.opendatadiscovery.oddplatform.api.contract.model.DatasetRelationship;
import org.opendatadiscovery.oddplatform.api.contract.model.DatasetRelationshipList;
import org.opendatadiscovery.oddplatform.api.contract.model.RelationshipsType;
import org.opendatadiscovery.oddplatform.service.RelationshipsService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@RestController
@RequiredArgsConstructor
public class RelationshipController implements RelationshipApi {
private final RelationshipsService relationshipsService;

@Override
public Mono<ResponseEntity<DatasetRelationshipList>> getRelationships(final Integer page,
final Integer size,
final RelationshipsType type,
final String query,
final ServerWebExchange exchange) {
return relationshipsService.getRelationships(page, size, type, query)
.map(ResponseEntity::ok);
}

@Override
public Mono<ResponseEntity<DatasetRelationship>> getRelationshipsById(final Long relationshipId,
final ServerWebExchange exchange) {
return relationshipsService.getRelationshipById(relationshipId)
.map(ResponseEntity::ok);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package org.opendatadiscovery.oddplatform.dto;

import org.opendatadiscovery.oddplatform.model.tables.pojos.DataEntityPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.RelationshipPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.ErdRelationshipDetailsPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.GraphRelationshipPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.RelationshipsPojo;

public record RelationshipDto(RelationshipPojo relationshipPojo,
public record RelationshipDto(RelationshipsPojo relationshipPojo,
DataEntityPojo sourceDataEntity,
DataEntityPojo targetDataEntity,
ErdRelationshipDetailsPojo erdRelationshipDetailsPojo,
GraphRelationshipPojo graphRelationshipPojo,
boolean isErd) {
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.opendatadiscovery.oddplatform.mapper;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.opendatadiscovery.oddplatform.api.contract.model.ERDRelationshipDetails;
import org.opendatadiscovery.oddplatform.api.contract.model.ERDRelationshipPairs;
import org.opendatadiscovery.oddplatform.model.tables.pojos.ErdRelationshipDetailsPojo;
import org.springframework.stereotype.Component;
import org.thymeleaf.util.ArrayUtils;

@Component
public class ErdRelationshipMapper {
public ERDRelationshipDetails mapPojoToDetails(final ErdRelationshipDetailsPojo erd) {
return new ERDRelationshipDetails()
.erdRelationshipsId(erd.getId())
.fieldsPairs(mapPairs(erd.getSourceDatasetFieldOddrn(), erd.getTargetDatasetFieldOddrn()))
.isIdentifying(erd.getIsIdentifying())
.cardinality(erd.getCardinality());
}

private List<ERDRelationshipPairs> mapPairs(final String[] source, final String[] target) {
if (ArrayUtils.isEmpty(source)) {
return null;
}

return IntStream.range(0, source.length).mapToObj(i -> new ERDRelationshipPairs()
.sourceDatasetFieldOddrn(source[i])
.targetDatasetFieldOddrn(target[i])).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,40 +6,29 @@
import java.util.List;
import java.util.Map;
import org.jooq.JSONB;
import org.mapstruct.Mapper;
import org.opendatadiscovery.oddplatform.api.contract.model.DatasetRelationship;
import org.opendatadiscovery.oddplatform.api.contract.model.GraphRelationshipAttributes;
import org.opendatadiscovery.oddplatform.api.contract.model.GraphRelationshipDetails;
import org.opendatadiscovery.oddplatform.api.contract.model.GraphRelationshipDetailsList;
import org.opendatadiscovery.oddplatform.model.tables.pojos.GraphRelationshipPojo;
import org.springframework.stereotype.Component;

@Mapper(config = MapperConfig.class,
uses = {
DateTimeMapper.class,
DataEntityMapper.class,
})
public abstract class GraphRelationshipMapper {
public GraphRelationshipDetailsList mapToDetails(final DatasetRelationship relationship,
final List<GraphRelationshipPojo> graphList) {
return new GraphRelationshipDetailsList()
.datasetRelationship(relationship)
.items(mapPojoListToDetailsList(graphList));
}

private List<GraphRelationshipDetails> mapPojoListToDetailsList(final List<GraphRelationshipPojo> graphList) {
return graphList.stream()
.map(this::mapPojoToDetails)
.toList();
}

@Component
public class GraphRelationshipMapper {
public GraphRelationshipDetails mapPojoToDetails(final GraphRelationshipPojo pojo) {
if (pojo == null) {
return null;
}

return new GraphRelationshipDetails()
.graphRelationshipsId(pojo.getId())
.isDirected(pojo.getIsDerected())
.graphRelationshipId(pojo.getId())
.isDirected(pojo.getIsDirected())
.attributes(mapGraphAttributes(pojo.getSpecificAttributes()));
}

private List<GraphRelationshipAttributes> mapGraphAttributes(final JSONB specificAttributes) {
if (specificAttributes == null) {
return List.of();
}

final ObjectReader reader = new ObjectMapper().readerFor(Map.class);

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

import java.util.List;
import java.util.stream.Collectors;
import org.mapstruct.Mapper;
import lombok.RequiredArgsConstructor;
import org.opendatadiscovery.oddplatform.api.contract.model.DatasetRelationship;
import org.opendatadiscovery.oddplatform.api.contract.model.DatasetRelationshipList;
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.utils.Page;
import org.springframework.stereotype.Component;

@RequiredArgsConstructor
@Component
public class RelationshipMapper {
private final ErdRelationshipMapper erdRelationshipMapper;
private final GraphRelationshipMapper graphRelationshipMapper;

@Mapper(config = MapperConfig.class,
uses = {
DateTimeMapper.class,
DataEntityMapper.class,
})
public abstract class RelationshipMapper {
public DatasetRelationshipList mapListToRelationshipList(final List<RelationshipDto> relationshipDtos) {
return new DatasetRelationshipList()
.items(mapToRelationshipList(relationshipDtos))
Expand All @@ -36,6 +38,14 @@ 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(item.isErd() ? DatasetRelationshipType.ERD : DatasetRelationshipType.GRAPH)
.erdRelationship(erdRelationshipMapper.mapPojoToDetails(item.erdRelationshipDetailsPojo()))
.graphRelationship(graphRelationshipMapper.mapPojoToDetails(item.graphRelationshipPojo()));
}

public DatasetRelationshipList mapListToRelationshipPage(final Page<RelationshipDto> relationshipDtoPage) {
return new DatasetRelationshipList()
.items(mapToRelationshipList(relationshipDtoPage.getData()))
.pageInfo(new PageInfo().total((relationshipDtoPage.getTotal())).hasNext(relationshipDtoPage.isHasNext()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import java.util.Map;
import org.opendatadiscovery.oddplatform.ingestion.contract.model.ERDRelationship;
import org.opendatadiscovery.oddplatform.ingestion.contract.model.Relationship;
import org.opendatadiscovery.oddplatform.model.tables.pojos.ErdRelationshipPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.RelationshipPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.ErdRelationshipDetailsPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.RelationshipsPojo;

public interface DatasetERDRelationIngestionMapper {
Map<RelationshipPojo, List<ErdRelationshipPojo>> mapERDRelations(final List<Relationship> erdRelationship,
final Long dataSourceId);
Map<RelationshipsPojo, ErdRelationshipDetailsPojo> mapERDRelations(final List<Relationship> erdRelationship,
final Long dataSourceId);

List<ErdRelationshipPojo> mapERDRelation(ERDRelationship field);
ErdRelationshipDetailsPojo mapERDRelation(ERDRelationship field);
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package org.opendatadiscovery.oddplatform.mapper.ingestion;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.opendatadiscovery.oddplatform.ingestion.contract.model.ERDRelationship;
import org.opendatadiscovery.oddplatform.ingestion.contract.model.Relationship;
import org.opendatadiscovery.oddplatform.model.tables.pojos.ErdRelationshipPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.RelationshipPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.ErdRelationshipDetailsPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.RelationshipsPojo;
import org.springframework.stereotype.Component;

@Component
Expand All @@ -18,30 +17,25 @@ public class DatasetERDRelationIngestionMapperImpl implements DatasetERDRelation
private final DatasetRelationIngestionMapper relationIngestionMapper;

@Override
public Map<RelationshipPojo, List<ErdRelationshipPojo>> mapERDRelations(final List<Relationship> relationships,
final Long dataSourceId) {
public Map<RelationshipsPojo, ErdRelationshipDetailsPojo>
mapERDRelations(final List<Relationship> relationships,
final Long dataSourceId) {
if (CollectionUtils.isEmpty(relationships)) {
return Map.of();
}

return relationships.stream()
.filter(item -> item.getErdRelationship() != null)
.collect(Collectors.toMap(item -> relationIngestionMapper.mapToPojo(item, dataSourceId),
relationship -> new ArrayList<>(mapERDRelation(relationship.getErdRelationship())), (a, b) -> b));
relationship -> mapERDRelation(relationship.getErdRelationship()), (a, b) -> b));
}

@Override
public List<ErdRelationshipPojo> mapERDRelation(final ERDRelationship erd) {
final List<ErdRelationshipPojo> erdRelationshipPojos = new ArrayList<>();

for (int i = 0; i < erd.getSourceDatasetFieldOddrnsList().size(); i++) {
erdRelationshipPojos.add(new ErdRelationshipPojo()
.setSourceDatasetFieldOddrn(erd.getSourceDatasetFieldOddrnsList().get(i))
.setTargetDatasetFieldOddrn(erd.getTargetDatasetFieldOddrnsList().get(i))
.setCardinality(erd.getCardinality().getValue())
.setIsIdentifying(erd.getIsIdentifying()));
}

return erdRelationshipPojos;
public ErdRelationshipDetailsPojo mapERDRelation(final ERDRelationship erd) {
return new ErdRelationshipDetailsPojo()
.setSourceDatasetFieldOddrn(erd.getSourceDatasetFieldOddrnsList().toArray(String[]::new))
.setTargetDatasetFieldOddrn(erd.getTargetDatasetFieldOddrnsList().toArray(String[]::new))
.setCardinality(erd.getCardinality().getValue())
.setIsIdentifying(erd.getIsIdentifying());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import org.opendatadiscovery.oddplatform.ingestion.contract.model.GraphRelationship;
import org.opendatadiscovery.oddplatform.ingestion.contract.model.Relationship;
import org.opendatadiscovery.oddplatform.model.tables.pojos.GraphRelationshipPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.RelationshipPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.RelationshipsPojo;

public interface DatasetGraphRelationIngestionMapper {
Map<RelationshipPojo, GraphRelationshipPojo> mapGraphRelations(final List<Relationship> relationships,
Map<RelationshipsPojo, GraphRelationshipPojo> mapGraphRelations(final List<Relationship> relationships,
final Long dataSourceId);

GraphRelationshipPojo mapGraphRelation(final GraphRelationship field);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import org.opendatadiscovery.oddplatform.ingestion.contract.model.GraphRelationship;
import org.opendatadiscovery.oddplatform.ingestion.contract.model.Relationship;
import org.opendatadiscovery.oddplatform.model.tables.pojos.GraphRelationshipPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.RelationshipPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.RelationshipsPojo;
import org.opendatadiscovery.oddplatform.utils.JSONSerDeUtils;
import org.springframework.stereotype.Component;

Expand All @@ -19,7 +19,7 @@ public class DatasetGraphRelationIngestionMapperImpl implements DatasetGraphRela
private final DatasetRelationIngestionMapper relationIngestionMapper;

@Override
public Map<RelationshipPojo, GraphRelationshipPojo> mapGraphRelations(final List<Relationship> relationships,
public Map<RelationshipsPojo, GraphRelationshipPojo> mapGraphRelations(final List<Relationship> relationships,
final Long dataSourceId) {
if (CollectionUtils.isEmpty(relationships)) {
return Map.of();
Expand All @@ -34,7 +34,7 @@ public Map<RelationshipPojo, GraphRelationshipPojo> mapGraphRelations(final List
@Override
public GraphRelationshipPojo mapGraphRelation(final GraphRelationship graphRelationship) {
return new GraphRelationshipPojo()
.setIsDerected(graphRelationship.getIsDirected())
.setIsDirected(graphRelationship.getIsDirected())
.setSpecificAttributes(JSONB.valueOf(JSONSerDeUtils.serializeJson(graphRelationship.getAttributes())));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package org.opendatadiscovery.oddplatform.mapper.ingestion;

import org.opendatadiscovery.oddplatform.ingestion.contract.model.Relationship;
import org.opendatadiscovery.oddplatform.model.tables.pojos.RelationshipPojo;
import org.opendatadiscovery.oddplatform.model.tables.pojos.RelationshipsPojo;

public interface DatasetRelationIngestionMapper {
RelationshipPojo mapToPojo(final Relationship relationship, final Long dataSourceId);
RelationshipsPojo mapToPojo(final Relationship relationship, final Long dataSourceId);
}
Loading

0 comments on commit b87a118

Please sign in to comment.