Example usage for android.database Cursor isNull

List of usage examples for android.database Cursor isNull

Introduction

In this page you can find the example usage for android.database Cursor isNull.

Prototype

boolean isNull(int columnIndex);

Source Link

Document

Returns true if the value in the indicated column is null.

Usage

From source file:org.opendatakit.utilities.test.AbstractODKDatabaseUtilsTest.java

public void testGetTableDefinitionEntry_ExpectPass() throws ActionNotAuthorizedException {
    String tableId = testTable;/*from   w  w w .jav a  2  s  .  c  om*/
    String testCol = "testColumn";
    String testColType = ElementDataType.integer.name();
    List<Column> columns = new ArrayList<Column>();
    columns.add(new Column(testCol, testCol, testColType, "[]"));
    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST);

    int testVal = 5;

    ContentValues cvValues = new ContentValues();
    String rowId = LocalizationUtils.genUUID();
    cvValues.put(testCol, testVal);
    ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues, rowId, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    String sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    String[] selArgs = { "" + testVal };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);
    assertEquals(cursor.getCount(), 1);

    int val = 0;
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
        val = cursor.getInt(ind);
    }

    assertEquals(val, testVal);

    ODKDatabaseImplUtils.AccessContext accessContextNoTableId = ODKDatabaseImplUtils.get().getAccessContext(db,
            null, activeUser, RoleConsts.ADMIN_ROLES_LIST);

    // Select everything out of the table
    String sel2 = "SELECT * FROM " + DatabaseConstants.TABLE_DEFS_TABLE_NAME + " WHERE "
            + TableDefinitionsColumns.TABLE_ID + " = ?";
    String[] selArgs2 = { "" + tableId };
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel2, selArgs2, null, accessContextNoTableId);
    assertEquals(cursor.getCount(), 1);

    String syncTimeVal = null;
    String schemaETagVal = null;
    String lastDataETagVal = null;
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(TableDefinitionsColumns.LAST_SYNC_TIME);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        syncTimeVal = cursor.getString(ind);

        ind = cursor.getColumnIndex(TableDefinitionsColumns.SCHEMA_ETAG);
        assertTrue(cursor.isNull(ind));

        ind = cursor.getColumnIndex(TableDefinitionsColumns.LAST_DATA_ETAG);
        assertTrue(cursor.isNull(ind));
    }

    // Now get the table definition entry
    TableDefinitionEntry tde = ODKDatabaseImplUtils.get().getTableDefinitionEntry(db, tableId);

    // Compare the table definition entry and the raw query results
    assertEquals(syncTimeVal, tde.getLastSyncTime());
    assertEquals(schemaETagVal, tde.getSchemaETag());
    assertEquals(lastDataETagVal, tde.getLastDataETag());

    // Drop the table now that the test is done
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId);
}

From source file:org.opendatakit.utilities.test.AbstractODKDatabaseUtilsTest.java

public void testInsertCheckpointRowIntoExistingTableWithIdWhenRowAlreadyExists_ExpectPass()
        throws ActionNotAuthorizedException {
    String tableId = testTable;/*from  w  w w  . j av a 2s  . c  o  m*/
    String testCol = "testColumn";
    String testColType = ElementDataType.string.name();
    String testVal = "test";
    String testVal2 = "test2";
    String rowId = LocalizationUtils.genUUID();
    List<Column> columns = new ArrayList<Column>();
    columns.add(new Column(testCol, testCol, testColType, "[]"));
    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST);

    ContentValues cvValues = new ContentValues();
    cvValues.put(testCol, testVal);
    ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues, rowId, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    String sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    String[] selArgs = { "" + testVal };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    String val = null;
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        val = cursor.getString(ind);
    }

    assertEquals(val, testVal);

    ContentValues updatedCvValues = new ContentValues();
    updatedCvValues.put(testCol, testVal2);
    ODKDatabaseImplUtils.get().insertCheckpointRowWithId(db, tableId, orderedColumns, updatedCvValues, rowId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    sel = "SELECT * FROM " + tableId;
    selArgs = new String[0];
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    assertEquals(cursor.getCount(), 2);

    // Select everything out of the table
    sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    selArgs = new String[1];
    selArgs[0] = "" + testVal2;
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    String val2 = null;
    String saveptType = null;
    while (cursor.moveToNext()) {
        // Get the actual value
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        val2 = cursor.getString(ind);

        // Get the savepoint_type
        ind = cursor.getColumnIndex(DataTableColumns.SAVEPOINT_TYPE);
        assertTrue(cursor.isNull(ind));

        // Get the conflict_type and make sure that it is null
        ind = cursor.getColumnIndex(DataTableColumns.CONFLICT_TYPE);
        assertTrue(cursor.isNull(ind));
    }

    assertEquals(val2, testVal2);

    // Also make sure that the savepoint_type
    // is empty
    assertNotSame(saveptType, SavepointTypeManipulator.incomplete());
    assertNotSame(saveptType, SavepointTypeManipulator.complete());

    // Drop the table now that the test is done
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId);
}

From source file:org.opendatakit.utilities.test.AbstractODKDatabaseUtilsTest.java

public void testGetTableHealthWhenTableHasChkpts_ExpectPass() throws ActionNotAuthorizedException {
    String tableId = testTable;/*from   ww  w.jav a2s .c  o m*/
    String testCol = "testColumn";
    String testColType = ElementDataType.integer.name();
    List<Column> columns = new ArrayList<Column>();
    columns.add(new Column(testCol, testCol, testColType, "[]"));
    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST);

    int testVal = 5;

    ContentValues cvValues = new ContentValues();
    String rowId = LocalizationUtils.genUUID();
    cvValues.put(testCol, testVal);
    ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues, rowId, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    String sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    String[] selArgs = { "" + testVal };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    int val = 0;
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
        val = cursor.getInt(ind);
    }

    assertEquals(val, testVal);

    int testVal2 = 200;
    ContentValues updatedCvValues = new ContentValues();
    updatedCvValues.put(testCol, testVal2);
    ODKDatabaseImplUtils.get().insertCheckpointRowWithId(db, tableId, orderedColumns, updatedCvValues, rowId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    sel = "SELECT * FROM " + tableId;
    selArgs = new String[0];
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    assertEquals(cursor.getCount(), 2);

    // Select everything out of the table
    sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    selArgs = new String[1];
    selArgs[0] = "" + testVal2;
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    int val2 = 0;
    String saveptType = null;
    while (cursor.moveToNext()) {
        // Get the actual value
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
        val2 = cursor.getInt(ind);

        // Get the savepoint_type
        ind = cursor.getColumnIndex(DataTableColumns.SAVEPOINT_TYPE);
        assertTrue(cursor.isNull(ind));

        // Get the conflict_type and make sure that it is null
        ind = cursor.getColumnIndex(DataTableColumns.CONFLICT_TYPE);
        assertTrue(cursor.isNull(ind));
    }

    assertEquals(val2, testVal2);

    // Also make sure that the savepoint_type
    // is empty
    assertNotSame(saveptType, SavepointTypeManipulator.incomplete());
    assertNotSame(saveptType, SavepointTypeManipulator.complete());

    // Test that the health of the table is CLEAN
    int health = ODKDatabaseImplUtils.get().getTableHealth(db, tableId);

    assertEquals(health, CursorUtils.TABLE_HEALTH_HAS_CHECKPOINTS);

    // Drop the table now that the test is done
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId);
}

From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java

@Test
public void testGetTableDefinitionEntry_ExpectPass() throws ActionNotAuthorizedException {
    String tableId = testTable;//from   w  w w.ja  v  a  2 s. c  o  m
    String testCol = "testColumn";
    String testColType = ElementDataType.integer.name();
    List<Column> columns = new ArrayList<Column>();
    columns.add(new Column(testCol, testCol, testColType, "[]"));
    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST);

    int testVal = 5;

    ContentValues cvValues = new ContentValues();
    String rowId = LocalizationUtils.genUUID();
    cvValues.put(testCol, testVal);
    ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues, rowId, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    String sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    String[] selArgs = { "" + testVal };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);
    assertEquals(cursor.getCount(), 1);

    int val = 0;
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
        val = cursor.getInt(ind);
    }

    assertEquals(val, testVal);

    ODKDatabaseImplUtils.AccessContext accessContextNoTableId = ODKDatabaseImplUtils.get().getAccessContext(db,
            null, activeUser, RoleConsts.ADMIN_ROLES_LIST);

    // Select everything out of the table
    String sel2 = "SELECT * FROM " + DatabaseConstants.TABLE_DEFS_TABLE_NAME + " WHERE "
            + TableDefinitionsColumns.TABLE_ID + " = ?";
    String[] selArgs2 = { "" + tableId };
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel2, selArgs2, null, accessContextNoTableId);
    assertEquals(cursor.getCount(), 1);

    String syncTimeVal = null;
    String schemaETagVal = null;
    String lastDataETagVal = null;
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(TableDefinitionsColumns.LAST_SYNC_TIME);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        syncTimeVal = cursor.getString(ind);

        ind = cursor.getColumnIndex(TableDefinitionsColumns.SCHEMA_ETAG);
        assertTrue(cursor.isNull(ind));

        ind = cursor.getColumnIndex(TableDefinitionsColumns.LAST_DATA_ETAG);
        assertTrue(cursor.isNull(ind));
    }

    // Now get the table definition entry
    TableDefinitionEntry tde = ODKDatabaseImplUtils.get().getTableDefinitionEntry(db, tableId);

    // Compare the table definition entry and the raw query results
    assertEquals(syncTimeVal, tde.getLastSyncTime());
    assertEquals(schemaETagVal, tde.getSchemaETag());
    assertEquals(lastDataETagVal, tde.getLastDataETag());

    // Drop the table now that the test is done
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId);
}

From source file:org.opendatakit.services.database.utlities.ODKDatabaseImplUtils.java

/**
 * Inserts a checkpoint row for the given rowId in the tableId. Checkpoint
 * rows are created by ODK Survey to hold intermediate values during the
 * filling-in of the form. They act as restore points in the Survey, should
 * the application die.//from  w ww  .ja v  a 2  s  .c  o m
 *
 * @param db
 * @param tableId
 * @param orderedColumns
 * @param cvValues
 * @param rowId
 * @param activeUser
 * @param rolesList
 * @param locale
 */
public void insertCheckpointRowWithId(OdkConnectionInterface db, String tableId, OrderedColumns orderedColumns,
        ContentValues cvValues, String rowId, String activeUser, String rolesList, String locale)
        throws ActionNotAuthorizedException {

    if (cvValues.size() <= 0) {
        throw new IllegalArgumentException(t + ": No values to add into table for checkpoint" + tableId);
    }

    // these are all managed in the database layer...
    // the user should NOT set them...

    if (cvValues.containsKey(DataTableColumns.SAVEPOINT_TIMESTAMP)) {
        throw new IllegalArgumentException(
                t + ": No user supplied savepoint timestamp can be included for a checkpoint");
    }

    if (cvValues.containsKey(DataTableColumns.SAVEPOINT_TYPE)) {
        throw new IllegalArgumentException(
                t + ": No user supplied savepoint type can be included for a checkpoint");
    }

    if (cvValues.containsKey(DataTableColumns.ROW_ETAG)) {
        throw new IllegalArgumentException(t + ": No user supplied row ETag can be included for a checkpoint");
    }

    if (cvValues.containsKey(DataTableColumns.SYNC_STATE)) {
        throw new IllegalArgumentException(
                t + ": No user supplied sync state can be included for a checkpoint");
    }

    if (cvValues.containsKey(DataTableColumns.CONFLICT_TYPE)) {
        throw new IllegalArgumentException(
                t + ": No user supplied conflict type can be included for a checkpoint");
    }

    if (cvValues.containsKey(DataTableColumns.FILTER_VALUE)) {
        throw new IllegalArgumentException(
                t + ": No user supplied filter value can be included for a checkpoint");
    }

    if (cvValues.containsKey(DataTableColumns.FILTER_TYPE)) {
        throw new IllegalArgumentException(
                t + ": No user supplied filter type can be included for a checkpoint");
    }

    // If a rowId is specified, a cursor will be needed to
    // get the current row to create a checkpoint with the relevant data
    Cursor c = null;
    try {
        // Allow the user to pass in no rowId if this is the first
        // checkpoint row that the user is adding
        if (rowId == null) {

            // TODO: is this even valid any more? I think we disallow this in the AIDL flow.

            String rowIdToUse = LocalizationUtils.genUUID();
            HashMap<String, Object> currValues = new HashMap<String, Object>();
            for (String key : cvValues.keySet()) {
                currValues.put(key, cvValues.get(key));
            }
            currValues.put(DataTableColumns._ID, rowIdToUse);
            currValues.put(DataTableColumns.SYNC_STATE, SyncState.new_row.name());
            insertCheckpointIntoExistingTable(db, tableId, orderedColumns, currValues, activeUser, rolesList,
                    locale, true, null, null);
            return;
        }

        StringBuilder b = new StringBuilder();
        b.append(K_DATATABLE_ID_EQUALS_PARAM).append(S_AND).append(DataTableColumns.SAVEPOINT_TIMESTAMP)
                .append(" IN (SELECT MAX(").append(DataTableColumns.SAVEPOINT_TIMESTAMP).append(") FROM ")
                .append(tableId).append(K_WHERE).append(K_DATATABLE_ID_EQUALS_PARAM).append(")");
        c = db.query(tableId, null, b.toString(), new Object[] { rowId, rowId }, null, null, null, null);
        c.moveToFirst();

        if (c.getCount() > 1) {
            throw new IllegalStateException(t + ": More than one checkpoint at a timestamp");
        }

        // Inserting a checkpoint for the first time
        if (c.getCount() <= 0) {
            HashMap<String, Object> currValues = new HashMap<String, Object>();
            for (String key : cvValues.keySet()) {
                currValues.put(key, cvValues.get(key));
            }
            currValues.put(DataTableColumns._ID, rowId);
            currValues.put(DataTableColumns.SYNC_STATE, SyncState.new_row.name());
            insertCheckpointIntoExistingTable(db, tableId, orderedColumns, currValues, activeUser, rolesList,
                    locale, true, null, null);
            return;
        } else {
            // Make sure that the conflict_type of any existing row
            // is null, otherwise throw an exception
            int conflictIndex = c.getColumnIndex(DataTableColumns.CONFLICT_TYPE);
            if (!c.isNull(conflictIndex)) {
                throw new IllegalStateException(
                        t + ":  A checkpoint cannot be added for a row that is in conflict");
            }

            HashMap<String, Object> currValues = new HashMap<String, Object>();
            for (String key : cvValues.keySet()) {
                currValues.put(key, cvValues.get(key));
            }

            // This is unnecessary
            // We should only have one row at this point
            //c.moveToFirst();

            String priorFilterType = null;
            String priorFilterValue = null;

            // Get the number of columns to iterate over and add
            // those values to the content values
            for (int i = 0; i < c.getColumnCount(); i++) {
                String name = c.getColumnName(i);

                if (currValues.containsKey(name)) {
                    continue;
                }

                // omitting savepoint timestamp will generate a new timestamp.
                if (name.equals(DataTableColumns.SAVEPOINT_TIMESTAMP)) {
                    continue;
                }

                // set savepoint type to null to mark this as a checkpoint
                if (name.equals(DataTableColumns.SAVEPOINT_TYPE)) {
                    currValues.put(name, null);
                    continue;
                }

                // sync state (a non-null field) should either remain 'new_row'
                // or be set to 'changed' for all other existing values.
                if (name.equals(DataTableColumns.SYNC_STATE)) {
                    String priorState = c.getString(i);
                    if (priorState.equals(SyncState.new_row.name())) {
                        currValues.put(name, SyncState.new_row.name());
                    } else {
                        currValues.put(name, SyncState.changed.name());
                    }
                    continue;
                }

                if (c.isNull(i)) {
                    currValues.put(name, null);
                    continue;
                }

                // otherwise, just copy the values over...
                Class<?> theClass = CursorUtils.getIndexDataType(c, i);
                Object object = CursorUtils.getIndexAsType(c, theClass, i);
                insertValueIntoContentValues(currValues, theClass, name, object);

                if (name.equals(DataTableColumns.FILTER_TYPE)) {
                    priorFilterType = c.getString(i);
                }

                if (name.equals(DataTableColumns.FILTER_VALUE)) {
                    priorFilterValue = c.getString(i);
                }
            }

            insertCheckpointIntoExistingTable(db, tableId, orderedColumns, currValues, activeUser, rolesList,
                    locale, false, priorFilterType, priorFilterValue);
        }
    } finally {
        if (c != null && !c.isClosed()) {
            c.close();
        }
    }
}

From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java

@Test
public void testInsertCheckpointRowIntoExistingTableWithIdWhenRowAlreadyExists_ExpectPass()
        throws ActionNotAuthorizedException {
    String tableId = testTable;/*from   w w  w .j  a v a 2s. c o m*/
    String testCol = "testColumn";
    String testColType = ElementDataType.string.name();
    String testVal = "test";
    String testVal2 = "test2";
    String rowId = LocalizationUtils.genUUID();
    List<Column> columns = new ArrayList<Column>();
    columns.add(new Column(testCol, testCol, testColType, "[]"));
    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST);

    ContentValues cvValues = new ContentValues();
    cvValues.put(testCol, testVal);
    ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues, rowId, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    String sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    String[] selArgs = { "" + testVal };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    String val = null;
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        val = cursor.getString(ind);
    }

    assertEquals(val, testVal);

    ContentValues updatedCvValues = new ContentValues();
    updatedCvValues.put(testCol, testVal2);
    ODKDatabaseImplUtils.get().insertCheckpointRowWithId(db, tableId, orderedColumns, updatedCvValues, rowId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    sel = "SELECT * FROM " + tableId;
    selArgs = new String[0];
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    assertEquals(cursor.getCount(), 2);

    // Select everything out of the table
    sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    selArgs = new String[1];
    selArgs[0] = "" + testVal2;
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    String val2 = null;
    String saveptType = null;
    while (cursor.moveToNext()) {
        // Get the actual value
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        val2 = cursor.getString(ind);

        // Get the savepoint_type
        ind = cursor.getColumnIndex(DataTableColumns.SAVEPOINT_TYPE);
        assertTrue(cursor.isNull(ind));

        // Get the conflict_type and make sure that it is null
        ind = cursor.getColumnIndex(DataTableColumns.CONFLICT_TYPE);
        assertTrue(cursor.isNull(ind));
    }

    assertEquals(val2, testVal2);

    // Also make sure that the savepoint_type
    // is empty
    assertNotSame(saveptType, SavepointTypeManipulator.incomplete());
    assertNotSame(saveptType, SavepointTypeManipulator.complete());

    // Drop the table now that the test is done
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId);
}

From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java

@Test
public void testGetTableHealthWhenTableHasChkpts_ExpectPass() throws ActionNotAuthorizedException {
    String tableId = testTable;/*from  www.j a va 2 s  .  c om*/
    String testCol = "testColumn";
    String testColType = ElementDataType.integer.name();
    List<Column> columns = new ArrayList<Column>();
    columns.add(new Column(testCol, testCol, testColType, "[]"));
    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST);

    int testVal = 5;

    ContentValues cvValues = new ContentValues();
    String rowId = LocalizationUtils.genUUID();
    cvValues.put(testCol, testVal);
    ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues, rowId, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    String sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    String[] selArgs = { "" + testVal };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    int val = 0;
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
        val = cursor.getInt(ind);
    }

    assertEquals(val, testVal);

    int testVal2 = 200;
    ContentValues updatedCvValues = new ContentValues();
    updatedCvValues.put(testCol, testVal2);
    ODKDatabaseImplUtils.get().insertCheckpointRowWithId(db, tableId, orderedColumns, updatedCvValues, rowId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    sel = "SELECT * FROM " + tableId;
    selArgs = new String[0];
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    assertEquals(cursor.getCount(), 2);

    // Select everything out of the table
    sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    selArgs = new String[1];
    selArgs[0] = "" + testVal2;
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    int val2 = 0;
    String saveptType = null;
    while (cursor.moveToNext()) {
        // Get the actual value
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_INTEGER);
        val2 = cursor.getInt(ind);

        // Get the savepoint_type
        ind = cursor.getColumnIndex(DataTableColumns.SAVEPOINT_TYPE);
        assertTrue(cursor.isNull(ind));

        // Get the conflict_type and make sure that it is null
        ind = cursor.getColumnIndex(DataTableColumns.CONFLICT_TYPE);
        assertTrue(cursor.isNull(ind));
    }

    assertEquals(val2, testVal2);

    // Also make sure that the savepoint_type
    // is empty
    assertNotSame(saveptType, SavepointTypeManipulator.incomplete());
    assertNotSame(saveptType, SavepointTypeManipulator.complete());

    // Test that the health of the table is CLEAN
    int health = ODKDatabaseImplUtils.get().getTableHealth(db, tableId);

    assertTrue(CursorUtils.getTableHealthHasChanges(health));

    // Drop the table now that the test is done
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId);
}

From source file:org.opendatakit.utilities.test.AbstractODKDatabaseUtilsTest.java

public void testSaveAsCompleteMostRecentCheckpointDataInTableWithId_ExpectPass()
        throws ActionNotAuthorizedException {
    String tableId = testTable;//from  ww  w  .jav  a  2 s. com
    String testCol = "testColumn";
    String testColType = ElementDataType.string.name();
    String testVal = "test";
    String testVal2 = "test2";
    String rowId = LocalizationUtils.genUUID();
    List<Column> columns = new ArrayList<Column>();
    columns.add(new Column(testCol, testCol, testColType, "[]"));
    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST);

    ContentValues cvValues = new ContentValues();
    cvValues.put(testCol, testVal);
    ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues, rowId, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    String sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    String[] selArgs = { "" + testVal };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    String val = null;
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        val = cursor.getString(ind);
    }

    assertEquals(val, testVal);

    ContentValues updatedCvValues = new ContentValues();
    updatedCvValues.put(testCol, testVal2);
    ODKDatabaseImplUtils.get().insertCheckpointRowWithId(db, tableId, orderedColumns, updatedCvValues, rowId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    sel = "SELECT * FROM " + tableId;
    selArgs = new String[0];
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    assertEquals(cursor.getCount(), 2);

    // Select everything out of the table
    sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    selArgs = new String[1];
    selArgs[0] = "" + testVal2;
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    String val2 = null;
    String saveptType = null;
    while (cursor.moveToNext()) {
        // Get the actual value
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        val2 = cursor.getString(ind);

        // Get the savepoint_timestamp
        ind = cursor.getColumnIndex(DataTableColumns.SAVEPOINT_TIMESTAMP);
        type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        saveptType = cursor.getString(ind);

        // Get the conflict_type and make sure that it is null
        ind = cursor.getColumnIndex(DataTableColumns.CONFLICT_TYPE);
        assertTrue(cursor.isNull(ind));
    }

    assertEquals(val2, testVal2);

    // Also make sure that the savepoint_type
    // is empty
    assertNotSame(saveptType, SavepointTypeManipulator.incomplete());
    assertNotSame(saveptType, SavepointTypeManipulator.complete());

    ODKDatabaseImplUtils.get().saveAsCompleteMostRecentCheckpointRowWithId(db, tableId, rowId);
    // Select everything out of the table
    sel = "SELECT * FROM " + tableId;
    selArgs = new String[0];
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    assertEquals(cursor.getCount(), 1);

    val2 = null;
    saveptType = null;
    while (cursor.moveToNext()) {
        // Get the actual value
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        val2 = cursor.getString(ind);

        // Get the savepoint_timestamp
        ind = cursor.getColumnIndex(DataTableColumns.SAVEPOINT_TYPE);
        type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        saveptType = cursor.getString(ind);

        // Get the conflict_type and make sure that it is null
        ind = cursor.getColumnIndex(DataTableColumns.CONFLICT_TYPE);
        assertTrue(cursor.isNull(ind));
    }

    assertEquals(val2, testVal2);
    assertEquals(saveptType, SavepointTypeManipulator.complete());

    // Drop the table now that the test is done
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId);
}

From source file:org.opendatakit.utilities.test.AbstractODKDatabaseUtilsTest.java

public void testSaveAsIncompleteMostRecentCheckpointDataInTableWithId_ExpectPass()
        throws ActionNotAuthorizedException {
    String tableId = testTable;//from   w w  w  .j av  a2s. c  om
    String testCol = "testColumn";
    String testColType = ElementDataType.string.name();
    String testVal = "test";
    String testVal2 = "test2";
    String rowId = LocalizationUtils.genUUID();
    List<Column> columns = new ArrayList<Column>();
    columns.add(new Column(testCol, testCol, testColType, "[]"));
    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST);

    ContentValues cvValues = new ContentValues();
    cvValues.put(testCol, testVal);
    ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues, rowId, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    String sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    String[] selArgs = { "" + testVal };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    String val = null;
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        val = cursor.getString(ind);
    }

    assertEquals(val, testVal);

    ContentValues updatedCvValues = new ContentValues();
    updatedCvValues.put(testCol, testVal2);
    ODKDatabaseImplUtils.get().insertCheckpointRowWithId(db, tableId, orderedColumns, updatedCvValues, rowId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    sel = "SELECT * FROM " + tableId;
    selArgs = new String[0];
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    assertEquals(cursor.getCount(), 2);

    // Select everything out of the table
    sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    selArgs = new String[1];
    selArgs[0] = "" + testVal2;
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    String val2 = null;
    String saveptType = null;
    while (cursor.moveToNext()) {
        // Get the actual value
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        val2 = cursor.getString(ind);

        // Get the savepoint_timestamp
        ind = cursor.getColumnIndex(DataTableColumns.SAVEPOINT_TIMESTAMP);
        type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        saveptType = cursor.getString(ind);

        // Get the conflict_type and make sure that it is null
        ind = cursor.getColumnIndex(DataTableColumns.CONFLICT_TYPE);
        assertTrue(cursor.isNull(ind));
    }

    assertEquals(val2, testVal2);

    // Also make sure that the savepoint_type
    // is empty
    assertNotSame(saveptType, SavepointTypeManipulator.incomplete());
    assertNotSame(saveptType, SavepointTypeManipulator.complete());

    ODKDatabaseImplUtils.get().saveAsIncompleteMostRecentCheckpointRowWithId(db, tableId, rowId);

    // Select everything out of the table
    sel = "SELECT * FROM " + tableId;
    selArgs = new String[0];
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    assertEquals(cursor.getCount(), 1);

    val2 = null;
    saveptType = null;
    while (cursor.moveToNext()) {
        // Get the actual value
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        val2 = cursor.getString(ind);

        // Get the savepoint_timestamp
        ind = cursor.getColumnIndex(DataTableColumns.SAVEPOINT_TYPE);
        type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        saveptType = cursor.getString(ind);

        // Get the conflict_type and make sure that it is null
        ind = cursor.getColumnIndex(DataTableColumns.CONFLICT_TYPE);
        assertTrue(cursor.isNull(ind));
    }

    assertEquals(val2, testVal2);
    assertEquals(saveptType, SavepointTypeManipulator.incomplete());

    // Drop the table now that the test is done
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId);
}

From source file:org.opendatakit.utilities.AbstractODKDatabaseUtilsTest.java

@Test
public void testSaveAsCompleteMostRecentCheckpointDataInTableWithId_ExpectPass()
        throws ActionNotAuthorizedException {
    String tableId = testTable;//from   w  w w  .j  av  a 2s .  co m
    String testCol = "testColumn";
    String testColType = ElementDataType.string.name();
    String testVal = "test";
    String testVal2 = "test2";
    String rowId = LocalizationUtils.genUUID();
    List<Column> columns = new ArrayList<Column>();
    columns.add(new Column(testCol, testCol, testColType, "[]"));
    OrderedColumns orderedColumns = ODKDatabaseImplUtils.get().createOrOpenTableWithColumns(db, tableId,
            columns);

    ODKDatabaseImplUtils.AccessContext accessContext = ODKDatabaseImplUtils.get().getAccessContext(db, tableId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST);

    ContentValues cvValues = new ContentValues();
    cvValues.put(testCol, testVal);
    ODKDatabaseImplUtils.get().insertRowWithId(db, tableId, orderedColumns, cvValues, rowId, activeUser,
            RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    String sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    String[] selArgs = { "" + testVal };
    Cursor cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    String val = null;
    while (cursor.moveToNext()) {
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        val = cursor.getString(ind);
    }

    assertEquals(val, testVal);

    ContentValues updatedCvValues = new ContentValues();
    updatedCvValues.put(testCol, testVal2);
    ODKDatabaseImplUtils.get().insertCheckpointRowWithId(db, tableId, orderedColumns, updatedCvValues, rowId,
            activeUser, RoleConsts.ADMIN_ROLES_LIST, currentLocale);

    // Select everything out of the table
    sel = "SELECT * FROM " + tableId;
    selArgs = new String[0];
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    assertEquals(cursor.getCount(), 2);

    // Select everything out of the table
    sel = "SELECT * FROM " + tableId + " WHERE " + testCol + " = ?";
    selArgs = new String[1];
    selArgs[0] = "" + testVal2;
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    String val2 = null;
    String saveptType = null;
    while (cursor.moveToNext()) {
        // Get the actual value
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        val2 = cursor.getString(ind);

        // Get the savepoint_timestamp
        ind = cursor.getColumnIndex(DataTableColumns.SAVEPOINT_TIMESTAMP);
        type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        saveptType = cursor.getString(ind);

        // Get the conflict_type and make sure that it is null
        ind = cursor.getColumnIndex(DataTableColumns.CONFLICT_TYPE);
        assertTrue(cursor.isNull(ind));
    }

    assertEquals(val2, testVal2);

    // Also make sure that the savepoint_type
    // is empty
    assertNotSame(saveptType, SavepointTypeManipulator.incomplete());
    assertNotSame(saveptType, SavepointTypeManipulator.complete());

    ODKDatabaseImplUtils.get().saveAsCompleteMostRecentCheckpointRowWithId(db, tableId, rowId);
    // Select everything out of the table
    sel = "SELECT * FROM " + tableId;
    selArgs = new String[0];
    cursor = ODKDatabaseImplUtils.get().rawQuery(db, sel, selArgs, null, accessContext);

    assertEquals(cursor.getCount(), 1);

    val2 = null;
    saveptType = null;
    while (cursor.moveToNext()) {
        // Get the actual value
        int ind = cursor.getColumnIndex(testCol);
        int type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        val2 = cursor.getString(ind);

        // Get the savepoint_timestamp
        ind = cursor.getColumnIndex(DataTableColumns.SAVEPOINT_TYPE);
        type = cursor.getType(ind);
        assertEquals(type, Cursor.FIELD_TYPE_STRING);
        saveptType = cursor.getString(ind);

        // Get the conflict_type and make sure that it is null
        ind = cursor.getColumnIndex(DataTableColumns.CONFLICT_TYPE);
        assertTrue(cursor.isNull(ind));
    }

    assertEquals(val2, testVal2);
    assertEquals(saveptType, SavepointTypeManipulator.complete());

    // Drop the table now that the test is done
    ODKDatabaseImplUtils.get().deleteTableAndAllData(db, tableId);
}