Search in sources :

Example 16 with RRule

use of net.fortuna.ical4j.model.property.RRule in project OpenOLAT by OpenOLAT.

the class ICalFileCalendarManager method createKalendarEventRecurringOccurence.

@Override
public KalendarEvent createKalendarEventRecurringOccurence(KalendarRecurEvent recurEvent) {
    KalendarEvent rootEvent = recurEvent.getCalendar().getEvent(recurEvent.getID(), null);
    VEvent vEvent = getVEvent(recurEvent);
    PropertyList vEventProperties = vEvent.getProperties();
    for (Iterator<?> objIt = vEventProperties.iterator(); objIt.hasNext(); ) {
        Object property = objIt.next();
        if (property instanceof RRule || property instanceof ExDate) {
            objIt.remove();
        }
    }
    try {
        Kalendar calendar = recurEvent.getCalendar();
        Date startDate = recurEvent.getOccurenceDate();
        String startString = CalendarUtils.formatRecurrenceDate(startDate, rootEvent.isAllDayEvent());
        RecurrenceId recurId;
        if (rootEvent.isAllDayEvent()) {
            recurId = new RecurrenceId(tz);
            recurId.setDate(CalendarUtils.createDate(startDate));
        } else {
            recurId = new RecurrenceId(startString, tz);
        }
        vEventProperties.add(recurId);
        KalendarEvent kEvent = getKalendarEvent(vEvent);
        kEvent.setKalendar(calendar);
        return kEvent;
    } catch (ParseException e) {
        log.error("", e);
        return null;
    }
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) PropertyList(net.fortuna.ical4j.model.PropertyList) RRule(net.fortuna.ical4j.model.property.RRule) Kalendar(org.olat.commons.calendar.model.Kalendar) ExDate(net.fortuna.ical4j.model.property.ExDate) KalendarEvent(org.olat.commons.calendar.model.KalendarEvent) RecurrenceId(net.fortuna.ical4j.model.property.RecurrenceId) ParseException(java.text.ParseException) Date(java.util.Date) ExDate(net.fortuna.ical4j.model.property.ExDate)

Example 17 with RRule

use of net.fortuna.ical4j.model.property.RRule in project OpenOLAT by OpenOLAT.

the class ICalFileCalendarManager method getRecurrenceRule.

/**
 * Build iCalendar-compliant recurrence rule
 * @param recurrence
 * @param recurrenceEnd
 * @return rrule
 */
@Override
public String getRecurrenceRule(String recurrence, Date recurrenceEnd) {
    if (recurrence != null) {
        // recurrence available
        // create recurrence rule
        StringBuilder sb = new StringBuilder();
        sb.append("FREQ=");
        if (recurrence.equals(KalendarEvent.WORKDAILY)) {
            // build rule for monday to friday
            sb.append(KalendarEvent.DAILY).append(";").append("BYDAY=MO,TU,WE,TH,FR");
        } else if (recurrence.equals(KalendarEvent.BIWEEKLY)) {
            // build rule for biweekly
            sb.append(KalendarEvent.WEEKLY).append(";").append("INTERVAL=2");
        } else {
            // normal supported recurrence
            sb.append(recurrence);
        }
        if (recurrenceEnd != null) {
            java.util.Calendar recurEndCal = java.util.Calendar.getInstance();
            recurEndCal.setTimeZone(tz);
            recurEndCal.setTime(recurrenceEnd);
            recurEndCal = CalendarUtils.getEndOfDay(recurEndCal);
            long recTime = recurEndCal.getTimeInMillis() - tz.getOffset(recurEndCal.getTimeInMillis());
            DateTime recurEndDT = new DateTime(recTime);
            if (tz != null) {
                recurEndDT.setTimeZone(tz);
            }
            sb.append(";").append(KalendarEvent.UNTIL).append("=").append(recurEndDT.toString());
        }
        try {
            Recur recur = new Recur(sb.toString());
            RRule rrule = new RRule(recur);
            return rrule.getValue();
        } catch (ParseException e) {
            log.error("cannot create recurrence rule: " + recurrence.toString(), e);
        }
    }
    return null;
}
Also used : RRule(net.fortuna.ical4j.model.property.RRule) ParseException(java.text.ParseException) DateTime(net.fortuna.ical4j.model.DateTime) Recur(net.fortuna.ical4j.model.Recur)

Example 18 with RRule

use of net.fortuna.ical4j.model.property.RRule in project opencast by opencast.

the class IndexServiceImplTest method testCreateEventInputNormalExpectsCreatedRecurringEvent.

@Test
public void testCreateEventInputNormalExpectsCreatedRecurringEvent() throws Exception {
    String expectedTitle = "Test Event Creation";
    String username = "akm220";
    String org = "mh_default_org";
    String[] creators = new String[] {};
    Id mpId = new IdImpl("mp-id");
    String testResourceLocation = "/events/create-recurring-event.json";
    JSONObject metadataJson = (JSONObject) parser.parse(IOUtils.toString(IndexServiceImplTest.class.getResourceAsStream(testResourceLocation)));
    Capture<String> mediapackageIdResult = EasyMock.newCapture();
    Capture<String> catalogIdResult = EasyMock.newCapture();
    Capture<String> filenameResult = EasyMock.newCapture();
    Capture<InputStream> catalogResult = EasyMock.newCapture();
    Capture<String> mediapackageTitleResult = EasyMock.newCapture();
    SecurityService securityService = setupSecurityService(username, org);
    Workspace workspace = EasyMock.createMock(Workspace.class);
    EasyMock.expect(workspace.put(EasyMock.capture(mediapackageIdResult), EasyMock.capture(catalogIdResult), EasyMock.capture(filenameResult), EasyMock.capture(catalogResult))).andReturn(new URI("catalog.xml"));
    EasyMock.expect(workspace.read(getClass().getResource("/dublincore.xml").toURI())).andAnswer(() -> getClass().getResourceAsStream("/dublincore.xml")).anyTimes();
    EasyMock.replay(workspace);
    // Create Common Event Catalog UI Adapter
    CommonEventCatalogUIAdapter commonEventCatalogUIAdapter = setupCommonCatalogUIAdapter(workspace).getA();
    // Setup mediapackage.
    MediaPackage mediapackage = EasyMock.createMock(MediaPackage.class);
    EasyMock.expect(mediapackage.clone()).andReturn(mediapackage).anyTimes();
    EasyMock.expect(mediapackage.getSeries()).andReturn(null).anyTimes();
    EasyMock.expect(mediapackage.getCatalogs(EasyMock.anyObject(MediaPackageElementFlavor.class))).andReturn(new Catalog[] { CatalogImpl.fromURI(getClass().getResource("/dublincore.xml").toURI()) });
    EasyMock.expect(mediapackage.getIdentifier()).andReturn(mpId).anyTimes();
    EasyMock.expect(mediapackage.getCreators()).andReturn(creators);
    mediapackage.addCreator("");
    EasyMock.expectLastCall();
    mediapackage.setTitle(EasyMock.capture(mediapackageTitleResult));
    EasyMock.expectLastCall().once();
    mediapackage.setTitle(EasyMock.anyString());
    EasyMock.expectLastCall().times(15);
    EasyMock.expect(mediapackage.getElements()).andReturn(new MediaPackageElement[] {}).anyTimes();
    EasyMock.expect(mediapackage.getCatalogs(EasyMock.anyObject(MediaPackageElementFlavor.class))).andReturn(new Catalog[] {}).anyTimes();
    mediapackage.setIdentifier(EasyMock.anyObject(Id.class));
    EasyMock.expectLastCall().anyTimes();
    mediapackage.setSeries(EasyMock.anyString());
    mediapackage.setSeriesTitle(EasyMock.anyString());
    EasyMock.expectLastCall();
    EasyMock.replay(mediapackage);
    CaptureAgentStateService captureAgentStateService = setupCaptureAgentStateService();
    // Setup scheduler service
    Capture<Date> recurrenceStart = EasyMock.newCapture();
    Capture<Date> recurrenceEnd = EasyMock.newCapture();
    Capture<RRule> rrule = EasyMock.newCapture();
    Capture duration = EasyMock.newCapture();
    Capture<TimeZone> tz = EasyMock.newCapture();
    Capture<Date> schedStart = EasyMock.newCapture();
    Capture<Date> schedEnd = EasyMock.newCapture();
    Capture<RRule> schedRRule = EasyMock.newCapture();
    Capture schedDuration = EasyMock.newCapture();
    Capture<TimeZone> schedTz = EasyMock.newCapture();
    Capture<MediaPackage> mp = EasyMock.newCapture();
    SchedulerService schedulerService = EasyMock.createNiceMock(SchedulerService.class);
    // Look up the expected periods
    EasyMock.expect(schedulerService.calculatePeriods(EasyMock.capture(rrule), EasyMock.capture(recurrenceStart), EasyMock.capture(recurrenceEnd), EasyMock.captureLong(duration), EasyMock.capture(tz))).andAnswer(new IAnswer<List<Period>>() {

        @Override
        public List<Period> answer() throws Throwable {
            return calculatePeriods(rrule.getValue(), recurrenceStart.getValue(), recurrenceEnd.getValue(), (Long) duration.getValue(), tz.getValue());
        }
    }).anyTimes();
    // The actual scheduling
    EasyMock.expect(schedulerService.addMultipleEvents(EasyMock.capture(schedRRule), EasyMock.capture(schedStart), EasyMock.capture(schedEnd), EasyMock.captureLong(schedDuration), EasyMock.capture(schedTz), EasyMock.anyString(), EasyMock.<Set<String>>anyObject(), EasyMock.capture(mp), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Opt<Boolean>>anyObject(), EasyMock.<Opt<String>>anyObject(), EasyMock.anyString())).andAnswer(new IAnswer<Map<String, Period>>() {

        @Override
        public Map<String, Period> answer() throws Throwable {
            List<Period> periods = calculatePeriods(schedRRule.getValue(), schedStart.getValue(), schedEnd.getValue(), (Long) schedDuration.getValue(), schedTz.getValue());
            Map<String, Period> mapping = new LinkedHashMap<>();
            int counter = 0;
            for (Period p : periods) {
                mapping.put(new IdImpl(UUID.randomUUID().toString()).compact(), p);
            }
            return mapping;
        }
    }).anyTimes();
    EasyMock.replay(schedulerService);
    // Run Test
    IndexServiceImpl indexServiceImpl = new IndexServiceImpl();
    indexServiceImpl.setAuthorizationService(setupAuthorizationService(mediapackage));
    indexServiceImpl.setIngestService(setupIngestService(mediapackage, Capture.<InputStream>newInstance()));
    indexServiceImpl.setCommonEventCatalogUIAdapter(commonEventCatalogUIAdapter);
    indexServiceImpl.addCatalogUIAdapter(commonEventCatalogUIAdapter);
    indexServiceImpl.setSecurityService(securityService);
    indexServiceImpl.setUserDirectoryService(noUsersUserDirectoryService);
    indexServiceImpl.setWorkspace(workspace);
    indexServiceImpl.setCaptureAgentStateService(captureAgentStateService);
    indexServiceImpl.setSchedulerService(schedulerService);
    String scheduledEvents = indexServiceImpl.createEvent(metadataJson, mediapackage);
    String[] ids = StringUtils.split(scheduledEvents, ",");
    // We should have as many scheduled events as we do periods
    Assert.assertTrue(ids.length == calculatePeriods(rrule.getValue(), recurrenceStart.getValue(), recurrenceEnd.getValue(), (Long) duration.getValue(), tz.getValue()).size());
    assertEquals("The catalog should have been added to the correct mediapackage", mpId.toString(), mediapackageIdResult.getValue());
    assertTrue("The catalog should have a new id", catalogIdResult.hasCaptured());
    assertTrue("The catalog should have a new filename", filenameResult.hasCaptured());
    assertTrue("The catalog should have been added to the input stream", catalogResult.hasCaptured());
    assertTrue("The mediapackage should have had its title updated", catalogResult.hasCaptured());
    assertEquals("The mediapackage title should have been updated.", expectedTitle, mediapackageTitleResult.getValue());
    assertTrue("The catalog should have been created", catalogResult.hasCaptured());
    // Assert that the start and end recurrence dates captured, along with the duration and recurrence rule
    // This is all used by the scheduling calculation, but not the actual scheduling call
    assertTrue(recurrenceStart.hasCaptured());
    assertTrue(recurrenceEnd.hasCaptured());
    assertTrue(duration.hasCaptured());
    assertTrue(rrule.hasCaptured());
    // Assert that the scheduling call has its necessary data
    assertTrue(schedStart.hasCaptured());
    assertTrue(schedEnd.hasCaptured());
    assertTrue(schedDuration.hasCaptured());
    assertTrue(schedRRule.hasCaptured());
    assertTrue(schedTz.hasCaptured());
    List<Period> pCheck = calculatePeriods(schedRRule.getValue(), schedStart.getValue(), schedEnd.getValue(), (Long) schedDuration.getValue(), schedTz.getValue());
    List<Period> pExpected = calculatePeriods(rrule.getValue(), recurrenceStart.getValue(), recurrenceEnd.getValue(), (Long) duration.getValue(), tz.getValue());
    // Assert that the first capture time is the same as the recurrence start
    assertEquals(pExpected.get(0).getStart(), pCheck.get(0).getStart());
    // Assert that the end of the last capture time is the same as the recurrence end
    assertEquals(pExpected.get(pExpected.size() - 1).getEnd(), pCheck.get(pCheck.size() - 1).getEnd());
}
Also used : SchedulerService(org.opencastproject.scheduler.api.SchedulerService) Set(java.util.Set) RRule(net.fortuna.ical4j.model.property.RRule) URI(java.net.URI) IdImpl(org.opencastproject.mediapackage.identifier.IdImpl) Capture(org.easymock.Capture) LinkedHashMap(java.util.LinkedHashMap) Opt(com.entwinemedia.fn.data.Opt) SecurityService(org.opencastproject.security.api.SecurityService) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) CommonEventCatalogUIAdapter(org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter) CaptureAgentStateService(org.opencastproject.capture.admin.api.CaptureAgentStateService) InputStream(java.io.InputStream) Period(net.fortuna.ical4j.model.Period) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) Catalog(org.opencastproject.mediapackage.Catalog) Date(java.util.Date) IAnswer(org.easymock.IAnswer) TimeZone(java.util.TimeZone) JSONObject(org.json.simple.JSONObject) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Id(org.opencastproject.mediapackage.identifier.Id) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Workspace(org.opencastproject.workspace.api.Workspace) Test(org.junit.Test)

Example 19 with RRule

use of net.fortuna.ical4j.model.property.RRule in project ofbiz-framework by apache.

the class ICalRecurConverter method visit.

@Override
public void visit(Intersection expr) {
    this.stateStack.push(this.state);
    VisitorState newState = new VisitorState();
    newState.isExcluded = this.state.isExcluded;
    newState.isIntersection = true;
    this.state = newState;
    for (TemporalExpression childExpr : expr.getExpressionSet()) {
        childExpr.accept(this);
    }
    this.state = this.stateStack.pop();
    if (newState.inclRecurList.size() > 0) {
        this.incRuleList.add(new RRule(this.consolidateRecurs(newState.inclRecurList)));
    }
    if (newState.exRecurList.size() > 0) {
        this.exRuleList.add(new ExRule(this.consolidateRecurs(newState.exRecurList)));
    }
}
Also used : TemporalExpression(org.apache.ofbiz.service.calendar.TemporalExpression) RRule(net.fortuna.ical4j.model.property.RRule) ExRule(net.fortuna.ical4j.model.property.ExRule)

Example 20 with RRule

use of net.fortuna.ical4j.model.property.RRule in project opencast by opencast.

the class SchedulerServiceImplTest method testFindConflictingEvents.

@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.<Boolean>none(), Opt.<String>none(), SchedulerService.ORIGIN);
    schedSvc.addEvent(new Date(currentTime + hours(24)), new Date(currentTime + hours(25)), "Device A", Collections.<String>emptySet(), mediaPackageB, wfProperties, Collections.<String, String>emptyMap(), Opt.some(true), Opt.<String>none(), SchedulerService.ORIGIN);
    schedSvc.addEvent(new Date(currentTime - hours(1)), new Date(currentTime - minutes(10)), "Device C", Collections.<String>emptySet(), mediaPackageC, wfProperties, Collections.<String, String>emptyMap(), Opt.<Boolean>none(), Opt.<String>none(), SchedulerService.ORIGIN);
    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.<Boolean>none(), Opt.<String>none(), SchedulerService.ORIGIN);
    {
        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());
    }
    {
        List<MediaPackage> events = schedSvc.findConflictingEvents("Device A", new RRule("FREQ=WEEKLY;BYDAY=SU,MO,TU,WE,TH,FR,SA"), start, new Date(start.getTime() + hours(48)), new Long(seconds(36)), TimeZone.getTimeZone("America/Chicago"));
        assertEquals(2, events.size());
    }
    {
        // Event A starts before event B, and ends during event B
        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());
        // Event A starts during event B, and ends after event B
        conflicts = schedSvc.findConflictingEvents("Device A", new Date(currentTime + hours(24) + minutes(30)), new Date(currentTime + hours(25) + minutes(30)));
        assertEquals(1, conflicts.size());
        // Event A starts at the same time as event B
        conflicts = schedSvc.findConflictingEvents("Device A", new Date(currentTime + hours(24)), new Date(currentTime + hours(24) + minutes(30)));
        assertEquals(1, conflicts.size());
        // Event A ends at the same time as event B
        conflicts = schedSvc.findConflictingEvents("Device A", new Date(currentTime + hours(24) + minutes(10)), new Date(currentTime + hours(25)));
        assertEquals(1, conflicts.size());
        // Event A is contained entirely within event B
        conflicts = schedSvc.findConflictingEvents("Device A", new Date(currentTime + hours(24) + minutes(10)), new Date(currentTime + hours(24) + minutes(50)));
        assertEquals(1, conflicts.size());
        // Event A contains event B entirely
        conflicts = schedSvc.findConflictingEvents("Device A", new Date(currentTime + hours(23)), new Date(currentTime + hours(26)));
        assertEquals(1, conflicts.size());
        // Event A ends with less than one minute before event B starts
        conflicts = schedSvc.findConflictingEvents("Device A", new Date(currentTime + hours(23)), new Date(currentTime + hours(24) - seconds(1)));
        assertEquals(1, conflicts.size());
        // Event A begins than one minute after event B ends
        conflicts = schedSvc.findConflictingEvents("Device A", new Date(currentTime + hours(25) + seconds(1)), new Date(currentTime + hours(27)));
        assertEquals(1, conflicts.size());
    }
}
Also used : RRule(net.fortuna.ical4j.model.property.RRule) MediaPackage(org.opencastproject.mediapackage.MediaPackage) PropertyList(net.fortuna.ical4j.model.PropertyList) DublinCoreCatalogList(org.opencastproject.metadata.dublincore.DublinCoreCatalogList) ArrayList(java.util.ArrayList) AccessControlList(org.opencastproject.security.api.AccessControlList) List(java.util.List) ComponentList(net.fortuna.ical4j.model.ComponentList) Date(java.util.Date) Test(org.junit.Test)

Aggregations

RRule (net.fortuna.ical4j.model.property.RRule)23 ParseException (java.text.ParseException)14 Date (java.util.Date)11 Recur (net.fortuna.ical4j.model.Recur)9 DateTime (net.fortuna.ical4j.model.DateTime)8 PropertyList (net.fortuna.ical4j.model.PropertyList)7 ExDate (net.fortuna.ical4j.model.property.ExDate)6 VEvent (net.fortuna.ical4j.model.component.VEvent)5 MediaPackage (org.opencastproject.mediapackage.MediaPackage)5 TimeZone (java.util.TimeZone)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 Period (net.fortuna.ical4j.model.Period)4 RecurrenceId (net.fortuna.ical4j.model.property.RecurrenceId)4 Kalendar (org.olat.commons.calendar.model.Kalendar)4 KalendarEvent (org.olat.commons.calendar.model.KalendarEvent)4 IOException (java.io.IOException)3 Calendar (java.util.Calendar)3 Path (javax.ws.rs.Path)3 Date (net.fortuna.ical4j.model.Date)3 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)3