Skip to content

Commit

Permalink
[3641] Separate even more the use of representation metadata from its…
Browse files Browse the repository at this point in the history
… content

Bug: eclipse-sirius#3641
Signed-off-by: Stéphane Bégaudeau <[email protected]>
  • Loading branch information
sbegaudeau committed Jun 25, 2024
1 parent c108b60 commit 449ad0a
Show file tree
Hide file tree
Showing 15 changed files with 228 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
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.projections.RepresentationDataMetadataOnly;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.services.api.IRepresentationDataSearchService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
Expand All @@ -49,10 +49,10 @@ public RepresentationApplicationService(IRepresentationDataSearchService represe
public Page<RepresentationMetadata> findAllByEditingContextId(String editingContextId, Pageable pageable) {
var representationData = new UUIDParser().parse(editingContextId)
.map(AggregateReference::<Project, UUID>to)
.map(this.representationDataSearchService::findAllByProject)
.map(this.representationDataSearchService::findAllMetadataByProject)
.orElse(List.of())
.stream()
.sorted(Comparator.comparing(RepresentationData::getLabel))
.sorted(Comparator.comparing(RepresentationDataMetadataOnly::label))
.toList();

int startIndex = (int) pageable.getOffset() * pageable.getPageSize();
Expand All @@ -63,8 +63,8 @@ public Page<RepresentationMetadata> findAllByEditingContextId(String editingCont
return new PageImpl<>(representationMetadata, pageable, representationData.size());
}

private RepresentationMetadata toRepresentationMetadata(RepresentationData representationData) {
return new RepresentationMetadata(representationData.getId().toString(), representationData.getKind(), representationData.getLabel(), representationData.getDescriptionId());
private RepresentationMetadata toRepresentationMetadata(RepresentationDataMetadataOnly representationData) {
return new RepresentationMetadata(representationData.id().toString(), representationData.kind(), representationData.label(), representationData.descriptionId());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public Optional<RepresentationMetadata> findByRepresentation(IRepresentation rep

@Override
public List<RepresentationMetadata> findAllByTargetObjectId(IEditingContext editingContext, String targetObjectId) {
return this.representationDataSearchService.findAllByTargetObjectId(targetObjectId)
return this.representationDataSearchService.findAllMetadataByTargetObjectId(targetObjectId)
.stream()
.map(representation -> new RepresentationMetadata(representation.getId().toString(), representation.getKind(), representation.getLabel(), representation.getDescriptionId()))
.map(representation -> new RepresentationMetadata(representation.id().toString(), representation.kind(), representation.label(), representation.descriptionId()))
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.representations.IRepresentation;
import org.eclipse.sirius.web.application.UUIDParser;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.RepresentationData;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.projections.RepresentationDataContentOnly;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.services.api.IRepresentationDataSearchService;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.services.api.IRepresentationDataUpdateService;
import org.slf4j.Logger;
Expand Down Expand Up @@ -60,7 +60,7 @@ public RepresentationSearchService(IRepresentationDataSearchService representati
@Override
public <T extends IRepresentation> Optional<T> findById(IEditingContext editingContext, String representationId, Class<T> representationClass) {
return new UUIDParser().parse(representationId)
.flatMap(this.representationDataSearchService::findById)
.flatMap(this.representationDataSearchService::findContentById)
.map(this::migratedContent)
.flatMap(this::toRepresentation)
.filter(representationClass::isInstance)
Expand All @@ -80,11 +80,11 @@ private Optional<IRepresentation> toRepresentation(String content) {
return optionalRepresentation;
}

private String migratedContent(RepresentationData representationData) {
private String migratedContent(RepresentationDataContentOnly representationData) {
List<IRepresentationMigrationParticipant> applicableParticipants = this.getApplicableMigrationParticipants(representationData);
if (!applicableParticipants.isEmpty()) {
try {
JsonNode rootJsonNode = this.objectMapper.readTree(representationData.getContent());
JsonNode rootJsonNode = this.objectMapper.readTree(representationData.content());
ObjectNode rootObjectNode = (ObjectNode) rootJsonNode;
var migrationService = new RepresentationMigrationService(applicableParticipants, rootObjectNode);
migrationService.parseProperties(rootObjectNode, this.objectMapper);
Expand All @@ -93,13 +93,13 @@ private String migratedContent(RepresentationData representationData) {
this.logger.warn(exception.getMessage());
}
}
return representationData.getContent();
return representationData.content();
}


private List<IRepresentationMigrationParticipant> getApplicableMigrationParticipants(RepresentationData representationData) {
var migrationVersion = representationData.getMigrationVersion();
var kind = representationData.getKind();
private List<IRepresentationMigrationParticipant> getApplicableMigrationParticipants(RepresentationDataContentOnly representationData) {
var migrationVersion = representationData.migrationVersion();
var kind = representationData.kind();

return this.migrationParticipants.stream()
.filter(migrationParticipant -> Objects.equals(migrationParticipant.getKind(), kind))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import org.eclipse.sirius.components.core.api.IObjectSearchService;
import org.eclipse.sirius.web.application.UUIDParser;
import org.eclipse.sirius.web.application.views.explorer.services.api.IExplorerNavigationService;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.RepresentationData;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.projections.RepresentationDataMetadataOnly;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.services.api.IRepresentationDataSearchService;
import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -52,13 +52,13 @@ public ExplorerNavigationService(IIdentityService identityService, IObjectSearch
public List<String> getAncestors(IEditingContext editingContext, String treeItemId) {
List<String> ancestorsIds = new ArrayList<>();

var optionalRepresentation = new UUIDParser().parse(treeItemId).flatMap(this.representationDataSearchService::findById);
var optionalRepresentationMetadata = new UUIDParser().parse(treeItemId).flatMap(this.representationDataSearchService::findMetadataById);
var optionalSemanticObject = this.objectSearchService.getObject(editingContext, treeItemId);

Optional<Object> optionalObject = Optional.empty();
if (optionalRepresentation.isPresent()) {
if (optionalRepresentationMetadata.isPresent()) {
// The first parent of a representation item is the item for its targetObject.
optionalObject = optionalRepresentation.map(RepresentationData::getTargetObjectId)
optionalObject = optionalRepresentationMetadata.map(RepresentationDataMetadataOnly::targetObjectId)
.flatMap(objectId -> this.objectSearchService.getObject(editingContext, objectId));
} else if (optionalSemanticObject.isPresent()) {
// The first parent of a semantic object item is the item for its actual container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,41 @@ public IResult<Void> renameProject(UUID projectId, String newName) {
result = new Success<>(null);
}

return result;
}

@Override
public IResult<Void> addNature(UUID projectId, String natureName) {
IResult<Void> result = null;

var optionalProject = this.projectRepository.findById(projectId);
if (optionalProject.isEmpty()) {
result = new Failure<>(this.messageService.notFound());
} else {
var project = optionalProject.get();
project.addNature(natureName);

this.projectRepository.save(project);
result = new Success<>(null);
}

return result;
}

@Override
public IResult<Void> removeNature(UUID projectId, String natureName) {
IResult<Void> result = null;

var optionalProject = this.projectRepository.findById(projectId);
if (optionalProject.isEmpty()) {
result = new Failure<>(this.messageService.notFound());
} else {
var project = optionalProject.get();
project.removeNature(natureName);

this.projectRepository.save(project);
result = new Success<>(null);
}

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@
*/
public interface IProjectUpdateService {
IResult<Void> renameProject(UUID projectId, String newName);

IResult<Void> addNature(UUID projectId, String natureName);

IResult<Void> removeNature(UUID projectId, String natureName);
}
Original file line number Diff line number Diff line change
@@ -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.domain.boundedcontexts.representationdata.projections;

/**
* Projection used to retrieve only the content and its associated data from the representation data.
*
* @author sbegaudeau
*/
public record RepresentationDataContentOnly(
String kind,
String content,
String lastMigrationPerformed,
String migrationVersion) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.UUID;

import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.RepresentationData;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.projections.RepresentationDataContentOnly;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.projections.RepresentationDataMetadataOnly;
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.ListCrudRepository;
Expand All @@ -31,25 +32,32 @@
@Repository
public interface IRepresentationDataRepository extends ListPagingAndSortingRepository<RepresentationData, UUID>, ListCrudRepository<RepresentationData, UUID> {
@Query("""
SELECT id, label, kind, target_object_id, description_id
FROM representation_data representationData
WHERE representationData.id = :id
""")
SELECT id, label, kind, target_object_id, description_id
FROM representation_data representationData
WHERE representationData.id = :id
""")
Optional<RepresentationDataMetadataOnly> findMetadataById(UUID id);

@Query("""
SELECT *
SELECT id, label, kind, target_object_id, description_id
FROM representation_data representationData
WHERE representationData.project_id = :projectId
""")
List<RepresentationData> findAllByProjectId(UUID projectId);
List<RepresentationDataMetadataOnly> findAllMetadataByProjectId(UUID projectId);

@Query("""
SELECT id, label, kind, target_object_id, description_id
FROM representation_data representationData
WHERE representationData.project_id = :projectId
WHERE representationData.target_object_id = :targetObjectId
""")
List<RepresentationDataMetadataOnly> findAllMetadataByProjectId(UUID projectId);
List<RepresentationDataMetadataOnly> findAllMetadataByTargetObjectId(String targetObjectId);

@Query("""
SELECT kind, content, last_migration_performed, migration_version
FROM representation_data representationData
WHERE representationData.id = :id
""")
Optional<RepresentationDataContentOnly> findContentById(UUID id);

@Query("""
SELECT representationData.project_id
Expand All @@ -64,11 +72,4 @@ SELECT CASE WHEN COUNT(*) > 0 THEN true ELSE false END
WHERE representationData.target_object_id = :targetObjectId
""")
boolean existAnyRepresentationForTargetObjectId(String targetObjectId);

@Query("""
SELECT *
FROM representation_data representationData
WHERE representationData.target_object_id = :targetObjectId
""")
List<RepresentationData> findAllByTargetObjectId(String targetObjectId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
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.projections.RepresentationDataContentOnly;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.projections.RepresentationDataMetadataOnly;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.repositories.IRepresentationDataRepository;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.services.api.IRepresentationDataSearchService;
Expand All @@ -45,33 +45,28 @@ public boolean existsById(UUID id) {
}

@Override
public Optional<RepresentationData> findById(UUID id) {
return this.representationDataRepository.findById(id);
public Optional<RepresentationDataContentOnly> findContentById(UUID id) {
return this.representationDataRepository.findContentById(id);
}

@Override
public Optional<RepresentationDataMetadataOnly> findMetadataById(UUID id) {
return this.representationDataRepository.findMetadataById(id);
}

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

@Override
public List<RepresentationDataMetadataOnly> findAllMetadataByProject(AggregateReference<Project, UUID> project) {
return this.representationDataRepository.findAllMetadataByProjectId(project.getId());
}

@Override
public boolean existAnyRepresentationForTargetObjectId(String targetObjectId) {
return this.representationDataRepository.existAnyRepresentationForTargetObjectId(targetObjectId);
public List<RepresentationDataMetadataOnly> findAllMetadataByTargetObjectId(String targetObjectId) {
return this.representationDataRepository.findAllMetadataByTargetObjectId(targetObjectId);
}

@Override
public List<RepresentationData> findAllByTargetObjectId(String targetObjectId) {
return this.representationDataRepository.findAllByTargetObjectId(targetObjectId);
public boolean existAnyRepresentationForTargetObjectId(String targetObjectId) {
return this.representationDataRepository.existAnyRepresentationForTargetObjectId(targetObjectId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
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.projections.RepresentationDataContentOnly;
import org.eclipse.sirius.web.domain.boundedcontexts.representationdata.projections.RepresentationDataMetadataOnly;
import org.springframework.data.jdbc.core.mapping.AggregateReference;

Expand All @@ -30,17 +30,15 @@ public interface IRepresentationDataSearchService {

boolean existsById(UUID id);

Optional<RepresentationData> findById(UUID id);
Optional<RepresentationDataContentOnly> findContentById(UUID id);

Optional<RepresentationDataMetadataOnly> findMetadataById(UUID id);

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

List<RepresentationDataMetadataOnly> findAllMetadataByProject(AggregateReference<Project, UUID> project);

boolean existAnyRepresentationForTargetObjectId(String targetObjectId);
List<RepresentationDataMetadataOnly> findAllMetadataByTargetObjectId(String targetObjectId);

List<RepresentationData> findAllByTargetObjectId(String targetObjectId);
boolean existAnyRepresentationForTargetObjectId(String targetObjectId);

Optional<AggregateReference<Project, UUID>> findProjectByRepresentationId(UUID representationId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public void givenRepresentationToCreateWhenMutationIsPerformedThenTheRepresentat
var event = this.domainEventCollector.getDomainEvents().get(0);
assertThat(event).isInstanceOf(RepresentationDataCreatedEvent.class);

assertThat(this.representationDataSearchService.findById(UUID.fromString(representationId))).isPresent();
assertThat(this.representationDataSearchService.existsById(UUID.fromString(representationId))).isTrue();
}

@Test
Expand Down Expand Up @@ -159,7 +159,7 @@ public void givenRepresentationToRenameWhenMutationIsPerformedThenTheRepresentat
public void givenRepresentationToDeleteWhenMutationIsPerformedThenTheRepresentationHasBeenDeleted() {
this.givenCommittedTransaction.commit();

assertThat(this.representationDataSearchService.findById(TestIdentifiers.EPACKAGE_PORTAL_REPRESENTATION)).isPresent();
assertThat(this.representationDataSearchService.existsById(TestIdentifiers.EPACKAGE_PORTAL_REPRESENTATION)).isTrue();

var input = new DeleteRepresentationInput(UUID.randomUUID(), TestIdentifiers.EPACKAGE_PORTAL_REPRESENTATION.toString());
var result = this.deleteRepresentationMutationRunner.run(input);
Expand All @@ -170,7 +170,7 @@ public void givenRepresentationToDeleteWhenMutationIsPerformedThenTheRepresentat
String typename = JsonPath.read(result, "$.data.deleteRepresentation.__typename");
assertThat(typename).isEqualTo(DeleteRepresentationSuccessPayload.class.getSimpleName());

assertThat(this.representationDataSearchService.findById(TestIdentifiers.EPACKAGE_PORTAL_REPRESENTATION)).isEmpty();
assertThat(this.representationDataSearchService.existsById(TestIdentifiers.EPACKAGE_PORTAL_REPRESENTATION)).isFalse();

assertThat(this.domainEventCollector.getDomainEvents()).hasSize(1);
var event = this.domainEventCollector.getDomainEvents().get(0);
Expand Down
Loading

0 comments on commit 449ad0a

Please sign in to comment.