@RequestMapping(value = "/saveEvent", method = RequestMethod.POST)
 public String saveEvent(
     HttpServletRequest request,
     @ModelAttribute("event") EditEvent event,
     BindingResult result,
     SessionStatus status,
     Model uiModel) {
   User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
   EditEvent eventReturned = null;
   try {
     eventReturned =
         calendarEventOAuthService.saveEvent(user.getUserId(), event, event.getEventId());
     if (eventReturned.getResponseCode() == HttpStatus.UNPROCESSABLE_ENTITY.value()) {
       Errors errors = ((Errors) result);
       for (Iterator iterator = eventReturned.getErrors().entrySet().iterator();
           iterator.hasNext(); ) {
         Map.Entry<String, List<String>> entry = (Map.Entry<String, List<String>>) iterator.next();
         for (String error : entry.getValue()) {
           errors.rejectValue(entry.getKey(), "", error);
         }
       }
       event.setDefaultCategories(eventReturned.getDefaultCategories());
       return "calendar/editEvent";
     }
   } catch (PageLevelException pageLevelException) {
     uiModel.addAttribute("message", pageLevelException.getMessage());
     return "calendar/message";
   }
   return "redirect:/calendar/month";
 }
  @RequestMapping(value = "/deleteEvent", method = RequestMethod.GET)
  public String deleteEvent(
      HttpServletRequest request,
      @RequestParam(required = true) Long eventId,
      @RequestParam(required = false) Long seriesId,
      @RequestParam(required = false) String date,
      Model uiModel) {
    User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
    SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
    Date selectedDate = null;
    try {
      if (date != null) {
        selectedDate = sdf.parse(date);
      }
    } catch (ParseException e) {

    }
    try {
      calendarEventOAuthService.deleteEvent(user.getUserId(), eventId, seriesId, selectedDate);
    } catch (PageLevelException pageLevelException) {
      uiModel.addAttribute("message", pageLevelException.getMessage());
      return "calendar/message";
    }
    return "redirect:/calendar/month";
  }
  @RequestMapping(value = "/listEvents", method = RequestMethod.GET)
  public String listEvents(
      HttpServletRequest request,
      Model uiModel,
      @RequestParam(required = true) String date,
      @RequestParam(required = true) String beginDate,
      @RequestParam(required = true) String endDate) {
    User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    Calendar selectedDate = Calendar.getInstance();
    Calendar beginCalendar = Calendar.getInstance();
    Calendar endCalendar = Calendar.getInstance();
    try {
      if (date != null) {
        selectedDate.setTime(sdf.parse(date));
      }
      if (beginDate != null) {
        beginCalendar.setTime(sdf.parse(beginDate));
      }
      if (endDate != null) {
        endCalendar.setTime(sdf.parse(endDate));
      }
    } catch (ParseException e) {
      return list(request, uiModel, date);
    }
    try {
      Filter filter = (Filter) request.getSession().getAttribute("calendar.event.filterId");
      ListViewEvents listViewEvents =
          calendarEventOAuthService.retrieveViewEventsList(
              user.getUserId(),
              selectedDate.getTime(),
              beginCalendar.getTime(),
              endCalendar.getTime(),
              filter != null ? filter.getFilterId() : null);

      Calendar currentEndDate = (Calendar) endCalendar.clone();
      Calendar previousDate = (Calendar) beginCalendar.clone();
      previousDate.add(Calendar.DATE, -listViewEvents.getAppData().getListViewFutureDaysLimit());
      endCalendar.add(Calendar.DATE, listViewEvents.getAppData().getListViewFutureDaysLimit());

      SimpleDateFormat my = new SimpleDateFormat("yyyyMM");
      uiModel.addAttribute("selectedDate", sdf.format(selectedDate.getTime()));
      uiModel.addAttribute("monthSelectedDate", my.format(selectedDate.getTime()));
      uiModel.addAttribute("beginDate", sdf.format(beginCalendar.getTime()));
      uiModel.addAttribute("endDate", sdf.format(endCalendar.getTime()));
      uiModel.addAttribute("currentEndDate", sdf.format(currentEndDate.getTime()));
      uiModel.addAttribute("previousDate", sdf.format(previousDate.getTime()));
      uiModel.addAttribute("days", listViewEvents.getAppData().getListViewFutureDaysLimit());
      uiModel.addAttribute("viewData", listViewEvents.getViewData());
      uiModel.addAttribute("appData", listViewEvents.getAppData());
      uiModel.addAttribute("events", listViewEvents.getEvents());
      uiModel.addAttribute("filter", filter);
    } catch (PageLevelException pageLevelException) {
      uiModel.addAttribute("message", pageLevelException.getMessage());
      return "calendar/message";
    }
    return "calendar/list";
  }
 @RequestMapping(value = "/refresh", method = RequestMethod.GET)
 public String refresh(HttpServletRequest request, Model uiModel) {
   User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
   try {
     calendarEventOAuthService.reloadPersonalCache(user.getUserId());
   } catch (PageLevelException pageLevelException) {
     uiModel.addAttribute("message", pageLevelException.getMessage());
     return "calendar/message";
   }
   uiModel.addAttribute("message", "Your calendar has been refreshed.");
   return "calendar/message";
 }
 @RequestMapping(method = RequestMethod.POST)
 public String submit(
     HttpServletRequest request,
     Model uiModel,
     @ModelAttribute("command") Object command,
     BindingResult result) {
   User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
   String service = user.getUserAttribute("service");
   user.removeUserAttribute("service");
   user.setUserAttribute("acked", "true");
   return "redirect:" + service;
 }
 @RequestMapping(value = "/pendingMeetings", method = RequestMethod.GET)
 public String pendingMeetings(HttpServletRequest request, Model uiModel) {
   User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
   try {
     PendingMeetings pendingMeetings =
         calendarEventOAuthService.retrievePendingMeetings(user.getUserId());
     uiModel.addAttribute("pendingMeetings", pendingMeetings.getPendingMeetings());
   } catch (PageLevelException pageLevelException) {
     uiModel.addAttribute("message", pageLevelException.getMessage());
     return "calendar/message";
   }
   return "calendar/pendingMeetings";
 }
 @RequestMapping(value = "/selectFilter", method = RequestMethod.POST)
 public String selectFilter(
     HttpServletRequest request, Model uiModel, @ModelAttribute("filter") Filter filter) {
   User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
   try {
     if (filter.getFilterId() != null && !"".equals(filter.getFilterId().trim())) {
       Filter filterFound =
           calendarEventOAuthService.retrieveFilter(user.getUserId(), filter.getFilterId());
       request.getSession().setAttribute("calendar.event.filterId", filterFound);
     }
   } catch (PageLevelException pageLevelException) {
     uiModel.addAttribute("message", pageLevelException.getMessage());
     return "calendar/message";
   }
   return "redirect:/calendar/options";
 }
 @RequestMapping(value = "/meetingAction", method = RequestMethod.GET)
 public String meetingAction(
     HttpServletRequest request,
     Model uiModel,
     @RequestParam(required = true) Long eventId,
     @RequestParam(required = true) String type,
     @RequestParam(required = false) Long seriesId,
     @RequestParam(required = false) Long occurrenceId,
     @RequestParam(required = false) String date,
     @RequestParam(required = false) String referer) {
   User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
   try {
     if (type != null) {
       if (type.equals("R")) {
         calendarEventOAuthService.removeMeeting(user.getUserId(), eventId);
       } else if (type.equals("K")) {
         calendarEventOAuthService.keepMeeting(user.getUserId(), eventId);
       } else {
         MeetingStatusChange meetingStatusChange = new MeetingStatusChange();
         meetingStatusChange.setEventId(eventId);
         meetingStatusChange.setStatus(type);
         calendarEventOAuthService.updateMeetingStatus(user.getUserId(), meetingStatusChange);
       }
     }
   } catch (PageLevelException pageLevelException) {
     uiModel.addAttribute("message", pageLevelException.getMessage());
     return "calendar/message";
   }
   if ("D".equals(type) || "R".equals(type) || "K".equals(type)) {
     return "redirect:/calendar/month";
   } else if (seriesId != null && date != null) {
     return "redirect:/calendar/invite?eventId="
         + eventId
         + "&seriesId="
         + seriesId
         + "&date="
         + date
         + (referer != null ? "&referer=" + referer : "");
   }
   return "redirect:/calendar/invite?eventId="
       + eventId
       + (occurrenceId != null ? "&occurrenceId=" + occurrenceId : "")
       + (date != null ? "&date=" + date : "")
       + (referer != null ? "&referer=" + referer : "");
 }
  @RequestMapping(value = "/invite", method = RequestMethod.GET)
  public String invite(
      HttpServletRequest request,
      Model uiModel,
      @RequestParam(required = true) Long eventId,
      @RequestParam(required = false) String referer,
      @RequestParam(required = false) Long seriesId,
      @RequestParam(required = false) Long occurrenceId,
      @RequestParam(required = false) String date,
      @RequestParam(required = false) String occurrenceDate) {
    User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
    SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
    Date selectedDate = null;
    try {
      if (date != null) {
        selectedDate = sdf.parse(date);
      }
    } catch (ParseException e) {

    }
    try {
      MeetingInvite invite =
          calendarEventOAuthService.retrieveMeeting(
              user.getUserId(), eventId, seriesId, selectedDate);
      uiModel.addAttribute("invite", invite);
      if (occurrenceId != null) {
        uiModel.addAttribute("occurrenceId", occurrenceId);
      }
      if (occurrenceDate != null) {
        Date occurDate = null;
        try {
          occurDate = sdf.parse(occurrenceDate);
        } catch (ParseException e) {

        }
        uiModel.addAttribute("date", sdf.format(occurDate));
      }
      uiModel.addAttribute("referer", referer);
    } catch (PageLevelException pageLevelException) {
      uiModel.addAttribute("message", pageLevelException.getMessage());
      return "calendar/message";
    }
    return "calendar/invite";
  }
  @RequestMapping(value = "/filters", method = RequestMethod.GET)
  public String filters(HttpServletRequest request, Model uiModel) {
    User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
    try {
      Filters filters = calendarEventOAuthService.retrieveFilters(user.getUserId());
      uiModel.addAttribute("filters", filters.getFilters());

      Filter filter = (Filter) request.getSession().getAttribute("calendar.event.filterId");
      if (filter != null) {
        uiModel.addAttribute("filter", filter);
      } else {
        uiModel.addAttribute("filter", new Filter());
      }
    } catch (PageLevelException pageLevelException) {
      uiModel.addAttribute("message", pageLevelException.getMessage());
      return "calendar/message";
    }
    return "calendar/filters";
  }
  @RequestMapping(value = "/event", method = RequestMethod.GET)
  public String event(
      HttpServletRequest request,
      Model uiModel,
      @RequestParam(required = true) Long eventId,
      @RequestParam(required = false) String date,
      @RequestParam(required = false) Long occurrenceId) {
    User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
    SimpleDateFormat sdf = new SimpleDateFormat("MMddyyyy");
    Date selectedDate = null;
    try {
      if (date != null) {
        selectedDate = sdf.parse(date);
        if (occurrenceId != null) {
          uiModel.addAttribute("occurrenceDate", date);
          selectedDate = null;
        }
      }
    } catch (ParseException e) {

    }
    try {
      ViewDetailedEvent event =
          calendarEventOAuthService.retrieveViewEventDetails(
              user.getUserId(), eventId, selectedDate);
      // uiModel.addAttribute("selectedDate",
      // sdf.format(selectedDate.getTime()));
      if (occurrenceId != null) {
        uiModel.addAttribute("occurrenceId", occurrenceId);
      }
      uiModel.addAttribute("event", event);
    } catch (PageLevelException pageLevelException) {
      uiModel.addAttribute("message", pageLevelException.getMessage());
      return "calendar/message";
    }
    return "calendar/eventView";
  }
  @RequestMapping(value = "/month", method = RequestMethod.GET)
  public String month(
      HttpServletRequest request, Model uiModel, @RequestParam(required = false) String date) {
    User user = (User) request.getSession().getAttribute(Constants.KME_USER_KEY);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    SimpleDateFormat my = new SimpleDateFormat("yyyyMM");
    Calendar selectedDate = Calendar.getInstance();
    try {
      if (date != null) {
        selectedDate.setTime(my.parse(date));
      }
    } catch (ParseException e) {

    }
    try {
      Filter filter = (Filter) request.getSession().getAttribute("calendar.event.filterId");
      MonthViewEvents monthEvents =
          calendarEventOAuthService.retrieveMonthEvents(
              user.getUserId(),
              selectedDate.getTime(),
              filter != null ? filter.getFilterId() : null);
      uiModel.addAttribute("viewData", monthEvents.getViewData());
      uiModel.addAttribute("appData", monthEvents.getAppData());

      int days = selectedDate.getActualMaximum(Calendar.DATE);

      Calendar startDate = (Calendar) selectedDate.clone();
      startDate.set(Calendar.DATE, selectedDate.getActualMinimum(Calendar.DATE));
      days += startDate.get(Calendar.DAY_OF_WEEK) - 1;

      Calendar endDate = (Calendar) selectedDate.clone();
      endDate.set(Calendar.DATE, selectedDate.getActualMaximum(Calendar.DATE));
      days += 7 - endDate.get(Calendar.DAY_OF_WEEK);

      startDate.set(Calendar.DAY_OF_WEEK, 1);
      Map<String, MobileDayOfMonth> daysInMonth = new LinkedHashMap<String, MobileDayOfMonth>();

      uiModel.addAttribute("selectedDate", sdf.format(selectedDate.getTime()));
      for (int i = 0; i < days; i++) {
        MobileDayOfMonth mobileDayOfMonth = new MobileDayOfMonth(startDate.get(Calendar.DATE));
        mobileDayOfMonth.setCurrentMonth(
            startDate.get(Calendar.MONTH) == selectedDate.get(Calendar.MONTH));
        if (!mobileDayOfMonth.isCurrentMonth()) {
          mobileDayOfMonth.setBeforeCurrentMonth(
              startDate.get(Calendar.MONTH) < selectedDate.get(Calendar.MONTH));
        }
        mobileDayOfMonth.setMonthYear(my.format(startDate.getTime()));
        mobileDayOfMonth.setDayOfWeek(startDate.get(Calendar.DAY_OF_WEEK));
        daysInMonth.put(sdf.format(startDate.getTime()), mobileDayOfMonth);
        startDate.add(Calendar.DATE, 1);
      }

      for (Iterator iterator = monthEvents.getEvents().entrySet().iterator();
          iterator.hasNext(); ) {
        Map.Entry<String, List<CalendarViewEvent>> entry =
            (Map.Entry<String, List<CalendarViewEvent>>) iterator.next();
        MobileDayOfMonth dayInMonth = daysInMonth.get(entry.getKey());
        dayInMonth.setHasEvents(true);
        dayInMonth.setEvents(entry.getValue());
      }
      uiModel.addAttribute("events", daysInMonth);

      Calendar previousCalendar = (Calendar) selectedDate.clone();
      previousCalendar.set(Calendar.DATE, 1);
      previousCalendar.getTime();
      previousCalendar.add(Calendar.MONTH, -1);

      Calendar nextCalendar = (Calendar) selectedDate.clone();
      nextCalendar.set(Calendar.DATE, 1);
      nextCalendar.getTime();
      nextCalendar.add(Calendar.MONTH, 1);

      uiModel.addAttribute("previousMonth", my.format(previousCalendar.getTime()));
      uiModel.addAttribute("nextMonth", my.format(nextCalendar.getTime()));
      uiModel.addAttribute("monthYear", my.format(selectedDate.getTime()));
      uiModel.addAttribute("today", sdf.format(new Date()));
      uiModel.addAttribute("filter", filter);
    } catch (PageLevelException pageLevelException) {
      uiModel.addAttribute("message", pageLevelException.getMessage());
      return "calendar/message";
    }
    return "calendar/month";
  }