DefaultSynchronizationDAO.java :  » App » paninoonto » it » paninoonto » dao » db » Android Open Source

Android Open Source » App » paninoonto 
paninoonto » it » paninoonto » dao » db » DefaultSynchronizationDAO.java
package it.paninoonto.dao.db;

import it.paninoonto.PaninoOntoFactory;
import it.paninoonto.Synchronization;
import it.paninoonto.dao.SynchronizationDAO;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * 
 * The default implementation of SynchronizationDAO.
 * <p/>
 * This DAO uses the application database as storage for the
 * {@link Synchronization} entities.
 * 
 * @author Mirko Luchi
 * 
 */
public class DefaultSynchronizationDAO implements SynchronizationDAO {

    /**
     * The logger tag.
     */
    private static final String TAG = DefaultSynchronizationDAO.class.getName();

    /**
     * SQL statement to create the synchronization table.
     */
    public static final String CREATE_TABLE = "create table if not exists synchronization(" +
            "_ID integer primary key autoincrement," +
            "SYNC_DATE datetime not null" +
            ");";

    /**
     * SQL statement to drop the synchronization table.
     */
    public static final String DROP_TABLE = "drop table if exists synchronization;";

    /**
     * The name of the table containing synchronization entities.
     */
    public static final String TABLE = "synchronization";

    /**
     * The name of the "id" column.
     */
    public static final String COLUMN_ID = "_ID";

    /**
     * The name of the "dater" column.
     */
    public static final String COLUMN_SYNC_DATE = "SYNC_DATE";

    /**
     * An array containing all the columns.
     */
    public static final String[] ALL_COLUMNS = new String[] {
            COLUMN_ID,
            COLUMN_SYNC_DATE
            };

    /**
     * The reference to the SQLite database this DAO queries.
     */
    private SQLiteDatabase db;


    /**
     * Create a new {@link DefaultSynchronizationDAO}.
     * 
     * @param db The SQLite db reference.
     */
    public DefaultSynchronizationDAO(SQLiteDatabase db) {
        if (db == null) {
            throw new NullPointerException("SQLite db is null!");
        }
        this.db = db;
    }

    @Override
    public List<Synchronization> getAll() {
        Log.d(TAG, "Getting all synchronizations");
        Cursor cursor = db.query(
                    true,
                    TABLE,
                    ALL_COLUMNS,
                    null,
                    null,
                    null,
                    null,
                    COLUMN_SYNC_DATE,
                    null);

        int n = cursor.getCount();
        List<Synchronization> synchronizations = new ArrayList<Synchronization>();
        for (int i = 0; i < n; i++) {
            Synchronization synchronization = asSynchronization(cursor, i);
            synchronizations.add(synchronization);
        }
        cursor.close();
        return synchronizations;
    }

    @Override
    public Synchronization getById(long id) {
        Cursor cursor = db.query(
                true,
                TABLE,
                ALL_COLUMNS,
                COLUMN_ID + " = ?",
                new String[] { Long.toString(id) },
                null,
                null,
                null,
                null);
        if (cursor.getCount() == 0) {
            return null;
        }
        cursor.moveToFirst();
        Synchronization synchronization = asSynchronization(cursor);
        cursor.close();
        return synchronization;
    }

    @Override
    public Synchronization getLast() {
        Cursor cursor = db.query(
                true,
                TABLE,
                ALL_COLUMNS,
                null,
                null,
                null,
                null,
                COLUMN_ID + " DESC", // order by date descending
                "1"); // limit to 1 result
        if (cursor.getCount() == 0) {
            return null;
        }
        cursor.moveToFirst();
        Synchronization synchronization = asSynchronization(cursor);
        cursor.close();
        return synchronization;
    }

    @Override
    public long insert(Synchronization synchronization) {
        Log.d(TAG, "Inserting synchronization: " + synchronization);
        ContentValues toInsert = asContentValues(synchronization);
        return db.insert(TABLE, null, toInsert);
    }

    @Override
    public void update(Synchronization synchronization) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void delete(long id) {
        Log.d(TAG, "Deleting synchronization with id: " + id);
        db.delete(
                TABLE,
                COLUMN_ID + " = ?",
                new String[] { Long.toString(id) }
                );
    }

    /**
     * Convenience method to put data contained in a {@link Synchronization} to
     * a {@link ContentValues} object.
     * 
     * @param s The {@link Synchronization}.
     * @return The {@link ContentValues}.
     */
    protected static ContentValues asContentValues(Synchronization s) {
        if (s == null) {
            throw new NullPointerException("Synchronization is null!");
        }
        Long id = s.getId();
        Date date = s.getSyncDate();

        ContentValues c = new ContentValues();
        c.put(COLUMN_ID, id);
        c.put(COLUMN_SYNC_DATE, date == null ? null : date.getTime());
        return c;
    }

    /**
     * Fetch data from the given {@link Cursor} at the current position and
     * store it in a {@link Synchronization} object.
     * 
     * @param c The {@link Cursor}.
     * @return The {@link Synchronization}.
     */
    public static Synchronization asSynchronization(Cursor c) {
        Synchronization s = PaninoOntoFactory.INSTANCE.createSynchronization();
        s.setId(c.getLong(c.getColumnIndex(COLUMN_ID)));
        s.setSyncDate(c.isNull(c.getColumnIndex(COLUMN_SYNC_DATE)) ? null : new Date(c.getLong(c.getColumnIndex(COLUMN_SYNC_DATE))));
        return s;
    }

    /**
     * Fetch data from the given {@link Cursor} at the given position and store
     * it in a {@link Synchronization} object.
     * 
     * @param c The {@link Cursor}.
     * @param i The position.
     * @return The {@link Synchronization}.
     */
    public static Synchronization asSynchronization(Cursor c, int i) {
        c.moveToPosition(i);
        return asSynchronization(c);
    }


}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.