Skip to content

Commit

Permalink
[3019] Restore support for the discovery of domains and views model
Browse files Browse the repository at this point in the history
Bug: #3019
Signed-off-by: Stéphane Bégaudeau <stephane.begaudeau@obeo.fr>
  • Loading branch information
sbegaudeau committed Mar 14, 2024
1 parent 2daf28a commit 86cc3e7
Show file tree
Hide file tree
Showing 31 changed files with 632 additions and 54 deletions.
2 changes: 1 addition & 1 deletion packages/emf/backend/sirius-components-emf/pom.xml
Expand Up @@ -102,7 +102,7 @@
<dependency>
<groupId>org.eclipse.sirius.emfjson</groupId>
<artifactId>org.eclipse.sirius.emfjson</artifactId>
<version>2.3.6-SNAPSHOT</version>
<version>2.3.7-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
5 changes: 5 additions & 0 deletions packages/sirius-web/backend/sirius-web-application/pom.xml
Expand Up @@ -144,6 +144,11 @@
<artifactId>sirius-components-view-gantt-edit</artifactId>
<version>2024.1.5</version>
</dependency>
<dependency>
<groupId>org.eclipse.sirius</groupId>
<artifactId>sirius-components-domain-emf</artifactId>
<version>2024.1.5</version>
</dependency>
</dependencies>

<build>
Expand Down
@@ -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.editingcontext.services;

import java.util.List;

import org.eclipse.sirius.web.domain.boundedcontexts.semanticdata.Document;

/**
* The data retrieved from the serialization of a document.
*
* @author sbegaudeau
*/
public record DocumentData(Document document, List<EPackageEntry> ePackageEntries) {
}
@@ -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.editingcontext.services;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Optional;

import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.sirius.components.emf.ResourceMetadataAdapter;
import org.eclipse.sirius.components.emf.services.JSONResourceFactory;
import org.eclipse.sirius.web.application.editingcontext.services.api.IDocumentToResourceService;
import org.eclipse.sirius.web.domain.boundedcontexts.semanticdata.Document;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
* Used to load documents as resources.
*
* @author sbegaudeau
*/
@Service
public class DocumentToResourceService implements IDocumentToResourceService {

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

@Override
public Optional<Resource> toResource(ResourceSet resourceSet, Document document) {
Optional<Resource> optionalResource = Optional.empty();

var resource = new JSONResourceFactory().createResourceFromPath(document.getId().toString());
try (var inputStream = new ByteArrayInputStream(document.getContent().getBytes())) {
resourceSet.getResources().add(resource);
resource.load(inputStream, null);

resource.eAdapters().add(new ResourceMetadataAdapter(document.getName()));

optionalResource = Optional.of(resource);
} catch (IOException | IllegalArgumentException exception) {
this.logger.warn("An error occured while loading document {}: {}.", document.getId(), exception.getMessage());
resourceSet.getResources().remove(resource);
}

return optionalResource;
}
}
@@ -0,0 +1,21 @@
/*******************************************************************************
* 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.services;

/**
* Used to store the EPackage used in a JsonResource.
*
* @author sbegaudeau
*/
public record EPackageEntry(String nsPrefix, String nsURI) {
}
Expand Up @@ -12,6 +12,7 @@
*******************************************************************************/
package org.eclipse.sirius.web.application.editingcontext.services;

import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
Expand All @@ -24,6 +25,7 @@
import org.eclipse.sirius.web.application.UUIDParser;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -65,11 +67,20 @@ public void persist(IEditingContext editingContext) {
new UUIDParser().parse(editingContext.getId())
.map(AggregateReference::<Project, UUID>to)
.ifPresent(project -> {
var documents = emfEditingContext.getDomain().getResourceSet().getResources().stream()
var documentData = emfEditingContext.getDomain().getResourceSet().getResources().stream()
.map(this.resourceToDocumentService::toDocument)
.flatMap(Optional::stream)
.collect(Collectors.toSet());
this.semanticDataUpdateService.updateDocuments(project, documents);

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(project, documents, domainUris);
});
}

Expand Down
Expand Up @@ -12,8 +12,6 @@
*******************************************************************************/
package org.eclipse.sirius.web.application.editingcontext.services;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
Expand All @@ -28,12 +26,11 @@
import org.eclipse.sirius.components.core.api.IEditingContextProcessor;
import org.eclipse.sirius.components.core.api.IEditingContextRepresentationDescriptionProvider;
import org.eclipse.sirius.components.core.api.IEditingContextSearchService;
import org.eclipse.sirius.components.emf.ResourceMetadataAdapter;
import org.eclipse.sirius.components.emf.services.EditingContextCrossReferenceAdapter;
import org.eclipse.sirius.components.emf.services.JSONResourceFactory;
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.IDocumentToResourceService;
import org.eclipse.sirius.web.application.editingcontext.services.api.IEditingDomainFactory;
import org.eclipse.sirius.web.domain.boundedcontexts.project.Project;
import org.eclipse.sirius.web.domain.boundedcontexts.project.services.api.IProjectSearchService;
Expand Down Expand Up @@ -63,6 +60,8 @@ public class EditingContextSearchService implements IEditingContextSearchService

private final ISemanticDataSearchService semanticDataSearchService;

private final IDocumentToResourceService documentToResourceService;

private final IEditingDomainFactory editingDomainFactory;

private final List<IEditingContextRepresentationDescriptionProvider> representationDescriptionProviders;
Expand All @@ -71,10 +70,11 @@ public class EditingContextSearchService implements IEditingContextSearchService

private final Timer timer;

public EditingContextSearchService(IProjectSearchService projectSearchService, ISemanticDataSearchService semanticDataSearchService, IEditingDomainFactory editingDomainFactory,
public EditingContextSearchService(IProjectSearchService projectSearchService, ISemanticDataSearchService semanticDataSearchService, IDocumentToResourceService documentToResourceService, IEditingDomainFactory editingDomainFactory,
List<IEditingContextRepresentationDescriptionProvider> representationDescriptionProviders, List<IEditingContextProcessor> editingContextProcessors, MeterRegistry meterRegistry) {
this.projectSearchService = Objects.requireNonNull(projectSearchService);
this.semanticDataSearchService = Objects.requireNonNull(semanticDataSearchService);
this.documentToResourceService = Objects.requireNonNull(documentToResourceService);
this.editingDomainFactory = Objects.requireNonNull(editingDomainFactory);
this.representationDescriptionProviders = Objects.requireNonNull(representationDescriptionProviders);
this.editingContextProcessors = Objects.requireNonNull(editingContextProcessors);
Expand Down Expand Up @@ -125,20 +125,7 @@ private void loadSemanticData(EditingContext editingContext, SemanticData semant
ResourceSet resourceSet = editingContext.getDomain().getResourceSet();
resourceSet.getLoadOptions().put(JsonResource.OPTION_SCHEMA_LOCATION, true);

var documents = semanticData.getDocuments();
for (var document : documents) {
var resource = new JSONResourceFactory().createResourceFromPath(document.getId().toString());

try (var inputStream = new ByteArrayInputStream(document.getContent().getBytes())) {
resourceSet.getResources().add(resource);
resource.load(inputStream, null);

resource.eAdapters().add(new ResourceMetadataAdapter(document.getName()));
} catch (IOException | IllegalArgumentException exception) {
this.logger.warn("An error occured while loading document {}: {}.", document.getId(), exception.getMessage());
resourceSet.getResources().remove(resource);
}
}
semanticData.getDocuments().forEach(document -> this.documentToResourceService.toResource(resourceSet, document));

// The ECrossReferenceAdapter must be set after the resource loading because it needs to resolve proxies in case
// of inter-resources references
Expand Down
@@ -0,0 +1,51 @@
/*******************************************************************************
* 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.services;

import com.google.gson.JsonElement;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.sirius.emfjson.resource.JsonResource;

/**
* Used to collect serialization data from JsonResource.
*
* @author sbegaudeau
*/
public class JsonResourceSerializationListener implements JsonResource.ISerializationListener {

private final List<EPackageEntry> ePackageEntries = new ArrayList<>();

@Override
public void onNsHeaderEntryAdded(String nsPrefix, String nsURI) {
this.ePackageEntries.add(new EPackageEntry(nsPrefix, nsURI));
}

@Override
public void onObjectSerialized(EObject eObject, JsonElement jsonElement) {
// Do nothing
}

@Override
public void onCrossReferenceURICreated(EObject eObject, EReference eReference, String s) {
// Do nothing
}

public List<EPackageEntry> getePackageEntries() {
return this.ePackageEntries;
}
}
Expand Up @@ -40,12 +40,15 @@ public class ResourceToDocumentService implements IResourceToDocumentService {
private final Logger logger = LoggerFactory.getLogger(ResourceToDocumentService.class);

@Override
public Optional<Document> toDocument(Resource resource) {
public Optional<DocumentData> toDocument(Resource resource) {
var serializationListener = new JsonResourceSerializationListener();

HashMap<Object, Object> options = new HashMap<>();
options.put(JsonResource.OPTION_ID_MANAGER, new EObjectIDManager());
options.put(JsonResource.OPTION_SCHEMA_LOCATION, true);
options.put(JsonResource.OPTION_SERIALIZATION_LISTENER, serializationListener);

Optional<Document> optionalDocument = Optional.empty();
Optional<DocumentData> optionalDocumentData = Optional.empty();
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
resource.save(outputStream, options);

Expand All @@ -71,10 +74,11 @@ public Optional<Document> toDocument(Resource resource) {
.name(name)
.content(content)
.build();
optionalDocument = Optional.of(document);
var documentData = new DocumentData(document, serializationListener.getePackageEntries());
optionalDocumentData = Optional.of(documentData);
} catch (IllegalArgumentException | IOException exception) {
this.logger.warn(exception.getMessage(), exception);
}
return optionalDocument;
return optionalDocumentData;
}
}
@@ -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.editingcontext.services.api;

import java.util.Optional;

import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.sirius.web.domain.boundedcontexts.semanticdata.Document;

/**
* Used to load documents as EMF resources.
*
* @author sbegaudeau
*/
public interface IDocumentToResourceService {
Optional<Resource> toResource(ResourceSet resourceSet, Document document);
}
Expand Up @@ -15,13 +15,13 @@
import java.util.Optional;

import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.sirius.web.domain.boundedcontexts.semanticdata.Document;
import org.eclipse.sirius.web.application.editingcontext.services.DocumentData;

/**
* Used to transform an EMF resource into a document.
*
* @author sbegaudeau
*/
public interface IResourceToDocumentService {
Optional<Document> toDocument(Resource resource);
Optional<DocumentData> toDocument(Resource resource);
}

0 comments on commit 86cc3e7

Please sign in to comment.