Search in sources :

Example 1 with InternalTransaction

use of org.eclipse.emf.transaction.impl.InternalTransaction 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

JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ChangeDescription (org.eclipse.emf.ecore.change.ChangeDescription)1 RollbackException (org.eclipse.emf.transaction.RollbackException)1 InternalTransaction (org.eclipse.emf.transaction.impl.InternalTransaction)1 ModelServerEditingDomain (org.eclipse.emfcloud.modelserver.emf.common.ModelServerEditingDomain)1