Skip to content

Commit

Permalink
[3019] Prepare support for the onboard area on the new architecture
Browse files Browse the repository at this point in the history
Bug: #3019
Signed-off-by: Stéphane Bégaudeau <stephane.begaudeau@obeo.fr>
  • Loading branch information
sbegaudeau committed Feb 21, 2024
1 parent 3f1a6fa commit 2351632
Show file tree
Hide file tree
Showing 7 changed files with 179 additions and 0 deletions.
@@ -0,0 +1,72 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.web.application.representation.controllers;

import java.util.Objects;

import org.eclipse.sirius.components.annotations.spring.graphql.QueryDataFetcher;
import org.eclipse.sirius.components.core.RepresentationMetadata;
import org.eclipse.sirius.components.graphql.api.IDataFetcherWithFieldCoordinates;
import org.eclipse.sirius.web.application.dto.PageInfoWithCount;
import org.eclipse.sirius.web.application.representation.services.api.IRepresentationApplicationService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;

import graphql.relay.Connection;
import graphql.relay.ConnectionCursor;
import graphql.relay.DefaultConnection;
import graphql.relay.DefaultConnectionCursor;
import graphql.relay.DefaultEdge;
import graphql.relay.Edge;
import graphql.relay.Relay;
import graphql.schema.DataFetchingEnvironment;

/**
* Data fetcher for the field EditingContext#representations.
*
* @author sbegaudeau
*/
@QueryDataFetcher(type = "EditingContext", field = "representations")
public class EditingContextRepresentationsDataFetcher implements IDataFetcherWithFieldCoordinates<Connection<RepresentationMetadata>> {

private final IRepresentationApplicationService representationApplicationService;

public EditingContextRepresentationsDataFetcher(IRepresentationApplicationService representationApplicationService) {
this.representationApplicationService = Objects.requireNonNull(representationApplicationService);
}

@Override
public Connection<RepresentationMetadata> get(DataFetchingEnvironment environment) throws Exception {
String editingContextId = environment.getSource();
var representationMetadataPage = this.representationApplicationService.findAllByEditingContextId(editingContextId, PageRequest.of(0, 20));
return this.toConnection(representationMetadataPage);
}

private Connection<RepresentationMetadata> toConnection(Page<RepresentationMetadata> representationMetadataPage) {
var edges = representationMetadataPage.stream().map(representationMetadata -> {
var globalId = new Relay().toGlobalId("RepresentationMetadata", representationMetadata.getId());
var cursor = new DefaultConnectionCursor(globalId);
return (Edge<RepresentationMetadata>) new DefaultEdge<>(representationMetadata, cursor);
}).toList();

ConnectionCursor startCursor = edges.stream().findFirst()
.map(Edge::getCursor)
.orElse(null);
ConnectionCursor endCursor = null;
if (!edges.isEmpty()) {
endCursor = edges.get(edges.size() - 1).getCursor();
}
var pageInfo = new PageInfoWithCount(startCursor, endCursor, representationMetadataPage.hasPrevious(), representationMetadataPage.hasNext(), representationMetadataPage.getTotalElements());
return new DefaultConnection<>(edges, pageInfo);
}
}
Expand Up @@ -12,14 +12,22 @@
*******************************************************************************/
package org.eclipse.sirius.web.application.representation.services;

import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

import org.eclipse.sirius.components.core.RepresentationMetadata;
import org.eclipse.sirius.web.application.UUIDParser;
import org.eclipse.sirius.web.application.representation.services.api.IRepresentationApplicationService;
import org.eclipse.sirius.web.domain.boundedcontexts.project.Project;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.RepresentationData;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.services.api.IRepresentationDataSearchService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jdbc.core.mapping.AggregateReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -45,6 +53,24 @@ public Optional<RepresentationMetadata> findRepresentationMetadataById(String re
.map(this::toRepresentationMetadata);
}

@Override
public Page<RepresentationMetadata> findAllByEditingContextId(String editingContextId, Pageable pageable) {
var representationData = new UUIDParser().parse(editingContextId)
.map(AggregateReference::<Project, UUID>to)
.map(this.representationDataSearchService::findAllByProject)
.orElse(List.of())
.stream()
.sorted(Comparator.comparing(RepresentationData::getLabel))
.toList();

int startIndex = (int) pageable.getOffset() * pageable.getPageSize();
int endIndex = Math.min(((int) pageable.getOffset() + 1) * pageable.getPageSize(), representationData.size());
var representationMetadata = representationData.subList(startIndex, endIndex).stream()
.map(this::toRepresentationMetadata)
.toList();
return new PageImpl<>(representationMetadata, pageable, representationData.size());
}

private RepresentationMetadata toRepresentationMetadata(RepresentationData representationData) {
return new RepresentationMetadata(representationData.getId().toString(), representationData.getKind(), representationData.getLabel(), representationData.getDescriptionId());
}
Expand Down
Expand Up @@ -15,6 +15,8 @@
import java.util.Optional;

import org.eclipse.sirius.components.core.RepresentationMetadata;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

/**
* Used to interact with representations.
Expand All @@ -23,4 +25,6 @@
*/
public interface IRepresentationApplicationService {
Optional<RepresentationMetadata> findRepresentationMetadataById(String representationId);

Page<RepresentationMetadata> findAllByEditingContextId(String editingContextId, Pageable pageable);
}
Expand Up @@ -12,9 +12,11 @@
*******************************************************************************/
package org.eclipse.sirius.web.domain.boundedcontexts.representationdata.repositories;

import java.util.List;
import java.util.UUID;

import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.RepresentationData;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.ListCrudRepository;
import org.springframework.data.repository.ListPagingAndSortingRepository;
import org.springframework.stereotype.Repository;
Expand All @@ -26,4 +28,10 @@
*/
@Repository
public interface IRepresentationDataRepository extends ListPagingAndSortingRepository<RepresentationData, UUID>, ListCrudRepository<RepresentationData, UUID> {
@Query("""
SELECT *
FROM representation_data representationData
WHERE representationData.project_id = :projectId
""")
List<RepresentationData> findAllByProjectId(UUID projectId);
}
Expand Up @@ -17,9 +17,11 @@
import java.util.Optional;
import java.util.UUID;

import org.eclipse.sirius.web.domain.boundedcontexts.project.Project;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.RepresentationData;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.repositories.IRepresentationDataRepository;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.services.api.IRepresentationDataSearchService;
import org.springframework.data.jdbc.core.mapping.AggregateReference;
import org.springframework.stereotype.Service;

/**
Expand All @@ -41,6 +43,11 @@ public Optional<RepresentationData> findById(UUID id) {
return this.representationDataRepository.findById(id);
}

@Override
public List<RepresentationData> findAllByProject(AggregateReference<Project, UUID> project) {
return this.representationDataRepository.findAllByProjectId(project.getId());
}

@Override
public List<RepresentationData> findAllByTargetObjectId(String targetObjectId) {
return List.of();
Expand Down
Expand Up @@ -16,7 +16,9 @@
import java.util.Optional;
import java.util.UUID;

import org.eclipse.sirius.web.domain.boundedcontexts.project.Project;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.RepresentationData;
import org.springframework.data.jdbc.core.mapping.AggregateReference;

/**
* Used to find representation data.
Expand All @@ -26,5 +28,7 @@
public interface IRepresentationDataSearchService {
Optional<RepresentationData> findById(UUID id);

List<RepresentationData> findAllByProject(AggregateReference<Project, UUID> project);

List<RepresentationData> findAllByTargetObjectId(String targetObjectId);
}
Expand Up @@ -16,6 +16,7 @@

import com.jayway.jsonpath.JsonPath;

import java.util.List;
import java.util.Map;

import org.eclipse.sirius.web.AbstractIntegrationTests;
Expand Down Expand Up @@ -52,6 +53,31 @@ query getRepresentationMetadata($editingContextId: ID!, $representationId: ID!)
}
""";

private static final String GET_ALL_REPRESENTATION_METADATA_QUERY = """
query getAllRepresentationMetadata($editingContextId: ID!) {
viewer {
editingContext(editingContextId: $editingContextId) {
representations {
edges {
node {
id
label
kind
}
}
pageInfo {
hasPreviousPage
hasNextPage
startCursor
endCursor
count
}
}
}
}
}
""";

@Autowired
private IGraphQLRequestor graphQLRequestor;

Expand All @@ -75,4 +101,36 @@ public void givenRepresentationIdWhenQueryIsPerformedThenTheRepresentationMetada
String label = JsonPath.read(result, "$.data.viewer.editingContext.representation.label");
assertThat(label).isEqualTo("Portal");
}

@Test
@DisplayName("Given an editing context id, when a query is performed, then all the representation metadata are returned")
@Sql(scripts = {"/scripts/initialize.sql"}, executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)
@Sql(scripts = {"/scripts/cleanup.sql"}, executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD, config = @SqlConfig(transactionMode = SqlConfig.TransactionMode.ISOLATED))
public void givenEditingContextIdWhenQueryIsPerformedThenAllTheRepresentationMetadataAreReturned() {
Map<String, Object> variables = Map.of(
"editingContextId", TestIdentifiers.ECORE_SAMPLE_PROJECT.toString()
);
var result = this.graphQLRequestor.execute(GET_ALL_REPRESENTATION_METADATA_QUERY, variables);

boolean hasPreviousPage = JsonPath.read(result, "$.data.viewer.editingContext.representations.pageInfo.hasPreviousPage");
assertThat(hasPreviousPage).isFalse();

boolean hasNextPage = JsonPath.read(result, "$.data.viewer.editingContext.representations.pageInfo.hasNextPage");
assertThat(hasNextPage).isFalse();

String startCursor = JsonPath.read(result, "$.data.viewer.editingContext.representations.pageInfo.startCursor");
assertThat(startCursor).isNotBlank();

String endCursor = JsonPath.read(result, "$.data.viewer.editingContext.representations.pageInfo.endCursor");
assertThat(endCursor).isNotBlank();

int count = JsonPath.read(result, "$.data.viewer.editingContext.representations.pageInfo.count");
assertThat(count).isEqualTo(1);

List<String> representationIds = JsonPath.read(result, "$.data.viewer.editingContext.representations.edges[*].node.id");
assertThat(representationIds).hasSize(1);

var firstRepresentationId = representationIds.get(0);
assertThat(firstRepresentationId).isEqualTo(TestIdentifiers.EPACKAGE_PORTAL_REPRESENTATION.toString());
}
}

0 comments on commit 2351632

Please sign in to comment.