Search in sources :

Example 1 with SqlTransactionCallback

use of com.bedatadriven.rebar.sql.client.SqlTransactionCallback in project activityinfo by bedatadriven.

the class CommandQueueTest method testUpdateSite.

@Test
public void testUpdateSite() {
    Map<String, Object> changes = new HashMap<String, Object>();
    changes.put("anInt", 34);
    changes.put("aString", "testing");
    final UpdateSite cmd = new UpdateSite(99, changes);
    db.transaction(new SqlTransactionCallback() {

        @Override
        public void begin(SqlTransaction tx) {
            queue.queue(tx, cmd);
        }
    });
    Collector<CommandQueue.QueueEntry> reread = Collector.newCollector();
    queue.peek(reread);
    assertThat(reread.getResult(), not(nullValue()));
    assertThat(cmd, equalTo(reread.getResult().getCommand()));
}
Also used : SqlTransactionCallback(com.bedatadriven.rebar.sql.client.SqlTransactionCallback) HashMap(java.util.HashMap) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) UpdateSite(org.activityinfo.legacy.shared.command.UpdateSite) Test(org.junit.Test)

Example 2 with SqlTransactionCallback

use of com.bedatadriven.rebar.sql.client.SqlTransactionCallback in project activityinfo by bedatadriven.

the class CommandQueueTest method testCreateSite.

@Test
public void testCreateSite() {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("anInt", 34);
    properties.put("aString", "testing");
    properties.put("aDouble", 3.0);
    properties.put("aBoolean", true);
    properties.put("anotherBoolean", false);
    properties.put("aTime", new Date());
    properties.put("aDate", new LocalDate(2011, 3, 15));
    final CreateSite cmd = new CreateSite(properties);
    db.transaction(new SqlTransactionCallback() {

        @Override
        public void begin(SqlTransaction tx) {
            queue.queue(tx, cmd);
        }
    });
    Collector<CommandQueue.QueueEntry> reread = Collector.newCollector();
    queue.peek(reread);
    assertThat(reread.getResult(), not(nullValue()));
    assertThat(cmd, equalTo(reread.getResult().getCommand()));
    Collector<Void> deleted = Collector.newCollector();
    queue.remove(reread.getResult(), deleted);
    Collector<CommandQueue.QueueEntry> entry2 = Collector.newCollector();
    queue.peek(entry2);
    assertThat(entry2.getResult(), is(nullValue()));
}
Also used : SqlTransactionCallback(com.bedatadriven.rebar.sql.client.SqlTransactionCallback) HashMap(java.util.HashMap) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) LocalDate(com.bedatadriven.rebar.time.calendar.LocalDate) Date(java.util.Date) LocalDate(com.bedatadriven.rebar.time.calendar.LocalDate) CreateSite(org.activityinfo.legacy.shared.command.CreateSite) Test(org.junit.Test)

Example 3 with SqlTransactionCallback

use of com.bedatadriven.rebar.sql.client.SqlTransactionCallback in project activityinfo by bedatadriven.

the class LocalDispatcher method executeOffline.

/**
 * Begins a new transaction, initializes a new ExecutionContext, and
 * executes the root command.
 *
 * @param command
 * @param callback
 */
private <R extends CommandResult> void executeOffline(final Command<R> command, final AsyncCallback<R> callback) {
    Log.debug("Executing command " + command + " OFFLINE.");
    try {
        final Collector<R> commandResult = Collector.newCollector();
        database.transaction(new SqlTransactionCallback() {

            @Override
            public void begin(SqlTransaction tx) {
                LocalExecutionContext context = new LocalExecutionContext(auth, tx, registry, commandQueue);
                context.execute(command, commandResult);
            }

            @Override
            public void onError(SqlException e) {
                Log.error("OFFLINE EXECUTION FAILED: " + command, e);
                callback.onFailure(e);
            }

            @Override
            public void onSuccess() {
                callback.onSuccess(commandResult.getResult());
            }
        });
    } catch (Exception e) {
        callback.onFailure(e);
    }
}
Also used : SqlTransactionCallback(com.bedatadriven.rebar.sql.client.SqlTransactionCallback) SqlException(com.bedatadriven.rebar.sql.client.SqlException) SqlTransaction(com.bedatadriven.rebar.sql.client.SqlTransaction) SqlException(com.bedatadriven.rebar.sql.client.SqlException)

Aggregations

SqlTransaction (com.bedatadriven.rebar.sql.client.SqlTransaction)3 SqlTransactionCallback (com.bedatadriven.rebar.sql.client.SqlTransactionCallback)3 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 SqlException (com.bedatadriven.rebar.sql.client.SqlException)1 LocalDate (com.bedatadriven.rebar.time.calendar.LocalDate)1 Date (java.util.Date)1 CreateSite (org.activityinfo.legacy.shared.command.CreateSite)1 UpdateSite (org.activityinfo.legacy.shared.command.UpdateSite)1