/**
   * Clear out the map and stuff an Entry into it in a format that can be inserted into a content
   * provider.
   *
   * <p>If a date is before 1970 or past 2038, ENTRY_INVALID is returned, and DTSTART is set to -1.
   * This is due to the current 32-bit time restriction and will be fixed in a future release.
   *
   * @return ENTRY_OK, ENTRY_DELETED, or ENTRY_INVALID
   */
  private int entryToContentValues(
      EventEntry event, Long syncLocalId, ContentValues map, Object info) {
    SyncInfo syncInfo = (SyncInfo) info;

    // There are 3 cases for parsing a date-time string:
    //
    // 1. The date-time string specifies a date and time with a time offset.
    //    (The "normal" format.)
    // 2. The date-time string is just a date, used for all-day events,
    //    with no time or time offset fields. (The "all-day" format.)
    // 3. The date-time string specifies a date and time, but no time
    //    offset.  (The "floating" format, not supported yet.)
    //
    // Case 1: Time.parse3339() converts the date-time string to UTC and
    // sets the Time.timezone to UTC.  It does not matter what the initial
    // Time.timezone field was set to.  The initial timezone is ignored.
    //
    // Case 2: The date-time string doesn't specify the time.
    // Time.parse3339() just sets the date but not the time (hour, minute,
    // second) fields.  (The time fields should be zero, meaning midnight.)
    // This code then sets the timezone to UTC (because this is an all-day
    // event).  It does not matter in this case either what the initial
    // Time.timezone field was set to.
    //
    // Case 3: This is a "floating time" (which we do not support yet).
    // In this case, it will matter what the initial Time.timezone is set
    // to.  It should use UTC.  If I specify a floating time of 1pm then I
    // want that event displayed at 1pm in every timezone.  The easiest way
    // to support this would be store it as 1pm in UTC and mark the event
    // as "isFloating" (with a new database column).  Then when displaying
    // the event, the code checks "isFloating" and just leaves the time at
    // 1pm without doing any conversion to the local timezone.
    //
    // So in all cases, it is correct to set the Time.timezone to UTC.
    Time time = new Time(Time.TIMEZONE_UTC);

    map.clear();

    // Base sync info
    map.put(Events._SYNC_ID, event.getId());
    String version = event.getEditUri();
    if (!StringUtils.isEmpty(version)) {
      // Always rewrite the edit URL to https for dasher account to avoid
      // redirection.
      map.put(Events._SYNC_VERSION, rewriteUrlforAccount(getAccount(), version));
    }

    // see if this is an exception to an existing event/recurrence.
    String originalId = event.getOriginalEventId();
    String originalStartTime = event.getOriginalEventStartTime();
    boolean isRecurrenceException = false;
    if (!StringUtils.isEmpty(originalId) && !StringUtils.isEmpty(originalStartTime)) {
      isRecurrenceException = true;
      time.parse3339(originalStartTime);
      map.put(Events.ORIGINAL_EVENT, originalId);
      map.put(Events.ORIGINAL_INSTANCE_TIME, time.toMillis(false /* use isDst */));
      map.put(Events.ORIGINAL_ALL_DAY, time.allDay ? 1 : 0);
    }

    // Event status
    byte status = event.getStatus();
    switch (status) {
      case EventEntry.STATUS_CANCELED:
        if (!isRecurrenceException) {
          return ENTRY_DELETED;
        }
        map.put(Events.STATUS, sCanceledStatus);
        break;
      case EventEntry.STATUS_TENTATIVE:
        map.put(Events.STATUS, sTentativeStatus);
        break;
      case EventEntry.STATUS_CONFIRMED:
        map.put(Events.STATUS, sConfirmedStatus);
        break;
      default:
        // should not happen
        return ENTRY_INVALID;
    }

    map.put(Events._SYNC_LOCAL_ID, syncLocalId);

    // Updated time, only needed for non-deleted items
    String updated = event.getUpdateDate();
    map.put(Events._SYNC_TIME, updated);
    map.put(Events._SYNC_DIRTY, 0);

    // visibility
    switch (event.getVisibility()) {
      case EventEntry.VISIBILITY_DEFAULT:
        map.put(Events.VISIBILITY, Events.VISIBILITY_DEFAULT);
        break;
      case EventEntry.VISIBILITY_CONFIDENTIAL:
        map.put(Events.VISIBILITY, Events.VISIBILITY_CONFIDENTIAL);
        break;
      case EventEntry.VISIBILITY_PRIVATE:
        map.put(Events.VISIBILITY, Events.VISIBILITY_PRIVATE);
        break;
      case EventEntry.VISIBILITY_PUBLIC:
        map.put(Events.VISIBILITY, Events.VISIBILITY_PUBLIC);
        break;
      default:
        // should not happen
        Log.e(TAG, "Unexpected visibility " + event.getVisibility());
        return ENTRY_INVALID;
    }

    // transparency
    switch (event.getTransparency()) {
      case EventEntry.TRANSPARENCY_OPAQUE:
        map.put(Events.TRANSPARENCY, Events.TRANSPARENCY_OPAQUE);
        break;
      case EventEntry.TRANSPARENCY_TRANSPARENT:
        map.put(Events.TRANSPARENCY, Events.TRANSPARENCY_TRANSPARENT);
        break;
      default:
        // should not happen
        Log.e(TAG, "Unexpected transparency " + event.getTransparency());
        return ENTRY_INVALID;
    }

    // html uri
    String htmlUri = event.getHtmlUri();
    if (!StringUtils.isEmpty(htmlUri)) {
      // TODO: convert this desktop url into a mobile one?
      // htmlUri = htmlUri.replace("/event?", "/mevent?"); // but a little more robust
      map.put(Events.HTML_URI, htmlUri);
    }

    // title
    String title = event.getTitle();
    if (!StringUtils.isEmpty(title)) {
      map.put(Events.TITLE, title);
    }

    // content
    String content = event.getContent();
    if (!StringUtils.isEmpty(content)) {
      map.put(Events.DESCRIPTION, content);
    }

    // where
    String where = event.getWhere();
    if (!StringUtils.isEmpty(where)) {
      map.put(Events.EVENT_LOCATION, where);
    }

    // Calendar ID
    map.put(Events.CALENDAR_ID, syncInfo.calendarId);

    // comments uri
    String commentsUri = event.getCommentsUri();
    if (commentsUri != null) {
      map.put(Events.COMMENTS_URI, commentsUri);
    }

    boolean timesSet = false;

    // see if there are any reminders for this event
    if (event.getReminders() != null) {
      // just store that we have reminders.  the caller will have
      // to update the reminders table separately.
      map.put(Events.HAS_ALARM, 1);
    }

    // see if there are any extended properties for this event
    if (event.getExtendedProperties() != null) {
      // just store that we have extended properties.  the caller will have
      // to update the extendedproperties table separately.
      map.put(Events.HAS_EXTENDED_PROPERTIES, 1);
    }

    // dtstart & dtend
    When when = event.getFirstWhen();
    if (when != null) {
      String startTime = when.getStartTime();
      if (!StringUtils.isEmpty(startTime)) {
        time.parse3339(startTime);

        // we also stash away the event's timezone.
        // this timezone might get overwritten below, if this event is
        // a recurrence (recurrences are defined in terms of the
        // timezone of the creator of the event).
        // note that we treat all day events as occurring in the UTC timezone, so
        // an event on 05/08/2007 occurs on 05/08/2007, no matter what timezone the device
        // is in.
        // TODO: handle the "floating" timezone.
        if (time.allDay) {
          map.put(Events.ALL_DAY, 1);
          map.put(Events.EVENT_TIMEZONE, Time.TIMEZONE_UTC);
        } else {
          map.put(Events.EVENT_TIMEZONE, syncInfo.calendarTimezone);
        }

        long dtstart = time.toMillis(false /* use isDst */);
        if (dtstart < 0) {
          if (Config.LOGD) {
            Log.d(TAG, "dtstart out of range: " + startTime);
          }
          map.put(Events.DTSTART, -1); // Flag to caller that date is out of range
          return ENTRY_INVALID;
        }
        map.put(Events.DTSTART, dtstart);

        timesSet = true;
      }

      String endTime = when.getEndTime();
      if (!StringUtils.isEmpty(endTime)) {
        time.parse3339(endTime);
        long dtend = time.toMillis(false /* use isDst */);
        if (dtend < 0) {
          if (Config.LOGD) {
            Log.d(TAG, "dtend out of range: " + endTime);
          }
          map.put(Events.DTSTART, -1); // Flag to caller that date is out of range
          return ENTRY_INVALID;
        }
        map.put(Events.DTEND, dtend);
      }
    }

    // rrule
    String recurrence = event.getRecurrence();
    if (!TextUtils.isEmpty(recurrence)) {
      ICalendar.Component recurrenceComponent = new ICalendar.Component("DUMMY", null /* parent */);
      ICalendar ical = null;
      try {
        ICalendar.parseComponent(recurrenceComponent, recurrence);
      } catch (ICalendar.FormatException fe) {
        if (Config.LOGD) {
          Log.d(TAG, "Unable to parse recurrence: " + recurrence);
        }
        return ENTRY_INVALID;
      }

      if (!RecurrenceSet.populateContentValues(recurrenceComponent, map)) {
        return ENTRY_INVALID;
      }

      timesSet = true;
    }

    if (!timesSet) {
      return ENTRY_INVALID;
    }

    map.put(SyncConstValue._SYNC_ACCOUNT, getAccount());
    return ENTRY_OK;
  }
  @Override
  protected String cursorToEntry(SyncContext context, Cursor c, Entry entry, Object info)
      throws ParseException {
    EventEntry event = (EventEntry) entry;
    SyncInfo syncInfo = (SyncInfo) info;

    String feedUrl = c.getString(c.getColumnIndex(Calendars.URL));

    // update the sync info.  this will be used later when we update the
    // provider with the results of sending this entry to the calendar
    // server.
    syncInfo.calendarId = c.getLong(c.getColumnIndex(Events.CALENDAR_ID));
    syncInfo.calendarTimezone = c.getString(c.getColumnIndex(Events.EVENT_TIMEZONE));
    if (TextUtils.isEmpty(syncInfo.calendarTimezone)) {
      // if the event timezone is not set -- e.g., when we're creating an
      // event on the device -- we will use the timezone for the calendar.
      syncInfo.calendarTimezone = c.getString(c.getColumnIndex(Events.TIMEZONE));
    }

    // id
    event.setId(c.getString(c.getColumnIndex(Events._SYNC_ID)));
    event.setEditUri(c.getString(c.getColumnIndex(Events._SYNC_VERSION)));

    // status
    byte status;
    int localStatus = c.getInt(c.getColumnIndex(Events.STATUS));
    switch (localStatus) {
      case Events.STATUS_CANCELED:
        status = EventEntry.STATUS_CANCELED;
        break;
      case Events.STATUS_CONFIRMED:
        status = EventEntry.STATUS_CONFIRMED;
        break;
      case Events.STATUS_TENTATIVE:
        status = EventEntry.STATUS_TENTATIVE;
        break;
      default:
        // should not happen
        status = EventEntry.STATUS_TENTATIVE;
        break;
    }
    event.setStatus(status);

    // visibility
    byte visibility;
    int localVisibility = c.getInt(c.getColumnIndex(Events.VISIBILITY));
    switch (localVisibility) {
      case Events.VISIBILITY_DEFAULT:
        visibility = EventEntry.VISIBILITY_DEFAULT;
        break;
      case Events.VISIBILITY_CONFIDENTIAL:
        visibility = EventEntry.VISIBILITY_CONFIDENTIAL;
        break;
      case Events.VISIBILITY_PRIVATE:
        visibility = EventEntry.VISIBILITY_PRIVATE;
        break;
      case Events.VISIBILITY_PUBLIC:
        visibility = EventEntry.VISIBILITY_PUBLIC;
        break;
      default:
        // should not happen
        Log.e(
            TAG,
            "Unexpected value for visibility: " + localVisibility + "; using default visibility.");
        visibility = EventEntry.VISIBILITY_DEFAULT;
        break;
    }
    event.setVisibility(visibility);

    byte transparency;
    int localTransparency = c.getInt(c.getColumnIndex(Events.TRANSPARENCY));
    switch (localTransparency) {
      case Events.TRANSPARENCY_OPAQUE:
        transparency = EventEntry.TRANSPARENCY_OPAQUE;
        break;
      case Events.TRANSPARENCY_TRANSPARENT:
        transparency = EventEntry.TRANSPARENCY_TRANSPARENT;
        break;
      default:
        // should not happen
        Log.e(
            TAG,
            "Unexpected value for transparency: "
                + localTransparency
                + "; using opaque transparency.");
        transparency = EventEntry.TRANSPARENCY_OPAQUE;
        break;
    }
    event.setTransparency(transparency);

    // could set the html uri, but there's no need to, since it should not be edited.

    // title
    event.setTitle(c.getString(c.getColumnIndex(Events.TITLE)));

    // description
    event.setContent(c.getString(c.getColumnIndex(Events.DESCRIPTION)));

    // where
    event.setWhere(c.getString(c.getColumnIndex(Events.EVENT_LOCATION)));

    // attendees
    long eventId = c.getInt(c.getColumnIndex(Events._SYNC_LOCAL_ID));
    addAttendeesToEntry(eventId, event);

    // comment uri
    event.setCommentsUri(c.getString(c.getColumnIndexOrThrow(Events.COMMENTS_URI)));

    Time utc = new Time(Time.TIMEZONE_UTC);

    boolean allDay = c.getInt(c.getColumnIndex(Events.ALL_DAY)) != 0;

    String startTime = null;
    String endTime = null;
    // start time
    int dtstartColumn = c.getColumnIndex(Events.DTSTART);
    if (!c.isNull(dtstartColumn)) {
      long dtstart = c.getLong(dtstartColumn);
      utc.set(dtstart);
      startTime = utc.format3339(allDay);
    }

    // end time
    int dtendColumn = c.getColumnIndex(Events.DTEND);
    if (!c.isNull(dtendColumn)) {
      long dtend = c.getLong(dtendColumn);
      utc.set(dtend);
      endTime = utc.format3339(allDay);
    }

    When when = new When(startTime, endTime);
    event.addWhen(when);

    // reminders
    Integer hasReminder = c.getInt(c.getColumnIndex(Events.HAS_ALARM));
    if (hasReminder != null && hasReminder.intValue() != 0) {
      addRemindersToEntry(eventId, event);
    }

    // extendedProperties
    Integer hasExtendedProperties = c.getInt(c.getColumnIndex(Events.HAS_EXTENDED_PROPERTIES));
    if (hasExtendedProperties != null && hasExtendedProperties.intValue() != 0) {
      addExtendedPropertiesToEntry(eventId, event);
    }

    long originalStartTime = -1;
    String originalId = c.getString(c.getColumnIndex(Events.ORIGINAL_EVENT));
    int originalStartTimeIndex = c.getColumnIndex(Events.ORIGINAL_INSTANCE_TIME);
    if (!c.isNull(originalStartTimeIndex)) {
      originalStartTime = c.getLong(originalStartTimeIndex);
    }
    if ((originalStartTime != -1) && !TextUtils.isEmpty(originalId)) {
      // We need to use the "originalAllDay" field for the original event
      // in order to format the "originalStartTime" correctly.
      boolean originalAllDay = c.getInt(c.getColumnIndex(Events.ORIGINAL_ALL_DAY)) != 0;

      Time originalTime = new Time(c.getString(c.getColumnIndex(Events.EVENT_TIMEZONE)));
      originalTime.set(originalStartTime);

      utc.set(originalStartTime);
      event.setOriginalEventStartTime(utc.format3339(originalAllDay));
      event.setOriginalEventId(originalId);
    }

    // recurrences.
    ICalendar.Component component = new ICalendar.Component("DUMMY", null /* parent */);
    if (RecurrenceSet.populateComponent(c, component)) {
      addRecurrenceToEntry(component, event);
    }

    // if this is a new entry, return the feed url.  otherwise, return null; the edit url is
    // already in the entry.
    if (event.getEditUri() == null) {
      return feedUrl;
    } else {
      return null;
    }
  }