Skip to content

Commit

Permalink
[3021] Improve the support of the Sirius Web user interface
Browse files Browse the repository at this point in the history
Bug: #3021
Signed-off-by: Stéphane Bégaudeau <stephane.begaudeau@obeo.fr>
  • Loading branch information
sbegaudeau committed Feb 20, 2024
1 parent 19b5f53 commit 11f727a
Show file tree
Hide file tree
Showing 14 changed files with 575 additions and 6 deletions.
@@ -0,0 +1,31 @@
/*******************************************************************************
* 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.editingcontext.controllers;

import org.eclipse.sirius.components.annotations.spring.graphql.QueryDataFetcher;
import org.eclipse.sirius.components.graphql.api.IDataFetcherWithFieldCoordinates;

import graphql.schema.DataFetchingEnvironment;

/**
* Data fetcher for the field EditingContext#id.
*
* @author sbegaudeau
*/
@QueryDataFetcher(type = "EditingContext", field = "id")
public class EditingContextIdDataFetcher implements IDataFetcherWithFieldCoordinates<String> {
@Override
public String get(DataFetchingEnvironment environment) throws Exception {
return environment.getSource();
}
}
@@ -0,0 +1,45 @@
/*******************************************************************************
* 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.editingcontext.controllers;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.sirius.components.annotations.spring.graphql.QueryDataFetcher;
import org.eclipse.sirius.components.graphql.api.IDataFetcherWithFieldCoordinates;
import org.eclipse.sirius.components.graphql.api.LocalContextConstants;
import org.eclipse.sirius.web.application.project.dto.ProjectDTO;

import graphql.execution.DataFetcherResult;
import graphql.schema.DataFetchingEnvironment;

/**
* Data fetcher for the field Project#currentEditingContext.
*
* @author sbegaudeau
*/
@QueryDataFetcher(type = "Project", field = "currentEditingContext")
public class ProjectCurrentEditingContextDataFetcher implements IDataFetcherWithFieldCoordinates<DataFetcherResult<String>> {
@Override
public DataFetcherResult<String> get(DataFetchingEnvironment environment) throws Exception {
ProjectDTO project = environment.getSource();
String editingContextId = project.id().toString();
Map<String, Object> localContext = new HashMap<>();
localContext.put(LocalContextConstants.EDITING_CONTEXT_ID, editingContextId);

return DataFetcherResult.<String>newResult()
.data(editingContextId)
.localContext(localContext)
.build();
}
}
@@ -0,0 +1,84 @@
/*******************************************************************************
* 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.project.controllers;

import java.util.Objects;
import java.util.Optional;

import org.eclipse.sirius.components.annotations.spring.graphql.QueryDataFetcher;
import org.eclipse.sirius.components.graphql.api.IDataFetcherWithFieldCoordinates;
import org.eclipse.sirius.web.application.dto.PageInfoWithCount;
import org.eclipse.sirius.web.application.project.dto.ProjectTemplateDTO;
import org.eclipse.sirius.web.application.project.services.api.IProjectTemplateApplicationService;
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 Viewer#projectTemplates.
*
* @author sbegaudeau
*/
@QueryDataFetcher(type = "DefaultViewer", field = "projectTemplates")
public class ViewerProjectTemplatesDataFetcher implements IDataFetcherWithFieldCoordinates<Connection<ProjectTemplateDTO>> {

private static final String PAGE_ARGUMENT = "page";

private static final String LIMIT_ARGUMENT = "limit";

private final IProjectTemplateApplicationService projectTemplateApplicationService;

public ViewerProjectTemplatesDataFetcher(IProjectTemplateApplicationService projectTemplateApplicationService) {
this.projectTemplateApplicationService = Objects.requireNonNull(projectTemplateApplicationService);
}

@Override
public Connection<ProjectTemplateDTO> get(DataFetchingEnvironment environment) throws Exception {
int page = Optional.<Integer> ofNullable(environment.getArgument(PAGE_ARGUMENT))
.filter(pageArgument -> pageArgument > 0)
.orElse(0);
int limit = Optional.<Integer> ofNullable(environment.getArgument(LIMIT_ARGUMENT))
.filter(limitArgument -> limitArgument > 0)
.orElse(20);

var pageable = PageRequest.of(page, limit);
var projectTemplatePage = this.projectTemplateApplicationService.findAll(pageable);
return this.toConnection(projectTemplatePage);
}

private Connection<ProjectTemplateDTO> toConnection(Page<ProjectTemplateDTO> projectTemplatePage) {
var edges = projectTemplatePage.stream().map(projectTemplateDTO -> {
var globalId = new Relay().toGlobalId("ProjectTemplate", projectTemplateDTO.id());
var cursor = new DefaultConnectionCursor(globalId);
return (Edge<ProjectTemplateDTO>) new DefaultEdge<>(projectTemplateDTO, 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, projectTemplatePage.hasPrevious(), projectTemplatePage.hasNext(), projectTemplatePage.getTotalElements());
return new DefaultConnection<>(edges, pageInfo);
}
}
@@ -0,0 +1,26 @@
/*******************************************************************************
* 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.project.dto;

import jakarta.validation.constraints.NotNull;

/**
* The DTO for project templates.
*
* @author sbegaudeau
*/
public record ProjectTemplateDTO(
@NotNull String id,
@NotNull String label,
@NotNull String imageURL) {
}
@@ -0,0 +1,58 @@
/*******************************************************************************
* 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.project.services;

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

import org.eclipse.sirius.web.application.project.dto.ProjectTemplateDTO;
import org.eclipse.sirius.web.application.project.services.api.IProjectTemplateApplicationService;
import org.eclipse.sirius.web.application.project.services.api.IProjectTemplateProvider;
import org.eclipse.sirius.web.application.project.services.api.ProjectTemplate;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

/**
* Used to interact with project templates.
*
* @author sbegaudeau
*/
@Service
public class ProjectTemplateApplicationService implements IProjectTemplateApplicationService {

private final List<IProjectTemplateProvider> projectTemplateProviders;

public ProjectTemplateApplicationService(List<IProjectTemplateProvider> projectTemplateProviders) {
this.projectTemplateProviders = Objects.requireNonNull(projectTemplateProviders);
}

@Override
public Page<ProjectTemplateDTO> findAll(Pageable pageable) {
var projectTemplates = this.projectTemplateProviders.stream()
.map(IProjectTemplateProvider::getProjectTemplates)
.flatMap(List::stream)
.sorted(Comparator.comparing(ProjectTemplate::label))
.toList();

int startIndex = (int) pageable.getOffset() * pageable.getPageSize();
int endIndex = Math.min(((int) pageable.getOffset() + 1) * pageable.getPageSize(), projectTemplates.size());
var projectTemplateDTOs = projectTemplates.subList(startIndex, endIndex).stream()
.map(projectTemplate -> new ProjectTemplateDTO(projectTemplate.id(), projectTemplate.label(), projectTemplate.imageURL()))
.toList();

return new PageImpl<>(projectTemplateDTOs, pageable, projectTemplates.size());
}
}
@@ -0,0 +1,26 @@
/*******************************************************************************
* 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.project.services.api;

import org.eclipse.sirius.web.application.project.dto.ProjectTemplateDTO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

/**
* Used to interact with project templates.
*
* @author sbegaudeau
*/
public interface IProjectTemplateApplicationService {
Page<ProjectTemplateDTO> findAll(Pageable pageable);
}
@@ -0,0 +1,31 @@
/*******************************************************************************
* 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.project.services.api;

import java.util.Optional;

import org.eclipse.sirius.components.core.RepresentationMetadata;
import org.eclipse.sirius.components.core.api.IEditingContext;

/**
* Initializes the contents of a new project created from a project template. The initializer can add new documents to
* the project, create initial representations, and optionally indicate such a representation to automatically open when
* the user is redirected to the newly create project.
*
* @author pcdavid
*/
public interface IProjectTemplateInitializer {
boolean canHandle(String projectTemplateId);

Optional<RepresentationMetadata> handle(String projectTemplateId, IEditingContext editingContext);
}
@@ -0,0 +1,25 @@
/*******************************************************************************
* 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.project.services.api;

import java.util.List;

/**
* Used to register project templates in the backend. This only considers templates' metadata, see
* {@link IProjectTemplateInitializer} for the definition of the actual contents of a project created from a template.
*
* @author pcdavid
*/
public interface IProjectTemplateProvider {
List<ProjectTemplate> getProjectTemplates();
}
@@ -0,0 +1,36 @@
/*******************************************************************************
* 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.project.services.api;

import java.util.List;
import java.util.Objects;

import jakarta.validation.constraints.NotNull;

/**
* Description of a project template.
*
* @author sbegaudeau
*/
public record ProjectTemplate(
@NotNull String id,
@NotNull String label,
@NotNull String imageURL,
@NotNull List<ProjectTemplateNature> natures) {
public ProjectTemplate {
Objects.requireNonNull(id);
Objects.requireNonNull(label);
Objects.requireNonNull(imageURL);
Objects.requireNonNull(natures);
}
}
@@ -0,0 +1,28 @@
/*******************************************************************************
* 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.project.services.api;

import java.util.Objects;

import jakarta.validation.constraints.NotNull;

/**
* The nature of a project template.
*
* @author sbegaudeau
*/
public record ProjectTemplateNature(@NotNull String id) {
public ProjectTemplateNature {
Objects.requireNonNull(id);
}
}

0 comments on commit 11f727a

Please sign in to comment.