Пример #1
0
  private void initPrivateCalendar() throws Exception {
    log.info("Inject private datas ....");
    // save setting
    try {
      setting = calService.getCalendarSetting(currentUser);
      log.info(String.format("Save calendar setting for user %s ....", currentUser));
    } catch (Exception e) {
      setting = newCalendarSetting();
      calService.saveCalendarSetting(currentUser, setting);
    }
    long t = System.currentTimeMillis(), t1 = t;

    t = System.currentTimeMillis();
    // save EventCategoy
    List<EventCategory> eventCategories = findEventCategorys();
    for (EventCategory evCat : eventCategories) {
      calService.saveEventCategory(currentUser, evCat, true);
      eventCategory.add(evCat);
      eventCategorys.add(evCat.getId());
    }
    log.info(
        String.format(
            "Saved %s eventCategories in %sms",
            eventCategories.size(), (System.currentTimeMillis() - t)));
    // save calendar
    List<Calendar> calendars = findCalendars(false);
    List<CalendarEvent> events;
    int index = 0, size = calendars.size(), evsCal, evs = 0, tas = 0;
    for (Calendar calendar : calendars) {
      t = System.currentTimeMillis();
      calService.saveUserCalendar(currentUser, calendar, true);
      privateCalendar.add(calendar.getId());
      // save Event
      events = findCalendarEvent(calendar.getId(), "0", CalendarEvent.TYPE_EVENT, false);
      for (CalendarEvent event : events) {
        calService.saveUserEvent(currentUser, calendar.getId(), event, true);
      }
      evsCal = events.size();
      evs += evsCal;
      // save Task
      events = findCalendarEvent(calendar.getId(), "0", CalendarEvent.TYPE_TASK, false);
      tas += events.size();
      for (CalendarEvent event : events) {
        calService.saveUserEvent(currentUser, calendar.getId(), event, true);
      }
      log.info(
          String.format(
              "Saved Calendar %s/%s with %s Events and %s Tasks in %sms",
              (++index), size, evsCal, events.size(), (System.currentTimeMillis()) - t));
    }
    log.info(
        String.format(
            "INITIALIZED EventCategorys=%s / Calendars=%s / Events=%s / Tasks=%s in %sms",
            eventCategories.size(), calendars.size(), evs, tas, (System.currentTimeMillis() - t1)));
    saveHistoryInject();
  }
 private TimeZone getUserTimeZone() {
   try {
     String username = ConversationState.getCurrent().getIdentity().getUserId();
     CalendarService calService =
         (CalendarService)
             PortalContainer.getInstance().getComponentInstanceOfType(CalendarService.class);
     CalendarSetting setting = calService.getCalendarSetting(username);
     return TimeZone.getTimeZone(setting.getTimeZone());
   } catch (Exception e) {
     if (LOG.isDebugEnabled()) LOG.error("Can not get time zone from user setting ", e);
     return null;
   }
 }
Пример #3
0
 private void setHistoryInject() {
   initDatas();
   try {
     String s = calService.getCalendarSetting(currentUser).getBaseURL();
     if (!isEmpty(s) && s.indexOf(";") > 0) {
       String[] strs = s.split(";");
       publicCalendar.addAll(convertStringToList(strs[0]));
       privateCalendar.addAll(convertStringToList(strs[1]));
       eventCategorys.addAll(convertStringToList(strs[2]));
       categoryIds.addAll(convertStringToList(strs[3]));
     }
   } catch (Exception e) {
     log.warn("Failed to get calendar settings", e);
   }
 }
  /**
   * convert date to user date format using calendar setting
   *
   * @param date
   * @return
   */
  private String getDateFormattedAfterUserSetting(String date) {
    try {
      String userId = ConversationState.getCurrent().getIdentity().getUserId();
      calService_ =
          (CalendarService)
              PortalContainer.getInstance().getComponentInstance(CalendarService.class); // not null
      CalendarSetting calSetting = calService_.getCalendarSetting(userId); // not null
      WebuiRequestContext requestContext = WebuiRequestContext.getCurrentInstance();
      Locale locale = requestContext.getParentAppRequestContext().getLocale();
      DateFormat format = new SimpleDateFormat(calSetting.getDateFormat(), locale);

      /* recurrenceId to Date then re-convert it to user date format */
      DateFormat format1 = new SimpleDateFormat("yyyyMMdd'T'HHmmss'Z'");
      Date eventDate = format1.parse(date);

      return format.format(eventDate);
    } catch (Exception e) {
      LOG.debug(e.getMessage());
    }
    return null;
  }
  /**
   * Build the repeating summary, i.e: daily every 2 days, until 02/03/2011. <br>
   * The summary structure is defined in resource bundle, it contains some parameters and </br> will
   * be replaced by values from repeatEvent. <br>
   *
   * <p>There are 6 parameters: {count}, {until}, {interval}, {byDays}, {theDay}, {theNumber}.<br>
   * Some labels in resource bundle to define numbers (the first, the second, ...) which were used
   * in summary
   *
   * @param repeatEvent the repeating event
   * @return summary string about repeating event
   * @throws Exception
   */
  public String buildRepeatSummary(CalendarEvent repeatEvent) {
    if (repeatEvent == null) return "";
    String repeatType = repeatEvent.getRepeatType();
    if (CalendarEvent.RP_NOREPEAT.equals(repeatType) || repeatType == null) return "";

    try {
      String userId = ConversationState.getCurrent().getIdentity().getUserId();
      calService_ =
          (CalendarService)
              PortalContainer.getInstance().getComponentInstance(CalendarService.class);
      CalendarSetting calSetting = calService_.getCalendarSetting(userId);
      WebuiRequestContext requestContext = WebuiRequestContext.getCurrentInstance();

      Locale locale = requestContext.getParentAppRequestContext().getLocale();
      DateFormat format = new SimpleDateFormat(calSetting.getDateFormat(), locale);
      DateFormatSymbols symbols = new DateFormatSymbols(locale);
      String[] dayOfWeeks = symbols.getWeekdays();

      int interval = (int) repeatEvent.getRepeatInterval();
      int count = (int) repeatEvent.getRepeatCount();
      Date until = repeatEvent.getRepeatUntilDate();
      String endType = RP_END_NEVER;
      if (count > 0) endType = RP_END_AFTER;
      if (until != null) endType = RP_END_BYDATE;

      StringBuilder pattern = new StringBuilder("");
      if (repeatType.equals(CalendarEvent.RP_DAILY)) {
        if (interval == 1) {
          // pattern = "Daily";
          pattern.append(CalendarUIActivity.getUICalendarLabel("daily"));
        } else {
          // pattern = "Every {interval} days";
          pattern.append(CalendarUIActivity.getUICalendarLabel("every-day"));
        }
        if (endType.equals(RP_END_AFTER)) {
          // pattern = "Daily, {count} times";
          // pattern = "Every {interval} days, {count} times";
          pattern.append(", " + CalendarUIActivity.getUICalendarLabel("count-times"));
        }
        if (endType.equals(RP_END_BYDATE)) {
          // pattern = "Daily, until {until}";
          // pattern = "Every {interval} days, until {until}";
          pattern.append(", " + CalendarUIActivity.getUICalendarLabel("until"));
        }

        return new String(pattern)
            .replace("{interval}", String.valueOf(interval))
            .replace("{count}", String.valueOf(repeatEvent.getRepeatCount()))
            .replace(
                "{until}",
                repeatEvent.getRepeatUntilDate() == null
                    ? ""
                    : format.format(repeatEvent.getRepeatUntilDate()));
      }

      if (repeatType.equals(CalendarEvent.RP_WEEKLY)) {
        if (interval == 1) {
          // pattern = "Weekly on {byDays}";
          pattern.append(CalendarUIActivity.getUICalendarLabel("weekly"));
        } else {
          // pattern = "Every {interval} weeks on {byDays}";
          pattern.append(CalendarUIActivity.getUICalendarLabel("every-week"));
        }
        if (endType.equals(RP_END_AFTER)) {
          // pattern = "Weekly on {byDays}, {count} times";
          // pattern = "Every {interval} weeks on {byDays}, {count} times";
          pattern.append(", " + CalendarUIActivity.getUICalendarLabel("count-times"));
        }
        if (endType.equals(RP_END_BYDATE)) {
          // pattern = "Weekly on {byDays}, until {until}";
          // pattern = "Every {interval} weeks on {byDays}, until {until}";
          pattern.append(", " + CalendarUIActivity.getUICalendarLabel("until"));
        }

        String[] weeklyByDays = repeatEvent.getRepeatByDay();
        StringBuffer byDays = new StringBuffer();
        for (int i = 0; i < weeklyByDays.length; i++) {
          if (i == 0) {
            byDays.append(dayOfWeeks[convertToDayOfWeek(weeklyByDays[0])]);
          } else {
            byDays.append(", ");
            byDays.append(dayOfWeeks[convertToDayOfWeek(weeklyByDays[i])]);
          }
        }
        return new String(pattern)
            .replace("{interval}", String.valueOf(interval))
            .replace("{count}", String.valueOf(repeatEvent.getRepeatCount()))
            .replace(
                "{until}",
                repeatEvent.getRepeatUntilDate() == null
                    ? ""
                    : format.format(repeatEvent.getRepeatUntilDate()))
            .replace("{byDays}", byDays.toString());
      }

      if (repeatType.equals(CalendarEvent.RP_MONTHLY)) {
        String monthlyType = RP_MONTHLY_BYMONTHDAY;
        if (repeatEvent.getRepeatByDay() != null && repeatEvent.getRepeatByDay().length > 0)
          monthlyType = RP_MONTHLY_BYDAY;

        if (interval == 1) {
          // pattern = "Monthly on"
          pattern.append(CalendarUIActivity.getUICalendarLabel("monthly"));
        } else {
          // pattern = "Every {interval} months on
          pattern.append(CalendarUIActivity.getUICalendarLabel("every-month"));
        }

        if (monthlyType.equals(RP_MONTHLY_BYDAY)) {
          // pattern = "Monthly on {theNumber} {theDay}
          // pattern = "Every {interval} months on {theNumber} {theDay}
          pattern.append(" " + CalendarUIActivity.getUICalendarLabel("monthly-by-day"));
        } else {
          // pattern = "Monthly on day {theDay}
          // pattern = "Every {interval} months on day {theDay}
          pattern.append(" " + CalendarUIActivity.getUICalendarLabel("monthly-by-month-day"));
        }

        if (endType.equals(RP_END_AFTER)) {
          pattern.append(", " + CalendarUIActivity.getUICalendarLabel("count-times"));
        }
        if (endType.equals(RP_END_BYDATE)) {
          pattern.append(", " + CalendarUIActivity.getUICalendarLabel("until"));
        }

        String theNumber = ""; // the first, the second, the third, ...
        String theDay =
            ""; // in monthly by day, it's Monday, Tuesday, ... (day of week), in monthly by
        // monthday, it's 1-31 (day of month)
        if (monthlyType.equals(RP_MONTHLY_BYDAY)) {
          java.util.Calendar temp = getCalendarInstanceBySetting(calSetting);
          temp.setTime(repeatEvent.getFromDateTime());
          int weekOfMonth = temp.get(java.util.Calendar.WEEK_OF_MONTH);
          java.util.Calendar temp2 = getCalendarInstanceBySetting(calSetting);
          temp2.setTime(temp.getTime());
          temp2.add(java.util.Calendar.DATE, 7);
          if (temp2.get(java.util.Calendar.MONTH) != temp.get(java.util.Calendar.MONTH))
            weekOfMonth = 5;
          int dayOfWeek = temp.get(java.util.Calendar.DAY_OF_WEEK);
          String[] weekOfMonths =
              new String[] {
                CalendarUIActivity.getUICalendarLabel("summary-the-first"),
                CalendarUIActivity.getUICalendarLabel("summary-the-second"),
                CalendarUIActivity.getUICalendarLabel("summary-the-third"),
                CalendarUIActivity.getUICalendarLabel("summary-the-fourth"),
                CalendarUIActivity.getUICalendarLabel("summary-the-last")
              };
          theNumber = weekOfMonths[weekOfMonth - 1];
          theDay = dayOfWeeks[dayOfWeek];
        } else {
          java.util.Calendar temp = getCalendarInstanceBySetting(calSetting);
          temp.setTime(repeatEvent.getFromDateTime());
          int dayOfMonth = temp.get(java.util.Calendar.DAY_OF_MONTH);
          theDay = String.valueOf(dayOfMonth);
        }
        return new String(pattern)
            .replace("{interval}", String.valueOf(interval))
            .replace("{count}", String.valueOf(repeatEvent.getRepeatCount()))
            .replace(
                "{until}",
                repeatEvent.getRepeatUntilDate() == null
                    ? ""
                    : format.format(repeatEvent.getRepeatUntilDate()))
            .replace("{theDay}", theDay)
            .replace("{theNumber}", theNumber);
      }

      if (repeatType.equals(CalendarEvent.RP_YEARLY)) {
        if (interval == 1) {
          // pattern = "Yearly on {theDay}"
          pattern.append(CalendarUIActivity.getUICalendarLabel("yearly"));
        } else {
          // pattern = "Every {interval} years on {theDay}"
          pattern.append(CalendarUIActivity.getUICalendarLabel("every-year"));
        }

        if (endType.equals(RP_END_AFTER)) {
          // pattern = "Yearly on {theDay}, {count} times"
          // pattern = "Every {interval} years on {theDay}, {count} times"
          pattern.append(", " + CalendarUIActivity.getUICalendarLabel("count-times"));
        }
        if (endType.equals(RP_END_BYDATE)) {
          // pattern = "Yearly on {theDay}, until {until}"
          // pattern = "Every {interval} years on {theDay}, until {until}"
          pattern.append(", " + CalendarUIActivity.getUICalendarLabel("until"));
        }

        String theDay = format.format(repeatEvent.getFromDateTime()); //
        return new String(pattern)
            .replace("{interval}", String.valueOf(interval))
            .replace("{count}", String.valueOf(repeatEvent.getRepeatCount()))
            .replace(
                "{until}",
                repeatEvent.getRepeatUntilDate() == null
                    ? ""
                    : format.format(repeatEvent.getRepeatUntilDate()))
            .replace("{theDay}", theDay);
      }
    } catch (Exception e) {
      LOG.info(e.getLocalizedMessage());
    }
    return null;
  }