@Test public void countEventsWithTimeRange() { ArgumentCaptor<Criterion> criterion = ArgumentCaptor.forClass(Criterion.class); Calendar from = Calendar.getInstance(); Calendar to = Calendar.getInstance(); AuditEvent eventTemplate = new AuditEvent(this.user, "127.0.0.1", "action", null, null, null); long count = this.store.countEvents(eventTemplate, from, to); Mockito.verify(this.criteria).setProjection(Matchers.any(RowCountProjection.class)); Mockito.verify(this.criteria, Mockito.times(2)).add(criterion.capture()); Assert.assertEquals(0, count); Example ex = (Example) criterion.getAllValues().get(0); Assert.assertEquals("example (wiki:XWiki.user (127.0.0.1): action on null at null)", ex.toString()); BetweenExpression timeFilter = (BetweenExpression) criterion.getAllValues().get(1); Assert.assertEquals("time between " + from.toString() + " and " + to.toString(), timeFilter.toString()); }
@Override public long countEvents(AuditEvent eventTemplate, Calendar fromTime, Calendar toTime) { try { Session session = this.sessionFactory.getSessionFactory().openSession(); Criteria c = session.createCriteria(AuditEvent.class); if (eventTemplate != null) { c.add(Example.create(eventTemplate)); } setTimeInterval(c, fromTime, toTime); c.setProjection(Projections.rowCount()); @SuppressWarnings("rawtypes") List foundEntries = c.list(); if (foundEntries != null && !foundEntries.isEmpty()) { return (long) foundEntries.get(0); } return 0; } catch (HibernateException ex) { this.logger.error("Failed to count audit event documents: {}", ex.getMessage(), ex); return -1; } }
HibernateAuditStore implements AuditStore { @Override public long countEvents(AuditEvent eventTemplate, Calendar fromTime, Calendar toTime) { try { Session session = this.sessionFactory.getSessionFactory().openSession(); Criteria c = session.createCriteria(AuditEvent.class); if (eventTemplate != null) { c.add(Example.create(eventTemplate)); } setTimeInterval(c, fromTime, toTime); c.setProjection(Projections.rowCount()); @SuppressWarnings("rawtypes") List foundEntries = c.list(); if (foundEntries != null && !foundEntries.isEmpty()) { return (long) foundEntries.get(0); } return 0; } catch (HibernateException ex) { this.logger.error("Failed to count audit event documents: {}", ex.getMessage(), ex); return -1; } } }
HibernateAuditStore implements AuditStore { @Override public long countEvents(AuditEvent eventTemplate, Calendar fromTime, Calendar toTime) { try { Session session = this.sessionFactory.getSessionFactory().openSession(); Criteria c = session.createCriteria(AuditEvent.class); if (eventTemplate != null) { c.add(Example.create(eventTemplate)); } setTimeInterval(c, fromTime, toTime); c.setProjection(Projections.rowCount()); @SuppressWarnings("rawtypes") List foundEntries = c.list(); if (foundEntries != null && !foundEntries.isEmpty()) { return (long) foundEntries.get(0); } return 0; } catch (HibernateException ex) { this.logger.error("Failed to count audit event documents: {}", ex.getMessage(), ex); return -1; } }  }
HibernateAuditStore implements AuditStore { @Override public long countEvents(AuditEvent eventTemplate, Calendar fromTime, Calendar toTime) { try { Session session = this.sessionFactory.getSessionFactory().openSession(); Criteria c = session.createCriteria(AuditEvent.class); if (eventTemplate != null) { c.add(Example.create(eventTemplate)); } setTimeInterval(c, fromTime, toTime); c.setProjection(Projections.rowCount()); @SuppressWarnings("rawtypes") List foundEntries = c.list(); if (foundEntries != null && !foundEntries.isEmpty()) { return (long) foundEntries.get(0); } return 0; } catch (HibernateException ex) { this.logger.error("Failed to count audit event documents: {}", ex.getMessage(), ex); return -1; } }  @Override void store(AuditEvent event); @Override List<AuditEvent> getEventsForEntity(DocumentReference entity); @Override List<AuditEvent> getEventsForEntity(DocumentReference entity, String type); @Override List<AuditEvent> getEventsForUser(User user); @Override List<AuditEvent> getEventsForUser(User user, String ip); @Override List<AuditEvent> getEventsForUser(User user, String ip, String type); @Override List<AuditEvent> getEvents(AuditEvent eventTemplate, Calendar fromTime, Calendar toTime, int start,
        int maxResults); @Override long countEventsForEntity(DocumentReference entity); @Override long countEventsForEntity(DocumentReference entity, String type); @Override long countEventsForUser(User user); @Override long countEventsForUser(User user, String ip); @Override long countEventsForUser(User user, String ip, String type); @Override long countEvents(AuditEvent eventTemplate, Calendar fromTime, Calendar toTime); }
HibernateAuditStore implements AuditStore { @Override public long countEvents(AuditEvent eventTemplate, Calendar fromTime, Calendar toTime) { try { Session session = this.sessionFactory.getSessionFactory().openSession(); Criteria c = session.createCriteria(AuditEvent.class); if (eventTemplate != null) { c.add(Example.create(eventTemplate)); } setTimeInterval(c, fromTime, toTime); c.setProjection(Projections.rowCount()); @SuppressWarnings("rawtypes") List foundEntries = c.list(); if (foundEntries != null && !foundEntries.isEmpty()) { return (long) foundEntries.get(0); } return 0; } catch (HibernateException ex) { this.logger.error("Failed to count audit event documents: {}", ex.getMessage(), ex); return -1; } }  @Override void store(AuditEvent event); @Override List<AuditEvent> getEventsForEntity(DocumentReference entity); @Override List<AuditEvent> getEventsForEntity(DocumentReference entity, String type); @Override List<AuditEvent> getEventsForUser(User user); @Override List<AuditEvent> getEventsForUser(User user, String ip); @Override List<AuditEvent> getEventsForUser(User user, String ip, String type); @Override List<AuditEvent> getEvents(AuditEvent eventTemplate, Calendar fromTime, Calendar toTime, int start,
        int maxResults); @Override long countEventsForEntity(DocumentReference entity); @Override long countEventsForEntity(DocumentReference entity, String type); @Override long countEventsForUser(User user); @Override long countEventsForUser(User user, String ip); @Override long countEventsForUser(User user, String ip, String type); @Override long countEvents(AuditEvent eventTemplate, Calendar fromTime, Calendar toTime);  }