Skip to content

Commit

Permalink
[4383] Add support for representation duplication
Browse files Browse the repository at this point in the history
Bug: #4383
Signed-off-by: Florian ROUËNÉ <[email protected]>
  • Loading branch information
frouene committed Jan 7, 2025
1 parent fe00ae3 commit 7dca70e
Show file tree
Hide file tree
Showing 19 changed files with 695 additions and 9 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ This may have some consequences for downstream applications which are embedding
+ Added by default to all sirius-web diagrams
- https://github.com/eclipse-sirius/sirius-web/issues/4346[#4346] [query] Add support for a query view.
Specifiers can contribute dedicated AQL services for this feature using implementations of `IInterpreterJavaServiceProvider`.
- https://github.com/eclipse-sirius/sirius-web/issues/3740[#3740] [sirius-web] Add support for object duplication from explorer
- https://github.com/eclipse-sirius/sirius-web/issues/3740[#3740] [sirius-web] Add support for semantic object duplication from explorer
- https://github.com/eclipse-sirius/sirius-web/issues/4383[#4383] [sirius-web] Add support for representation duplication from explorer

=== Improvements

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021, 2024 Obeo.
* Copyright (c) 2021, 2025 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
Expand All @@ -25,6 +25,8 @@ public interface IRepresentationPersistenceService {

void save(ICause cause, IEditingContext editingContext, IRepresentation representation);

void save(ICause cause, IEditingContext editingContext, String representationId, String representationContent, String representationKind);

/**
* Empty implementation, used for mocks in unit tests.
*
Expand All @@ -37,6 +39,12 @@ public void save(ICause cause, IEditingContext editingContext, IRepresentation r
// Do nothing
}

@Override
public void save(ICause cause, IEditingContext editingContext, String representationId, String representationContent, String representationKind) {
//Do nothing
}


}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2021, 2024 Obeo.
* Copyright (c) 2021, 2025 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
Expand All @@ -24,6 +24,9 @@
* @author sbegaudeau
*/
public interface IRepresentationSearchService {

Optional<IRepresentation> findById(IEditingContext editingContext, String representationId);

<T extends IRepresentation> Optional<T> findById(IEditingContext editingContext, String representationId, Class<T> representationClass);

boolean existByIdAndKind(String representationId, List<String> kinds);
Expand All @@ -35,6 +38,11 @@ public interface IRepresentationSearchService {
*/
class NoOp implements IRepresentationSearchService {

@Override
public Optional<IRepresentation> findById(IEditingContext editingContext, String representationId) {
return Optional.empty();
}

@Override
public <T extends IRepresentation> Optional<T> findById(IEditingContext editingContext, String representationId, Class<T> representationClass) {
return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* Copyright (c) 2024, 2025 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
Expand Down Expand Up @@ -40,6 +40,11 @@ public class PortalServicesTests {
private static final IEditingContext NOOP_EDITING_CONTEXT = new IEditingContext.NoOp();

private static final IRepresentationSearchService NOOP_SEARCH_SERVICE = new IRepresentationSearchService() {
@Override
public Optional<IRepresentation> findById(IEditingContext editingContext, String representationId) {
return Optional.empty();
}

@Override
public <T extends IRepresentation> Optional<T> findById(IEditingContext editingContext, String representationId, Class<T> representationClass) {
return Optional.empty();
Expand Down Expand Up @@ -136,6 +141,11 @@ public void testPreventDirectLoop() {
.build();

IRepresentationSearchService mockSearchService = new IRepresentationSearchService() {
@Override
public Optional<IRepresentation> findById(IEditingContext editingContext, String representationId) {
return Optional.empty();
}

@Override
public <T extends IRepresentation> Optional<T> findById(IEditingContext editingContext, String representationId, Class<T> representationClass) {
return Optional.of(representationClass.cast(portal));
Expand Down Expand Up @@ -167,6 +177,11 @@ public void testPreventIndirectLoop() {
portalsRepository.add(portal2);

IRepresentationSearchService mockSearchService = new IRepresentationSearchService() {
@Override
public Optional<IRepresentation> findById(IEditingContext editingContext, String representationId) {
return Optional.empty();
}

@Override
public <T extends IRepresentation> Optional<T> findById(IEditingContext editingContext, String representationId, Class<T> representationClass) {
return portalsRepository.stream().filter(portal -> portal.getId().equals(representationId)).findFirst().map(representationClass::cast);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* Copyright (c) 2024, 2025 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
Expand Down Expand Up @@ -85,6 +85,15 @@ public void save(ICause cause, IEditingContext editingContext, IRepresentation r
}
}

@Override
@Transactional
public void save(ICause cause, IEditingContext editingContext, String representationId, String representationContent, String representationKind) {
new UUIDParser().parse(representationId).ifPresent(representationUUID -> {
var migrationData = this.getInitialMigrationData(representationKind);
this.representationContentCreationService.create(cause, representationUUID, representationContent, migrationData.lastMigrationPerformed(), migrationData.migrationVersion());
});
}

private String toString(IRepresentation representation) {
String content = "";
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2024 Obeo.
* Copyright (c) 2024, 2025 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
Expand Down Expand Up @@ -58,6 +58,13 @@ public RepresentationSearchService(IRepresentationMetadataSearchService represen
this.objectMapper = Objects.requireNonNull(objectMapper);
}

@Override
public Optional<IRepresentation> findById(IEditingContext editingContext, String representationId) {
return new UUIDParser().parse(representationId)
.flatMap(this.representationMetadataSearchService::findMetadataById)
.flatMap(this::getRepresentation);
}

@Override
public <T extends IRepresentation> Optional<T> findById(IEditingContext editingContext, String representationId, Class<T> representationClass) {
return new UUIDParser().parse(representationId)
Expand All @@ -75,7 +82,7 @@ private Optional<IRepresentation> getRepresentation(RepresentationMetadata repre

@Override
public boolean existByIdAndKind(String representationId, List<String> kinds) {
Optional<UUID> uuid = new UUIDParser().parse(representationId);
Optional<UUID> uuid = new UUIDParser().parse(representationId);
return uuid.filter(value -> this.representationMetadataSearchService.existsByIdAndKind(value, kinds)).isPresent();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************************************
* Copyright (c) 2025 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.views.explorer.controllers;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Objects;
import java.util.concurrent.CompletableFuture;

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.IEditingContextDispatcher;
import org.eclipse.sirius.web.application.views.explorer.dto.DuplicateRepresentationInput;

import graphql.schema.DataFetchingEnvironment;

/**
* Data fetcher for the field Mutation#duplicateRepresentation.
*
* @author frouene
*/
@MutationDataFetcher(type = "Mutation", field = "duplicateRepresentation")
public class MutationDuplicateRepresentationDataFetcher implements IDataFetcherWithFieldCoordinates<CompletableFuture<IPayload>> {

private static final String INPUT_ARGUMENT = "input";

private final ObjectMapper objectMapper;

private final IEditingContextDispatcher editingContextDispatcher;

public MutationDuplicateRepresentationDataFetcher(ObjectMapper objectMapper, IEditingContextDispatcher editingContextDispatcher) {
this.objectMapper = Objects.requireNonNull(objectMapper);
this.editingContextDispatcher = Objects.requireNonNull(editingContextDispatcher);
}

@Override
public CompletableFuture<IPayload> get(DataFetchingEnvironment environment) throws Exception {
Object argument = environment.getArgument(INPUT_ARGUMENT);
var input = this.objectMapper.convertValue(argument, DuplicateRepresentationInput.class);

return this.editingContextDispatcher.dispatchMutation(input.editingContextId(), input).toFuture();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*******************************************************************************
* Copyright (c) 2025 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.views.explorer.dto;

import java.util.UUID;

import org.eclipse.sirius.components.core.api.IInput;

import jakarta.validation.constraints.NotNull;

/**
* The input object of the duplicate representation mutation.
*
* @author frouene
*/
public record DuplicateRepresentationInput(@NotNull UUID id, @NotNull String editingContextId, @NotNull String representationId) implements IInput {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*******************************************************************************
* Copyright (c) 2025 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.views.explorer.dto;

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

import org.eclipse.sirius.components.core.RepresentationMetadata;
import org.eclipse.sirius.components.core.api.IPayload;
import org.eclipse.sirius.components.representations.Message;

import jakarta.validation.constraints.NotNull;

/**
* The payload of the duplicate representation mutation.
*
* @author frouene
*/
public record DuplicateRepresentationSuccessPayload(@NotNull UUID id, @NotNull RepresentationMetadata representationMetadata, @NotNull List<Message> messages) implements IPayload {

}
Loading

0 comments on commit 7dca70e

Please sign in to comment.