/**
   * Method to handle the ajax operation getProjectResponsiblePerson.
   *
   * <p>Gets all the responsible person of the project and return as json.
   *
   * @return the result code.
   * @throws Exception if error.
   */
  public String getProjectResponsiblePerson() throws Exception {
    List<Map<String, Object>> result = new LinkedList<Map<String, Object>>();

    try {
      List<ResponsiblePerson> allResponsiblePeople =
          getMilestoneResponsiblePersonService().getAllResponsiblePeople(getDirectProjectId());

      for (ResponsiblePerson p : allResponsiblePeople) {

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("userId", p.getUserId());
        data.put("name", p.getName());
        result.add(data);
      }

      setResult(result);

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

      return ERROR;
    }

    return SUCCESS;
  }
  /**
   * Gets the project milestones calendar dat via ajax.
   *
   * @return the result code.
   * @throws Exception if there is any error.
   */
  public String getProjectMilestoneCalendarData() throws Exception {
    List<Map<String, Object>> result = new LinkedList<Map<String, Object>>();

    try {
      final List<Milestone> allMilestones =
          this.getMilestoneService()
              .getAll(getFormData().getProjectId(), ALL_MILESTONE_STATUS, SortOrder.ASCENDING);

      for (Milestone m : allMilestones) {
        Map<String, Object> data = new HashMap<String, Object>();

        data.put("title", m.getName());
        data.put("status", m.getStatus().toString().toLowerCase());
        if (m.isCompleted()) {
          data.put("start", CALENDAR_DATE_FORMAT.format(m.getCompletionDate()));
        } else {
          data.put("start", CALENDAR_DATE_FORMAT.format(m.getDueDate()));
        }

        data.put("description", m.getDescription());

        if (m.getOwners() != null && m.getOwners().size() > 0) {
          ResponsiblePerson rp = m.getOwners().get(0);

          if (rp != null) {
            Map<String, Object> person = new HashMap<String, Object>();
            person.put("name", rp.getName());
            person.put("color", DEFAULT_USER_HANDLE_CLASS);
            person.put("url", DEFAULT_USER_HANDLE_URL);

            data.put("person", person);
          }
        }

        result.add(data);
      }
      setResult(result);

    } catch (Throwable e) {
      // set the error message into the ajax response
      if (getModel() != null) {
        setResult(e);
      }
      return ERROR;
    }
    return SUCCESS;
  }
  /**
   * This method gets all responsible people for the given project ID. If none found, returns an
   * empty list.
   *
   * <p>Updates in version 1.1 - removes the reference to DataProvider and
   * DirectProjectMetadataService. Use native hibernate query to get the copilots and managers.
   *
   * @param projectId the ID of the direct project
   * @return the responsible people
   * @throws ProjectMilestoneManagementException If there are any errors during the execution of
   *     this method
   */
  @Transactional(readOnly = true)
  public List<ResponsiblePerson> getAllResponsiblePeople(long projectId)
      throws ProjectMilestoneManagementException {
    final String signature = CLASS_NAME + ".getAllResponsiblePeople(long projectId)";
    final Date entranceTimestamp = new Date();
    // Log entrance
    LoggingWrapperUtility.logEntrance(
        logger, signature, new String[] {"projectId"}, new Object[] {projectId}, true, Level.DEBUG);

    try {

      // Create list for responsible people:
      List<ResponsiblePerson> people = new ArrayList<ResponsiblePerson>();
      Set<Long> duplicationChecking = new HashSet<Long>();

      List copilotList =
          getSession()
              .getNamedQuery(GET_COPILOTS_FOR_PROJECT)
              .setParameter(DIRECT_PROJECT_ID_PARAM, projectId)
              .list();

      for (Object o : copilotList) {
        ResponsiblePerson p = (ResponsiblePerson) o;
        if (!duplicationChecking.contains(p.getUserId())) {
          people.add(p);
          duplicationChecking.add(p.getUserId());
        }
      }

      List managersList =
          sessionFactory
              .getCurrentSession()
              .getNamedQuery(GET_MANAGERS_FOR_PROJECT)
              .setParameter(DIRECT_PROJECT_ID_PARAM, projectId)
              .list();

      for (Object o : managersList) {
        ResponsiblePerson p = (ResponsiblePerson) o;
        if (!duplicationChecking.contains(p.getUserId())) {
          people.add(p);
          duplicationChecking.add(p.getUserId());
        }
      }

      // update all the responsible person id to 0, because they are using for looking up
      for (ResponsiblePerson p : people) {
        p.setId(0);
      }

      // Log exit
      LoggingWrapperUtility.logExit(logger, signature, new Object[] {people}, entranceTimestamp);

      // Return people
      return people;
    } catch (NumberFormatException e) {
      throw LoggingWrapperUtility.logException(
          logger,
          signature,
          new ProjectMilestoneManagementException(
              "NumberFormatException occurs while parsing long to string", e),
          true,
          Level.ERROR);
    } catch (Exception e) {
      throw LoggingWrapperUtility.logException(
          logger,
          signature,
          new ProjectMilestoneManagementException(
              "Exception occurs while getting all ResponsiblePeople", e),
          true,
          Level.ERROR);
    }
  }