@Override protected <T extends OverdueCheckNotificationKey> boolean cleanupFutureNotificationsFormTransaction(final EntitySqlDaoWrapperFactory<EntitySqlDao> entitySqlDaoWrapperFactory, final Collection<NotificationEventWithMetadata<T>> futureNotifications, final DateTime futureNotificationTime, final NotificationQueue overdueQueue) { boolean shouldInsertNewNotification = true; if (futureNotifications.size() > 0) { // Results are ordered by effective date asc final DateTime earliestExistingNotificationDate = futureNotifications.iterator().next().getEffectiveDate(); final int minIndexToDeleteFrom; if (earliestExistingNotificationDate.isBefore(futureNotificationTime)) { // We don't have to insert a new one. For sanity, delete any other future notification minIndexToDeleteFrom = 1; shouldInsertNewNotification = false; } else { // We win - we are before any other already recorded. Delete all others. minIndexToDeleteFrom = 0; } int index = 0; final Iterator<NotificationEventWithMetadata<T>> it = futureNotifications.iterator(); while (it.hasNext()) { final NotificationEventWithMetadata<T> cur = it.next(); if (minIndexToDeleteFrom <= index) { overdueQueue.removeNotificationFromTransaction(entitySqlDaoWrapperFactory.getSqlDao(), cur.getRecordId()); } index++; } } return shouldInsertNewNotification; }
@Override public Void inTransaction(final EntitySqlDaoWrapperFactory<EntitySqlDao> entitySqlDaoWrapperFactory) throws Exception { final Collection<NotificationEventWithMetadata<T>> futureNotifications = getFutureNotificationsForAccountInTransaction(entitySqlDaoWrapperFactory, checkOverdueQueue, accountId, clazz, context); for (final NotificationEventWithMetadata<T> notification : futureNotifications) { checkOverdueQueue.removeNotificationFromTransaction(entitySqlDaoWrapperFactory.getSqlDao(), notification.getRecordId()); } return null; } });