-
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.
[1301] Adapt sirius-web code to support inter-model references
* Use a correct URI to instantiate a JSONResource. The URI should have a scheme "sirius" * Only resources with "sirius" scheme are persisted and displayed in the Explorer * This commit also ensures that, if a specific resource factory has been registered, it will be used to instantiate the XMI resource at download * Forbid the upload of a single document that contains proxies. The check is not done when uploading a project. #1301 Signed-off-by: Laurent Fasani <laurent.fasani@obeo.fr>
- Loading branch information
Showing
27 changed files
with
439 additions
and
111 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
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
...s-api/src/main/java/org/eclipse/sirius/web/services/api/document/RewriteProxiesInput.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) 2022 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.document; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
import org.eclipse.sirius.components.core.api.IInput; | ||
|
||
/** | ||
* The input object for the operation to rewrite broken proxy URIs in documents. | ||
* | ||
* @author pcdavid | ||
*/ | ||
public class RewriteProxiesInput implements IInput { | ||
private final UUID id; | ||
|
||
private final String editingContextId; | ||
|
||
private final Map<String, String> oldDocumentIdToNewDocumentId; | ||
|
||
public RewriteProxiesInput(UUID id, String editingContextId, Map<String, String> oldDocumentIdToNewDocumentId) { | ||
this.id = Objects.requireNonNull(id); | ||
this.editingContextId = Objects.requireNonNull(editingContextId); | ||
this.oldDocumentIdToNewDocumentId = Objects.requireNonNull(oldDocumentIdToNewDocumentId); | ||
} | ||
|
||
@Override | ||
public UUID getId() { | ||
return this.id; | ||
} | ||
|
||
public String getEditingContextId() { | ||
return this.editingContextId; | ||
} | ||
|
||
public Map<String, String> getOldDocumentIdToNewDocumentId() { | ||
return this.oldDocumentIdToNewDocumentId; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
String pattern = "{0} '{'id: {1}, editingContextId: {2}, oldDocumentIdToNewDocumentId: {3}'}'"; //$NON-NLS-1$ | ||
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id, this.editingContextId, this.oldDocumentIdToNewDocumentId); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
.../main/java/org/eclipse/sirius/web/services/api/document/RewriteProxiesSuccessPayload.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,44 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 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.document; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
import org.eclipse.sirius.components.core.api.IPayload; | ||
|
||
/** | ||
* The payload of the rewrite proxies operation. | ||
* | ||
* @author pcdavid | ||
*/ | ||
public final class RewriteProxiesSuccessPayload implements IPayload { | ||
|
||
private final UUID id; | ||
|
||
public RewriteProxiesSuccessPayload(UUID id) { | ||
this.id = Objects.requireNonNull(id); | ||
} | ||
|
||
@Override | ||
public UUID getId() { | ||
return this.id; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
String pattern = "{0} '{'id: {1}'}'"; //$NON-NLS-1$ | ||
return MessageFormat.format(pattern, this.getClass().getSimpleName(), this.id); | ||
} | ||
} |
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
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
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
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
101 changes: 101 additions & 0 deletions
101
...s/src/main/java/org/eclipse/sirius/web/services/documents/RewriteProxiesEventHandler.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,101 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 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.documents; | ||
|
||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
import org.eclipse.emf.common.util.URI; | ||
import org.eclipse.emf.ecore.InternalEObject; | ||
import org.eclipse.emf.ecore.resource.Resource; | ||
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain; | ||
import org.eclipse.sirius.components.collaborative.api.ChangeDescription; | ||
import org.eclipse.sirius.components.collaborative.api.ChangeKind; | ||
import org.eclipse.sirius.components.collaborative.api.IEditingContextEventHandler; | ||
import org.eclipse.sirius.components.core.api.ErrorPayload; | ||
import org.eclipse.sirius.components.core.api.IEditingContext; | ||
import org.eclipse.sirius.components.core.api.IInput; | ||
import org.eclipse.sirius.components.core.api.IPayload; | ||
import org.eclipse.sirius.components.emf.services.EditingContext; | ||
import org.eclipse.sirius.web.services.api.document.RewriteProxiesInput; | ||
import org.eclipse.sirius.web.services.api.document.RewriteProxiesSuccessPayload; | ||
import org.eclipse.sirius.web.services.messages.IServicesMessageService; | ||
import org.springframework.stereotype.Service; | ||
|
||
import reactor.core.publisher.Sinks.Many; | ||
import reactor.core.publisher.Sinks.One; | ||
|
||
/** | ||
* The event handler to rewrite broken proxy URIs in documents (typically after an upload where the newly created | ||
* documents have different ids). | ||
* | ||
* @author pcdavid | ||
*/ | ||
@Service | ||
public class RewriteProxiesEventHandler implements IEditingContextEventHandler { | ||
private final IServicesMessageService messageService; | ||
|
||
public RewriteProxiesEventHandler(IServicesMessageService messageService) { | ||
this.messageService = Objects.requireNonNull(messageService); | ||
} | ||
|
||
@Override | ||
public boolean canHandle(IEditingContext editingContext, IInput input) { | ||
return input instanceof RewriteProxiesInput; | ||
} | ||
|
||
@Override | ||
public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, IEditingContext editingContext, IInput input) { | ||
IPayload payload = new ErrorPayload(input.getId(), this.messageService.unexpectedError()); | ||
ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, editingContext.getId(), input); | ||
|
||
if (input instanceof RewriteProxiesInput && editingContext instanceof EditingContext) { | ||
RewriteProxiesInput rewriteInput = (RewriteProxiesInput) input; | ||
AdapterFactoryEditingDomain adapterFactoryEditingDomain = ((EditingContext) editingContext).getDomain(); | ||
int totalRewrittenCount = 0; | ||
for (Resource resource : adapterFactoryEditingDomain.getResourceSet().getResources()) { | ||
totalRewrittenCount += this.rewriteProxyURIs(resource, rewriteInput.getOldDocumentIdToNewDocumentId()); | ||
} | ||
if (totalRewrittenCount > 0) { | ||
changeDescription = new ChangeDescription(ChangeKind.SEMANTIC_CHANGE, editingContext.getId(), input); | ||
} | ||
payload = new RewriteProxiesSuccessPayload(input.getId()); | ||
} | ||
|
||
payloadSink.tryEmitValue(payload); | ||
changeDescriptionSink.tryEmitNext(changeDescription); | ||
} | ||
|
||
private int rewriteProxyURIs(Resource resource, Map<String, String> oldDocumentIdToNewDocumentId) { | ||
AtomicInteger rewrittenCount = new AtomicInteger(); | ||
resource.getAllContents().forEachRemaining(eObject -> { | ||
eObject.eCrossReferences().forEach(target -> { | ||
InternalEObject internalEObject = (InternalEObject) target; | ||
if (internalEObject.eIsProxy()) { | ||
URI proxyURI = internalEObject.eProxyURI(); | ||
String oldDocumentId = proxyURI.path().substring(1); | ||
String newDocumentId = oldDocumentIdToNewDocumentId.get(oldDocumentId); | ||
if (newDocumentId != null) { | ||
String prefix = EditingContext.RESOURCE_SCHEME + ":///"; //$NON-NLS-1$ | ||
URI newProxyURI = URI.createURI(proxyURI.toString().replace(prefix + oldDocumentId, prefix + newDocumentId)); | ||
internalEObject.eSetProxyURI(newProxyURI); | ||
rewrittenCount.incrementAndGet(); | ||
} | ||
} | ||
}); | ||
}); | ||
return rewrittenCount.get(); | ||
} | ||
|
||
} |
Oops, something went wrong.