SchedulerServiceDatabaseImpl implements SchedulerServiceDatabase { @Override @SuppressWarnings("unchecked") public List<ExtendedEventDto> search( Opt<String> captureAgentId, Opt<Date> optStartsFrom, Opt<Date> optStartsTo, Opt<Date> optEndFrom, Opt<Date> optEndTo, Opt<Integer> limit) throws SchedulerServiceDatabaseException { final EntityManager em = emf.createEntityManager(); final Date startsFrom = optStartsFrom.getOr(new Date(0)); final Date farIntoTheFuture = DateTime.now().plusYears(30).toDate(); final Date startsTo = optStartsTo.getOr(farIntoTheFuture); final Date endFrom = optEndFrom.getOr(new Date(0)); final Date endTo = optEndTo.getOr(farIntoTheFuture); final Query query; if (captureAgentId.isSome()) { query = em.createNamedQuery("ExtendedEvent.searchEventsCA") .setParameter("org", securityService.getOrganization().getId()) .setParameter("ca", captureAgentId.get()) .setParameter("startFrom", startsFrom) .setParameter("startTo", startsTo) .setParameter("endFrom", endFrom) .setParameter("endTo", endTo); } else { query = em.createNamedQuery("ExtendedEvent.searchEvents") .setParameter("org", securityService.getOrganization().getId()) .setParameter("startFrom", startsFrom) .setParameter("startTo", startsTo) .setParameter("endFrom", endFrom) .setParameter("endTo", endTo); } try { if (limit.isSome()) { return query.setMaxResults(limit.get()).getResultList(); } return query.getResultList(); } catch (Exception e) { throw new SchedulerServiceDatabaseException(e); } finally { em.close(); } }  void setEntityManagerFactory(EntityManagerFactory emf); void setSecurityService(SecurityService securityService); void activate(ComponentContext cc); @Override synchronized void touchLastEntry(String agentId); @Override Date getLastModified(String agentId); @Override @SuppressWarnings("unchecked") Map<String, Date> getLastModifiedDates(); @Override void storeEvent(String mediapackageId, String organizationId, Opt<String> captureAgentId, Opt<Date> start,
          Opt<Date> end, Opt<String> source, Opt<String> recordingState, Opt<Long> recordingLastHeard,
          Opt<String> presenters, Opt<Date> lastModifiedDate, Opt<String> checksum, Opt<Map<String,
          String>> workflowProperties, Opt<Map<String, String>> caProperties
  ); @Override @SuppressWarnings("unchecked") List<String> getEvents(String captureAgentId, Date start, Date end, int separationMillis); @Override @SuppressWarnings("unchecked") List<ExtendedEventDto> search(
      Opt<String> captureAgentId,
      Opt<Date> optStartsFrom,
      Opt<Date> optStartsTo,
      Opt<Date> optEndFrom,
      Opt<Date> optEndTo,
      Opt<Integer> limit); @Override List<ExtendedEventDto> getKnownRecordings(); @Override void deleteEvent(String mediapackageId); @Override List<ExtendedEventDto> getEvents(); @Override Opt<ExtendedEventDto> getEvent(String mediapackageId, String orgId); @Override Opt<ExtendedEventDto> getEvent(String mediapackageId); @Override void resetRecordingState(String mediapackageId); @Override int countEvents(); static final String PERSISTENCE_UNIT; }
@Test public void testSearch() throws Exception { final long oneHourMillis = 3600_000; final long nowMillis = new Date().getTime(); final String mpId = "mpId"; final String agentId = "agent1"; final String orgId = new DefaultOrganization().getId(); final Date[] eventStartDates = { new Date(nowMillis - 4 * oneHourMillis), new Date(nowMillis - oneHourMillis), new Date(nowMillis + oneHourMillis), new Date(nowMillis + 5 * oneHourMillis), }; final List<Integer> indeces = Arrays.asList(0, 1, 2, 3); Collections.shuffle(indeces); for (int i : indeces) { final Date start = eventStartDates[i]; final Date end = new Date(start.getTime() + 2 * oneHourMillis); schedulerDatabase.storeEvent( mpId + i, orgId, Opt.some(agentId), Opt.some(start), Opt.some(end), Opt.none(), Opt.none(), Opt.none(), Opt.none(), Opt.none(), Opt.none(), Opt.none(), Opt.none() ); } final List<ExtendedEventDto> events1 = schedulerDatabase.search( Opt.none(), Opt.some(new Date(nowMillis)), Opt.none(), Opt.none(), Opt.none(), Opt.none() ); Assert.assertEquals(2, events1.size()); Assert.assertTrue(events1.get(0).getMediaPackageId().equals(mpId + 2)); Assert.assertTrue(events1.get(1).getMediaPackageId().equals(mpId + 3)); final List<ExtendedEventDto> events2 = schedulerDatabase.search( Opt.none(), Opt.none(), Opt.some(new Date(nowMillis)), Opt.none(), Opt.none(), Opt.none() ); Assert.assertEquals(2, events2.size()); Assert.assertTrue(events2.get(0).getMediaPackageId().equals(mpId + 0)); Assert.assertTrue(events2.get(1).getMediaPackageId().equals(mpId + 1)); final List<ExtendedEventDto> events3 = schedulerDatabase.search( Opt.none(), Opt.none(), Opt.none(), Opt.some(new Date(nowMillis)), Opt.none(), Opt.none() ); Assert.assertEquals(3, events3.size()); Assert.assertTrue(events3.get(0).getMediaPackageId().equals(mpId + 1)); Assert.assertTrue(events3.get(1).getMediaPackageId().equals(mpId + 2)); Assert.assertTrue(events3.get(2).getMediaPackageId().equals(mpId + 3)); final List<ExtendedEventDto> events4 = schedulerDatabase.search( Opt.none(), Opt.none(), Opt.none(), Opt.none(), Opt.some(new Date(nowMillis)), Opt.none() ); Assert.assertEquals(1, events4.size()); Assert.assertTrue(events4.get(0).getMediaPackageId().equals(mpId + 0)); Assert.assertTrue(schedulerDatabase.search( Opt.none(), Opt.some(new Date(nowMillis)), Opt.some(new Date(nowMillis)), Opt.none(), Opt.none(), Opt.none() ).isEmpty()); Assert.assertTrue(schedulerDatabase.search( Opt.none(), Opt.none(), Opt.none(), Opt.some(new Date(nowMillis)), Opt.some(new Date(nowMillis)), Opt.none() ).isEmpty()); Assert.assertTrue(schedulerDatabase.search( Opt.none(), Opt.some(new Date(nowMillis)), Opt.none(), Opt.none(), Opt.some(new Date(nowMillis)), Opt.none() ).isEmpty()); final List<ExtendedEventDto> events5 = schedulerDatabase.search( Opt.none(), Opt.some(new Date(nowMillis + oneHourMillis)), Opt.none(), Opt.none(), Opt.none(), Opt.none() ); Assert.assertEquals(2, events5.size()); Assert.assertTrue(events5.get(0).getMediaPackageId().equals(mpId + 2)); Assert.assertTrue(events5.get(1).getMediaPackageId().equals(mpId + 3)); final List<ExtendedEventDto> events6 = schedulerDatabase.search( Opt.none(), Opt.none(), Opt.some(new Date(nowMillis + oneHourMillis)), Opt.none(), Opt.none(), Opt.none() ); Assert.assertEquals(2, events6.size()); Assert.assertTrue(events6.get(0).getMediaPackageId().equals(mpId + 0)); Assert.assertTrue(events6.get(1).getMediaPackageId().equals(mpId + 1)); final List<ExtendedEventDto> events7 = schedulerDatabase.search( Opt.none(), Opt.none(), Opt.none(), Opt.some(new Date(nowMillis + oneHourMillis)), Opt.none(), Opt.none() ); Assert.assertEquals(3, events7.size()); Assert.assertTrue(events7.get(0).getMediaPackageId().equals(mpId + 1)); Assert.assertTrue(events7.get(1).getMediaPackageId().equals(mpId + 2)); Assert.assertTrue(events7.get(2).getMediaPackageId().equals(mpId + 3)); final List<ExtendedEventDto> events8 = schedulerDatabase.search( Opt.none(), Opt.none(), Opt.none(), Opt.none(), Opt.some(new Date(nowMillis + oneHourMillis)), Opt.none() ); Assert.assertEquals(1, events8.size()); Assert.assertTrue(events8.get(0).getMediaPackageId().equals(mpId + 0)); final List<ExtendedEventDto> events9 = schedulerDatabase.search( Opt.none(), Opt.none(), Opt.none(), Opt.none(), Opt.none(), Opt.none() ); Assert.assertEquals(4, events9.size()); Assert.assertTrue(events9.get(0).getMediaPackageId().equals(mpId + 0)); Assert.assertTrue(events9.get(1).getMediaPackageId().equals(mpId + 1)); Assert.assertTrue(events9.get(2).getMediaPackageId().equals(mpId + 2)); Assert.assertTrue(events9.get(3).getMediaPackageId().equals(mpId + 3)); final List<ExtendedEventDto> eventsA = schedulerDatabase.search( Opt.none(), Opt.none(), Opt.some(new Date(nowMillis)), Opt.none(), Opt.some(new Date(nowMillis)), Opt.none() ); Assert.assertEquals(1, eventsA.size()); Assert.assertTrue(eventsA.get(0).getMediaPackageId().equals(mpId + 0)); final List<ExtendedEventDto> eventsB = schedulerDatabase.search( Opt.none(), Opt.none(), Opt.some(new Date(nowMillis)), Opt.some(new Date(nowMillis)), Opt.none(), Opt.none() ); Assert.assertEquals(1, eventsB.size()); Assert.assertTrue(eventsB.get(0).getMediaPackageId().equals(mpId + 1)); final List<ExtendedEventDto> eventsC = schedulerDatabase.search( Opt.none(), Opt.some(new Date(nowMillis)), Opt.none(), Opt.some(new Date(nowMillis)), Opt.none(), Opt.none() ); Assert.assertEquals(2, eventsC.size()); Assert.assertTrue(eventsC.get(0).getMediaPackageId().equals(mpId + 2)); Assert.assertTrue(eventsC.get(1).getMediaPackageId().equals(mpId + 3)); }