Search in sources :

Example 1 with VEvent

use of net.fortuna.ical4j.model.component.VEvent in project camel by apache.

the class ICalDataFormatTest method createTestCalendar.

/**
     * Creates test calendar instance.
     * 
     * @return ICal calendar object.
     */
protected Calendar createTestCalendar() throws ParseException {
    // Create a TimeZone
    TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
    TimeZone timezone = registry.getTimeZone("America/New_York");
    VTimeZone tz = timezone.getVTimeZone();
    // Start Date is on: April 1, 2013, 9:00 am
    java.util.Calendar startDate = new GregorianCalendar();
    startDate.setTimeZone(timezone);
    startDate.set(java.util.Calendar.MONTH, java.util.Calendar.APRIL);
    startDate.set(java.util.Calendar.DAY_OF_MONTH, 1);
    startDate.set(java.util.Calendar.YEAR, 2013);
    startDate.set(java.util.Calendar.HOUR_OF_DAY, 17);
    startDate.set(java.util.Calendar.MINUTE, 0);
    startDate.set(java.util.Calendar.SECOND, 0);
    // End Date is on: April 1, 2013, 13:00
    java.util.Calendar endDate = new GregorianCalendar();
    endDate.setTimeZone(timezone);
    endDate.set(java.util.Calendar.MONTH, java.util.Calendar.APRIL);
    endDate.set(java.util.Calendar.DAY_OF_MONTH, 1);
    endDate.set(java.util.Calendar.YEAR, 2013);
    endDate.set(java.util.Calendar.HOUR_OF_DAY, 21);
    endDate.set(java.util.Calendar.MINUTE, 0);
    endDate.set(java.util.Calendar.SECOND, 0);
    // Create the event
    PropertyList propertyList = new PropertyList();
    propertyList.add(new DtStamp("20130324T180000Z"));
    propertyList.add(new DtStart(new DateTime(startDate.getTime())));
    propertyList.add(new DtEnd(new DateTime(endDate.getTime())));
    propertyList.add(new Summary("Progress Meeting"));
    VEvent meeting = new VEvent(propertyList);
    // add timezone info..
    meeting.getProperties().add(tz.getTimeZoneId());
    // generate unique identifier..
    meeting.getProperties().add(new Uid("00000000"));
    // add attendees..
    Attendee dev1 = new Attendee(URI.create("mailto:dev1@mycompany.com"));
    dev1.getParameters().add(Role.REQ_PARTICIPANT);
    dev1.getParameters().add(new Cn("Developer 1"));
    meeting.getProperties().add(dev1);
    Attendee dev2 = new Attendee(URI.create("mailto:dev2@mycompany.com"));
    dev2.getParameters().add(Role.OPT_PARTICIPANT);
    dev2.getParameters().add(new Cn("Developer 2"));
    meeting.getProperties().add(dev2);
    // Create a calendar
    net.fortuna.ical4j.model.Calendar icsCalendar = new net.fortuna.ical4j.model.Calendar();
    icsCalendar.getProperties().add(Version.VERSION_2_0);
    icsCalendar.getProperties().add(new ProdId("-//Events Calendar//iCal4j 1.0//EN"));
    icsCalendar.getProperties().add(CalScale.GREGORIAN);
    // Add the event and print
    icsCalendar.getComponents().add(meeting);
    return icsCalendar;
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) GregorianCalendar(java.util.GregorianCalendar) Calendar(net.fortuna.ical4j.model.Calendar) TimeZoneRegistry(net.fortuna.ical4j.model.TimeZoneRegistry) GregorianCalendar(java.util.GregorianCalendar) Cn(net.fortuna.ical4j.model.parameter.Cn) ProdId(net.fortuna.ical4j.model.property.ProdId) Calendar(net.fortuna.ical4j.model.Calendar) DateTime(net.fortuna.ical4j.model.DateTime) Attendee(net.fortuna.ical4j.model.property.Attendee) Uid(net.fortuna.ical4j.model.property.Uid) VTimeZone(net.fortuna.ical4j.model.component.VTimeZone) TimeZone(net.fortuna.ical4j.model.TimeZone) DtStamp(net.fortuna.ical4j.model.property.DtStamp) PropertyList(net.fortuna.ical4j.model.PropertyList) DtStart(net.fortuna.ical4j.model.property.DtStart) Summary(net.fortuna.ical4j.model.property.Summary) DtEnd(net.fortuna.ical4j.model.property.DtEnd)

Example 2 with VEvent

use of net.fortuna.ical4j.model.component.VEvent in project LAMSADE-tools by LAntoine.

the class Conference method generateCalendarFile.

/**
	 * Generates a vcal file with the conference details with this object's data
	 *
	 * @param filename:
	 *            the path including the name of the file to be generated
	 * @throws IOException
	 * @throws ValidationException
	 * @throws ParserException
	 *
	 * @author Javier Martínez
	 */
public void generateCalendarFile(String filename) throws IOException, ValidationException, ParserException {
    String calFile = filename;
    // start time
    java.util.Calendar startCal = java.util.Calendar.getInstance();
    startCal.setTime(java.sql.Date.valueOf(getStart_date()));
    // end time
    java.util.Calendar endCal = java.util.Calendar.getInstance();
    endCal.setTime(java.sql.Date.valueOf(getEnd_date()));
    String subject = "Conference";
    String description = "A conference with a fee of " + getEntry_fee();
    String hostEmail = "";
    // Creating a new calendar
    net.fortuna.ical4j.model.Calendar calendar = new net.fortuna.ical4j.model.Calendar();
    calendar.getProperties().add(new ProdId("-//Ben Fortuna//iCal4j 1.0//EN"));
    calendar.getProperties().add(Version.VERSION_2_0);
    calendar.getProperties().add(CalScale.GREGORIAN);
    SimpleDateFormat sdFormat = new SimpleDateFormat("yyyyMMdd'T'hhmmss'Z'");
    String strDate = sdFormat.format(startCal.getTime());
    net.fortuna.ical4j.model.Date startDt = null;
    try {
        startDt = new net.fortuna.ical4j.model.Date(strDate);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    long diff = endCal.getTimeInMillis() - startCal.getTimeInMillis();
    int min = (int) (diff / (1000 * 60));
    Dur dur = new Dur(0, 0, min, 0);
    // Creating a meeting event
    VEvent meeting = new VEvent(startDt, dur, subject);
    // This is where you would add a location if there was one
    // meeting.getProperties().add(new Location(location));
    meeting.getProperties().add(new Description());
    try {
        meeting.getProperties().getProperty(Property.DESCRIPTION).setValue(description);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }
    calendar.getComponents().add(meeting);
    FileOutputStream fout = null;
    try {
        fout = new FileOutputStream(calFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    CalendarOutputter outputter = new CalendarOutputter();
    outputter.setValidating(false);
    try {
        outputter.output(calendar, fout);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ValidationException e) {
        e.printStackTrace();
    }
    System.out.println(meeting);
}
Also used : Dur(net.fortuna.ical4j.model.Dur) VEvent(net.fortuna.ical4j.model.component.VEvent) Description(net.fortuna.ical4j.model.property.Description) ValidationException(net.fortuna.ical4j.model.ValidationException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) ProdId(net.fortuna.ical4j.model.property.ProdId) FileOutputStream(java.io.FileOutputStream) ParseException(java.text.ParseException) CalendarOutputter(net.fortuna.ical4j.data.CalendarOutputter) SimpleDateFormat(java.text.SimpleDateFormat)

Example 3 with VEvent

use of net.fortuna.ical4j.model.component.VEvent in project openhab1-addons by openhab.

the class EventReloaderJob method loadEvents.

public void loadEvents(String filename, org.joda.time.DateTime lastResourceChangeFS, final InputStream inputStream, final CalDavConfig config, final List<String> oldEventIds, boolean readFromFile) throws IOException, ParserException {
    CalendarBuilder builder = new CalendarBuilder();
    InputStreamReader is = new InputStreamReader(inputStream, config.getCharset());
    BufferedReader in = new BufferedReader(is, 50);
    final UnfoldingReader uin = new UnfoldingReader(in, 50, true);
    Calendar calendar = builder.build(uin);
    uin.close();
    // log.trace("calendar: {}", calendar);
    EventContainer eventContainer = new EventContainer(config.getKey());
    eventContainer.setFilename(filename);
    eventContainer.setLastChanged(lastResourceChangeFS);
    org.joda.time.DateTime loadFrom = org.joda.time.DateTime.now().minusMinutes(config.getHistoricLoadMinutes());
    org.joda.time.DateTime loadTo = org.joda.time.DateTime.now().plusMinutes(config.getPreloadMinutes());
    final ComponentList<CalendarComponent> vEventComponents = calendar.getComponents(Component.VEVENT);
    if (vEventComponents.size() == 0) {
        log.debug("could not find a VEVENT from calendar build, based on file {}", filename);
        // no events inside
        if (!readFromFile) {
            Util.storeToDisk(config.getKey(), filename, calendar);
        }
        return;
    }
    org.joda.time.DateTime lastModifedVEventOverAll = null;
    for (CalendarComponent comp : vEventComponents) {
        VEvent vEvent = (VEvent) comp;
        log.trace("loading event: " + vEvent.getUid().getValue() + ":" + vEvent.getSummary().getValue());
        // fallback, because 'LastModified' in VEvent is optional
        org.joda.time.DateTime lastModifedVEvent = lastResourceChangeFS;
        if (vEvent.getLastModified() != null) {
            lastModifedVEvent = new org.joda.time.DateTime(vEvent.getLastModified().getDateTime());
            log.trace("overriding lastmodified from file FS ({}) with event's last-modified property ({})", lastResourceChangeFS, lastModifedVEvent);
        }
        if (!config.isLastModifiedFileTimeStampValid()) {
            if (lastModifedVEventOverAll == null || lastModifedVEvent.isAfter(lastModifedVEventOverAll)) {
                lastModifedVEventOverAll = lastModifedVEvent;
            }
            if (eventContainer != null && !lastModifedVEvent.isBefore(eventContainer.getLastChanged())) {
                // to be created
                if (eventContainer.getCalculatedUntil() != null && vEventComponents.size() == 1 && eventContainer.getCalculatedUntil().isAfter(org.joda.time.DateTime.now().plusMinutes(config.getReloadMinutes()))) {
                    // the event is calculated as long as the next reload
                    // interval can handle this
                    log.trace("skipping resource processing {}, not changed", filename);
                    continue;
                }
                if (eventContainer.isHistoricEvent()) {
                    // no more upcoming events, do nothing
                    log.trace("skipping resource processing {}, not changed", filename);
                    continue;
                }
            }
        }
        Period period = new Period(new DateTime(loadFrom.toDate()), new DateTime(loadTo.toDate()));
        PeriodList periods = vEvent.calculateRecurrenceSet(period);
        periods = periods.normalise();
        String eventId = vEvent.getUid().getValue();
        final String eventName = vEvent.getSummary().getValue();
        // no more upcoming events
        if (periods.size() > 0) {
            if (vEvent.getConsumedTime(new net.fortuna.ical4j.model.Date(), new net.fortuna.ical4j.model.Date(org.joda.time.DateTime.now().plusYears(10).getMillis())).size() == 0) {
                log.trace("event will never be occur (historic): {}", eventName);
                eventContainer.setHistoricEvent(true);
            }
        }
        // expecting this is for every vEvent inside a calendar equals
        eventContainer.setEventId(eventId);
        eventContainer.setCalculatedUntil(loadTo);
        for (Period p : periods) {
            org.joda.time.DateTime start = getDateTime("start", p.getStart(), p.getRangeStart());
            org.joda.time.DateTime end = getDateTime("end", p.getEnd(), p.getRangeEnd());
            CalDavEvent event = new CalDavEvent(eventName, vEvent.getUid().getValue(), config.getKey(), start, end);
            event.setLastChanged(lastModifedVEvent);
            if (vEvent.getLocation() != null) {
                event.setLocation(vEvent.getLocation().getValue());
            }
            if (vEvent.getDescription() != null) {
                event.setContent(vEvent.getDescription().getValue());
            }
            event.getCategoryList().addAll(readCategory(vEvent));
            event.setFilename(filename);
            log.trace("adding event: " + event.getShortName());
            eventContainer.getEventList().add(event);
        }
    }
    if (lastModifedVEventOverAll != null && !config.isLastModifiedFileTimeStampValid()) {
        eventContainer.setLastChanged(lastModifedVEventOverAll);
        log.debug("changing eventcontainer last modified to {}", lastModifedVEventOverAll);
    }
    // if (!eventContainer.getEventList().isEmpty()) {
    CalDavLoaderImpl.instance.addEventToMap(eventContainer, true);
    if (!readFromFile) {
        Util.storeToDisk(config.getKey(), filename, calendar);
    }
// }
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) InputStreamReader(java.io.InputStreamReader) CalendarBuilder(net.fortuna.ical4j.data.CalendarBuilder) EventContainer(org.openhab.io.caldav.internal.EventStorage.EventContainer) CalendarComponent(net.fortuna.ical4j.model.component.CalendarComponent) UnfoldingReader(net.fortuna.ical4j.data.UnfoldingReader) Calendar(net.fortuna.ical4j.model.Calendar) Period(net.fortuna.ical4j.model.Period) PeriodList(net.fortuna.ical4j.model.PeriodList) DateTime(net.fortuna.ical4j.model.DateTime) LocalDateTime(org.joda.time.LocalDateTime) CalDavEvent(org.openhab.io.caldav.CalDavEvent) BufferedReader(java.io.BufferedReader) CalendarRuntime(org.openhab.io.caldav.internal.EventStorage.CalendarRuntime)

Example 4 with VEvent

use of net.fortuna.ical4j.model.component.VEvent in project openhab1-addons by openhab.

the class Util method createCalendar.

public static Calendar createCalendar(CalDavEvent calDavEvent, DateTimeZone timeZone) {
    TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
    TimeZone timezone = registry.getTimeZone(timeZone.getID());
    Calendar calendar = new Calendar();
    calendar.getProperties().add(Version.VERSION_2_0);
    calendar.getProperties().add(new ProdId("openHAB"));
    VEvent vEvent = new VEvent();
    vEvent.getProperties().add(new Summary(calDavEvent.getName()));
    vEvent.getProperties().add(new Description(calDavEvent.getContent()));
    final DtStart dtStart = new DtStart(new net.fortuna.ical4j.model.DateTime(calDavEvent.getStart().toDate()));
    dtStart.setTimeZone(timezone);
    vEvent.getProperties().add(dtStart);
    final DtEnd dtEnd = new DtEnd(new net.fortuna.ical4j.model.DateTime(calDavEvent.getEnd().toDate()));
    dtEnd.setTimeZone(timezone);
    vEvent.getProperties().add(dtEnd);
    vEvent.getProperties().add(new Uid(calDavEvent.getId()));
    vEvent.getProperties().add(Clazz.PUBLIC);
    vEvent.getProperties().add(new LastModified(new net.fortuna.ical4j.model.DateTime(calDavEvent.getLastChanged().toDate())));
    calendar.getComponents().add(vEvent);
    return calendar;
}
Also used : VEvent(net.fortuna.ical4j.model.component.VEvent) Description(net.fortuna.ical4j.model.property.Description) Calendar(net.fortuna.ical4j.model.Calendar) TimeZoneRegistry(net.fortuna.ical4j.model.TimeZoneRegistry) ProdId(net.fortuna.ical4j.model.property.ProdId) LastModified(net.fortuna.ical4j.model.property.LastModified) Uid(net.fortuna.ical4j.model.property.Uid) DateTimeZone(org.joda.time.DateTimeZone) TimeZone(net.fortuna.ical4j.model.TimeZone) DtStart(net.fortuna.ical4j.model.property.DtStart) Summary(net.fortuna.ical4j.model.property.Summary) DtEnd(net.fortuna.ical4j.model.property.DtEnd)

Example 5 with VEvent

use of net.fortuna.ical4j.model.component.VEvent in project zm-mailbox by Zimbra.

the class TestUserServlet method testIcsImportExport.

/**
     * Test that can import into a new calendar from ICALENDAR containing an inline ATTACHment.
     * Test that it is possible to export calendar entry with an attachment with the attachment
     * inlined if icalAttach=inline or ignoring the attachment if icalAttach=none
     */
@Test
public void testIcsImportExport() throws IOException, ValidationException, ServiceException {
    ZMailbox mbox = TestUtil.getZMailbox(USER_NAME);
    String calName = NAME_PREFIX + "2ndCalendar";
    String calUri = String.format("/%s?fmt=ics", calName);
    TestUtil.createFolder(mbox, calName, ZFolder.View.appointment);
    net.fortuna.ical4j.model.Calendar calendar = new net.fortuna.ical4j.model.Calendar();
    calendar.getProperties().add(new ProdId("-//ZimbraTest 1.0//EN"));
    calendar.getProperties().add(Version.VERSION_2_0);
    calendar.getProperties().add(CalScale.GREGORIAN);
    java.util.Calendar start = java.util.Calendar.getInstance();
    start.set(java.util.Calendar.MONTH, java.util.Calendar.SEPTEMBER);
    start.set(java.util.Calendar.DAY_OF_MONTH, 03);
    VEvent wwII = new VEvent(new Date(start.getTime()), NAME_PREFIX + " Declarations of war");
    wwII.getProperties().getProperty(Property.DTSTART).getParameters().add(Value.DATE);
    wwII.getProperties().add(new Uid("3-14159"));
    Attach attach = new Attach("Attachment.\nIsn't it short.".getBytes(MimeConstants.P_CHARSET_ASCII));
    attach.getParameters().add(new XParameter("X-APPLE-FILENAME", "short.txt"));
    attach.getParameters().add(new FmtType(MimeConstants.CT_TEXT_PLAIN));
    wwII.getProperties().add(attach);
    calendar.getComponents().add(wwII);
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    CalendarOutputter outputter = new CalendarOutputter();
    outputter.setValidating(false);
    outputter.output(calendar, buf);
    URI uri = mbox.getRestURI(calUri);
    HttpClient client = mbox.getHttpClient(uri);
    PostMethod post = new PostMethod(uri.toString());
    post.setRequestEntity(new InputStreamRequestEntity(new ByteArrayInputStream(buf.toByteArray()), MimeConstants.CT_TEXT_CALENDAR));
    ZimbraLog.test.info("testIcsImportExport:ICS to be imported:%s", new String(buf.toByteArray()));
    TestCalDav.HttpMethodExecutor.execute(client, post, HttpStatus.SC_OK);
    uri = mbox.getRestURI(calUri + "&icalAttach=inline");
    GetMethod get = new GetMethod(uri.toString());
    TestCalDav.HttpMethodExecutor executor = new TestCalDav.HttpMethodExecutor(client, get, HttpStatus.SC_OK);
    String respIcal = new String(executor.responseBodyBytes, MimeConstants.P_CHARSET_UTF8);
    ZimbraLog.test.info("testIcsImportExport:ICS exported (with icalAttach=inline):%s", respIcal);
    int attachNdx = respIcal.indexOf("ATTACH;");
    Assert.assertTrue("ATTACH should be present", -1 != attachNdx);
    String fromAttach = respIcal.substring(attachNdx);
    Assert.assertTrue("BINARY should be present", -1 != fromAttach.indexOf("VALUE=BINARY"));
    uri = mbox.getRestURI(calUri + "&icalAttach=none");
    get = new GetMethod(uri.toString());
    executor = new TestCalDav.HttpMethodExecutor(client, get, HttpStatus.SC_OK);
    respIcal = new String(executor.responseBodyBytes, MimeConstants.P_CHARSET_UTF8);
    ZimbraLog.test.debug("testIcsImportExport:ICS exported (with icalAttach=none):%s", respIcal);
    Assert.assertTrue("ATTACH should be present", -1 == respIcal.indexOf("ATTACH;"));
    uri = mbox.getRestURI(calUri);
    get = new GetMethod(uri.toString());
    executor = new TestCalDav.HttpMethodExecutor(client, get, HttpStatus.SC_OK);
    respIcal = new String(executor.responseBodyBytes, MimeConstants.P_CHARSET_UTF8);
    ZimbraLog.test.debug("testIcsImportExport:ICS exported (default - same as icalAttach=none):%s", respIcal);
    Assert.assertTrue("ATTACH should be present", -1 == respIcal.indexOf("ATTACH;"));
}
Also used : InputStreamRequestEntity(org.apache.commons.httpclient.methods.InputStreamRequestEntity) PostMethod(org.apache.commons.httpclient.methods.PostMethod) ProdId(net.fortuna.ical4j.model.property.ProdId) URI(java.net.URI) ZMailbox(com.zimbra.client.ZMailbox) XParameter(net.fortuna.ical4j.model.parameter.XParameter) VEvent(net.fortuna.ical4j.model.component.VEvent) Attach(net.fortuna.ical4j.model.property.Attach) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Date(net.fortuna.ical4j.model.Date) Uid(net.fortuna.ical4j.model.property.Uid) ByteArrayInputStream(java.io.ByteArrayInputStream) SharedByteArrayInputStream(javax.mail.util.SharedByteArrayInputStream) HttpClient(org.apache.commons.httpclient.HttpClient) FmtType(net.fortuna.ical4j.model.parameter.FmtType) GetMethod(org.apache.commons.httpclient.methods.GetMethod) CalendarOutputter(net.fortuna.ical4j.data.CalendarOutputter) Test(org.junit.Test)

Aggregations

VEvent (net.fortuna.ical4j.model.component.VEvent)5 ProdId (net.fortuna.ical4j.model.property.ProdId)4 Calendar (net.fortuna.ical4j.model.Calendar)3 Uid (net.fortuna.ical4j.model.property.Uid)3 CalendarOutputter (net.fortuna.ical4j.data.CalendarOutputter)2 DateTime (net.fortuna.ical4j.model.DateTime)2 TimeZone (net.fortuna.ical4j.model.TimeZone)2 TimeZoneRegistry (net.fortuna.ical4j.model.TimeZoneRegistry)2 Description (net.fortuna.ical4j.model.property.Description)2 DtEnd (net.fortuna.ical4j.model.property.DtEnd)2 DtStart (net.fortuna.ical4j.model.property.DtStart)2 Summary (net.fortuna.ical4j.model.property.Summary)2 ZMailbox (com.zimbra.client.ZMailbox)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1