summaryrefslogtreecommitdiffstats
path: root/src/org/ryuslash/Dailies/DailiesDataSource.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/ryuslash/Dailies/DailiesDataSource.java')
-rw-r--r--src/org/ryuslash/Dailies/DailiesDataSource.java152
1 files changed, 152 insertions, 0 deletions
diff --git a/src/org/ryuslash/Dailies/DailiesDataSource.java b/src/org/ryuslash/Dailies/DailiesDataSource.java
new file mode 100644
index 0000000..d679f5f
--- /dev/null
+++ b/src/org/ryuslash/Dailies/DailiesDataSource.java
@@ -0,0 +1,152 @@
+package org.ryuslash.Dailies;
+
+import java.text.DateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+import android.content.ContentValues;
+import android.content.Context;
+import android.database.Cursor;
+import android.database.SQLException;
+import android.database.sqlite.SQLiteDatabase;
+
+public class DailiesDataSource
+{
+ private SQLiteDatabase database;
+ private SQLiteHelper dbHelper;
+ private String[] allColumns = { SQLiteHelper.COLUMN_ID,
+ SQLiteHelper.COLUMN_TITLE,
+ SQLiteHelper.COLUMN_ALARM,
+ SQLiteHelper.COLUMN_HOUR,
+ SQLiteHelper.COLUMN_MINUTE,
+ SQLiteHelper.COLUMN_LAST_COMPLETED };
+
+ public DailiesDataSource(Context context)
+ {
+ dbHelper = new SQLiteHelper(context);
+ }
+
+ public void open() throws SQLException
+ {
+ database = dbHelper.getWritableDatabase();
+ }
+
+ public void close()
+ {
+ dbHelper.close();
+ }
+
+ private String dateToString(Date date)
+ {
+ DateFormat df = DateFormat.getDateInstance();
+ return df.format(date);
+ }
+
+ private ContentValues createValues(String title, boolean alarm,
+ int hour, int minute, Date date)
+ {
+ ContentValues values = new ContentValues();
+
+
+ values.put(SQLiteHelper.COLUMN_TITLE, title);
+ values.put(SQLiteHelper.COLUMN_ALARM, alarm ? 1 : 0);
+ values.put(SQLiteHelper.COLUMN_HOUR, hour);
+ values.put(SQLiteHelper.COLUMN_MINUTE, minute);
+
+ if (date != null)
+ values.put(SQLiteHelper.COLUMN_LAST_COMPLETED,
+ dateToString(date));
+
+ return values;
+ }
+
+ private ContentValues createValues(String title, boolean alarm,
+ int hour, int minute)
+ {
+ return createValues(title, alarm, hour, minute, null);
+ }
+
+ public Daily createDaily(String title, boolean alarm, int hour,
+ int minute)
+ {
+ ContentValues values = createValues(title, alarm, hour, minute);
+ long insertId = database.insert(SQLiteHelper.TABLE_DAILY, null,
+ values);
+ Daily daily = new Daily(insertId, title, alarm, hour, minute);
+
+ return daily;
+ }
+
+ public void updateDaily(long id, String title, boolean alarm,
+ int hour, int minute, Date last_completed)
+ {
+ ContentValues values = createValues(title, alarm, hour, minute,
+ last_completed);
+ String whereArgs[] = { "" + id };
+
+ database.update(SQLiteHelper.TABLE_DAILY, values, "id = ?",
+ whereArgs);
+ }
+
+ public void deleteDaily(Daily daily)
+ {
+ deleteDaily(daily.getId());
+ }
+
+ public void deleteDaily(long id)
+ {
+ database.delete(SQLiteHelper.TABLE_DAILY,
+ SQLiteHelper.COLUMN_ID + " = " + id, null);
+ }
+
+ public List<Daily> getDailiesList()
+ {
+ List<Daily> dailies = new ArrayList<Daily>();
+ Cursor cursor = database.query(
+ SQLiteHelper.TABLE_DAILY, allColumns, null, null, null,
+ null, "last_completed DESC, hour, minute"
+ );
+
+ cursor.moveToFirst();
+
+ while(!cursor.isAfterLast()) {
+ Daily daily = cursorToDaily(cursor);
+
+ dailies.add(daily);
+ cursor.moveToNext();
+ }
+
+ cursor.close();
+
+ return dailies;
+ }
+
+ public Daily getById(long id)
+ {
+ Cursor cursor = database.query(SQLiteHelper.TABLE_DAILY,
+ allColumns,
+ SQLiteHelper.COLUMN_ID + " = ?",
+ new String[] { "" + id },
+ null, null, null);
+ cursor.moveToFirst();
+ Daily daily = cursorToDaily(cursor);
+ cursor.close();
+
+ return daily;
+ }
+
+ private Daily cursorToDaily(Cursor cursor)
+ {
+ Daily daily = new Daily();
+
+ daily.setId(cursor.getLong(0));
+ daily.setTitle(cursor.getString(1));
+ daily.setAlarm(cursor.getInt(2) == 1);
+ daily.setHour(cursor.getInt(3));
+ daily.setMinute(cursor.getInt(4));
+ daily.setLastCompleted(cursor.getString(5));
+
+ return daily;
+ }
+}