Search in sources :

Example 1 with DecodingException

use of org.eclipse.emfcloud.modelserver.common.codecs.DecodingException in project emfcloud-modelserver by eclipse-emfcloud.

the class DefaultModelResourceManagerTest method beforeTests.

@Before
public void beforeTests() throws DecodingException {
    when(serverConfig.getWorkspaceRootURI()).thenReturn(URI.createFileURI(getCWD().getAbsolutePath() + "/" + RESOURCE_PATH));
    modelResourceManager = Guice.createInjector(new AbstractModule() {

        private Multibinder<EPackageConfiguration> ePackageConfigurationBinder;

        private ArrayList<Class<? extends EPackageConfiguration>> ePackageConfigurations;

        @Override
        protected void configure() {
            ePackageConfigurations = Lists.newArrayList(EcorePackageConfiguration.class, CommandPackageConfiguration.class);
            ePackageConfigurationBinder = Multibinder.newSetBinder(binder(), EPackageConfiguration.class);
            ePackageConfigurations.forEach(c -> ePackageConfigurationBinder.addBinding().to(c));
            bind(ServerConfiguration.class).toInstance(serverConfig);
            bind(CommandCodec.class).toInstance(commandCodec);
            bind(ModelWatchersManager.class).toInstance(watchersManager);
            bind(AdapterFactory.class).toInstance(new EcoreAdapterFactory());
            bind(ModelRepository.class).to(DefaultModelRepository.class).in(Scopes.SINGLETON);
            bind(ModelResourceManager.class).to(DefaultModelResourceManager.class).in(Scopes.SINGLETON);
            bind(JsonPatchHelper.class).toInstance(jsonPatchHelper);
        }
    }).getInstance(DefaultModelResourceManager.class);
}
Also used : EcoreFactory(org.eclipse.emf.ecore.EcoreFactory) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) EcoreAdapterFactory(org.eclipse.emf.ecore.util.EcoreAdapterFactory) DefaultModelResourceManager(org.eclipse.emfcloud.modelserver.emf.common.DefaultModelResourceManager) JsonPatchTestException(org.eclipse.emfcloud.modelserver.common.patch.JsonPatchTestException) ModelRepository(org.eclipse.emfcloud.modelserver.emf.common.ModelRepository) Map(java.util.Map) JsonPatchHelper(org.eclipse.emfcloud.modelserver.emf.util.JsonPatchHelper) Json(org.eclipse.emfcloud.modelserver.jsonschema.Json) EPackageConfiguration(org.eclipse.emfcloud.modelserver.emf.configuration.EPackageConfiguration) CommandCodec(org.eclipse.emfcloud.modelserver.edit.CommandCodec) ServerConfiguration(org.eclipse.emfcloud.modelserver.emf.configuration.ServerConfiguration) EObject(org.eclipse.emf.ecore.EObject) ModelResourceManager(org.eclipse.emfcloud.modelserver.emf.common.ModelResourceManager) Multibinder(com.google.inject.multibindings.Multibinder) EPackage(org.eclipse.emf.ecore.EPackage) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Assert.assertFalse(org.junit.Assert.assertFalse) Resource(org.eclipse.emf.ecore.resource.Resource) Optional(java.util.Optional) DecodingException(org.eclipse.emfcloud.modelserver.common.codecs.DecodingException) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) JsonPatchException(org.eclipse.emfcloud.modelserver.common.patch.JsonPatchException) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) URI(org.eclipse.emf.common.util.URI) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) ArrayList(java.util.ArrayList) ModelWatchersManager(org.eclipse.emfcloud.modelserver.emf.common.watchers.ModelWatchersManager) Lists(com.google.common.collect.Lists) EClass(org.eclipse.emf.ecore.EClass) AdapterFactory(org.eclipse.emf.common.notify.AdapterFactory) Before(org.junit.Before) Answers(org.mockito.Answers) Assert.assertNotNull(org.junit.Assert.assertNotNull) EcoreUtil(org.eclipse.emf.ecore.util.EcoreUtil) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Scopes(com.google.inject.Scopes) File(java.io.File) DefaultModelRepository(org.eclipse.emfcloud.modelserver.emf.common.DefaultModelRepository) Mockito.verify(org.mockito.Mockito.verify) EcorePackageConfiguration(org.eclipse.emfcloud.modelserver.emf.configuration.EcorePackageConfiguration) CommandPackageConfiguration(org.eclipse.emfcloud.modelserver.emf.configuration.CommandPackageConfiguration) Guice(com.google.inject.Guice) Assert.assertEquals(org.junit.Assert.assertEquals) AbstractModule(com.google.inject.AbstractModule) ModelRepository(org.eclipse.emfcloud.modelserver.emf.common.ModelRepository) DefaultModelRepository(org.eclipse.emfcloud.modelserver.emf.common.DefaultModelRepository) EPackageConfiguration(org.eclipse.emfcloud.modelserver.emf.configuration.EPackageConfiguration) DefaultModelResourceManager(org.eclipse.emfcloud.modelserver.emf.common.DefaultModelResourceManager) EClass(org.eclipse.emf.ecore.EClass) DefaultModelResourceManager(org.eclipse.emfcloud.modelserver.emf.common.DefaultModelResourceManager) ModelResourceManager(org.eclipse.emfcloud.modelserver.emf.common.ModelResourceManager) EcoreAdapterFactory(org.eclipse.emf.ecore.util.EcoreAdapterFactory) AbstractModule(com.google.inject.AbstractModule) Before(org.junit.Before)

Example 2 with DecodingException

use of org.eclipse.emfcloud.modelserver.common.codecs.DecodingException in project emfcloud-modelserver by eclipse-emfcloud.

the class DefaultModelResourceManager method execute.

@Override
public CCommandExecutionResult execute(final String modeluri, final CCommand clientCommand) {
    try {
        ResourceSet resourceSet = getResourceSet(modeluri);
        ModelServerEditingDomain domain = getEditingDomain(resourceSet);
        URI uri = createURI(modeluri);
        // resolve client command
        ReadResourceSet readResourceSet = new ReadResourceSet(domain);
        readResourceSet.resolve(clientCommand, "$command.res");
        // translate to server EMF command
        Command command = commandCodec.clientToServer(uri, domain, clientCommand);
        Command modelServerCommand = ModelServerCommand.wrap(command, clientCommand);
        // execute command
        CommandExecutionContext context = executeCommand(domain, modelServerCommand, clientCommand);
        // create result
        CCommandExecutionResult result = createExecutionResult(context);
        readResourceSet.resolve(result, "$command.exec.res");
        return result;
    } catch (DecodingException exception) {
        LOG.error("Encoding of " + clientCommand + " failed: " + exception.getMessage());
        throw new IllegalArgumentException(exception);
    }
}
Also used : CCommandExecutionResult(org.eclipse.emfcloud.modelserver.command.CCommandExecutionResult) CCommand(org.eclipse.emfcloud.modelserver.command.CCommand) ModelServerCommand(org.eclipse.emfcloud.modelserver.edit.ModelServerCommand) Command(org.eclipse.emf.common.command.Command) DecodingException(org.eclipse.emfcloud.modelserver.common.codecs.DecodingException) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI)

Example 3 with DecodingException

use of org.eclipse.emfcloud.modelserver.common.codecs.DecodingException in project emfcloud-modelserver by eclipse-emfcloud.

the class RemoveCommandContribution method serverCommand.

public static RemoveCommand serverCommand(final EditingDomain domain, final CCommand command) throws DecodingException {
    EObject owner = command.getOwner();
    EStructuralFeature feature = owner.eClass().getEStructuralFeature(command.getFeature());
    if (!command.getIndices().isEmpty()) {
        return (RemoveCommand) RemoveCommand.create(domain, owner, feature, Ints.toArray(command.getIndices()));
    }
    if (!command.getObjectValues().isEmpty()) {
        return (RemoveCommand) RemoveCommand.create(domain, owner, feature, command.getObjectValues());
    }
    if (!command.getDataValues().isEmpty()) {
        return (RemoveCommand) RemoveCommand.create(domain, owner, feature, command.getDataValues());
    }
    throw new DecodingException("incomplete remove command specification");
}
Also used : RemoveCommand(org.eclipse.emf.edit.command.RemoveCommand) EObject(org.eclipse.emf.ecore.EObject) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) DecodingException(org.eclipse.emfcloud.modelserver.common.codecs.DecodingException)

Example 4 with DecodingException

use of org.eclipse.emfcloud.modelserver.common.codecs.DecodingException in project emfcloud-modelserver by eclipse-emfcloud.

the class DefaultModelController method executePatchCommand.

protected void executePatchCommand(final Context ctx, final String modelURI, final EObject root, final PatchCommand<?> pCommand) {
    CCommandExecutionResult result;
    if (isCCommand(pCommand)) {
        try {
            result = this.modelRepository.executeCommand(modelURI, getCCommand(pCommand));
        } catch (DecodingException ex) {
            decodingError(ctx, ex);
            return;
        }
    } else if (isJsonPatch(pCommand)) {
        ArrayNode jsonPatch = getJsonPatch(pCommand);
        try {
            result = this.modelRepository.executeCommand(modelURI, jsonPatch);
        } catch (JsonPatchTestException | JsonPatchException ex) {
            LOG.error(ex.getMessage(), ex);
            return;
        }
    } else {
        // TODO Handle unsupported Patch/Command type
        return;
    }
    JsonNode patchResult = getJSONPatchUpdate(ctx, modelURI, root, result);
    if (patchResult != null) {
        successPatch(ctx, patchResult, "Model '%s' successfully updated", modelURI);
        sessionController.commandExecuted(modelURI, Suppliers.ofInstance(result), Suppliers.ofInstance(patchResult));
    } else {
        success(ctx, "Model '%s' successfully updated", modelURI);
    }
}
Also used : CCommandExecutionResult(org.eclipse.emfcloud.modelserver.command.CCommandExecutionResult) DecodingException(org.eclipse.emfcloud.modelserver.common.codecs.DecodingException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode)

Example 5 with DecodingException

use of org.eclipse.emfcloud.modelserver.common.codecs.DecodingException in project emfcloud-modelserver by eclipse-emfcloud.

the class DefaultModelController method executeCommand.

@Override
public void executeCommand(final Context ctx, final String modelURI) {
    withModel(ctx, modelURI, root -> {
        Optional<CCommand> command = readPayload(ctx).filter(CCommand.class::isInstance).map(CCommand.class::cast);
        if (command.isEmpty()) {
            return;
        }
        try {
            CCommandExecutionResult execution = modelRepository.executeCommand(modelURI, command.get());
            success(ctx, "Model '%s' successfully updated", modelURI);
            sessionController.commandExecuted(modelURI, Suppliers.ofInstance(execution), Suppliers.memoize(() -> getJSONPatchUpdate(ctx, modelURI, root, execution)));
        } catch (DecodingException exception) {
            decodingError(ctx, exception);
        }
    });
}
Also used : CCommandExecutionResult(org.eclipse.emfcloud.modelserver.command.CCommandExecutionResult) CCommand(org.eclipse.emfcloud.modelserver.command.CCommand) DecodingException(org.eclipse.emfcloud.modelserver.common.codecs.DecodingException)

Aggregations

DecodingException (org.eclipse.emfcloud.modelserver.common.codecs.DecodingException)8 URI (org.eclipse.emf.common.util.URI)3 EObject (org.eclipse.emf.ecore.EObject)3 Resource (org.eclipse.emf.ecore.resource.Resource)3 CCommand (org.eclipse.emfcloud.modelserver.command.CCommand)3 CCommandExecutionResult (org.eclipse.emfcloud.modelserver.command.CCommandExecutionResult)3 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 IOException (java.io.IOException)2 Command (org.eclipse.emf.common.command.Command)2 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 Lists (com.google.common.collect.Lists)1 AbstractModule (com.google.inject.AbstractModule)1 Guice (com.google.inject.Guice)1 Scopes (com.google.inject.Scopes)1 Multibinder (com.google.inject.multibindings.Multibinder)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1