|
|
@@ -3,16 +3,48 @@ package org.uab.android.persistentdata.sqlite;
|
|
|
import android.content.ContentValues;
|
|
|
import android.content.Context;
|
|
|
import android.database.Cursor;
|
|
|
+import android.database.sqlite.SQLiteDatabase;
|
|
|
|
|
|
public class SQLiteDataRepository {
|
|
|
|
|
|
- private DatabaseOpenHelper databaseOpenHelper;
|
|
|
+ // Helper object to obtain a reference to the database
|
|
|
+ private DatabaseOpenHelper databaseOpenHelper;
|
|
|
+
|
|
|
+ // Reference to the SQLiteDatabase to store and retrieve data
|
|
|
+ private SQLiteDatabase sqliteDatabase;
|
|
|
|
|
|
+ // Private constructor for the SQLiteDataRepository class
|
|
|
+ // Creates a new DatabaseOpenHelper object and holds a reference to the actual database
|
|
|
public SQLiteDataRepository(Context context) {
|
|
|
|
|
|
this.databaseOpenHelper = new DatabaseOpenHelper(context);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Opens the database for read-only operations
|
|
|
+ */
|
|
|
+ public void openDatabaseForReadOnly() {
|
|
|
+
|
|
|
+ this.sqliteDatabase = this.databaseOpenHelper.getReadableDatabase();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Opens the database for read and write operations
|
|
|
+ */
|
|
|
+ public void openDatabaseForWrite() {
|
|
|
+
|
|
|
+ this.sqliteDatabase = this.databaseOpenHelper.getWritableDatabase();
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Saves the information for a particular course.
|
|
|
+ *
|
|
|
+ * @param courseName Name of the course.
|
|
|
+ * @param numberOfCredits Number of credits of the course.
|
|
|
+ * @param checkBoxState String identifying the state of the
|
|
|
+ * days of the week check boxes.
|
|
|
+ * @param hour Starting hour for the course.
|
|
|
+ */
|
|
|
public void saveCourse(String courseName, int numberOfCredits,
|
|
|
String checkBoxState, String hour) {
|
|
|
|
|
|
@@ -22,19 +54,25 @@ public class SQLiteDataRepository {
|
|
|
values.put(DatabaseOpenHelper.COURSE_DAYS_OF_WEEK, checkBoxState);
|
|
|
values.put(DatabaseOpenHelper.COURSE_STARTS_AT, hour);
|
|
|
|
|
|
- this.databaseOpenHelper.getWritableDatabase().insert(
|
|
|
+ this.sqliteDatabase.insert(
|
|
|
DatabaseOpenHelper.COURSES_TABLE_NAME,
|
|
|
null,
|
|
|
values);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns a specific course based on its ID.
|
|
|
+ *
|
|
|
+ * @param courseId The unique course ID.
|
|
|
+ *
|
|
|
+ * @return An iterator over the obtained registers.
|
|
|
+ */
|
|
|
public Cursor fetchCourseById(int courseId) {
|
|
|
|
|
|
String selection = DatabaseOpenHelper._ID + " = ?";
|
|
|
String[] selectionArgs = new String[] { String.valueOf(courseId) };
|
|
|
|
|
|
- Cursor result = this.databaseOpenHelper.getReadableDatabase()
|
|
|
- .query(
|
|
|
+ Cursor result = this.sqliteDatabase.query(
|
|
|
DatabaseOpenHelper.COURSES_TABLE_NAME,
|
|
|
DatabaseOpenHelper.COLUMNS,
|
|
|
selection,
|
|
|
@@ -45,10 +83,14 @@ public class SQLiteDataRepository {
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns all saved courses.
|
|
|
+ *
|
|
|
+ * @return An iterator over the list of courses from the database.
|
|
|
+ */
|
|
|
public Cursor fetchAllCourses() {
|
|
|
|
|
|
- Cursor result = this.databaseOpenHelper.getReadableDatabase()
|
|
|
- .query(
|
|
|
+ Cursor result = this.sqliteDatabase.query(
|
|
|
DatabaseOpenHelper.COURSES_TABLE_NAME,
|
|
|
DatabaseOpenHelper.COLUMNS,
|
|
|
null,
|
|
|
@@ -58,4 +100,15 @@ public class SQLiteDataRepository {
|
|
|
null);
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Release the database resources
|
|
|
+ */
|
|
|
+ public void release() {
|
|
|
+
|
|
|
+ if ( sqliteDatabase != null ) {
|
|
|
+ sqliteDatabase.close();
|
|
|
+ sqliteDatabase = null;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|