Search in sources :

Example 66 with DateTime

use of net.fortuna.ical4j.model.DateTime in project openmeetings by apache.

the class IcalUtils method parseAppointmentstoCalendar.

/**
 * Parses a List of Appointments into a VCALENDAR component.
 *
 * @param appointments List of Appointments for the Calendar
 * @param ownerId      Owner of the Appointments
 * @return VCALENDAR representation of the Appointments
 */
public Calendar parseAppointmentstoCalendar(List<Appointment> appointments, Long ownerId) {
    String tzid = parseTimeZone(null, userDao.get(ownerId)).getID();
    TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
    net.fortuna.ical4j.model.TimeZone timeZone = registry.getTimeZone(tzid);
    if (timeZone == null) {
        throw new NoSuchElementException("Unable to get time zone by id provided: " + tzid);
    }
    Calendar icsCalendar = new Calendar();
    icsCalendar.getProperties().add(new ProdId(PROD_ID));
    icsCalendar.getProperties().add(Version.VERSION_2_0);
    icsCalendar.getProperties().add(CalScale.GREGORIAN);
    icsCalendar.getComponents().add(timeZone.getVTimeZone());
    for (Appointment appointment : appointments) {
        DateTime start = new DateTime(appointment.getStart()), end = new DateTime(appointment.getEnd());
        VEvent meeting = new VEvent(start, end, appointment.getTitle());
        meeting = addVEventpropsfromAppointment(appointment, meeting);
        icsCalendar.getComponents().add(meeting);
    }
    return icsCalendar;
}
Also used : Appointment(org.apache.openmeetings.db.entity.calendar.Appointment) VEvent(net.fortuna.ical4j.model.component.VEvent) OmCalendar(org.apache.openmeetings.db.entity.calendar.OmCalendar) Calendar(net.fortuna.ical4j.model.Calendar) TimeZoneRegistry(net.fortuna.ical4j.model.TimeZoneRegistry) ProdId(net.fortuna.ical4j.model.property.ProdId) NoSuchElementException(java.util.NoSuchElementException) DateTime(net.fortuna.ical4j.model.DateTime)

Example 67 with DateTime

use of net.fortuna.ical4j.model.DateTime in project openolat by klemens.

the class CalendarImportTest method testImportRecurringCal.

@Test
public void testImportRecurringCal() throws IOException, ParserException {
    InputStream in = CalendarImportTest.class.getResourceAsStream("RecurringEvent.ics");
    CalendarBuilder builder = new CalendarBuilder();
    Calendar calendar = builder.build(in);
    assertNotNull(calendar);
    VEvent rootEvent = null;
    VEvent exceptionEvent = null;
    for (Iterator<?> iter = calendar.getComponents().iterator(); iter.hasNext(); ) {
        Object comp = iter.next();
        if (comp instanceof VEvent) {
            VEvent vevent = (VEvent) comp;
            if (vevent.getRecurrenceId() == null) {
                rootEvent = vevent;
            } else {
                exceptionEvent = vevent;
            }
        }
    }
    assertNotNull(rootEvent);
    assertNotNull(exceptionEvent);
    java.util.Date startDate = CalendarUtils.getDate(2016, java.util.Calendar.OCTOBER, 10);
    DateTime start = new DateTime(startDate);
    java.util.Date endDate = CalendarUtils.getDate(2016, java.util.Calendar.NOVEMBER, 10);
    DateTime end = new DateTime(endDate);
    Period period = new Period(start, end);
    PeriodList pList = rootEvent.calculateRecurrenceSet(period);
    for (Object obj : pList) {
        Period p = (Period) obj;
        System.out.println("Period: " + p.getStart());
    }
    RecurrenceId recurrenceId = exceptionEvent.getRecurrenceId();
    Date recurrenceDate = recurrenceId.getDate();
    System.out.println("Recurrence: " + recurrenceDate);
    exceptionEvent.getSequence();
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) CalendarBuilder(net.fortuna.ical4j.data.CalendarBuilder) InputStream(java.io.InputStream) Date(java.util.Date) Calendar(net.fortuna.ical4j.model.Calendar) Period(net.fortuna.ical4j.model.Period) PeriodList(net.fortuna.ical4j.model.PeriodList) DateTime(net.fortuna.ical4j.model.DateTime) Date(java.util.Date) RecurrenceId(net.fortuna.ical4j.model.property.RecurrenceId) Test(org.junit.Test)

Example 68 with DateTime

use of net.fortuna.ical4j.model.DateTime in project openolat by klemens.

the class ICalFileCalendarManager method getRecurringEventsInPeriod.

private final List<KalendarRecurEvent> getRecurringEventsInPeriod(KalendarEvent kEvent, Date periodStart, Date periodEnd, TimeZone userTz) {
    VEvent vEvent = getVEvent(kEvent);
    if (vEvent.getEndDate() == null || vEvent.getStartDate().getDate().after(vEvent.getEndDate().getDate())) {
        return Collections.emptyList();
    }
    // calculate the events in the specified period
    Period recurringPeriod = new Period(new DateTime(periodStart), new DateTime(periodEnd));
    PeriodList periodList = vEvent.calculateRecurrenceSet(recurringPeriod);
    List<KalendarRecurEvent> recurringEvents = new ArrayList<>(periodList.size());
    for (Object obj : periodList) {
        Period period = (Period) obj;
        Date date = period.getStart();
        java.util.Calendar eventStartCal = java.util.Calendar.getInstance();
        eventStartCal.clear();
        eventStartCal.setTime(kEvent.getBegin());
        java.util.Calendar eventEndCal = java.util.Calendar.getInstance();
        eventEndCal.clear();
        eventEndCal.setTime(kEvent.getEnd());
        java.util.Calendar recurStartCal = java.util.Calendar.getInstance();
        recurStartCal.clear();
        if (userTz == null) {
            recurStartCal.setTimeInMillis(date.getTime());
        } else {
            recurStartCal.setTimeInMillis(date.getTime() - userTz.getOffset(date.getTime()));
        }
        long duration = kEvent.getEnd().getTime() - kEvent.getBegin().getTime();
        java.util.Calendar beginCal = java.util.Calendar.getInstance();
        beginCal.clear();
        beginCal.set(recurStartCal.get(java.util.Calendar.YEAR), recurStartCal.get(java.util.Calendar.MONTH), recurStartCal.get(java.util.Calendar.DATE), eventStartCal.get(java.util.Calendar.HOUR_OF_DAY), eventStartCal.get(java.util.Calendar.MINUTE), eventStartCal.get(java.util.Calendar.SECOND));
        java.util.Calendar endCal = java.util.Calendar.getInstance();
        endCal.clear();
        endCal.setTimeInMillis(beginCal.getTimeInMillis() + duration);
        boolean original = false;
        if (kEvent.getBegin().compareTo(beginCal.getTime()) == 0) {
            // prevent doubled events
            original = true;
        }
        Date recurrenceEnd = getRecurrenceEndDate(kEvent.getRecurrenceRule());
        if (kEvent.isAllDayEvent() && recurrenceEnd != null && recurStartCal.getTime().after(recurrenceEnd)) {
            // workaround for ical4j-bug in all day events
            continue;
        }
        KalendarRecurEvent recurEvent = new KalendarRecurEvent(kEvent.getID(), original, kEvent.getSubject(), beginCal.getTime(), endCal.getTime());
        recurEvent.setOccurenceDate(beginCal.getTime());
        recurEvent.setSourceEvent(kEvent);
        recurringEvents.add(recurEvent);
    }
    return recurringEvents;
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) ArrayList(java.util.ArrayList) Period(net.fortuna.ical4j.model.Period) PeriodList(net.fortuna.ical4j.model.PeriodList) DateTime(net.fortuna.ical4j.model.DateTime) Date(java.util.Date) ExDate(net.fortuna.ical4j.model.property.ExDate) KalendarRecurEvent(org.olat.commons.calendar.model.KalendarRecurEvent)

Example 69 with DateTime

use of net.fortuna.ical4j.model.DateTime in project openolat by klemens.

the class ICalFileCalendarManager method getRecurrenceEndDate.

/**
 * @param rule
 * @return date of recurrence end
 */
@Override
public Date getRecurrenceEndDate(String rule) {
    if (rule != null) {
        try {
            TimeZone ltz = calendarModule.getDefaultTimeZone();
            Recur recur = new Recur(rule);
            Date dUntil = recur.getUntil();
            DateTime dtUntil = dUntil == null ? null : new DateTime(dUntil.getTime());
            if (dtUntil != null) {
                if (ltz != null) {
                    dtUntil.setTimeZone(ltz);
                }
                return dtUntil;
            }
        } catch (ParseException e) {
            log.error("cannot restore recurrence rule", e);
        }
    }
    return null;
}
Also used : TimeZone(net.fortuna.ical4j.model.TimeZone) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) ParseException(java.text.ParseException) Date(java.util.Date) ExDate(net.fortuna.ical4j.model.property.ExDate) DateTime(net.fortuna.ical4j.model.DateTime) Recur(net.fortuna.ical4j.model.Recur)

Example 70 with DateTime

use of net.fortuna.ical4j.model.DateTime in project android by nextcloud.

the class SaveCalendar method getDateTime.

private Date getDateTime(Cursor cur, String dbName, String dbTzName, Calendar cal) {
    int i = getColumnIndex(cur, dbName);
    if (i == -1 || cur.isNull(i)) {
        Log_OC.e(TAG, "No valid " + dbName + " column found, index: " + Integer.toString(i));
        return null;
    }
    if (cal == null) {
        // Ignore timezone for date-only dates
        return utcDateFromMs(cur.getLong(i));
    } else if (dbTzName == null) {
        Log_OC.e(TAG, "No valid tz " + dbName + " column given");
    }
    String tz = getString(cur, dbTzName);
    final boolean isUtc = isUtcTimeZone(tz);
    DateTime dt = new DateTime(isUtc);
    if (dt.isUtc() != isUtc) {
        throw new RuntimeException("UTC mismatch after construction");
    }
    dt.setTime(cur.getLong(i));
    if (dt.isUtc() != isUtc) {
        throw new RuntimeException("UTC mismatch after setTime");
    }
    if (!isUtc) {
        if (mTzRegistry == null) {
            mTzRegistry = TimeZoneRegistryFactory.getInstance().createRegistry();
            if (mTzRegistry == null) {
                throw new RuntimeException("Failed to create TZ registry");
            }
        }
        TimeZone t = mTzRegistry.getTimeZone(tz);
        if (t == null) {
            Log_OC.e(TAG, "Unknown TZ " + tz + ", assuming UTC");
        } else {
            dt.setTimeZone(t);
            if (!mInsertedTimeZones.contains(t)) {
                cal.getComponents().add(t.getVTimeZone());
                mInsertedTimeZones.add(t);
            }
        }
    }
    return dt;
}
Also used : TimeZone(net.fortuna.ical4j.model.TimeZone) SuppressLint(android.annotation.SuppressLint) DateTime(net.fortuna.ical4j.model.DateTime)

Aggregations

DateTime (net.fortuna.ical4j.model.DateTime)70 Period (net.fortuna.ical4j.model.Period)20 VEvent (net.fortuna.ical4j.model.component.VEvent)18 BwDateTime (org.bedework.calfacade.BwDateTime)15 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)15 Date (java.util.Date)13 TimeZone (net.fortuna.ical4j.model.TimeZone)13 ParseException (java.text.ParseException)12 DtStart (net.fortuna.ical4j.model.property.DtStart)11 Date (net.fortuna.ical4j.model.Date)10 Dur (net.fortuna.ical4j.model.Dur)10 PeriodList (net.fortuna.ical4j.model.PeriodList)10 PropertyList (net.fortuna.ical4j.model.PropertyList)9 DtStamp (net.fortuna.ical4j.model.property.DtStamp)9 LastModified (net.fortuna.ical4j.model.property.LastModified)9 Uid (net.fortuna.ical4j.model.property.Uid)9 Calendar (net.fortuna.ical4j.model.Calendar)8 Recur (net.fortuna.ical4j.model.Recur)8 ExDate (net.fortuna.ical4j.model.property.ExDate)8 GregorianCalendar (java.util.GregorianCalendar)7