-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[1567] Add support for project templates
Bug: #1567 Signed-off-by: Axel RICHARD <axel.richard@obeo.fr>
- Loading branch information
1 parent
5a26218
commit 46a9bd3
Showing
23 changed files
with
1,632 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...irius/web/graphql/datafetchers/mutation/MutationCreateProjectFromTemplateDataFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 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.graphql.datafetchers.mutation; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import java.util.Objects; | ||
|
||
import org.eclipse.sirius.components.annotations.spring.graphql.MutationDataFetcher; | ||
import org.eclipse.sirius.components.core.api.IPayload; | ||
import org.eclipse.sirius.components.graphql.api.IDataFetcherWithFieldCoordinates; | ||
import org.eclipse.sirius.components.graphql.api.IExceptionWrapper; | ||
import org.eclipse.sirius.web.services.api.projects.CreateProjectFromTemplateInput; | ||
import org.eclipse.sirius.web.services.api.projects.IProjectService; | ||
|
||
import graphql.schema.DataFetchingEnvironment; | ||
|
||
/** | ||
* The data fetcher used to create a project from a template. | ||
* | ||
* @author pcdavid | ||
*/ | ||
@MutationDataFetcher(type = "Mutation", field = "createProjectFromTemplate") | ||
public class MutationCreateProjectFromTemplateDataFetcher implements IDataFetcherWithFieldCoordinates<IPayload> { | ||
|
||
private static final String INPUT_ARGUMENT = "input"; | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
private final IExceptionWrapper exceptionWrapper; | ||
|
||
private final IProjectService projectService; | ||
|
||
public MutationCreateProjectFromTemplateDataFetcher(ObjectMapper objectMapper, IExceptionWrapper exceptionWrapper, IProjectService projectService) { | ||
this.objectMapper = Objects.requireNonNull(objectMapper); | ||
this.exceptionWrapper = Objects.requireNonNull(exceptionWrapper); | ||
this.projectService = Objects.requireNonNull(projectService); | ||
} | ||
|
||
@Override | ||
public IPayload get(DataFetchingEnvironment environment) throws Exception { | ||
Object argument = environment.getArgument(INPUT_ARGUMENT); | ||
var input = this.objectMapper.convertValue(argument, CreateProjectFromTemplateInput.class); | ||
|
||
return this.exceptionWrapper.wrap(() -> this.projectService.createProject(input), input); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...g/eclipse/sirius/web/graphql/datafetchers/project/ProjectTemplateImageURLDataFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 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.graphql.datafetchers.project; | ||
|
||
import org.eclipse.sirius.components.annotations.spring.graphql.QueryDataFetcher; | ||
import org.eclipse.sirius.components.graphql.api.IDataFetcherWithFieldCoordinates; | ||
import org.eclipse.sirius.components.graphql.api.URLConstants; | ||
import org.eclipse.sirius.web.services.api.projects.ProjectTemplate; | ||
|
||
import graphql.schema.DataFetchingEnvironment; | ||
|
||
/** | ||
* The data fetcher used to concatenate the server image URL to the project template image path. | ||
* | ||
* @author pcdavid | ||
*/ | ||
@QueryDataFetcher(type = "ProjectTemplate", field = "imageURL") | ||
public class ProjectTemplateImageURLDataFetcher implements IDataFetcherWithFieldCoordinates<String> { | ||
|
||
@Override | ||
public String get(DataFetchingEnvironment environment) throws Exception { | ||
ProjectTemplate projectTemplate = environment.getSource(); | ||
return URLConstants.IMAGE_BASE_PATH + projectTemplate.getImageURL(); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
...ava/org/eclipse/sirius/web/graphql/datafetchers/user/UserProjectTemplatesDataFetcher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 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.graphql.datafetchers.user; | ||
|
||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.stream.Collectors; | ||
|
||
import org.eclipse.sirius.components.annotations.spring.graphql.QueryDataFetcher; | ||
import org.eclipse.sirius.components.graphql.api.IDataFetcherWithFieldCoordinates; | ||
import org.eclipse.sirius.web.graphql.pagination.PageInfoWithCount; | ||
import org.eclipse.sirius.web.services.api.projects.IProjectTemplateProvider; | ||
import org.eclipse.sirius.web.services.api.projects.ProjectTemplate; | ||
|
||
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.PageInfo; | ||
import graphql.relay.Relay; | ||
import graphql.schema.DataFetchingEnvironment; | ||
|
||
/** | ||
* The data fetcher used to retrieve all the project templates accessible to a given viewer. | ||
* | ||
* @author pcdavid | ||
*/ | ||
@QueryDataFetcher(type = "User", field = "projectTemplates") | ||
public class UserProjectTemplatesDataFetcher implements IDataFetcherWithFieldCoordinates<Connection<ProjectTemplate>> { | ||
private static final String PAGE_ARGUMENT = "page"; | ||
|
||
private static final String LIMIT_ARGUMENT = "limit"; | ||
|
||
private final List<IProjectTemplateProvider> projectTemplateProviders; | ||
|
||
public UserProjectTemplatesDataFetcher(List<IProjectTemplateProvider> projectTemplateProviders) { | ||
this.projectTemplateProviders = Objects.requireNonNull(projectTemplateProviders); | ||
} | ||
|
||
@Override | ||
public Connection<ProjectTemplate> get(DataFetchingEnvironment environment) throws Exception { | ||
int page = this.getPage(environment); | ||
int limit = this.getLimit(environment); | ||
|
||
// @formatter:off | ||
List<ProjectTemplate> allProjectTemplates = this.projectTemplateProviders.stream() | ||
.flatMap(projectTemplateProvider -> projectTemplateProvider.getProjectTemplates().stream()) | ||
.sorted(Comparator.comparing(ProjectTemplate::getLabel)) | ||
.collect(Collectors.toList()); | ||
List<Edge<ProjectTemplate>> projectTemplateEdges = allProjectTemplates.subList(page * limit, Math.min((page + 1) * limit, allProjectTemplates.size())).stream() | ||
.map(projectTemplate -> { | ||
String value = new Relay().toGlobalId("ProjectTemplate", projectTemplate.getId()); | ||
ConnectionCursor cursor = new DefaultConnectionCursor(value); | ||
return new DefaultEdge<>(projectTemplate, cursor); | ||
}) | ||
.collect(Collectors.toList()); | ||
// @formatter:on | ||
|
||
ConnectionCursor startCursor = projectTemplateEdges.stream().findFirst().map(Edge::getCursor).orElse(null); | ||
ConnectionCursor endCursor = null; | ||
if (!projectTemplateEdges.isEmpty()) { | ||
endCursor = projectTemplateEdges.get(projectTemplateEdges.size() - 1).getCursor(); | ||
} | ||
PageInfo pageInfo = new PageInfoWithCount(startCursor, endCursor, false, false, allProjectTemplates.size()); | ||
return new DefaultConnection<>(projectTemplateEdges, pageInfo); | ||
} | ||
|
||
private int getPage(DataFetchingEnvironment environment) { | ||
// @formatter:off | ||
return Optional.<Integer> ofNullable(environment.getArgument(PAGE_ARGUMENT)) | ||
.filter(page -> page.intValue() > 0) | ||
.orElse(0) | ||
.intValue(); | ||
// @formatter:on | ||
} | ||
|
||
private int getLimit(DataFetchingEnvironment environment) { | ||
// @formatter:off | ||
return Optional.<Integer> ofNullable(environment.getArgument(LIMIT_ARGUMENT)) | ||
.filter(limit -> limit.intValue() > 0) | ||
.orElse(20) | ||
.intValue(); | ||
// @formatter:on | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
...ain/java/org/eclipse/sirius/web/services/api/projects/CreateProjectFromTemplateInput.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 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.services.api.projects; | ||
|
||
import java.util.UUID; | ||
|
||
import org.eclipse.sirius.components.core.api.IInput; | ||
|
||
/** | ||
* The input object of the create project from a template mutation. | ||
* | ||
* @author pcdavid | ||
*/ | ||
public record CreateProjectFromTemplateInput(UUID id, String templateId) implements IInput { | ||
} |
65 changes: 65 additions & 0 deletions
65
...org/eclipse/sirius/web/services/api/projects/CreateProjectFromTemplateSuccessPayload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2023 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.services.api.projects; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
import org.eclipse.sirius.components.core.RepresentationMetadata; | ||
import org.eclipse.sirius.components.core.api.IPayload; | ||
|
||
/** | ||
* The payload of the create project from template mutation. | ||
* | ||
* @author pcdavid | ||
*/ | ||
public final class CreateProjectFromTemplateSuccessPayload implements IPayload { | ||
|
||
private final UUID id; | ||
|
||
private final Project project; | ||
|
||
private final RepresentationMetadata representationToOpen; | ||
|
||
public CreateProjectFromTemplateSuccessPayload(UUID id, Project project, RepresentationMetadata representationToOpen) { | ||
this.id = Objects.requireNonNull(id); | ||
this.project = Objects.requireNonNull(project); | ||
this.representationToOpen = representationToOpen; | ||
} | ||
|
||
@Override | ||
public UUID getId() { | ||
return this.id; | ||
} | ||
|
||
public Project getProject() { | ||
return this.project; | ||
} | ||
|
||
public RepresentationMetadata getRepresentationToOpen() { | ||
return this.representationToOpen; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
if (this.representationToOpen != null) { | ||
String pattern = "{0} '{'id: {1}, project: '{'id: {2}, name: {3} '}', representationToOpen: '{' id: {4}, label: {5} '}' '}'"; | ||
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.project.getId(), this.project.getName(), this.representationToOpen.getId(), | ||
this.representationToOpen.getLabel()); | ||
} else { | ||
String pattern = "{0} '{'id: {1}, project: '{'id: {2}, name: {3} '}', representationToOpen: null '}'"; | ||
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.project.getId(), this.project.getName()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.