Search in sources :

Example 1 with CCommand

use of org.eclipse.emfcloud.modelserver.command.CCommand in project emfcloud-modelserver by eclipse-emfcloud.

the class DefaultModelControllerTest method executeCommand.

@Test
public void executeCommand() throws EncodingException, DecodingException {
    ResourceSet rset = new ResourceSetImpl();
    String modeluri = "SuperBrewer3000.json";
    JsonResource res = createJsonResource(modeluri);
    rset.getResources().add(res);
    final EClass task = EcoreFactory.eINSTANCE.createEClass();
    res.getContents().add(task);
    CCommand setCommand = CCommandFactory.eINSTANCE.createCommand();
    setCommand.setType(EMFCommandType.SET);
    setCommand.setOwner(task);
    setCommand.setFeature("name");
    setCommand.getDataValues().add("Foo");
    JsonResource cmdRes = new JsonResource(URI.createURI("$command.json"));
    cmdRes.getContents().add(setCommand);
    final LinkedHashMap<String, List<String>> queryParams = new LinkedHashMap<>();
    queryParams.put(ModelServerPathParametersV1.MODEL_URI, Collections.singletonList(modeluri));
    when(context.queryParamMap()).thenReturn(queryParams);
    when(context.body()).thenReturn(Json.object(Json.prop(JsonResponseMember.DATA, new JsonCodec().encode(setCommand))).toString());
    when(modelRepository.getModel(modeluri)).thenReturn(Optional.of(task));
    CCommandExecutionResult result = CCommandFactory.eINSTANCE.createCommandExecutionResult();
    result.setSource(EcoreUtil.copy(setCommand));
    result.setType(CommandExecutionType.EXECUTE);
    when(modelRepository.executeCommand(eq(modeluri), any(CCommand.class))).thenReturn(result);
    modelController.executeCommand(context, modeluri);
    // unload to proxify
    res.unload();
    verify(modelRepository).executeCommand(eq(modeluri), argThat(eEqualTo(setCommand)));
    // No subscribers registered for this incrementalUpdate, therefore no pre-encoded commands are created and the map
    // can remain empty for this test
    verify(sessionController).commandExecuted(eq(modeluri), eq(Suppliers.ofInstance(result)), anySupplier());
}
Also used : EClass(org.eclipse.emf.ecore.EClass) JsonCodec(org.eclipse.emfcloud.modelserver.emf.common.codecs.JsonCodec) CCommandExecutionResult(org.eclipse.emfcloud.modelserver.command.CCommandExecutionResult) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) CCommand(org.eclipse.emfcloud.modelserver.command.CCommand) JsonResource(org.eclipse.emfcloud.jackson.resource.JsonResource) List(java.util.List) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 2 with CCommand

use of org.eclipse.emfcloud.modelserver.command.CCommand in project emfcloud-modelserver by eclipse-emfcloud.

the class DefaultModelController method getCCommand.

private CCommand getCCommand(final PatchCommand<?> pCommand) {
    Object data = pCommand.getData();
    if (data instanceof CCommand) {
        CCommand cCommand = (CCommand) data;
        resolveWorkspaceURIs(cCommand);
        return cCommand;
    }
    return null;
}
Also used : CCommand(org.eclipse.emfcloud.modelserver.command.CCommand) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject)

Example 3 with CCommand

use of org.eclipse.emfcloud.modelserver.command.CCommand in project emfcloud-modelserver by eclipse-emfcloud.

the class DefaultModelController method resolveWorkspaceURIs.

/**
 * In V1 (And with the Theia client), clients were expected to include
 * the complete URIs when referencing EObjects (including the workspace
 * path prefix). With V2 and standalone client, the client no longer has
 * knowledge about the workspace location, and may not be able to provide
 * full URIs. Resolve relative URIs against the current workspace root.
 *
 * @param jsonResource
 */
protected void resolveWorkspaceURIs(final CCommand cCommand) {
    if (serverConfiguration.getWorkspaceRootURI() == null) {
        return;
    }
    TreeIterator<EObject> allContents = cCommand.eAllContents();
    doResolveWorkspaceURIs(cCommand);
    while (allContents.hasNext()) {
        EObject next = allContents.next();
        new CommandSwitch<Void>() {

            @Override
            public Void caseCommand(final CCommand object) {
                doResolveWorkspaceURIs(object);
                return null;
            }
        }.doSwitch(next);
    }
}
Also used : CCommand(org.eclipse.emfcloud.modelserver.command.CCommand) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject)

Example 4 with CCommand

use of org.eclipse.emfcloud.modelserver.command.CCommand 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 5 with CCommand

use of org.eclipse.emfcloud.modelserver.command.CCommand in project emfcloud-modelserver by eclipse-emfcloud.

the class DefaultModelResourceManager method undo.

@Override
public Optional<CCommandExecutionResult> undo(final String modeluri) {
    ResourceSet resourceSet = getResourceSet(modeluri);
    ModelServerEditingDomain domain = getEditingDomain(resourceSet);
    Optional<Command> undoCommand = domain.getUndoableCommand();
    if (undoCommand.isEmpty()) {
        return Optional.empty();
    }
    Optional<CCommand> clientCommand = ModelServerCommand.getClientCommand(undoCommand.get());
    // Note: In API V2, when applying changes to the model via Json Patch, we don't have a client command
    Optional<CommandExecutionContext> context = undoCommand(domain, undoCommand.get(), clientCommand);
    return context.map(ctx -> {
        CCommandExecutionResult result = createExecutionResult(ctx);
        ReadResourceSet readResourceSet = new ReadResourceSet(domain);
        readResourceSet.resolve(result, "$command.undo.res");
        return result;
    });
}
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) CCommand(org.eclipse.emfcloud.modelserver.command.CCommand) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet)

Aggregations

CCommand (org.eclipse.emfcloud.modelserver.command.CCommand)49 EClass (org.eclipse.emf.ecore.EClass)9 Command (org.eclipse.emf.common.command.Command)8 CCommandExecutionResult (org.eclipse.emfcloud.modelserver.command.CCommandExecutionResult)8 Test (org.junit.Test)8 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)7 EAttribute (org.eclipse.emf.ecore.EAttribute)5 InternalEObject (org.eclipse.emf.ecore.InternalEObject)5 CCompoundCommand (org.eclipse.emfcloud.modelserver.command.CCompoundCommand)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 EDataType (org.eclipse.emf.ecore.EDataType)4 JsonResource (org.eclipse.emfcloud.jackson.resource.JsonResource)4 ModelServerCommand (org.eclipse.emfcloud.modelserver.edit.ModelServerCommand)4 IOException (java.io.IOException)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 Rule (okhttp3.mock.Rule)3 Buffer (okio.Buffer)3 CompoundCommand (org.eclipse.emf.common.command.CompoundCommand)3 EClassifier (org.eclipse.emf.ecore.EClassifier)3