net.fortuna.ical4j.model.TimeZoneRegistryFactory Java Examples

The following examples show how to use net.fortuna.ical4j.model.TimeZoneRegistryFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IcsCalendarBuilder.java    From fredbet with Creative Commons Attribution Share Alike 4.0 International 7 votes vote down vote up
public byte[] build() {
    TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
    TimeZone timezone = registry.getTimeZone(timeZone);
    VTimeZone tz = timezone.getVTimeZone();
    VEvent vEvent = new VEvent(toDate(this.start), toDate(this.end), this.title);
    vEvent.getProperties().add(tz.getTimeZoneId());
    vEvent.getProperties().add(uidGenerator.generateUid());
    vEvent.getProperties().add(new Description(this.content));
    vEvent.getProperties().add(new Location(this.location));

    net.fortuna.ical4j.model.Calendar icsCalendar = new net.fortuna.ical4j.model.Calendar();
    icsCalendar.getProperties().add(new ProdId("-//FredBet//iCal4j 1.0//EN"));
    icsCalendar.getProperties().add(CalScale.GREGORIAN);
    icsCalendar.getProperties().add(Version.VERSION_2_0);

    icsCalendar.getComponents().add(vEvent);

    try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
        CalendarOutputter outputter = new CalendarOutputter();
        outputter.output(icsCalendar, out);
        return out.toByteArray();
    } catch (IOException e) {
        LOG.error(e.getMessage());
        return null;
    }
}
 
Example #2
Source File: Calendar_EventRepeatMaster.java    From o2oa with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * 获取重复时间表达式中结束的日期
 * @return
 */
public Date getRecurrenceEndDate() {
	final TimeZone tz = TimeZoneRegistryFactory.getInstance().createRegistry()
			.getTimeZone(java.util.Calendar.getInstance().getTimeZone().getID());
	if (recurrenceRule != null) {
		try {
			final Recur recur = new Recur(recurrenceRule);
			final Date dUntil = recur.getUntil();
			final DateTime dtUntil = dUntil == null ? null : new DateTime(dUntil.getTime());
			if (dtUntil != null) {
				dtUntil.setTimeZone(tz);
				return dtUntil;
			}
		} catch (final ParseException e) {
			System.out.println("cannot restore recurrence rule");
			e.printStackTrace();
		}
	}
	return null;
}
 
Example #3
Source File: Util.java    From openhab1-addons with Eclipse Public License 2.0 6 votes vote down vote up
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;
}
 
Example #4
Source File: CalendarDaoICalFileImpl.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * [spring]
 */
private CalendarDaoICalFileImpl() {
    this.fStorageBase = new File(WebappHelper.getUserDataRoot() + "/calendars");
    if (!fStorageBase.exists()) {
        if (!fStorageBase.mkdirs()) {
            throw new OLATRuntimeException("Error creating calendar base directory at: " + fStorageBase.getAbsolutePath(), null);
        }
    }
    createCalendarFileDirectories();
    // set parser to relax (needed for allday events
    // see http://sourceforge.net/forum/forum.php?thread_id=1253735&forum_id=368291
    System.setProperty("ical4j.unfolding.relaxed", "true");
    // initialize timezone
    tz = TimeZoneRegistryFactory.getInstance().createRegistry().getTimeZone(java.util.Calendar.getInstance().getTimeZone().getID());
    // INSTANCE = this;
}
 
Example #5
Source File: CalendarEntry.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * @param rule
 * @return date of recurrence end
 */
public Date getRecurrenceEndDate() {
    final TimeZone tz = TimeZoneRegistryFactory.getInstance().createRegistry().getTimeZone(java.util.Calendar.getInstance().getTimeZone().getID());

    if (recurrenceRule != null) {
        try {
            final Recur recur = new Recur(recurrenceRule);
            final Date dUntil = recur.getUntil();
            final DateTime dtUntil = dUntil == null ? null : new DateTime(dUntil.getTime());
            if (dtUntil != null) {
                dtUntil.setTimeZone(tz);
                return dtUntil;
            }
        } catch (final ParseException e) {
            log.error("cannot restore recurrence rule", e);
        }
    }

    return null;
}
 
Example #6
Source File: CalendarDaoICalFileImpl.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * [spring]
 */
private CalendarDaoICalFileImpl() {
    this.fStorageBase = new File(WebappHelper.getUserDataRoot() + "/calendars");
    if (!fStorageBase.exists()) {
        if (!fStorageBase.mkdirs()) {
            throw new OLATRuntimeException("Error creating calendar base directory at: " + fStorageBase.getAbsolutePath(), null);
        }
    }
    createCalendarFileDirectories();
    // set parser to relax (needed for allday events
    // see http://sourceforge.net/forum/forum.php?thread_id=1253735&forum_id=368291
    System.setProperty("ical4j.unfolding.relaxed", "true");
    // initialize timezone
    tz = TimeZoneRegistryFactory.getInstance().createRegistry().getTimeZone(java.util.Calendar.getInstance().getTimeZone().getID());
    // INSTANCE = this;
}
 
Example #7
Source File: CalendarEntry.java    From olat with Apache License 2.0 6 votes vote down vote up
/**
 * @param rule
 * @return date of recurrence end
 */
public Date getRecurrenceEndDate() {
    final TimeZone tz = TimeZoneRegistryFactory.getInstance().createRegistry().getTimeZone(java.util.Calendar.getInstance().getTimeZone().getID());

    if (recurrenceRule != null) {
        try {
            final Recur recur = new Recur(recurrenceRule);
            final Date dUntil = recur.getUntil();
            final DateTime dtUntil = dUntil == null ? null : new DateTime(dUntil.getTime());
            if (dtUntil != null) {
                dtUntil.setTimeZone(tz);
                return dtUntil;
            }
        } catch (final ParseException e) {
            log.error("cannot restore recurrence rule", e);
        }
    }

    return null;
}
 
Example #8
Source File: IcalUtils.java    From openmeetings with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #9
Source File: ICalUtils.java    From camel-quarkus with Apache License 2.0 5 votes vote down vote up
protected static Calendar createTestCalendar(ZonedDateTime start, ZonedDateTime end, String summary, String attendee) {
    // Create a TimeZone
    TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
    String tzId = start.getZone().getId();
    TimeZone timezone = registry.getTimeZone(tzId.equals("Z") ? "UTC" : tzId);
    VTimeZone tz = timezone.getVTimeZone();

    // Create the event
    PropertyList propertyList = new PropertyList();
    DateTime ts = new DateTime(true);
    ts.setTime(0);
    propertyList.add(new DtStamp(ts));
    propertyList.add(new DtStart(toDateTime(start, registry)));
    propertyList.add(new DtEnd(toDateTime(end, registry)));
    propertyList.add(new Summary(summary));
    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:" + attendee));
    dev1.getParameters().add(Role.REQ_PARTICIPANT);
    dev1.getParameters().add(new Cn(attendee));
    meeting.getProperties().add(dev1);

    // 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;
}
 
Example #10
Source File: CalendarDaoImpl.java    From cosmo with Apache License 2.0 5 votes vote down vote up
@Override
public Set<Item> findEvents(CollectionItem collection, Date rangeStart, Date rangeEnd, String timezoneId,
        boolean expandRecurringEvents) {

    // Create a NoteItemFilter that filters by parent
    NoteItemFilter itemFilter = new NoteItemFilter();
    itemFilter.setParent(collection);

    // and EventStamp by timeRange
    EventStampFilter eventFilter = new EventStampFilter();

    if (timezoneId != null) {
        TimeZone timeZone = TimeZoneRegistryFactory.getInstance().createRegistry().getTimeZone(timezoneId);
        eventFilter.setTimezone(timeZone);
    }

    eventFilter.setTimeRange(rangeStart, rangeEnd);
    eventFilter.setExpandRecurringEvents(expandRecurringEvents);
    itemFilter.getStampFilters().add(eventFilter);

    try {
        return itemFilterProcessor.processFilter(itemFilter);
    } catch (HibernateException e) {
        this.em.clear();
        throw SessionFactoryUtils.convertHibernateAccessException(e);
    }
}
 
Example #11
Source File: ICalFormatTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
private 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:[email protected]"));
    dev1.getParameters().add(Role.REQ_PARTICIPANT);
    dev1.getParameters().add(new Cn("Developer 1"));
    meeting.getProperties().add(dev1);

    Attendee dev2 = new Attendee(URI.create("mailto:[email protected]"));
    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;
}
 
Example #12
Source File: EntityConverterTest.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
   * Tests convert task.
   * @throws Exception - if something is wrong this exception is thrown.
   */
  @Test
  public void testConvertTask() throws Exception {
      @SuppressWarnings("unused")
TimeZoneRegistry registry =
          TimeZoneRegistryFactory.getInstance().createRegistry();
      NoteItem master = new MockNoteItem();
      master.setDisplayName("displayName");
      master.setBody("body");
      master.setIcalUid("icaluid");
      master.setClientModifiedDate(new DateTime("20070101T100000Z"));
      master.setTriageStatus(TriageStatusUtil.initialize(new MockTriageStatus()));
      
      Calendar cal = converter.convertNote(master);
      cal.validate();
      
      Assert.assertEquals(1, cal.getComponents().size());
      
      ComponentList<VToDo> comps = cal.getComponents(Component.VTODO);
      Assert.assertEquals(1, comps.size());
      VToDo task = comps.get(0);
      
      Assert.assertNull(task.getDateCompleted());
      Assert.assertNull(ICalendarUtils.getXProperty("X-OSAF-STARRED", task));
      
      DateTime completeDate = new DateTime("20080122T100000Z");
      
      master.getTriageStatus().setCode(TriageStatus.CODE_DONE);
      master.getTriageStatus().setRank(TriageStatusUtil.getRank(completeDate.getTime()));
      master.addStamp(new MockTaskStamp());
      
      cal = converter.convertNote(master);
      task = (VToDo) cal.getComponents().get(0);
      
      Completed completed = task.getDateCompleted();
      Assert.assertNotNull(completed);
      Assert.assertEquals(completeDate.getTime(), completed.getDate().getTime());
      Assert.assertEquals("TRUE", ICalendarUtils.getXProperty("X-OSAF-STARRED", task));
      
  }
 
Example #13
Source File: ExternalCalendaringServiceImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public VTimeZone getTimeZone(boolean timeIsLocal) {
	//timezone. All dates are in GMT so we need to explicitly set that
	TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
	
	//To prevent NPE on timezone
	TimeZone timezone = null;
	if (timeIsLocal == true) {
		timezone = registry.getTimeZone(timeService.getLocalTimeZone().getID());
	}
	if (timezone == null) {
		//This is guaranteed to return timezone if timeIsLocal == false or it fails and returns null
		timezone = registry.getTimeZone("GMT");
	}
	return timezone.getVTimeZone();
}
 
Example #14
Source File: TestHelper.java    From cosmo with Apache License 2.0 5 votes vote down vote up
/**
 * Makes dummy calendar with event.
 * @return The calendar.
 */
public Calendar makeDummyCalendarWithEvent() {
    Calendar cal = makeDummyCalendar();

    VEvent e1 = makeDummyEvent();
    cal.getComponents().add(e1);

    VTimeZone tz1 = TimeZoneRegistryFactory.getInstance().createRegistry().
    getTimeZone("America/Los_Angeles").getVTimeZone();
    cal.getComponents().add(tz1);

    return cal;
}
 
Example #15
Source File: BaseCalendarService.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
protected void printICalSchedule(String calendarName, List<String> calRefs, OutputStream os)
//	protected void printICalSchedule(String calRef, OutputStream os) 
		throws PermissionException
	{
		// generate iCal text file 
		net.fortuna.ical4j.model.Calendar ical = new net.fortuna.ical4j.model.Calendar();
		ical.getProperties().add(new ProdId("-//SakaiProject//iCal4j 1.0//EN"));
		ical.getProperties().add(Version.VERSION_2_0);
		ical.getProperties().add(CalScale.GREGORIAN);
		ical.getProperties().add(new XProperty("X-WR-CALNAME", calendarName));
		ical.getProperties().add(new XProperty("X-WR-CALDESC", calendarName));
		
		TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry(); 
		TzId tzId = new TzId( m_timeService.getLocalTimeZone().getID() ); 
		ical.getComponents().add(registry.getTimeZone(tzId.getValue()).getVTimeZone());
		
		CalendarOutputter icalOut = new CalendarOutputter();
		int numEvents = generateICal(ical, calRefs);
			
		try 
		{
			if (numEvents > 0) {
				icalOut.output( ical, os );
			}
		}
		catch (Exception e)
		{
           log.warn(".printICalSchedule(): ", e);
		}
	}
 
Example #16
Source File: BaseCalendarService.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
protected void printICalSchedule(String calendarName, List<String> calRefs, OutputStream os)
//	protected void printICalSchedule(String calRef, OutputStream os) 
		throws PermissionException
	{
		// generate iCal text file 
		net.fortuna.ical4j.model.Calendar ical = new net.fortuna.ical4j.model.Calendar();
		ical.getProperties().add(new ProdId("-//SakaiProject//iCal4j 1.0//EN"));
		ical.getProperties().add(Version.VERSION_2_0);
		ical.getProperties().add(CalScale.GREGORIAN);
		ical.getProperties().add(new XProperty("X-WR-CALNAME", calendarName));
		ical.getProperties().add(new XProperty("X-WR-CALDESC", calendarName));
		
		TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry(); 
		TzId tzId = new TzId( m_timeService.getLocalTimeZone().getID() ); 
		ical.getComponents().add(registry.getTimeZone(tzId.getValue()).getVTimeZone());
		
		CalendarOutputter icalOut = new CalendarOutputter();
		int numEvents = generateICal(ical, calRefs);
			
		try 
		{
			if (numEvents > 0) {
				icalOut.output( ical, os );
			}
		}
		catch (Exception e)
		{
           log.warn(".printICalSchedule(): ", e);
		}
	}
 
Example #17
Source File: ExternalCalendaringServiceImpl.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
public VTimeZone getTimeZone(boolean timeIsLocal) {
	//timezone. All dates are in GMT so we need to explicitly set that
	TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
	
	//To prevent NPE on timezone
	TimeZone timezone = null;
	if (timeIsLocal == true) {
		timezone = registry.getTimeZone(timeService.getLocalTimeZone().getID());
	}
	if (timezone == null) {
		//This is guaranteed to return timezone if timeIsLocal == false or it fails and returns null
		timezone = registry.getTimeZone("GMT");
	}
	return timezone.getVTimeZone();
}
 
Example #18
Source File: IcalUtils.java    From openmeetings with Apache License 2.0 5 votes vote down vote up
/**
 * Methods to parse Appointment to iCalendar according RFC 2445
 *
 * @param appointment to be converted to iCalendar
 * @return iCalendar representation of the Appointment
 */
public Calendar parseAppointmenttoCalendar(Appointment appointment) {
	String tzid = parseTimeZone(null, appointment.getOwner()).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("-//Events Calendar//Apache Openmeetings//EN"));
	icsCalendar.getProperties().add(Version.VERSION_2_0);
	icsCalendar.getProperties().add(CalScale.GREGORIAN);
	icsCalendar.getComponents().add(timeZone.getVTimeZone());

	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;
}
 
Example #19
Source File: TestSendIcalMessage.java    From openmeetings with Apache License 2.0 4 votes vote down vote up
public void simpleInvitionIcalLink() {
	// Create a TimeZone
	TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
	TimeZone timezone = registry.getTimeZone("America/Mexico_City");
	VTimeZone tz = timezone.getVTimeZone();

	// Start Date is on: April 1, 2008, 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, 2008);
	startDate.set(java.util.Calendar.HOUR_OF_DAY, 9);
	startDate.set(java.util.Calendar.MINUTE, 0);
	startDate.set(java.util.Calendar.SECOND, 0);

	// End Date is on: April 1, 2008, 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, 2008);
	endDate.set(java.util.Calendar.HOUR_OF_DAY, 13);
	endDate.set(java.util.Calendar.MINUTE, 0);
	endDate.set(java.util.Calendar.SECOND, 0);

	// Create the event
	String eventName = "Progress Meeting";
	DateTime start = new DateTime(startDate.getTime());
	DateTime end = new DateTime(endDate.getTime());
	VEvent meeting = new VEvent(start, end, eventName);

	// add timezone info..
	meeting.getProperties().add(tz.getTimeZoneId());

	// generate unique identifier..
	Uid uid = new Uid(randomUUID().toString());
	meeting.getProperties().add(uid);

	// add attendees..
	Attendee dev1 = new Attendee(URI.create("mailto:[email protected]"));
	dev1.getParameters().add(Role.REQ_PARTICIPANT);
	dev1.getParameters().add(new Cn("Developer 1"));
	meeting.getProperties().add(dev1);

	Attendee dev2 = new Attendee(URI.create("mailto:[email protected]"));
	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(
			new ProdId("-//Events Calendar//iCal4j 1.0//EN"));
	icsCalendar.getProperties().add(CalScale.GREGORIAN);
	icsCalendar.getProperties().add(Version.VERSION_2_0);

	// Add the event and print
	icsCalendar.getComponents().add(meeting);

	Organizer orger = new Organizer(URI.create("[email protected]"));
	orger.getParameters().add(new Cn("Sebastian Wagner"));
	meeting.getProperties().add(orger);

	icsCalendar.getProperties().add(Method.REQUEST);

	log.debug(icsCalendar.toString());

	ByteArrayOutputStream bout = new ByteArrayOutputStream();
	CalendarOutputter outputter = new CalendarOutputter();
	try {
		outputter.output(icsCalendar, bout);
		iCalMimeBody = bout.toByteArray();

		sendIcalMessage();
	} catch (Exception e) {
		log.error("Error", e);
	}
}
 
Example #20
Source File: CalendarUtils.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * Build iCalendar-compliant recurrence rule
 * 
 * @param recurrence
 * @param recurrenceEnd
 * @return rrule
 */
public static String getRecurrenceRule(final String recurrence, final Date recurrenceEnd) {
    final TimeZone tz = TimeZoneRegistryFactory.getInstance().createRegistry().getTimeZone(java.util.Calendar.getInstance().getTimeZone().getID());

    if (recurrence != null) { // recurrence available
        // create recurrence rule
        final StringBuilder sb = new StringBuilder();
        sb.append("FREQ=");
        if (recurrence.equals(CalendarEntry.WORKDAILY)) {
            // build rule for monday to friday
            sb.append(CalendarEntry.DAILY);
            sb.append(";");
            sb.append("BYDAY=MO,TU,WE,TH,FR");
        } else if (recurrence.equals(CalendarEntry.BIWEEKLY)) {
            // build rule for biweekly
            sb.append(CalendarEntry.WEEKLY);
            sb.append(";");
            sb.append("INTERVAL=2");
        } else {
            // normal supported recurrence
            sb.append(recurrence);
        }
        if (recurrenceEnd != null) {
            final DateTime recurEndDT = new DateTime(recurrenceEnd.getTime());
            recurEndDT.setTimeZone(tz);
            sb.append(";");
            sb.append(CalendarEntry.UNTIL);
            sb.append("=");
            sb.append(recurEndDT.toString());
        }
        try {
            final Recur recur = new Recur(sb.toString());
            final RRule rrule = new RRule(recur);
            return rrule.getValue();
        } catch (final ParseException e) {
            log.error("cannot create recurrence rule: " + recurrence.toString(), e);
        }
    }

    return null;
}
 
Example #21
Source File: CalendarUtils.java    From olat with Apache License 2.0 4 votes vote down vote up
/**
 * Build iCalendar-compliant recurrence rule
 * 
 * @param recurrence
 * @param recurrenceEnd
 * @return rrule
 */
public static String getRecurrenceRule(final String recurrence, final Date recurrenceEnd) {
    final TimeZone tz = TimeZoneRegistryFactory.getInstance().createRegistry().getTimeZone(java.util.Calendar.getInstance().getTimeZone().getID());

    if (recurrence != null) { // recurrence available
        // create recurrence rule
        final StringBuilder sb = new StringBuilder();
        sb.append("FREQ=");
        if (recurrence.equals(CalendarEntry.WORKDAILY)) {
            // build rule for monday to friday
            sb.append(CalendarEntry.DAILY);
            sb.append(";");
            sb.append("BYDAY=MO,TU,WE,TH,FR");
        } else if (recurrence.equals(CalendarEntry.BIWEEKLY)) {
            // build rule for biweekly
            sb.append(CalendarEntry.WEEKLY);
            sb.append(";");
            sb.append("INTERVAL=2");
        } else {
            // normal supported recurrence
            sb.append(recurrence);
        }
        if (recurrenceEnd != null) {
            final DateTime recurEndDT = new DateTime(recurrenceEnd.getTime());
            recurEndDT.setTimeZone(tz);
            sb.append(";");
            sb.append(CalendarEntry.UNTIL);
            sb.append("=");
            sb.append(recurEndDT.toString());
        }
        try {
            final Recur recur = new Recur(sb.toString());
            final RRule rrule = new RRule(recur);
            return rrule.getValue();
        } catch (final ParseException e) {
            log.error("cannot create recurrence rule: " + recurrence.toString(), e);
        }
    }

    return null;
}
 
Example #22
Source File: TestHelper.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Makes dummy event.
 * @return The event.
 */
public VEvent makeDummyEvent() {
    String serial = Integer.toString(++eseq);
    String summary = "dummy" + serial;

    // tomorrow
    java.util.Calendar start = java.util.Calendar.getInstance();
    start.add(java.util.Calendar.DAY_OF_MONTH, 1);
    start.set(java.util.Calendar.HOUR_OF_DAY, 9);
    start.set(java.util.Calendar.MINUTE, 30);

    // 1 hour duration
    Dur duration = new Dur(0, 1, 0, 0);
 
    VEvent event = new VEvent(new Date(start.getTime()), duration, summary);
    event.getProperties().add(new Uid(serial));
 
    // add timezone information
    VTimeZone tz = TimeZoneRegistryFactory.getInstance().createRegistry().
        getTimeZone("America/Los_Angeles").getVTimeZone();
    String tzValue =
        tz.getProperties().getProperty(Property.TZID).getValue();
    net.fortuna.ical4j.model.parameter.TzId tzParam =
        new net.fortuna.ical4j.model.parameter.TzId(tzValue);
    event.getProperties().getProperty(Property.DTSTART).
        getParameters().add(tzParam);

    // add an alarm for 5 minutes before the event with an xparam
    // on the description
    Dur trigger = new Dur(0, 0, -5, 0);
    VAlarm alarm = new VAlarm(trigger);
    alarm.getProperties().add(Action.DISPLAY);
    Description description = new Description("Meeting at 9:30am");
    XParameter xparam = new XParameter("X-COSMO-TEST-PARAM", "deadbeef");
    description.getParameters().add(xparam);
    alarm.getProperties().add(description);
    alarm.getProperties().add(new Description("Meeting at 9:30am"));
    event.getAlarms().add(alarm);

    // add an x-property with an x-param
    XProperty xprop = new XProperty("X-COSMO-TEST-PROP", "abc123");
    xprop.getParameters().add(xparam);
    event.getProperties().add(xprop);

    return event;
}
 
Example #23
Source File: EntityConverterTest.java    From cosmo with Apache License 2.0 4 votes vote down vote up
/**
 * Tests convert event.
 * @throws Exception - if something is wrong this exception is thrown.
 */
@Test
public void testConvertEvent() throws Exception {
    TimeZoneRegistry registry =
        TimeZoneRegistryFactory.getInstance().createRegistry();
    NoteItem master = new MockNoteItem();
    master.setDisplayName("displayName");
    master.setBody("body");
    master.setIcalUid("icaluid");
    master.setClientModifiedDate(new DateTime("20070101T100000Z"));
    EventStamp eventStamp = new MockEventStamp(master);
    eventStamp.createCalendar();
    eventStamp.setStartDate(new DateTime("20070212T074500"));
    master.addStamp(eventStamp);
    
    Calendar cal = converter.convertNote(master);
    cal.validate();
    
    // date has no timezone, so there should be no timezones
    Assert.assertEquals(0, cal.getComponents(Component.VTIMEZONE).size());
  
    eventStamp.setStartDate(new DateTime("20070212T074500",TIMEZONE_REGISTRY.getTimeZone("America/Chicago")));
    
    cal = converter.convertNote(master);
    cal.validate();
    
    // should be a single VEVENT
    ComponentList<VEvent> comps = cal.getComponents(Component.VEVENT);
    Assert.assertEquals(1, comps.size());
    VEvent event = (VEvent) comps.get(0);
    
    // test VALUE=DATE-TIME is not present
    Assert.assertNull(event.getStartDate().getParameter(Parameter.VALUE));
    
    // test item properties got merged into calendar
    Assert.assertEquals("displayName", event.getSummary().getValue());
    Assert.assertEquals("body", event.getDescription().getValue());
    Assert.assertEquals("icaluid", event.getUid().getValue());
    Assert.assertEquals(master.getClientModifiedDate().getTime(), event.getDateStamp().getDate().getTime());
     
    // date has timezone, so there should be a timezone
    Assert.assertEquals(1, cal.getComponents(Component.VTIMEZONE).size());
    
    eventStamp.setEndDate(new DateTime("20070212T074500",TIMEZONE_REGISTRY.getTimeZone("America/Los_Angeles")));
    
    cal = converter.convertNote(master);
    cal.validate();
    
    // dates have 2 different timezones, so there should be 2 timezones
    Assert.assertEquals(2, cal.getComponents(Component.VTIMEZONE).size());
    
    // add timezones to master event calendar
    eventStamp.getEventCalendar().getComponents().add(registry.getTimeZone("America/Chicago").getVTimeZone());
    eventStamp.getEventCalendar().getComponents().add(registry.getTimeZone("America/Los_Angeles").getVTimeZone());
    
    cal = converter.convertNote(master);
    cal.validate();
    Assert.assertEquals(2, cal.getComponents(Component.VTIMEZONE).size());
}
 
Example #24
Source File: StandardItemFilterProcessorTest.java    From cosmo with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
    registry = TimeZoneRegistryFactory.getInstance().createRegistry();
}
 
Example #25
Source File: InstanceList.java    From cosmo with Apache License 2.0 2 votes vote down vote up
/**
 * Set the timezone to use when converting floating times to
 * UTC.  Only used if isUTC is set to true.
 *
 * @param timezone The timezone.
 */
public void setTimezone(TimeZone timezone) {
    if(timezone != null ){
        this.timezone = TimeZoneRegistryFactory.getInstance().createRegistry().getTimeZone(timezone.getID());
    }
}