Search in sources :

Example 1 with RollbackException

use of org.eclipse.emf.transaction.RollbackException in project AGREE by loonwerks.

the class EphemeralImplementationUtil method cleanup.

/**
 * Delete the accumulated ephemeral component implementations by deleting their containing {@link Resource}.
 * <p>
 * This method is intended to be called immediately prior to the instance of this utility going out of scope.
 * However, it may be called multiple times, deleting the ephemeral implementations and resource accumulated to
 * that point.
 */
public void cleanup() {
    final TransactionalEditingDomain domain = WorkspaceEditingDomainFactory.INSTANCE.createEditingDomain();
    // We execute this command on the command stack because otherwise, we will not
    // have write permissions on the editing domain.
    Command cmd = new RecordingCommand(domain) {

        @Override
        protected void doExecute() {
            try {
                Iterator<Resource> iter = ephemeralResources.iterator();
                while (iter.hasNext()) {
                    Resource res = iter.next();
                    res.delete(Collections.EMPTY_MAP);
                    iter.remove();
                }
            } catch (IOException e) {
                setErrorMessage(e.getMessage());
                e.printStackTrace();
            }
        }
    };
    try {
        ((TransactionalCommandStack) domain.getCommandStack()).execute(cmd, null);
    } catch (InterruptedException | RollbackException e) {
        setErrorMessage(e.getMessage());
        e.printStackTrace();
    }
}
Also used : TransactionalEditingDomain(org.eclipse.emf.transaction.TransactionalEditingDomain) RecordingCommand(org.eclipse.emf.transaction.RecordingCommand) TransactionalCommandStack(org.eclipse.emf.transaction.TransactionalCommandStack) RecordingCommand(org.eclipse.emf.transaction.RecordingCommand) Command(org.eclipse.emf.common.command.Command) Resource(org.eclipse.emf.ecore.resource.Resource) IOException(java.io.IOException) RollbackException(org.eclipse.emf.transaction.RollbackException)

Example 2 with RollbackException

use of org.eclipse.emf.transaction.RollbackException in project emfcloud-modelserver by eclipse-emfcloud.

the class JsonPatchHelper method getJsonPatch.

public JsonNode getJsonPatch(final EObject root, final CCommandExecutionResult result) throws EncodingException {
    // TODO Support multiple resource patches.
    // See issue https://github.com/eclipse-emfcloud/emfcloud-modelserver/issues/159
    JsonNode newModel = getCurrentModel(root);
    ChangeDescription cd = (ChangeDescription) result.getChangeDescription();
    ModelServerEditingDomain editingDomain = modelManager.getEditingDomain(root.eResource().getResourceSet());
    JsonNode oldModel = null;
    try {
        // Compute the old model by reverting the current change. We need to do that in a write-transaction.
        // Note: we could also apply it on a read-only copy of the model, but this could potentially modify
        // the IDs (which are part of the Resource; not of the EObjects), causing unexpected patch changes.
        InternalTransaction transaction = editingDomain.startTransaction(false, null);
        try {
            cd.applyAndReverse();
            oldModel = getCurrentModel(root);
            cd.applyAndReverse();
            transaction.commit();
        } catch (RollbackException e) {
            LOG.error("Failed to generate JsonPatch", e);
            return null;
        } finally {
            if (transaction != null && transaction.isActive()) {
                rollback(transaction, editingDomain);
            }
        }
    } catch (InterruptedException e) {
        LOG.error("Failed to generate JsonPatch", e);
        return null;
    }
    return diffModel(oldModel, newModel);
}
Also used : ChangeDescription(org.eclipse.emf.ecore.change.ChangeDescription) JsonNode(com.fasterxml.jackson.databind.JsonNode) InternalTransaction(org.eclipse.emf.transaction.impl.InternalTransaction) RollbackException(org.eclipse.emf.transaction.RollbackException) ModelServerEditingDomain(org.eclipse.emfcloud.modelserver.emf.common.ModelServerEditingDomain)

Aggregations

RollbackException (org.eclipse.emf.transaction.RollbackException)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 IOException (java.io.IOException)1 Command (org.eclipse.emf.common.command.Command)1 ChangeDescription (org.eclipse.emf.ecore.change.ChangeDescription)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 RecordingCommand (org.eclipse.emf.transaction.RecordingCommand)1 TransactionalCommandStack (org.eclipse.emf.transaction.TransactionalCommandStack)1 TransactionalEditingDomain (org.eclipse.emf.transaction.TransactionalEditingDomain)1 InternalTransaction (org.eclipse.emf.transaction.impl.InternalTransaction)1 ModelServerEditingDomain (org.eclipse.emfcloud.modelserver.emf.common.ModelServerEditingDomain)1