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;
}
Aggregations