diff --git a/orcid-persistence/src/main/java/org/orcid/persistence/dao/EventDao.java b/orcid-persistence/src/main/java/org/orcid/persistence/dao/EventDao.java index d59675c850c..78a2bf775f6 100644 --- a/orcid-persistence/src/main/java/org/orcid/persistence/dao/EventDao.java +++ b/orcid-persistence/src/main/java/org/orcid/persistence/dao/EventDao.java @@ -14,6 +14,8 @@ public interface EventDao { void createEvent(EventEntity eventEntity); EventEntity find(long id); + + void delete(long id); List findAll(); diff --git a/orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/EventDaoImpl.java b/orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/EventDaoImpl.java index 6d4d2d95185..e3e71561c4e 100644 --- a/orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/EventDaoImpl.java +++ b/orcid-persistence/src/main/java/org/orcid/persistence/dao/impl/EventDaoImpl.java @@ -34,6 +34,12 @@ public EventEntity find(long id) { return entityManager.find(EventEntity.class, id); } + @Override + @Transactional + public void delete(long id) { + entityManager.remove(find(id)); + } + @Override public List findAll() { TypedQuery query = entityManager.createQuery("from EventEntity", EventEntity.class); @@ -43,7 +49,7 @@ public List findAll() { @Override @Transactional public void deleteEventsByDate(Integer numberOfDays) { - String query = "DELETE FROM event where CAST(date_created as date) < CAST(now() - (CAST('1' AS INTERVAL DAY) * 1) as date) * :numberOfDays)"; + String query = "DELETE FROM event where CAST(date_created as date) < CAST(now() - (CAST('1' AS INTERVAL DAY) * :numberOfDays) as date)"; Query queryDelete = entityManager.createNativeQuery(query); queryDelete.setParameter("numberOfDays", numberOfDays); queryDelete.executeUpdate(); diff --git a/orcid-persistence/src/test/java/org/orcid/persistence/dao/EventDaoTest.java b/orcid-persistence/src/test/java/org/orcid/persistence/dao/EventDaoTest.java index 9525f635b50..025c8b0d440 100644 --- a/orcid-persistence/src/test/java/org/orcid/persistence/dao/EventDaoTest.java +++ b/orcid-persistence/src/test/java/org/orcid/persistence/dao/EventDaoTest.java @@ -9,6 +9,7 @@ import java.util.List; import javax.annotation.Resource; +import javax.persistence.EntityManager; import org.apache.commons.lang3.reflect.FieldUtils; import org.junit.AfterClass; @@ -77,5 +78,7 @@ public void testWriteEvent() throws IllegalAccessException { assertEquals(eventEntity.getId(), fromDb.getId()); assertEquals(eventEntity.getLabel(), fromDb.getLabel()); assertNotNull(fromDb.getDateCreated()); + + eventDao.delete(eventEntity.getId()); } } diff --git a/orcid-persistence/src/test/java/org/orcid/persistence/dao/EventStatsDaoTest.java b/orcid-persistence/src/test/java/org/orcid/persistence/dao/EventStatsDaoTest.java index 3eb231576b5..badd5330c9a 100644 --- a/orcid-persistence/src/test/java/org/orcid/persistence/dao/EventStatsDaoTest.java +++ b/orcid-persistence/src/test/java/org/orcid/persistence/dao/EventStatsDaoTest.java @@ -9,6 +9,9 @@ import org.springframework.test.context.ContextConfiguration; import javax.annotation.Resource; +import java.time.Instant; +import java.time.LocalDate; +import java.time.ZoneId; import java.util.Date; import java.util.List; @@ -42,7 +45,9 @@ private void createEvents() { EventEntity eventEntity = new EventEntity(); eventEntity.setEventType("Sign-In"); eventEntity.setClientId("Client " + 1); - eventEntity.setDateCreated(new Date()); + LocalDate date = LocalDate.now().minusDays(1); + Instant instant = date.atStartOfDay(ZoneId.systemDefault()).toInstant(); + eventEntity.setDateCreated(Date.from(instant)); eventDao.createEvent(eventEntity); } }