示例#1
1
  @Override
  public void init() throws ServletException {
    // TODO Auto-generated method stub
    super.init();
    filePath = getServletContext().getRealPath("") + File.separator + "schedule.ics";
    try {
      ical.getProperties().add(new ProdId("-//Ben Fortuna//iCal4j 1.0//EN"));
      ical.getProperties().add(Version.VERSION_2_0);
      ical.getProperties().add(CalScale.GREGORIAN);
    } catch (Exception e) {
      System.out.println("iCalendar failed to import");
    }
    registry = TimeZoneRegistryFactory.getInstance().createRegistry();
    timezone = registry.getTimeZone("America/New_York");
    tz = timezone.getVTimeZone();

    try {
      // Register the driver.
      Class.forName("org.postgresql.Driver").newInstance();
    } catch (Exception e) {
      e.printStackTrace();
    }
    String url = "jdbc:postgresql://hopper.cs.wlu.edu/corsola";
    try {
      con = DriverManager.getConnection(url, "web", "");
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
示例#2
0
  @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;
  }
  private void addVTimeZone(Calendar calendar) {

    if (timeZone != null) {
      VTimeZone tz = timeZone.getVTimeZone();
      calendar.getComponents().add(tz);
    }
  }
  private DtStart getStartDateProperty(Date startDate) {

    DateTime date = convertRaplaLocaleToUTC(startDate);
    TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
    net.fortuna.ical4j.model.TimeZone tz = registry.getTimeZone(timeZone.getID());
    date.setTimeZone(tz);
    return new DtStart(date);
  }
  @Inject
  public Export2iCalConverter(
      TimeZoneConverter timezoneConverter,
      RaplaLocale raplaLocale,
      Logger logger,
      ClientFacade facade)
      throws RaplaException {
    this.timezoneConverter = timezoneConverter;
    this.facade = facade;
    this.raplaLocale = raplaLocale;
    this.logger = logger;
    TimeZone zone = timezoneConverter.getImportExportTimeZone();

    calendar = raplaLocale.createCalendar();
    DynamicType[] dynamicTypes =
        facade.getDynamicTypes(DynamicTypeAnnotations.VALUE_CLASSIFICATION_TYPE_RESOURCE);
    for (DynamicType type : dynamicTypes) {
      if (type.getAnnotation(DynamicTypeAnnotations.KEY_LOCATION) != null) {
        hasLocationType = true;
      }
    }
    CompatibilityHints.setHintEnabled(CompatibilityHints.KEY_RELAXED_VALIDATION, true);

    RaplaConfiguration config =
        facade
            .getSystemPreferences()
            .getEntry(Export2iCalPlugin.ICAL_CONFIG, new RaplaConfiguration());
    global_export_attendees =
        config
            .getChild(Export2iCalPlugin.EXPORT_ATTENDEES)
            .getValueAsBoolean(Export2iCalPlugin.DEFAULT_exportAttendees);
    global_export_attendees_participation_status =
        config
            .getChild(Export2iCalPlugin.EXPORT_ATTENDEES_PARTICIPATION_STATUS)
            .getValue(Export2iCalPlugin.DEFAULT_attendee_participation_status);

    try {
      exportAttendeesAttribute =
          config.getChild(Export2iCalPlugin.EXPORT_ATTENDEES_EMAIL_ATTRIBUTE).getValue();
    } catch (ConfigurationException e) {
      exportAttendeesAttribute = "";
      getLogger().info("ExportAttendeesMailAttribute is not set. So do not export as meeting");
    }
    if (zone != null) {
      final String timezoneId = zone.getID();

      try {
        TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
        timeZone = registry.getTimeZone(timezoneId);
      } catch (Exception rc) {
        final VTimeZone vTimeZone = new VTimeZone();
        timeZone = new net.fortuna.ical4j.model.TimeZone(vTimeZone);
        final int rawOffset = zone.getRawOffset();
        timeZone.setRawOffset(rawOffset);
      }
    }
  }
  /**
   * Fügt die Wiederholungen des übergebenen Appointment-Objekts dem übergebenen
   * Event-Objekt hinzu.
   *
   * @param appointment Ein Rapla Appointment.
   */
  private void addRepeatings(Appointment appointment, PropertyList properties) {
    Repeating repeating = appointment.getRepeating();

    if (repeating == null) {
      return;
    }

    // This returns the strings DAYLY, WEEKLY, MONTHLY, YEARLY
    String type = repeating.getType().toString().toUpperCase();

    Recur recur;

    // here is evaluated, if a COUNT is set in Rapla, or an enddate is
    // specified
    if (repeating.getNumber() == -1) {
      recur = new Recur(type, -1);
    } else if (repeating.isFixedNumber()) {
      recur = new Recur(type, repeating.getNumber());
    } else {
      net.fortuna.ical4j.model.Date endDate = new net.fortuna.ical4j.model.Date(repeating.getEnd());
      // TODO do we need to translate the enddate in utc?
      recur = new Recur(type, endDate);
    }

    if (repeating.isDaily()) {
      // DAYLY -> settings : intervall
      recur.setInterval(repeating.getInterval());

    } else if (repeating.isWeekly()) {
      // WEEKLY -> settings : every nTh Weekday
      recur.setInterval(repeating.getInterval());

      calendar.setTime(appointment.getStart());
      recur.getDayList().add(WeekDay.getWeekDay(calendar));
    } else if (repeating.isMonthly()) {
      // MONTHLY -> settings : every nTh Weekday
      recur.setInterval(repeating.getInterval());
      calendar.setTime(appointment.getStart());
      int weekofmonth =
          Math.round(calendar.get(java.util.Calendar.DAY_OF_MONTH) / DateTools.DAYS_PER_WEEK) + 1;
      recur.getDayList().add(new WeekDay(WeekDay.getWeekDay(calendar), weekofmonth));
    } else if (repeating.isYearly()) {
      // YEARLY -> settings : every nTh day mTh Monthname
      calendar.setTime(appointment.getStart());
      calendar.get(java.util.Calendar.DAY_OF_YEAR);
    } else {
      getLogger().warn("Invalid data in recurrency rule!");
    }

    properties.add(new RRule(recur));

    // bugfix - if rapla has no exceptions, an empty EXDATE: element is
    // produced. This may bother some iCal tools
    if (repeating.getExceptions().length == 0) {
      return;
    }

    // Add exception dates
    // DateList dl = new DateList(Value.DATE);

    ExDate exDate = new ExDate();
    TimeZoneRegistry registry = TimeZoneRegistryFactory.getInstance().createRegistry();
    net.fortuna.ical4j.model.TimeZone tz = registry.getTimeZone(timeZone.getID());

    // rku: use seperate EXDATE for each exception
    for (Iterator<Date> itExceptions = Arrays.asList(repeating.getExceptions()).iterator();
        itExceptions.hasNext(); ) {
      // DateList dl = new DateList(Value.DATE);
      Date date = itExceptions.next();
      // dl.add(new net.fortuna.ical4j.model.Date( date));
      java.util.Calendar cal = raplaLocale.createCalendar();
      cal.setTime(date);
      int year = cal.get(java.util.Calendar.YEAR);
      int day_of_year = cal.get(java.util.Calendar.DAY_OF_YEAR);
      cal.setTime(appointment.getStart());
      cal.set(java.util.Calendar.YEAR, year);
      cal.set(java.util.Calendar.DAY_OF_YEAR, day_of_year);
      int offset =
          (int) (tz.getOffset(DateTools.cutDate(date).getTime()) / DateTools.MILLISECONDS_PER_HOUR);
      cal.add(java.util.Calendar.HOUR, -offset);
      Date dateToSave = cal.getTime();
      net.fortuna.ical4j.model.DateTime dateTime = new net.fortuna.ical4j.model.DateTime();
      dateTime.setTime(dateToSave.getTime());
      exDate.getDates().add(dateTime);
    }
    exDate.setTimeZone(tz);

    properties.add(exDate);
    // properties.add(new ExDate(dl));
  }
  /** {@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;
  }