SchedulerServiceImpl extends AbstractIndexProducer implements SchedulerService, ManagedService { @Override public List<MediaPackage> findConflictingEvents(String captureDeviceID, Date startDate, Date endDate) throws SchedulerException { try { final Organization organization = securityService.getOrganization(); final User user = SecurityUtil.createSystemUser(systemUserName, organization); List<MediaPackage> conflictingEvents = new ArrayList(); SecurityUtil.runAs(securityService, organization, user, () -> { try { conflictingEvents.addAll(persistence.getEvents(captureDeviceID, startDate, endDate, Util.EVENT_MINIMUM_SEPARATION_MILLISECONDS) .stream().map(this::getEventMediaPackage).collect(Collectors.toList())); } catch (SchedulerServiceDatabaseException e) { logger.error("Failed to get conflicting events", e); } }); return conflictingEvents; } catch (Exception e) { throw new SchedulerException(e); } }  void setMessageSender(MessageSender messageSender); void setMessageReceiver(MessageReceiver messageReceiver); void setPersistence(SchedulerServiceDatabase persistence); void setSeriesService(SeriesService seriesService); void setSecurityService(SecurityService securityService); void setAssetManager(AssetManager assetManager); void setWorkspace(Workspace workspace); void setAuthorizationService(AuthorizationService authorizationService); void setOrgDirectoryService(OrganizationDirectoryService orgDirectoryService); void addCatalogUIAdapter(EventCatalogUIAdapter catalogUIAdapter); void removeCatalogUIAdapter(EventCatalogUIAdapter catalogUIAdapter); void activate(ComponentContext cc); @Override void deactivate(); @Override void updated(Dictionary<String, ?> properties); @Override void addEvent(Date startDateTime, Date endDateTime, String captureAgentId, Set<String> userIds,
          MediaPackage mediaPackage, Map<String, String> wfProperties, Map<String, String> caMetadata,
          Opt<String> schedulingSource); @Override Map<String, Period> addMultipleEvents(RRule rRule, Date start, Date end, Long duration, TimeZone tz,
          String captureAgentId, Set<String> userIds, MediaPackage templateMp, Map<String, String> wfProperties,
          Map<String, String> caMetadata, Opt<String> schedulingSource); @Override void updateEvent(final String mpId, Opt<Date> startDateTime, Opt<Date> endDateTime, Opt<String> captureAgentId,
          Opt<Set<String>> userIds, Opt<MediaPackage> mediaPackage, Opt<Map<String, String>> wfProperties,
          Opt<Map<String, String>> caMetadata); @Override void updateEvent(final String mpId, Opt<Date> startDateTime, Opt<Date> endDateTime, Opt<String> captureAgentId,
          Opt<Set<String>> userIds, Opt<MediaPackage> mediaPackage, Opt<Map<String, String>> wfProperties,
          Opt<Map<String, String>> caMetadata, boolean allowConflict); @Override synchronized void removeEvent(String mediaPackageId); @Override MediaPackage getMediaPackage(String mediaPackageId); @Override DublinCoreCatalog getDublinCore(String mediaPackageId); @Override TechnicalMetadata getTechnicalMetadata(String mediaPackageId); @Override Map<String, String> getWorkflowConfig(String mediaPackageId); @Override Map<String, String> getCaptureAgentConfiguration(String mediaPackageId); @Override int getEventCount(); @Override List<MediaPackage> search(Opt<String> captureAgentId, Opt<Date> startsFrom, Opt<Date> startsTo,
          Opt<Date> endFrom, Opt<Date> endTo); @Override Opt<MediaPackage> getCurrentRecording(String captureAgentId); @Override Opt<MediaPackage> getUpcomingRecording(String captureAgentId); @Override List<MediaPackage> findConflictingEvents(String captureDeviceID, Date startDate, Date endDate); @Override List<MediaPackage> findConflictingEvents(String captureAgentId, RRule rrule, Date start, Date end,
          long duration, TimeZone tz); @Override String getCalendar(Opt<String> captureAgentId, Opt<String> seriesId, Opt<Date> cutoff); @Override String getScheduleLastModified(String captureAgentId); @Override void removeScheduledRecordingsBeforeBuffer(long buffer); @Override boolean updateRecordingState(String id, String state); @Override Recording getRecordingState(String id); @Override void removeRecording(String id); @Override Map<String, Recording> getKnownRecordings(); @Override void repopulate(final String indexName); @Override MessageReceiver getMessageReceiver(); @Override Service getService(); @Override String getClassName(); @Override MessageSender getMessageSender(); @Override SecurityService getSecurityService(); @Override String getSystemUserName(); static final String WORKFLOW_CONFIG_PREFIX; }
@Test public void testFindConflictingEvents() throws Exception { final long currentTime = System.currentTimeMillis(); final MediaPackage mediaPackageA = generateEvent(Opt.<String> none()); final MediaPackage mediaPackageB = generateEvent(Opt.<String> none()); final MediaPackage mediaPackageC = generateEvent(Opt.<String> none()); final MediaPackage mediaPackageD = generateEvent(Opt.<String> none()); schedSvc.addEvent(new Date(currentTime + seconds(10)), new Date(currentTime + hours(1) + seconds(10)), "Device A", Collections.<String> emptySet(), mediaPackageA, wfProperties, Collections.<String, String> emptyMap(), Opt.<String> none()); schedSvc.addEvent(new Date(currentTime + hours(24)), new Date(currentTime + hours(25)), "Device A", Collections.<String> emptySet(), mediaPackageB, wfProperties, Collections.<String, String> emptyMap(), Opt.<String> none()); schedSvc.addEvent(new Date(currentTime - hours(1)), new Date(currentTime - minutes(10)), "Device C", Collections.<String> emptySet(), mediaPackageC, wfProperties, Collections.<String, String> emptyMap(), Opt.<String> none()); schedSvc.addEvent(new Date(currentTime + seconds(10)), new Date(currentTime + hours(1) + seconds(10)), "Device D", Collections.<String> emptySet(), mediaPackageD, wfProperties, Collections.<String, String> emptyMap(), Opt.<String> none()); { List<MediaPackage> allEvents = schedSvc.search(Opt.<String> none(), Opt.<Date> none(), Opt.<Date> none(), Opt.<Date> none(), Opt.<Date> none()); assertEquals(4, allEvents.size()); } final Date start = new Date(currentTime); final Date end = new Date(currentTime + hours(2)); { List<MediaPackage> events = schedSvc.search(Opt.some("Some Other Device"), Opt.some(start), Opt.<Date> none(), Opt.<Date> none(), Opt.some(end)); assertEquals(0, events.size()); } { List<MediaPackage> events = schedSvc.search(Opt.some("Device A"), Opt.some(start), Opt.<Date> none(), Opt.<Date> none(), Opt.some(end)); assertEquals(1, events.size()); } { ZonedDateTime startZdt = ZonedDateTime.ofInstant(start.toInstant(), ZoneOffset.UTC); List<MediaPackage> events = schedSvc.findConflictingEvents("Device A", new RRule("FREQ=WEEKLY;BYDAY=SU,MO,TU,WE,TH,FR,SA;BYHOUR=" + startZdt.getHour() + ";BYMINUTE=" + startZdt.getMinute()), start, new Date(start.getTime() + hours(48)), new Long(seconds(36)), TimeZone.getTimeZone("America/Chicago")); assertEquals(2, events.size()); } { List<MediaPackage> conflicts = schedSvc.findConflictingEvents("Device A", new RRule("FREQ=WEEKLY;BYDAY=WE;BYHOUR=16;BYMINUTE=0"), new Date(1550332800000L), new Date(1550336100000L), 1000, TimeZone.getTimeZone("Africa/Johannesburg")); assertEquals(0, conflicts.size()); } { List<MediaPackage> conflicts = schedSvc.findConflictingEvents("Device A", new Date(currentTime + hours(23) + minutes(30)), new Date(currentTime + hours(24) + minutes(30))); assertEquals(1, conflicts.size()); conflicts = schedSvc.findConflictingEvents("Device A", new Date(currentTime + hours(24) + minutes(30)), new Date(currentTime + hours(25) + minutes(30))); assertEquals(1, conflicts.size()); conflicts = schedSvc.findConflictingEvents("Device A", new Date(currentTime + hours(24)), new Date(currentTime + hours(24) + minutes(30))); assertEquals(1, conflicts.size()); conflicts = schedSvc.findConflictingEvents("Device A", new Date(currentTime + hours(24) + minutes(10)), new Date(currentTime + hours(25))); assertEquals(1, conflicts.size()); conflicts = schedSvc.findConflictingEvents("Device A", new Date(currentTime + hours(24) + minutes(10)), new Date(currentTime + hours(24) + minutes(50))); assertEquals(1, conflicts.size()); conflicts = schedSvc.findConflictingEvents("Device A", new Date(currentTime + hours(23)), new Date(currentTime + hours(26))); assertEquals(1, conflicts.size()); conflicts = schedSvc.findConflictingEvents("Device A", new Date(currentTime + hours(23)), new Date(currentTime + hours(24) - seconds(1))); assertEquals(1, conflicts.size()); conflicts = schedSvc.findConflictingEvents("Device A", new Date(currentTime + hours(25) + seconds(1)), new Date(currentTime + hours(27))); assertEquals(1, conflicts.size()); } }