Skip to content

Commit

Permalink
[4444] Allow multiple implementation of IEditingContextSaver & IEditi…
Browse files Browse the repository at this point in the history
…ngContextLoader

Bug: #4444
Signed-off-by: Michaël Charfadi <[email protected]>
  • Loading branch information
mcharfadi committed Jan 20, 2025
1 parent 13adadd commit bb225ad
Show file tree
Hide file tree
Showing 21 changed files with 510 additions and 103 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
It will allow us to create more complex test data shared by all integration tests to detect additional problems.
- https://github.com/eclipse-sirius/sirius-web/issues/4416[#4416] [sirius-web] The `IEditingContextMigrationParticipantPredicate` will now use the id of the editing context instead of the whole editing context in order to find out if the migration participant should be activated.
- https://github.com/eclipse-sirius/sirius-web/issues/4418[#4418] [sirius-web] Change type of project_id from UUID to String
- https://github.com/eclipse-sirius/sirius-web/issues/4444[#4444] [sirius-web] Allow multiple implementation of IEditingContextSaver & IEditingContextLoader


=== Dependency update
Expand Down Expand Up @@ -77,6 +78,7 @@ It can be activated using `sirius.web.graphql.tracing=true` since it is not enab
Some additional log has also been contributed on the frontend in order to view more easily the order and time of the GraphQL requests and responses.
- https://github.com/eclipse-sirius/sirius-web/issues/4430[#4430] [diagram] Add the PNG export of a diagram.
- https://github.com/eclipse-sirius/sirius-web/issues/4346[#4346] [query] The current selection is now available using both the variables `selection: Sequence{Object}` for the full selection and `self: Object` for the first selected object.
- https://github.com/eclipse-sirius/sirius-web/issues/4444[#4444] [sirius-web] Allow multiple implementation of IEditingContextSaver & IEditingContextLoader


=== Improvements
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 @@ -43,9 +43,6 @@ public ViewerEditingContextDataFetcher(IEditingContextApplicationService editing
@Override
public DataFetcherResult<String> get(DataFetchingEnvironment environment) throws Exception {
String editingContextId = environment.getArgument(EDITING_CONTEXT_ID_ARGUMENT);
if (!this.editingContextApplicationService.existsById(editingContextId)) {
return null;
}

Map<String, Object> localContext = new HashMap<>();
localContext.put(LocalContextConstants.EDITING_CONTEXT_ID, editingContextId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,25 @@
*******************************************************************************/
package org.eclipse.sirius.web.application.editingcontext.services;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.core.api.IEditingContextProcessor;
import org.eclipse.sirius.components.core.api.IEditingContextRepresentationDescriptionProvider;
import org.eclipse.sirius.components.emf.services.EditingContextCrossReferenceAdapter;
import org.eclipse.sirius.emfjson.resource.JsonResource;
import org.eclipse.sirius.web.application.UUIDParser;
import org.eclipse.sirius.web.application.editingcontext.EditingContext;
import org.eclipse.sirius.web.application.editingcontext.services.api.IEditingContextLoader;
import org.eclipse.sirius.web.application.editingcontext.services.api.IEditingContextMigrationParticipantPredicate;
import org.eclipse.sirius.web.application.editingcontext.services.api.IEditingDomainFactory;
import org.eclipse.sirius.web.application.editingcontext.services.api.IResourceLoader;
import org.eclipse.sirius.web.domain.boundedcontexts.project.services.api.IProjectSearchService;
import org.eclipse.sirius.web.domain.boundedcontexts.semanticdata.SemanticData;
import org.eclipse.sirius.web.domain.boundedcontexts.semanticdata.services.api.ISemanticDataSearchService;
import org.slf4j.Logger;
Expand All @@ -49,17 +56,34 @@ public class EditingContextLoader implements IEditingContextLoader {

private final List<IEditingContextProcessor> editingContextProcessors;

private final IProjectSearchService projectSearchService;

private final List<IEditingContextMigrationParticipantPredicate> migrationParticipantPredicates;

public EditingContextLoader(ISemanticDataSearchService semanticDataSearchService, IResourceLoader resourceLoader, List<IEditingContextRepresentationDescriptionProvider> representationDescriptionProviders, List<IEditingContextProcessor> editingContextProcessors, List<IEditingContextMigrationParticipantPredicate> migrationParticipantPredicates) {
private final IEditingDomainFactory editingDomainFactory;

public EditingContextLoader(ISemanticDataSearchService semanticDataSearchService, IResourceLoader resourceLoader, List<IEditingContextRepresentationDescriptionProvider> representationDescriptionProviders, List<IEditingContextProcessor> editingContextProcessors, IProjectSearchService projectSearchService, List<IEditingContextMigrationParticipantPredicate> migrationParticipantPredicates, IEditingDomainFactory editingDomainFactory) {
this.semanticDataSearchService = Objects.requireNonNull(semanticDataSearchService);
this.resourceLoader = Objects.requireNonNull(resourceLoader);
this.representationDescriptionProviders = Objects.requireNonNull(representationDescriptionProviders);
this.editingContextProcessors = Objects.requireNonNull(editingContextProcessors);
this.projectSearchService = Objects.requireNonNull(projectSearchService);
this.migrationParticipantPredicates = Objects.requireNonNull(migrationParticipantPredicates);
this.editingDomainFactory = Objects.requireNonNull(editingDomainFactory);
}

public void load(EditingContext editingContext, String projectId) {
public IEditingContext load(String editingContextId) {
var project = this.projectSearchService.findById(editingContextId);

AdapterFactoryEditingDomain editingDomain = this.editingDomainFactory.createEditingDomain(project.get());
EditingContext editingContext = new EditingContext(project.get().getId(), editingDomain, new HashMap<>(), new ArrayList<>());

this.toEditingContext(editingContext, project.get().getId());

return editingContext;
}

public void toEditingContext(EditingContext editingContext, String projectId) {
this.editingContextProcessors.forEach(processor -> processor.preProcess(editingContext));

this.semanticDataSearchService.findByProject(AggregateReference.to(projectId))
Expand All @@ -73,6 +97,11 @@ public void load(EditingContext editingContext, String projectId) {
this.editingContextProcessors.forEach(processor -> processor.postProcess(editingContext));
}

@Override
public boolean canHandle(String projectId) {
return new UUIDParser().parse(projectId).isPresent() && this.projectSearchService.existsById(projectId);
}

private void loadSemanticData(EditingContext editingContext, SemanticData semanticData) {
ResourceSet resourceSet = editingContext.getDomain().getResourceSet();
resourceSet.getLoadOptions().put(JsonResource.OPTION_SCHEMA_LOCATION, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,21 @@
*******************************************************************************/
package org.eclipse.sirius.web.application.editingcontext.services;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.core.api.IEditingContextPersistenceService;
import org.eclipse.sirius.components.emf.services.api.IEMFEditingContext;
import org.eclipse.sirius.components.events.ICause;
import org.eclipse.sirius.web.application.editingcontext.services.api.IEditingContextMigrationParticipantPredicate;
import org.eclipse.sirius.web.application.editingcontext.services.api.IEditingContextPersistenceFilter;
import org.eclipse.sirius.web.application.editingcontext.services.api.IResourceToDocumentService;
import org.eclipse.sirius.web.domain.boundedcontexts.project.Project;
import org.eclipse.sirius.web.domain.boundedcontexts.semanticdata.Document;
import org.eclipse.sirius.web.domain.boundedcontexts.semanticdata.services.api.ISemanticDataUpdateService;
import org.eclipse.sirius.web.application.editingcontext.services.api.IEditingContextSaver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.jdbc.core.mapping.AggregateReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;

/**
* Used to save the editing context.
*
Expand All @@ -48,23 +37,14 @@ public class EditingContextPersistenceService implements IEditingContextPersiste

private static final String TIMER_NAME = "siriusweb_editingcontext_save";

private final ISemanticDataUpdateService semanticDataUpdateService;

private final IResourceToDocumentService resourceToDocumentService;
private final List<IEditingContextSaver> editingContextSavers;

private final List<IEditingContextPersistenceFilter> persistenceFilters;

private final List<IEditingContextMigrationParticipantPredicate> migrationParticipantPredicates;
private final Logger logger = LoggerFactory.getLogger(EditingContextPersistenceService.class);

private final Timer timer;

private final Logger logger = LoggerFactory.getLogger(EditingContextPersistenceService.class);

public EditingContextPersistenceService(ISemanticDataUpdateService semanticDataUpdateService, IResourceToDocumentService resourceToDocumentService, List<IEditingContextPersistenceFilter> persistenceFilters, List<IEditingContextMigrationParticipantPredicate> migrationParticipantPredicates, MeterRegistry meterRegistry) {
this.semanticDataUpdateService = Objects.requireNonNull(semanticDataUpdateService);
this.resourceToDocumentService = Objects.requireNonNull(resourceToDocumentService);
this.persistenceFilters = Objects.requireNonNull(persistenceFilters);
this.migrationParticipantPredicates = Objects.requireNonNull(migrationParticipantPredicates);
public EditingContextPersistenceService(List<IEditingContextSaver> editingContextSavers, MeterRegistry meterRegistry) {
this.editingContextSavers = Objects.requireNonNull(editingContextSavers);
this.timer = Timer.builder(TIMER_NAME).register(meterRegistry);
}

Expand All @@ -73,28 +53,9 @@ public EditingContextPersistenceService(ISemanticDataUpdateService semanticDataU
public void persist(ICause cause, IEditingContext editingContext) {
long start = System.currentTimeMillis();

if (editingContext instanceof IEMFEditingContext emfEditingContext) {
var applyMigrationParticipants = this.migrationParticipantPredicates.stream().anyMatch(predicate -> predicate.test(emfEditingContext.getId()));
AggregateReference<Project, String> projectId = AggregateReference.to(editingContext.getId());

var documentData = emfEditingContext.getDomain().getResourceSet().getResources().stream()
.filter(resource -> IEMFEditingContext.RESOURCE_SCHEME.equals(resource.getURI().scheme()))
.filter(resource -> this.persistenceFilters.stream().allMatch(filter -> filter.shouldPersist(resource)))
.map(resource -> this.resourceToDocumentService.toDocument(resource, applyMigrationParticipants))
.flatMap(Optional::stream)
.collect(Collectors.toSet());

var documents = new LinkedHashSet<Document>();
var domainUris = new LinkedHashSet<String>();

documentData.forEach(data -> {
documents.add(data.document());
domainUris.addAll(data.ePackageEntries().stream().map(EPackageEntry::nsURI).toList());
});

this.semanticDataUpdateService.updateDocuments(cause, projectId, documents, domainUris);
}

editingContextSavers.stream()
.filter(editingContextSaver -> editingContextSaver.canHandle(editingContext))
.forEach(editingContextSaver -> editingContextSaver.save(cause, editingContext));
long end = System.currentTimeMillis();
this.timer.record(end - start, TimeUnit.MILLISECONDS);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
/*******************************************************************************
* 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.editingcontext.services;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.core.api.IEditingContextSearchService;
import org.eclipse.sirius.components.emf.services.api.IEMFEditingContext;
import org.eclipse.sirius.components.events.ICause;
import org.eclipse.sirius.web.application.UUIDParser;
import org.eclipse.sirius.web.application.editingcontext.services.api.IEditingContextMigrationParticipantPredicate;
import org.eclipse.sirius.web.application.editingcontext.services.api.IEditingContextPersistenceFilter;
import org.eclipse.sirius.web.application.editingcontext.services.api.IEditingContextSaver;
import org.eclipse.sirius.web.application.editingcontext.services.api.IResourceToDocumentService;
import org.eclipse.sirius.web.domain.boundedcontexts.project.Project;
import org.eclipse.sirius.web.domain.boundedcontexts.semanticdata.Document;
import org.eclipse.sirius.web.domain.boundedcontexts.semanticdata.services.api.ISemanticDataSearchService;
import org.eclipse.sirius.web.domain.boundedcontexts.semanticdata.services.api.ISemanticDataUpdateService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.jdbc.core.mapping.AggregateReference;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
* Used to save an editing context.
*
* @author mcharfadi
*/
@Service
public class EditingContextSaver implements IEditingContextSaver {

private static final String TIMER_NAME = "siriusweb_editingcontext_save";

private final ISemanticDataUpdateService semanticDataUpdateService;

private final IResourceToDocumentService resourceToDocumentService;

private final List<IEditingContextPersistenceFilter> persistenceFilters;

private final List<IEditingContextMigrationParticipantPredicate> migrationParticipantPredicates;

private final ISemanticDataSearchService semanticDataSearchService;

private final Timer timer;

private final Logger logger = LoggerFactory.getLogger(EditingContextSaver.class);

public EditingContextSaver(ISemanticDataUpdateService semanticDataUpdateService, IResourceToDocumentService resourceToDocumentService, IEditingContextSearchService editingContextSearchService, List<IEditingContextPersistenceFilter> persistenceFilters, List<IEditingContextMigrationParticipantPredicate> migrationParticipantPredicates, ISemanticDataSearchService semanticDataSearchService, MeterRegistry meterRegistry) {
this.semanticDataUpdateService = Objects.requireNonNull(semanticDataUpdateService);
this.resourceToDocumentService = Objects.requireNonNull(resourceToDocumentService);
this.persistenceFilters = Objects.requireNonNull(persistenceFilters);
this.migrationParticipantPredicates = Objects.requireNonNull(migrationParticipantPredicates);
this.semanticDataSearchService = Objects.requireNonNull(semanticDataSearchService);
this.timer = Timer.builder(TIMER_NAME).register(meterRegistry);
}

@Transactional
@Override
public void save(ICause cause, IEditingContext editingContext) {
long start = System.currentTimeMillis();

if (editingContext instanceof IEMFEditingContext emfEditingContext) {
var applyMigrationParticipants = this.migrationParticipantPredicates.stream().anyMatch(predicate -> predicate.test(emfEditingContext.getId()));
AggregateReference<Project, String> projectId = AggregateReference.to(editingContext.getId());

var documentData = emfEditingContext.getDomain().getResourceSet().getResources().stream()
.filter(resource -> IEMFEditingContext.RESOURCE_SCHEME.equals(resource.getURI().scheme()))
.filter(resource -> this.persistenceFilters.stream().allMatch(filter -> filter.shouldPersist(resource)))
.map(resource -> this.resourceToDocumentService.toDocument(resource, applyMigrationParticipants))
.flatMap(Optional::stream)
.collect(Collectors.toSet());

var documents = new LinkedHashSet<Document>();
var domainUris = new LinkedHashSet<String>();

documentData.forEach(data -> {
documents.add(data.document());
domainUris.addAll(data.ePackageEntries().stream().map(EPackageEntry::nsURI).toList());
});

this.semanticDataSearchService.findByProject(projectId).ifPresent(semanticData -> {
this.semanticDataUpdateService.updateDocuments(cause, semanticData.getId(), documents, domainUris);
});

}

long end = System.currentTimeMillis();
this.timer.record(end - start, TimeUnit.MILLISECONDS);

this.logger.atDebug()
.setMessage("EditingContext {}: {}ms to persist the semantic data")
.addArgument(editingContext.getId())
.addArgument(() -> String.format("%1$6s", end - start))
.log();
}

@Override
public boolean canHandle(IEditingContext editingContext) {
return editingContext instanceof IEMFEditingContext && new UUIDParser().parse(editingContext.getId()).isPresent();
}
}
Loading

0 comments on commit bb225ad

Please sign in to comment.