/**
   * @param ownerIdentifier
   * @param model
   * @param fbo
   * @param bindingResult
   * @return
   * @throws NotAVisitorException
   * @throws OwnerNotFoundException
   * @throws SchedulingException
   */
  @RequestMapping(method = RequestMethod.POST)
  protected String createAppointment(
      final ModelMap model,
      @PathVariable("ownerIdentifier") final String ownerIdentifier,
      @Valid @ModelAttribute(COMMAND_ATTR_NAME) final CreateAppointmentFormBackingObject fbo,
      BindingResult bindingResult)
      throws NotAVisitorException, OwnerNotFoundException, SchedulingException {
    CalendarAccountUserDetailsImpl currentUser =
        (CalendarAccountUserDetailsImpl)
            SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    IScheduleVisitor visitor = currentUser.getScheduleVisitor();

    if (bindingResult.hasErrors()) {
      return "visitor/create-appointment-form";
    }

    IScheduleOwner selectedOwner = null;
    if (StringUtils.isNumeric(ownerIdentifier)) {
      Long ownerId = Long.parseLong(ownerIdentifier);
      selectedOwner = findOwnerForVisitor(visitor, ownerId);
    } else {
      PublicProfile profile = publicProfileDao.locatePublicProfileByKey(ownerIdentifier);
      if (null != profile) {
        selectedOwner = ownerDao.locateOwnerByAvailableId(profile.getOwnerId());
      }
    }

    if (null == selectedOwner) {
      throw new OwnerNotFoundException("no owner found for " + ownerIdentifier);
    }

    validateChosenStartTime(
        selectedOwner.getPreferredVisibleWindow(), fbo.getTargetBlock().getStartTime());

    AvailableBlock finalAppointmentBlock = fbo.getTargetBlock();
    if (fbo.isDoubleLengthAvailable()) {
      // check if selected meeting duration matches meeting durations maxLength
      // if it's greater, then we need to look up the next block in the schedule and attempt to
      // combine
      if (fbo.getSelectedDuration() == fbo.getMeetingDurations().getMaxLength()) {
        finalAppointmentBlock =
            availableScheduleDao.retrieveTargetDoubleLengthBlock(
                selectedOwner, finalAppointmentBlock.getStartTime());
      }
    }
    if (null == finalAppointmentBlock) {
      throw new SchedulingException("requested time is not available");
    }

    VEvent event =
        schedulingAssistantService.scheduleAppointment(
            visitor, selectedOwner, finalAppointmentBlock, fbo.getReason());
    model.put("event", event);
    model.put("owner", selectedOwner);
    model.put("ownerRemindersPreference", selectedOwner.getRemindersPreference());
    return "visitor/create-appointment-success";
  }
  /**
   * @param ownerId
   * @param highContrast
   * @param visitorUsername
   * @return
   * @throws NotAVisitorException
   * @throws CalendarUserNotFoundException
   */
  @RequestMapping(
      value = "/admin/schedule-debug/{ownerIdentifier}/view.html",
      method = RequestMethod.GET)
  public String displaySchedule(
      @PathVariable("ownerIdentifier") long ownerIdentifier,
      @RequestParam(value = "highContrast", required = false, defaultValue = "false")
          boolean highContrast,
      @RequestParam(value = "weekStart", required = false, defaultValue = "0") int weekStart,
      @RequestParam(value = "visitorUsername", required = true) String visitorUsername,
      ModelMap model)
      throws NotAVisitorException, CalendarAccountNotFoundException {

    ICalendarAccount visitorAccount = this.calendarAccountDao.getCalendarAccount(visitorUsername);
    if (visitorAccount == null) {
      throw new NotAVisitorException(visitorUsername + " not found");
    }
    IScheduleVisitor visitor = this.visitorDao.toVisitor(visitorAccount);
    model.addAttribute("visitor", visitor);
    IScheduleOwner selectedOwner = ownerDao.locateOwnerByAvailableId(ownerIdentifier);
    if (selectedOwner == null) {
      throw new CalendarAccountNotFoundException("no owner found for id " + ownerIdentifier);
    }
    VisibleScheduleRequestConstraints requestConstraints =
        VisibleScheduleRequestConstraints.newInstance(selectedOwner, weekStart);
    if (LOG.isDebugEnabled()) {
      LOG.debug(
          "displaySchedule request, visitor: "
              + visitor
              + "; weekStart: "
              + weekStart
              + "requestConstraints "
              + requestConstraints);
    }

    Calendar ownerCalendar =
        this.calendarDataDao.getCalendar(
            selectedOwner.getCalendarAccount(),
            requestConstraints.getTargetStartDate(),
            requestConstraints.getTargetEndDate());
    model.addAttribute("ownerCalendarData", ownerCalendar.toString());
    model.addAttribute("noteboard", selectedOwner.getPreference(Preferences.NOTEBOARD));
    model.addAttribute("owner", selectedOwner);
    model.addAttribute("highContrast", highContrast);

    VisibleSchedule schedule;

    if (selectedOwner.hasMeetingLimit()) {
      // we have to look at the whole visible schedule for attendings
      schedule = schedulingAssistantService.getVisibleSchedule(visitor, selectedOwner);
      if (selectedOwner.isExceedingMeetingLimit(schedule.getAttendingCount())) {
        // return attending only view
        List<AvailableBlock> attendingList = schedule.getAttendingList();
        model.addAttribute("attendingList", attendingList);
        return "admin/debug-already-attending";
      } else {
        // extract start->end from visibleSchedule
        schedule =
            schedule.subset(
                requestConstraints.getTargetStartDate(), requestConstraints.getTargetEndDate());
      }

    } else {
      // only pull start->end of schedule
      schedule =
          schedulingAssistantService.getVisibleSchedule(
              visitor,
              selectedOwner,
              requestConstraints.getTargetStartDate(),
              requestConstraints.getTargetEndDate());
    }

    model.addAttribute("visibleSchedule", schedule);
    model.addAttribute("scheduleStart", schedule.getScheduleStart());
    model.addAttribute("prevWeekStart", requestConstraints.getPrevWeekIndex());
    model.addAttribute("nextWeekStart", requestConstraints.getNextWeekIndex());
    model.addAttribute("weekStart", requestConstraints.getConstrainedWeekStart());
    model.addAttribute("ownerVisitorSamePerson", selectedOwner.isSamePerson(visitor));
    return "admin/debug-visible-schedule";
  }
  /**
   * @param model
   * @param startTimePhrase
   * @param ownerId
   * @return
   * @throws InputFormatException
   * @throws SchedulingException
   * @throws OwnerNotFoundException
   * @throws NotAVisitorException
   */
  @RequestMapping(method = RequestMethod.GET)
  protected String setupForm(
      final ModelMap model,
      @RequestParam(value = "startTime", required = true) final String startTimePhrase,
      @PathVariable("ownerIdentifier") final String ownerIdentifier)
      throws InputFormatException, SchedulingException, OwnerNotFoundException,
          NotAVisitorException {
    CalendarAccountUserDetailsImpl currentUser =
        (CalendarAccountUserDetailsImpl)
            SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    IScheduleVisitor visitor = currentUser.getScheduleVisitor();

    IScheduleOwner selectedOwner = null;
    if (StringUtils.isNumeric(ownerIdentifier)) {
      Long ownerId = Long.parseLong(ownerIdentifier);
      selectedOwner = findOwnerForVisitor(visitor, ownerId);
    } else {
      PublicProfile profile = publicProfileDao.locatePublicProfileByKey(ownerIdentifier);
      if (null != profile) {
        selectedOwner = ownerDao.locateOwnerByAvailableId(profile.getOwnerId());
      }
    }

    if (null == selectedOwner) {
      throw new OwnerNotFoundException("no owner found for " + ownerIdentifier);
    }
    model.put("owner", selectedOwner);
    Date startTime = CommonDateOperations.parseDateTimePhrase(startTimePhrase);
    validateChosenStartTime(selectedOwner.getPreferredVisibleWindow(), startTime);

    AvailableBlock targetBlock = availableScheduleDao.retrieveTargetBlock(selectedOwner, startTime);
    if (null == targetBlock) {
      throw new SchedulingException("requested time is not available");
    }

    if (selectedOwner.hasMeetingLimit()) {
      VisibleSchedule sched = schedulingAssistantService.getVisibleSchedule(visitor, selectedOwner);
      int attendingCount = sched.getAttendingCount();
      if (selectedOwner.isExceedingMeetingLimit(attendingCount)) {
        // visitor has already matched owner's appointment limit
        LOG.warn(
            "blocked attempt to use create form by visitor: "
                + visitor
                + ", target owner: "
                + selectedOwner);
        return "redirect:view.html";
      }
    }

    VEvent event = schedulingAssistantService.getExistingAppointment(targetBlock, selectedOwner);
    if (event != null) {
      model.put("event", event);
      if (this.eventUtils.isAttendingAsVisitor(event, visitor.getCalendarAccount())) {
        // redirect the visitor to the cancel form
        StringBuilder redirect = new StringBuilder("redirect:cancel.html?r=true&startTime=");
        SimpleDateFormat dateFormat = CommonDateOperations.getDateTimeFormat();
        redirect.append(startTimePhrase);
        redirect.append("&endTime=");
        redirect.append(dateFormat.format(targetBlock.getEndTime()));
        return redirect.toString();
      }

      Integer visitorLimit = this.eventUtils.getEventVisitorLimit(event);
      model.put("visitorLimit", visitorLimit);
      if (this.eventUtils.getScheduleVisitorCount(event) >= visitorLimit) {
        return "visitor/appointment-full";
      }
    }
    CreateAppointmentFormBackingObject fbo =
        new CreateAppointmentFormBackingObject(
            targetBlock, selectedOwner.getPreferredMeetingDurations());
    model.addAttribute(COMMAND_ATTR_NAME, fbo);
    return "visitor/create-appointment-form";
  }