Search in sources :

Example 1 with Command

use of org.activityinfo.legacy.shared.command.Command in project activityinfo by bedatadriven.

the class BatchCommandHandlerAsync method execute.

@Override
public void execute(BatchCommand batch, ExecutionContext context, final AsyncCallback<BatchResult> callback) {
    if (batch.getCommands().isEmpty()) {
        LOGGER.warning("Received empty batch command");
        callback.onSuccess(new BatchResult(Lists.<CommandResult>newArrayList()));
    } else {
        final ArrayList<CommandResult> results = new ArrayList<>();
        for (Command command : batch.getCommands()) {
            results.add(null);
        }
        final boolean[] finished = new boolean[batch.getCommands().size()];
        final List<Throwable> exceptions = Lists.newArrayList();
        for (int i = 0; i != batch.getCommands().size(); ++i) {
            final int commandIndex = i;
            context.execute(batch.getCommands().get(i), new AsyncCallback<CommandResult>() {

                @Override
                public void onFailure(Throwable caught) {
                    if (exceptions.isEmpty()) {
                        exceptions.add(caught);
                        callback.onFailure(caught);
                    }
                }

                @Override
                public void onSuccess(CommandResult result) {
                    results.set(commandIndex, result);
                    finished[commandIndex] = true;
                    if (all(finished)) {
                        callback.onSuccess(new BatchResult(results));
                    }
                }
            });
        }
    }
}
Also used : Command(org.activityinfo.legacy.shared.command.Command) BatchCommand(org.activityinfo.legacy.shared.command.BatchCommand) ArrayList(java.util.ArrayList) BatchResult(org.activityinfo.legacy.shared.command.result.BatchResult) CommandResult(org.activityinfo.legacy.shared.command.result.CommandResult)

Example 2 with Command

use of org.activityinfo.legacy.shared.command.Command in project activityinfo by bedatadriven.

the class JsonRpcServlet method deserialize.

private Command deserialize(String json) {
    Command command;
    try {
        command = objectMapper.readValue(json, Command.class);
    } catch (BadRpcRequest e) {
        LOGGER.log(Level.SEVERE, "Failed to deserialize command", e);
        throw e;
    } catch (Exception e) {
        LOGGER.log(Level.SEVERE, "Failed to deserialize command", e);
        throw new BadRpcRequest("Unexpected exception deserializing the command.");
    }
    validator.assertValid(command);
    return command;
}
Also used : Command(org.activityinfo.legacy.shared.command.Command) IllegalAccessCommandException(org.activityinfo.legacy.shared.exception.IllegalAccessCommandException) CommandException(org.activityinfo.legacy.shared.exception.CommandException) WebApplicationException(javax.ws.rs.WebApplicationException)

Example 3 with Command

use of org.activityinfo.legacy.shared.command.Command in project activityinfo by bedatadriven.

the class JsonRpcServlet method execute.

@POST
public Response execute(String json) {
    // All RPC Commands require authentication
    if (user.get().isAnonymous()) {
        return Response.status(Response.Status.UNAUTHORIZED).build();
    }
    Command command = deserialize(json);
    CommandResult result = execute(command);
    return serializeResult(result);
}
Also used : Command(org.activityinfo.legacy.shared.command.Command) CommandResult(org.activityinfo.legacy.shared.command.result.CommandResult) POST(javax.ws.rs.POST)

Example 4 with Command

use of org.activityinfo.legacy.shared.command.Command in project activityinfo by bedatadriven.

the class DispatcherStub method execute.

@Override
public <T extends CommandResult> void execute(Command<T> command, AsyncCallback<T> callback) {
    if (command instanceof BatchCommand) {
        BatchCommand batch = (BatchCommand) command;
        List<CommandResult> results = new ArrayList<CommandResult>();
        for (Command batchCmd : batch.getCommands()) {
            results.add(findResult(batchCmd));
        }
        callback.onSuccess((T) new BatchResult(results));
    } else {
        callback.onSuccess((T) findResult(command));
    }
}
Also used : Command(org.activityinfo.legacy.shared.command.Command) BatchCommand(org.activityinfo.legacy.shared.command.BatchCommand) ArrayList(java.util.ArrayList) BatchCommand(org.activityinfo.legacy.shared.command.BatchCommand) BatchResult(org.activityinfo.legacy.shared.command.result.BatchResult) CommandResult(org.activityinfo.legacy.shared.command.result.CommandResult)

Example 5 with Command

use of org.activityinfo.legacy.shared.command.Command in project activityinfo by bedatadriven.

the class MockRemoteCommandService method execute.

@Override
public void execute(String authToken, String locale, List<Command> cmds, AsyncCallback<List<CommandResult>> callback) {
    List<CommandResult> results = new ArrayList<CommandResult>();
    for (Command cmd : cmds) {
        Integer count = commandCounts.get(cmd.getClass());
        commandCounts.put(cmd.getClass(), count == null ? 1 : count + 1);
        if (schema != null && cmd instanceof GetSchema) {
            results.add(schema);
        } else {
            results.add(mockExecute(cmd));
        }
    }
    callback.onSuccess(results);
}
Also used : Command(org.activityinfo.legacy.shared.command.Command) ArrayList(java.util.ArrayList) GetSchema(org.activityinfo.legacy.shared.command.GetSchema) CommandResult(org.activityinfo.legacy.shared.command.result.CommandResult)

Aggregations

Command (org.activityinfo.legacy.shared.command.Command)6 CommandResult (org.activityinfo.legacy.shared.command.result.CommandResult)4 ArrayList (java.util.ArrayList)3 BatchCommand (org.activityinfo.legacy.shared.command.BatchCommand)2 BatchResult (org.activityinfo.legacy.shared.command.result.BatchResult)2 IOException (java.io.IOException)1 POST (javax.ws.rs.POST)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 GetSchema (org.activityinfo.legacy.shared.command.GetSchema)1 CommandException (org.activityinfo.legacy.shared.exception.CommandException)1 IllegalAccessCommandException (org.activityinfo.legacy.shared.exception.IllegalAccessCommandException)1 JsonNode (org.codehaus.jackson.JsonNode)1 JsonProcessingException (org.codehaus.jackson.JsonProcessingException)1 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 UnrecognizedPropertyException (org.codehaus.jackson.map.exc.UnrecognizedPropertyException)1 ObjectNode (org.codehaus.jackson.node.ObjectNode)1