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());
}
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;
}
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);
}
}
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);
}
}
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;
});
}
Aggregations