@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; }
private void insertOverdueCheckAndVerifyQueueContent(final Account account, final int nbDaysInFuture, final int expectedNbDaysInFuture) throws IOException { final DateTime futureNotificationTime = testReferenceTime.plusDays(nbDaysInFuture); final OverdueCheckNotificationKey notificationKey = new OverdueCheckNotificationKey(account.getId()); checkPoster.insertOverdueNotification(account.getId(), futureNotificationTime, OverdueCheckNotifier.OVERDUE_CHECK_NOTIFIER_QUEUE, notificationKey, internalCallContext); final Collection<NotificationEventWithMetadata<OverdueCheckNotificationKey>> notificationsForKey = getNotificationsForOverdueable(account); Assert.assertEquals(notificationsForKey.size(), 1); final NotificationEventWithMetadata nm = notificationsForKey.iterator().next(); Assert.assertEquals(nm.getEvent(), notificationKey); Assert.assertEquals(nm.getEffectiveDate(), testReferenceTime.plusDays(expectedNbDaysInFuture)); }
private <T extends NotificationEvent> List<NotificationEventWithMetadata<T>> getFutureNotificationsInternal(final Class<T> typeX, final NotificationSqlDao transactionalDao, final String searchKey, final Long searchKeyValue) { final List<NotificationEventWithMetadata<T>> result = new LinkedList<NotificationEventWithMetadata<T>>(); final List<NotificationEventModelDao> entries = transactionalDao.getReadyQueueEntriesForSearchKey(getFullQName(), searchKeyValue, config.getTableName(), searchKey); for (NotificationEventModelDao cur : entries) { final T event = (T) DefaultQueueLifecycle.deserializeEvent(cur.getClassName(), objectMapper, cur.getEventJson()); final NotificationEventWithMetadata<T> foo = new NotificationEventWithMetadata<T>(cur.getRecordId(), cur.getUserToken(), cur.getCreatedDate(), cur.getSearchKey1(), cur.getSearchKey2(), event, cur.getFutureUserToken(), cur.getEffectiveDate(), cur.getQueueName()); result.add(foo); } return result; }
@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; } });
private <T extends NotificationEvent> List<NotificationEventWithMetadata<T>> getFutureNotificationsInternal(final Class<T> type, final Long searchKey1, final Transmogrifier transmogrifier) { final List<NotificationEventWithMetadata<T>> result = new ArrayList<NotificationEventWithMetadata<T>>(); synchronized (notifications) { for (final NotificationEventModelDao notification : notifications) { if (notification.getSearchKey1().equals(searchKey1) && type.getName().equals(notification.getClassName()) && notification.getEffectiveDate().isAfter(clock.getUTCNow())) { final T event = (T) DefaultQueueLifecycle.deserializeEvent(notification.getClassName(), objectMapper, notification.getEventJson()); final NotificationEventWithMetadata<T> foo = new NotificationEventWithMetadata<T>(notification.getRecordId(), notification.getUserToken(), notification.getCreatedDate(), notification.getSearchKey1(), notification.getSearchKey2(), event, notification.getFutureUserToken(), notification.getEffectiveDate(), notification.getQueueName()); result.add(foo); } } } return result; }