/** * get calendar by user setting (timezone, first day of week) * * @param calendarSetting * @return calendar object */ public static Calendar getCalendarInstanceBySetting(CalendarSetting calendarSetting) { Calendar calendar = GregorianCalendar.getInstance(); calendar.setLenient(false); calendar.setTimeZone(TimeZone.getTimeZone(calendarSetting.getTimeZone())); calendar.setFirstDayOfWeek(Integer.parseInt(calendarSetting.getWeekStartOn())); calendar.setMinimalDaysInFirstWeek(4); return calendar; }
/** * Gets the default view in user setting If the saved view is not in view types any more (for * example YearView is removed in PLF 4), this method returns DayView as default view and save * this view to the setting * * @return the view */ public static String getViewInSetting() { CalendarSetting calendarSetting = getCurrentUserCalendarSetting(); try { return UICalendarViewContainer.TYPES[Integer.parseInt(calendarSetting.getViewType())]; } catch (ArrayIndexOutOfBoundsException e) { resetViewInSetting(calendarSetting); return UICalendarViewContainer.TYPES[0]; } catch (NumberFormatException nfe) { resetViewInSetting(calendarSetting); return UICalendarViewContainer.TYPES[0]; } }
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; } }
private void saveHistoryInject() throws Exception { baseURL = publicCalendar.toString(); baseURL += ";" + privateCalendar.toString(); baseURL += ";" + eventCategorys.toString(); baseURL += ";" + categoryIds.toString(); setting.setBaseURL(baseURL); calService.saveCalendarSetting(currentUser, setting); }
private CalendarSetting newCalendarSetting() { CalendarSetting setting = new CalendarSetting(); setting.setViewType("1"); setting.setBaseURL(baseURL); setting.setWeekStartOn(String.valueOf(java.util.Calendar.MONDAY)); setting.setWorkingTimeBegin("08:00"); setting.setWorkingTimeEnd("18:00"); setting.setShowWorkingTime(false); setting.setTimeZone("Asia/Ho_Chi_Minh"); return setting; }
// save a default view type for Calendar Setting when cannot get the view type private static void resetViewInSetting(CalendarSetting calendarSetting) { try { calendarSetting.setViewType(CalendarSetting.DAY_VIEW); getCalendarService().saveCalendarSetting(getCurrentUser(), calendarSetting); setCurrentCalendarSetting(calendarSetting); } catch (Exception e) { if (log.isDebugEnabled()) { log.debug("Cant save Calendar Setting", 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; }
private Calendar newPrivateCalendar() { Calendar calendar = new Calendar(); calendar.setCalendarOwner(currentUser); calendar.setDataInit(true); calendar.setName(calRandomWords(5)); calendar.setDescription(randomWords(20)); calendar.setCalendarColor(getRandomColor()); calendar.setEditPermission(new String[] {}); calendar.setGroups(new String[] {}); calendar.setViewPermission(new String[] {}); calendar.setPrivateUrl(EMPTY); calendar.setPublicUrl(EMPTY); calendar.setPublic(false); calendar.setLocale(DEFAULT_LOCATION); calendar.setTimeZone(setting.getTimeZone()); return calendar; }
/** * 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; }