/**
   * Redirects to different view by checking the view type.
   *
   * @return the result code.
   * @throws Exception if there is error.
   */
  @Override
  public String execute() throws Exception {
    String resultCode = super.execute();
    if (SUCCESS.equals(resultCode)) {
      if (formData.getViewType().equals(ProjectMilestoneViewForm.LIST_VIEW)) {
        return ProjectMilestoneViewForm.LIST_VIEW;
      }
      if (formData.getViewType().equals(ProjectMilestoneViewForm.CALENDAR_VIEW)) {
        return ProjectMilestoneViewForm.CALENDAR_VIEW;
      }
      if (formData.getViewType().equals(ProjectMilestoneViewForm.MULTIPLE_CREATION_VIEW)) {
        return ProjectMilestoneViewForm.MULTIPLE_CREATION_VIEW;
      }
    }

    return resultCode;
  }
  /**
   * Main execution for the action, prepare different set of data according to the view type.
   *
   * @throws Exception if there is any error.
   */
  @Override
  protected void executeAction() throws Exception {

    // set responsible person data
    final List<ResponsiblePerson> allResponsiblePeople =
        getMilestoneResponsiblePersonService()
            .getAllResponsiblePeople(getFormData().getProjectId());
    getViewData().setResponsiblePersons(allResponsiblePeople);

    // right side bar data
    List<ProjectBriefDTO> projects =
        DataProvider.getUserProjects(getSessionData().getCurrentUserId());
    List<TypedContestBriefDTO> contests =
        DataProvider.getProjectTypedContests(
            getSessionData().getCurrentUserId(), formData.getProjectId());
    UserProjectsDTO userProjectsDTO = new UserProjectsDTO();
    userProjectsDTO.setProjects(projects);

    viewData.setUserProjects(userProjectsDTO);

    // put project into the session
    if (contests.size() > 0) {
      getSessionData().setCurrentProjectContext(contests.get(0).getProject());
    } else {
      for (ProjectBriefDTO p : projects) {
        if (p.getId() == getFormData().getProjectId()) {
          getSessionData().setCurrentProjectContext(p);
          break;
        }
      }
    }
    getSessionData().setCurrentSelectDirectProjectID(getFormData().getProjectId());

    // set project contests
    getSessionData().setCurrentProjectContests(contests);
  }
  /**
   * Gets the project milestone list view data with ajax.
   *
   * @return the result code.
   * @throws Exception if there is any error.
   * @since 1.1
   */
  public String getProjectMilestoneListData() throws Exception {
    try {

      List<Milestone> overdueMilestones =
          getMilestoneService()
              .getAll(
                  formData.getProjectId(),
                  Arrays.asList(new MilestoneStatus[] {MilestoneStatus.OVERDUE}),
                  SortOrder.ASCENDING);
      List<Milestone> upcomingMilestones =
          getMilestoneService()
              .getAll(
                  formData.getProjectId(),
                  Arrays.asList(new MilestoneStatus[] {MilestoneStatus.UPCOMING}),
                  SortOrder.ASCENDING);
      List<Milestone> completedMilestones =
          getMilestoneService()
              .getAll(
                  formData.getProjectId(),
                  Arrays.asList(new MilestoneStatus[] {MilestoneStatus.COMPLETED}),
                  SortOrder.DESCENDING);

      List<MilestoneContestDTO> milestoneContestAssociations =
          DataProvider.getMilestoneContestAssociations(
              formData.getProjectId(), 0, DirectUtils.getTCSubjectFromSession().getUserId());

      Map<Long, List<MilestoneContestDTO>> tempMapping =
          new HashMap<Long, List<MilestoneContestDTO>>();

      for (MilestoneContestDTO mcd : milestoneContestAssociations) {
        if (tempMapping.get(mcd.getMilestoneId()) == null) {
          tempMapping.put(mcd.getMilestoneId(), new ArrayList<MilestoneContestDTO>());
        }

        tempMapping.get(mcd.getMilestoneId()).add(mcd);
      }

      Map<String, List<ProjectMilestoneDTO>> result =
          new HashMap<String, List<ProjectMilestoneDTO>>();

      result.put("overdue", new ArrayList<ProjectMilestoneDTO>());
      result.put("upcoming", new ArrayList<ProjectMilestoneDTO>());
      result.put("completed", new ArrayList<ProjectMilestoneDTO>());

      for (Milestone m : overdueMilestones) {
        ProjectMilestoneDTO pmd = new ProjectMilestoneDTO();
        pmd.setMilestone(m);
        pmd.setContests(tempMapping.get(m.getId()));
        result.get("overdue").add(pmd);
      }

      for (Milestone m : upcomingMilestones) {
        ProjectMilestoneDTO pmd = new ProjectMilestoneDTO();
        pmd.setMilestone(m);
        pmd.setContests(tempMapping.get(m.getId()));
        result.get("upcoming").add(pmd);
      }

      for (Milestone m : completedMilestones) {
        ProjectMilestoneDTO pmd = new ProjectMilestoneDTO();
        pmd.setMilestone(m);
        pmd.setContests(tempMapping.get(m.getId()));
        result.get("completed").add(pmd);
      }

      ObjectMapper m = new ObjectMapper();

      setResult(m.convertValue(result, Map.class));

    } catch (Throwable e) {
      // set the error message into the ajax response
      if (getModel() != null) {
        setResult(e);
      }
      return ERROR;
    }
    return SUCCESS;
  }