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; }
private static VEvent generateAllDayEventForScheduleActivity( final Date date, final ScheduledActivity scheduledActivity, String baseUrl, Boolean includeSubject) { if (scheduledActivity.getCurrentState().getMode().equals(ScheduledActivityMode.SCHEDULED) || scheduledActivity .getCurrentState() .getMode() .equals(ScheduledActivityMode.CONDITIONAL)) { try { VEvent vEvent = new VEvent( new net.fortuna.ical4j.model.Date(date.getTime()), createEventSummary(scheduledActivity, includeSubject)); vEvent.getProperties().add(new Uid(scheduledActivity.getGridId())); vEvent.getProperties().add(new Transp(Property.TRANSP)); vEvent.getProperty(Property.TRANSP).setValue("TRANSPARENT"); Description description = new Description(createEventDescription(scheduledActivity)); vEvent.getProperties().add(description); vEvent.getProperties().add(new Url()); vEvent.getProperty(Property.URL).setValue(createActivityURL(scheduledActivity, baseUrl)); vEvent.getProperties().add(new DtEnd()); return vEvent; } catch (URISyntaxException use) { throw new StudyCalendarSystemException("Error while creating ics calendar", use); } catch (IOException e) { throw new StudyCalendarSystemException("Error while creating ics calendar", e); } catch (ParseException pe) { throw new StudyCalendarSystemException("Error while creating ics calendar", pe); } } return null; }
@Override @SuppressWarnings("unchecked") public ByteArrayOutputStream toEntity() throws IOException { net.fortuna.ical4j.model.Calendar ical = new net.fortuna.ical4j.model.Calendar(); ical.getProperties().add(Version.VERSION_2_0); ical.getProperties() .add(new ProdId("-//bitfire web engineering//DAVdroid " + Constants.APP_VERSION + "//EN")); VEvent event = new VEvent(); PropertyList props = event.getProperties(); if (uid != null) props.add(new Uid(uid)); props.add(dtStart); if (dtEnd != null) props.add(dtEnd); if (duration != null) props.add(duration); if (rrule != null) props.add(rrule); if (rdate != null) props.add(rdate); if (exrule != null) props.add(exrule); if (exdate != null) props.add(exdate); if (summary != null && !summary.isEmpty()) props.add(new Summary(summary)); if (location != null && !location.isEmpty()) props.add(new Location(location)); if (description != null && !description.isEmpty()) props.add(new Description(description)); if (status != null) props.add(status); if (!opaque) props.add(Transp.TRANSPARENT); if (organizer != null) props.add(organizer); props.addAll(attendees); if (forPublic != null) event.getProperties().add(forPublic ? Clazz.PUBLIC : Clazz.PRIVATE); event.getAlarms().addAll(alarms); props.add(new LastModified()); ical.getComponents().add(event); // add VTIMEZONE components net.fortuna.ical4j.model.TimeZone tzStart = (dtStart == null ? null : dtStart.getTimeZone()), tzEnd = (dtEnd == null ? null : dtEnd.getTimeZone()); if (tzStart != null) ical.getComponents().add(tzStart.getVTimeZone()); if (tzEnd != null && tzEnd != tzStart) ical.getComponents().add(tzEnd.getVTimeZone()); CalendarOutputter output = new CalendarOutputter(false); ByteArrayOutputStream os = new ByteArrayOutputStream(); try { output.output(ical, os); } catch (ValidationException e) { Log.e(TAG, "Generated invalid iCalendar"); } return os; }
/** * @param newUid * @param newEvent * @return */ public VEvent newEvent(String newUid, String newEvent) { VEvent ve = new VEvent(); DtStart dtStart = new DtStart(new DateTime()); Summary summary = new Summary(newEvent); Uid uid = new Uid(newUid); ve.getProperties().add(dtStart); ve.getProperties().add(summary); ve.getProperties().add(uid); return ve; }
/** * Adds attendees to an existing event with a given role Common logic for addAttendeesToEvent and * addChairAttendeestoEvent * * @param vevent the VEvent to add the attendess too * @param attendees list of Users that have been invited to the event * @param role the role with which to add each user * @return the VEvent for the given event or null if there was an error */ protected VEvent addAttendeesToEventWithRole(VEvent vevent, List<User> attendees, Role role) { if (!isIcsEnabled()) { log.debug( "ExternalCalendaringService is disabled. Enable via calendar.ics.generation.enabled=true in sakai.properties"); return null; } // add attendees to event with 'required participant' role if (attendees != null) { for (User u : attendees) { Attendee a = new Attendee(createMailURI(u.getEmail())); a.getParameters().add(role); a.getParameters().add(new Cn(u.getDisplayName())); a.getParameters().add(PartStat.ACCEPTED); a.getParameters().add(Rsvp.FALSE); vevent.getProperties().add(a); } } if (log.isDebugEnabled()) { log.debug("VEvent with attendees:" + vevent); } return vevent; }
/** {@inheritDoc} */ public VEvent cancelEvent(VEvent vevent) { if (!isIcsEnabled()) { log.debug( "ExternalCalendaringService is disabled. Enable via calendar.ics.generation.enabled=true in sakai.properties"); return null; } // You can only have one status so make sure we remove any previous ones. vevent.getProperties().removeAll(vevent.getProperties(Property.STATUS)); vevent.getProperties().add(Status.VEVENT_CANCELLED); // Must define a sequence for cancellations. If one was not defined when the event was created // use 1 if (vevent.getProperties().getProperty(Property.SEQUENCE) == null) { vevent.getProperties().add(new Sequence("1")); } if (log.isDebugEnabled()) { log.debug("VEvent cancelled:" + vevent); } return vevent; }
@Override @SuppressWarnings("unchecked") public String encode(List<CalendarEvent> events) { if (events == null || events.isEmpty()) { throw new IllegalArgumentException("The calendar events must be defined to encode them"); } Calendar calendarIcs = new Calendar(); calendarIcs.getProperties().add(new ProdId("-//Silverpeas//iCal4j 1.1//FR")); calendarIcs.getProperties().add(Version.VERSION_2_0); calendarIcs.getProperties().add(CalScale.GREGORIAN); List<VEvent> iCalEvents = new ArrayList<VEvent>(); ByteArrayOutputStream output = new ByteArrayOutputStream(10240); for (CalendarEvent event : events) { Date startDate = anICal4JDateCodec().encode(event.getStartDate()); Date endDate = anICal4JDateCodec().encode(event.getEndDate()); VEvent iCalEvent; if (event.isOnAllDay() && startDate.equals(endDate)) { iCalEvent = new VEvent(startDate, event.getTitle()); } else { iCalEvent = new VEvent(startDate, endDate, event.getTitle()); } // Generate UID iCalEvent.getProperties().add(generator.generateUid()); // Add recurring data if any if (event.isRecurring()) { CalendarEventRecurrence eventRecurrence = event.getRecurrence(); Recur recur = anICal4JRecurrenceCodec().encode(eventRecurrence); iCalEvent.getProperties().add(new RRule(recur)); iCalEvent.getProperties().add(exceptionDatesFrom(eventRecurrence)); } // Add Description iCalEvent.getProperties().add(new Description(event.getDescription())); // Add Classification iCalEvent.getProperties().add(new Clazz(event.getAccessLevel())); // Add Priority iCalEvent.getProperties().add(new Priority(event.getPriority())); // Add location if any if (!event.getLocation().isEmpty()) { iCalEvent.getProperties().add(new Location(event.getLocation())); } // Add event URL if any if (event.getUrl() != null) { try { iCalEvent.getProperties().add(new Url(event.getUrl().toURI())); } catch (URISyntaxException ex) { throw new EncodingException(ex.getMessage(), ex); } } // Add Categories TextList categoryList = new TextList(event.getCategories().asArray()); if (!categoryList.isEmpty()) { iCalEvent.getProperties().add(new Categories(categoryList)); } // Add attendees for (String attendee : event.getAttendees().asList()) { try { iCalEvent.getProperties().add(new Attendee(attendee)); } catch (URISyntaxException ex) { throw new EncodingException("Malformed attendee URI: " + attendee, ex); } } iCalEvents.add(iCalEvent); } calendarIcs.getComponents().addAll(iCalEvents); CalendarOutputter outputter = new CalendarOutputter(); try { outputter.output(calendarIcs, output); return output.toString(CharEncoding.UTF_8); } catch (Exception ex) { throw new EncodingException( "The encoding of the events in iCal formatted text has failed!", ex); } finally { IOUtils.closeQuietly(output); } }
/** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String[] courseids = request.getParameterValues("courses"); // Saving the course ids as a session attribute (not necessary, might be better for saving) HttpSession session = request.getSession(true); session.setAttribute("courses", courseids); // Get course and time info for each id if (courseids == null) { } else { try { for (String id : courseids) { Integer courseid = Integer.parseInt(id); PreparedStatement getCourse = con.prepareStatement( "SELECT title, department, number, section " + "FROM courses WHERE id=?;"); getCourse.setInt(1, courseid); PreparedStatement getTime = con.prepareStatement( "SELECT day, starttime, endtime FROM times " + "WHERE course_id=?;"); getTime.setInt(1, courseid); ResultSet courseResult = getCourse.executeQuery(); ResultSet timesResult = getTime.executeQuery(); while (courseResult.next()) { // Generating the course from the DB, lets us utilize timeblocks Course course = new Course(); course.setTitle(courseResult.getString("title")); course.setDepartment(courseResult.getString("department")); course.setNumber(courseResult.getString("number")); course.setSection(courseResult.getString("section")); String courseTitle = course.getDepartment() + course.getNumber() + "-" + course.getSection() + ": " + course.getTitle(); while (timesResult.next()) { course.addTime( timesResult.getString("day").charAt(0), timesResult.getInt("starttime"), timesResult.getInt("endtime")); } // Now parse the timeshttp://ical4j.sourceforge.net/introduction.html for the timeblocks Map<String, Set<Character>> timeblocks = course.getTimeBlocks(); for (String startend : timeblocks.keySet()) { String[] StartEnd = startend.split("-"); Integer startInt = Integer.parseInt(StartEnd[0]); Integer endInt = Integer.parseInt(StartEnd[1]); java.util.Calendar startDate = this.getTimeCalendar(startInt); java.util.Calendar endDate = this.getTimeCalendar(endInt); DateTime startTime = new DateTime(startDate.getTime()); DateTime endTime = new DateTime(endDate.getTime()); VEvent courseEvent = new VEvent(startTime, endTime, courseTitle); Recur recur = new Recur(Recur.WEEKLY, null); // add the proper days to the recurrence for (char day : timeblocks.get(startend)) { recur.getDayList().add(dayMap.get(day)); } courseEvent.getProperties().add(new RRule(recur)); // Get unique identifier UidGenerator ug = new UidGenerator("uidGen"); Uid uid = ug.generateUid(); courseEvent.getProperties().add(uid); // Finally, add the time block to the calendar this.ical.getComponents().add(courseEvent); } } } } catch (SQLException e) { e.printStackTrace(); } } // Time to handle file download try { // making the file and outputting the calendar info File file = new File("mycalendar.ics"); int length = 0; FileOutputStream fout = new FileOutputStream(file); CalendarOutputter outputter = new CalendarOutputter(); outputter.output(ical, fout); ServletOutputStream outStream = response.getOutputStream(); response.setContentType("text/calendar"); response.setContentLength((int) file.length()); // sets HTTP header response.setHeader("Content-Disposition", "attachment; filename=calendar.ics"); byte[] byteBuffer = new byte[BUFSIZE]; DataInputStream in = new DataInputStream(new FileInputStream(file)); // reads the file's bytes and writes them to the response stream while ((in != null) && ((length = in.read(byteBuffer)) != -1)) { outStream.write(byteBuffer, 0, length); } in.close(); outStream.close(); } catch (ValidationException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
@Override @SuppressWarnings("unchecked") public void parseEntity(@NonNull InputStream entity) throws IOException, InvalidResourceException { net.fortuna.ical4j.model.Calendar ical; try { CalendarBuilder builder = new CalendarBuilder(); ical = builder.build(entity); if (ical == null) throw new InvalidResourceException("No iCalendar found"); } catch (ParserException e) { throw new InvalidResourceException(e); } // event ComponentList events = ical.getComponents(Component.VEVENT); if (events == null || events.isEmpty()) throw new InvalidResourceException("No VEVENT found"); VEvent event = (VEvent) events.get(0); if (event.getUid() != null) uid = event.getUid().getValue(); else { Log.w(TAG, "Received VEVENT without UID, generating new one"); generateUID(); } if ((dtStart = event.getStartDate()) == null || (dtEnd = event.getEndDate()) == null) throw new InvalidResourceException("Invalid start time/end time/duration"); if (hasTime(dtStart)) { validateTimeZone(dtStart); validateTimeZone(dtEnd); } // all-day events and "events on that day": // * related UNIX times must be in UTC // * must have a duration (set to one day if missing) if (!hasTime(dtStart) && !dtEnd.getDate().after(dtStart.getDate())) { Log.i(TAG, "Repairing iCal: DTEND := DTSTART+1"); Calendar c = Calendar.getInstance(TimeZone.getTimeZone(Time.TIMEZONE_UTC)); c.setTime(dtStart.getDate()); c.add(Calendar.DATE, 1); dtEnd.setDate(new Date(c.getTimeInMillis())); } rrule = (RRule) event.getProperty(Property.RRULE); rdate = (RDate) event.getProperty(Property.RDATE); exrule = (ExRule) event.getProperty(Property.EXRULE); exdate = (ExDate) event.getProperty(Property.EXDATE); if (event.getSummary() != null) summary = event.getSummary().getValue(); if (event.getLocation() != null) location = event.getLocation().getValue(); if (event.getDescription() != null) description = event.getDescription().getValue(); status = event.getStatus(); opaque = true; if (event.getTransparency() == Transp.TRANSPARENT) opaque = false; organizer = event.getOrganizer(); for (Object o : event.getProperties(Property.ATTENDEE)) attendees.add((Attendee) o); Clazz classification = event.getClassification(); if (classification != null) { if (classification == Clazz.PUBLIC) forPublic = true; else if (classification == Clazz.CONFIDENTIAL || classification == Clazz.PRIVATE) forPublic = false; } this.alarms = event.getAlarms(); }
/** {@inheritDoc} */ public VEvent createEvent(CalendarEvent event, List<User> attendees) { if (!isIcsEnabled()) { log.debug( "ExternalCalendaringService is disabled. Enable via calendar.ics.generation.enabled=true in sakai.properties"); return null; } // timezone. All dates are in GMT so we need to explicitly set that TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry(); TimeZone timezone = registry.getTimeZone("GMT"); VTimeZone tz = timezone.getVTimeZone(); // start and end date DateTime start = new DateTime(getStartDate(event.getRange()).getTime()); DateTime end = new DateTime(getEndDate(event.getRange()).getTime()); // create event incl title/summary VEvent vevent = new VEvent(start, end, event.getDisplayName()); // add timezone vevent.getProperties().add(tz.getTimeZoneId()); // add uid to event // could come from the vevent_uuid field in the calendar event, otherwise from event ID. String uuid = null; if (StringUtils.isNotBlank(event.getField("vevent_uuid"))) { uuid = event.getField("vevent_uuid"); } else { uuid = event.getId(); } vevent.getProperties().add(new Uid(uuid)); // add sequence to event // will come from the vevent_sequnece field in the calendar event, otherwise skip it String sequence = null; if (StringUtils.isNotBlank(event.getField("vevent_sequence"))) { sequence = event.getField("vevent_sequence"); vevent.getProperties().add(new Sequence(sequence)); } // add description to event vevent.getProperties().add(new Description(event.getDescription())); // add location to event vevent.getProperties().add(new Location(event.getLocation())); // add organiser to event if (StringUtils.isNotBlank(event.getCreator())) { String creatorEmail = sakaiProxy.getUserEmail(event.getCreator()); URI mailURI = createMailURI(creatorEmail); Cn commonName = new Cn(sakaiProxy.getUserDisplayName(event.getCreator())); Organizer organizer = new Organizer(mailURI); organizer.getParameters().add(commonName); vevent.getProperties().add(organizer); } // add attendees to event with 'required participant' role vevent = addAttendeesToEvent(vevent, attendees); // add URL to event, if present String url = null; if (StringUtils.isNotBlank(event.getField("vevent_url"))) { url = event.getField("vevent_url"); Url u = new Url(); try { u.setValue(url); vevent.getProperties().add(u); } catch (URISyntaxException e) { // it doesnt matter, ignore it } } if (log.isDebugEnabled()) { log.debug("VEvent:" + vevent); } return vevent; }