Skip to content

Commit b166515

Browse files
committed
Fix delete function
1 parent 814fe74 commit b166515

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

orcid-core/src/main/java/org/orcid/core/manager/v3/impl/NotificationManagerImpl.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -850,14 +850,17 @@ public void autoDeleteNotifications() {
850850
calendar.add(Calendar.MONTH, - autoDeleteMonths);
851851
Date createdBefore = calendar.getTime();
852852
LOGGER.info("About to auto delete notifications created before {}", createdBefore);
853-
List<NotificationEntity> notificationsToDelete = Collections.<NotificationEntity> emptyList();
854853
int numDeleted = 0;
855854
do {
856-
numDeleted = notificationDao.deleteNotificationsCreatedBefore(createdBefore, autoArchiveBatchSize);
857-
LOGGER.info("Deleted {} old notifications", numDeleted);
855+
try {
856+
numDeleted = notificationDao.deleteNotificationsCreatedBefore(createdBefore, autoDeleteBatchSize);
857+
LOGGER.info("Deleted {} old notifications", numDeleted);
858+
} catch (Exception e) {
859+
LOGGER.error("Error deleting notifications", e);
860+
// Exit the loop if we get an error
861+
numDeleted = 0;
862+
}
858863
} while (numDeleted > 0);
859864
}
860865

861-
862-
863866
}

orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/NotificationDaoImpl.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,19 +230,35 @@ public List<NotificationEntity> findNotificationsToSend(Date effectiveDate, Stri
230230
@Override
231231
@Transactional
232232
public int archiveNotificationsCreatedBefore(Date createdBefore, int batchSize) {
233-
Query archiveQuery = entityManager.createNativeQuery("update notification set archive_date=now() where id in (select id from notification where archived_date is null and date_created < :createdBefore)");
233+
Query archiveQuery = entityManager.createNativeQuery("update notification set archived_date=now() where id in (select id from notification where archived_date is null and date_created < :createdBefore limit :batchSize)");
234234
archiveQuery.setParameter("createdBefore", createdBefore);
235-
archiveQuery.setMaxResults(batchSize);
235+
archiveQuery.setParameter("batchSize", batchSize);
236236
return archiveQuery.executeUpdate();
237237
}
238238

239239
@Override
240240
@Transactional
241241
public int deleteNotificationsCreatedBefore(Date createdBefore, int batchSize) {
242-
Query archiveQuery = entityManager.createNativeQuery("delete notification where id in (select id from notification where date_created < :createdBefore)");
243-
archiveQuery.setParameter("createdBefore", createdBefore);
244-
archiveQuery.setMaxResults(batchSize);
245-
return archiveQuery.executeUpdate();
242+
TypedQuery<Long> query = entityManager.createQuery("select id from NotificationEntity where dateCreated < :createdBefore", Long.class);
243+
query.setParameter("createdBefore", createdBefore);
244+
query.setMaxResults(batchSize);
245+
List<Long> ids = query.getResultList();
246+
if(ids != null && !ids.isEmpty()) {
247+
// Remove notification items
248+
Query deleteNotificationItems = entityManager.createNativeQuery("delete from notification_item where notification_id in (:ids)");
249+
deleteNotificationItems.setParameter("ids", ids);
250+
deleteNotificationItems.executeUpdate();
251+
252+
// Remove notification works
253+
Query deleteNotificationWorks = entityManager.createNativeQuery("delete from notification_work where notification_id in (:ids)");
254+
deleteNotificationWorks.setParameter("ids", ids);
255+
deleteNotificationWorks.executeUpdate();
256+
257+
Query deleteNotification = entityManager.createNativeQuery("delete from notification where id in (:ids)");
258+
deleteNotification.setParameter("ids", ids);
259+
return deleteNotification.executeUpdate();
260+
}
261+
return 0;
246262
}
247263

248264
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)