package org.ccci.gto.android.common.db;

import android.database.Cursor;

public abstract class AbstractMapper<T> implements Mapper<T> {
    /**
     * returns a boolean value stored in the specified column. (SQLite doesn't
     * support booleans, so we fake it using an integer)
     *
     * @param c        SQLite results Cursor
     * @param field    the name of the column
     * @param defValue the default value
     * @return
     */
    protected final boolean getBool(final Cursor c, final String field, final boolean defValue) {
        final int index = c.getColumnIndex(field);
        if (index != -1) {
            final int value = c.getInt(index);
            if (value == 1) {
                return true;
            } else if (value == 0) {
                return false;
            }
        }
        return defValue;
    }

    protected final int getInt(final Cursor c, final String field) {
        return this.getInt(c, field, 0);
    }

    protected final int getInt(final Cursor c, final String field, final int defValue) {
        final int index = c.getColumnIndex(field);
        return index != -1 ? c.getInt(index) : defValue;
    }

    protected final long getLong(final Cursor c, final String field) {
        return this.getInt(c, field, 0);
    }

    protected final long getLong(final Cursor c, final String field, final long defValue) {
        final int index = c.getColumnIndex(field);
        return index != -1 ? c.getLong(index) : defValue;
    }

    protected final String getString(final Cursor c, final String field) {
        return this.getString(c, field, null);
    }

    protected final String getString(final Cursor c, final String field, final String defValue) {
        final int index = c.getColumnIndex(field);
        return index != -1 ? c.getString(index) : defValue;
    }
}

@@ -34,16 +34,54 @@
 on.db;%0A%0A
+import android.content.ContentValues;%0A
 import a
@@ -1870,11 +1870,488 @@
 e;%0A    %7D
+%0A%0A    @Override%0A    public final ContentValues toContentValues(final T obj, final String%5B%5D projection) %7B%0A        // only add values in the projection%0A        final ContentValues values = new ContentValues();%0A        for (final String field : projection) %7B%0A            this.mapField(values, field, obj);%0A        %7D%0A        return values;%0A    %7D%0A%0A    protected void mapField(final ContentValues values, final String field, final T obj) %7B%0A        // ignore unrecognized fields%0A    %7D
 %0A%7D%0A

add support for inheritting code from parent classes for toContentValues