/**
   * Tests <code>{@link
   * CopilotProjectDAOImpl#create(com.topcoder.direct.services.copilot.model.IdentifiableEntity)}
   * </code> method.
   *
   * <p>{@link CopilotDAOException} is expected.
   *
   * @throws CopilotDAOException if any error occurs
   */
  @Test(expected = CopilotDAOException.class)
  public void testCreateFailure1() throws CopilotDAOException {

    CopilotProject copilotProject = TestHelper.createCopilotProject();
    copilotProject.setId(1);
    hibernateTemplate.save(copilotProject.getStatus());

    instance.create(copilotProject);
  }
  /**
   * Tests <code>{@link
   * CopilotProjectDAOImpl#update(com.topcoder.direct.services.copilot.model.IdentifiableEntity)}
   * </code> method.
   *
   * <p>{@link CopilotDAOException} is expected.
   *
   * @throws CopilotDAOException if any error occurs
   */
  @Test(expected = CopilotDAOException.class)
  public void testUpdateFailure2() throws CopilotDAOException {

    CopilotProject copilotProject = TestHelper.createCopilotProject();
    copilotProject.setId(1L);

    ApplicationContext applicationContext =
        new ClassPathXmlApplicationContext("invalidApplicationContext.xml");
    CopilotProjectDAOImpl copilotProjectDAO = new CopilotProjectDAOImpl();
    copilotProjectDAO.setSessionFactory(
        (SessionFactory) applicationContext.getBean("sessionFactory"));

    copilotProjectDAO.update(copilotProject);
  }
  /**
   * The main logic to create the new project and assign permissions to the new project.
   *
   * @throws Exception if there is any error.
   */
  @Override
  protected void executeAction() throws Exception {
    TCSubject currentUser = DirectStrutsActionsHelper.getTCSubjectFromSession();

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

    // create new project first
    ProjectServiceFacade projectServiceFacade = getProjectServiceFacade();

    if (null == projectServiceFacade) {
      throw new IllegalStateException("The project service facade is not initialized");
    }

    // create the project data with the input parameters.
    ProjectData projectData = new ProjectData();
    projectData.setName(getProjectName());
    projectData.setDescription(getProjectDescription());
    // prepare the project answer.
    for (ProjectAnswer answer : getProjectData().getProjectAnswers()) {
      if (answer.getOptionAnswers() != null) {
        for (ProjectAnswerOption answerOption : answer.getOptionAnswers()) {
          answerOption.setProjectAnswer(answer);
        }
      }
      // replace XWorkList by ArrayList.
      if (answer.getMultipleAnswers() != null) {
        List<String> s = new ArrayList<String>();
        for (String mAnswer : answer.getMultipleAnswers()) {
          s.add(mAnswer);
        }
        answer.setMultipleAnswers(s);
      }
    }
    projectData.setProjectAnswers(getProjectData().getProjectAnswers());

    // set project billing account id if exists
    if (getProjectData().getProjectBillingAccountId() > 0) {

      // check if user has access to the billing account
      if (!canAccessBillingAccount(getProjectData().getProjectBillingAccountId())) {
        throw new IllegalArgumentException(
            "You don't have permission to access the billing account you set");
      }
      projectData.setProjectBillingAccountId(getProjectData().getProjectBillingAccountId());
    }

    if (forums != null) {
      Map<String, String> forumsMap = new HashMap<String, String>();
      for (ProjectForumTemplateDTO forum : forums) {
        forumsMap.put(forum.getForumName(), forum.getForumDescription());
      }
      // delegate to ProjectServiceFacade to create the project.
      projectData =
          projectServiceFacade.createTCDirectProject(
              currentUser, projectData, getPermissions(), forumsMap);
    } else {
      // delegate to ProjectServiceFacade to create the project.
      projectData =
          projectServiceFacade.createTCDirectProject(currentUser, projectData, getPermissions());
    }

    if (getProjectData().getProjectBillingAccountId() > 0) {
      DirectUtils.updateDirectProjectBugContestFee(
          DirectUtils.getTCSubjectFromSession(),
          projectData.getProjectId(),
          getProjectServiceFacade(),
          getProjectContestFeeService(),
          getProjectContestFeePercentageService());
    }

    // put data into result
    result.put("projectName", projectData.getName());
    result.put("projectId", String.valueOf(projectData.getProjectId()));

    if (!isCreateCopilotPosting()) {
      // check whether has copilots to add
      if (getCopilotIds() != null && getCopilotIds().length > 0) {
        // add the copilot into the new project
        List<CopilotProject> copilotProjects = new ArrayList<CopilotProject>();
        List<Boolean> removeFlags = new ArrayList<Boolean>();

        for (long id : getCopilotIds()) {
          removeFlags.add(false);

          CopilotProject copilotProject = new CopilotProject();
          copilotProject.setTcDirectProjectId(projectData.getProjectId());
          copilotProject.setCopilotProfileId(id);
          copilotProject.setId(0);
          copilotProjects.add(copilotProject);
        }

        // update copilots projects
        getContestServiceFacade().updateCopilotProjects(currentUser, copilotProjects, removeFlags);
      }

      if (presentationProject) {
        // create the draft copilot contest
        createPPTCopilotDraftPosting(projectData);
        // create JIRA issue
        Map<String, Object> conetstResult = (Map<String, Object>) getResult();
        String description =
            "Copilot Opportunities: " + copilotURLPrefix + conetstResult.get("projectId");
        JiraRpcServiceWrapper.createIssue(
            pptJIRAProject, pptJIRAIssueTypeId, projectName, description, pptJIRAIssueReporter);
      }
    } else {
      createCopilotDraftPosting(projectData);
    }

    setResult(result);
  }
 /**
  * Tests <code>{@link
  * CopilotProjectDAOImpl#update(com.topcoder.direct.services.copilot.model.IdentifiableEntity)}
  * </code> method when entity has negative id.
  *
  * <p>{@link IllegalArgumentException} is expected.
  *
  * @throws CopilotDAOException if any error occurs
  */
 @Test(expected = IllegalArgumentException.class)
 public void testUpdateNegative() throws CopilotDAOException {
   CopilotProject copilotProject = new CopilotProject();
   copilotProject.setId(-1);
   instance.update(copilotProject);
 }