Search in sources :

Example 1 with Reminder

use of ve.com.abicelis.remindy.model.reminder.Reminder in project Remindy by abicelis.

the class RemindyDAO method getReminderOfTask.

/**
 * Returns a Reminder given its taskId and reminderType
 * @param taskId The ID of the task
 * @param reminderType The Type of reminder
 */
public Reminder getReminderOfTask(int taskId, @NonNull ReminderType reminderType) throws CouldNotGetDataException, SQLiteConstraintException {
    SQLiteDatabase db = mDatabaseHelper.getWritableDatabase();
    Reminder reminder;
    String tableName, whereClause;
    switch(reminderType) {
        case ONE_TIME:
            whereClause = RemindyContract.OneTimeReminderTable.COLUMN_NAME_TASK_FK.getName() + " =?";
            tableName = RemindyContract.OneTimeReminderTable.TABLE_NAME;
            break;
        case REPEATING:
            whereClause = RemindyContract.RepeatingReminderTable.COLUMN_NAME_TASK_FK.getName() + " =?";
            tableName = RemindyContract.RepeatingReminderTable.TABLE_NAME;
            break;
        case LOCATION_BASED:
            whereClause = RemindyContract.LocationBasedReminderTable.COLUMN_NAME_TASK_FK.getName() + " =?";
            tableName = RemindyContract.LocationBasedReminderTable.TABLE_NAME;
            break;
        default:
            throw new CouldNotGetDataException("ReminderType is invalid. Type=" + reminderType);
    }
    Cursor cursor = db.query(tableName, null, whereClause, new String[] { String.valueOf(taskId) }, null, null, null);
    try {
        if (cursor.getCount() == 0)
            throw new CouldNotGetDataException("Specified Reminder not found in the database. Passed id=" + taskId);
        if (cursor.getCount() > 1)
            throw new SQLiteConstraintException("Database UNIQUE constraint failure, more than one Reminder found. Passed id=" + taskId);
        cursor.moveToNext();
        switch(reminderType) {
            case ONE_TIME:
                reminder = getOneTimeReminderFromCursor(cursor);
                break;
            case REPEATING:
                reminder = getRepeatingReminderFromCursor(cursor);
                break;
            case LOCATION_BASED:
                reminder = getLocationBasedReminderFromCursor(cursor);
                int placeId = ((LocationBasedReminder) reminder).getPlaceId();
                try {
                    ((LocationBasedReminder) reminder).setPlace(getPlace(placeId));
                } catch (PlaceNotFoundException | SQLiteConstraintException e) {
                    throw new CouldNotGetDataException("Error trying to get Place for Location-based Reminder", e);
                }
                break;
            default:
                throw new CouldNotGetDataException("ReminderType is invalid. Type=" + reminderType);
        }
    } finally {
        cursor.close();
    }
    return reminder;
}
Also used : CouldNotGetDataException(ve.com.abicelis.remindy.exception.CouldNotGetDataException) RepeatingReminder(ve.com.abicelis.remindy.model.reminder.RepeatingReminder) Reminder(ve.com.abicelis.remindy.model.reminder.Reminder) LocationBasedReminder(ve.com.abicelis.remindy.model.reminder.LocationBasedReminder) OneTimeReminder(ve.com.abicelis.remindy.model.reminder.OneTimeReminder) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) LocationBasedReminder(ve.com.abicelis.remindy.model.reminder.LocationBasedReminder) PlaceNotFoundException(ve.com.abicelis.remindy.exception.PlaceNotFoundException) SQLiteConstraintException(android.database.sqlite.SQLiteConstraintException) Cursor(android.database.Cursor)

Aggregations

Cursor (android.database.Cursor)1 SQLiteConstraintException (android.database.sqlite.SQLiteConstraintException)1 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)1 CouldNotGetDataException (ve.com.abicelis.remindy.exception.CouldNotGetDataException)1 PlaceNotFoundException (ve.com.abicelis.remindy.exception.PlaceNotFoundException)1 LocationBasedReminder (ve.com.abicelis.remindy.model.reminder.LocationBasedReminder)1 OneTimeReminder (ve.com.abicelis.remindy.model.reminder.OneTimeReminder)1 Reminder (ve.com.abicelis.remindy.model.reminder.Reminder)1 RepeatingReminder (ve.com.abicelis.remindy.model.reminder.RepeatingReminder)1