/**
   * @see
   *     org.springframework.web.servlet.mvc.AbstractController#handleRequestInternal(javax.servlet.http.HttpServletRequest,
   *     javax.servlet.http.HttpServletResponse)
   */
  @Override
  protected ModelAndView handleRequestInternal(
      HttpServletRequest request, HttpServletResponse response) throws Exception {

    String outResponse = "";
    User user = ControllerUtil.getSignedInUser();
    Set<User> owners = new TreeSet<User>();
    owners.add(user);

    Project project = projectService.getById(Long.parseLong(request.getParameter(PROJECTID)));

    CreateOtmlModuleParameters params = new CreateOtmlModuleParameters();
    params.setName(project.getCurnit().getSdsCurnit().getName());
    params.setUrl(RooloOtmlModuleDao.defaultOtrunkCurnitUrl);
    params.setRetrieveotmlurl(
        Util.getPortalUrl(request) + "/repository/retrieveotml.html?otmlModuleId=");
    byte[] otmlbytes = (byte[]) project.getCurnit().accept(new CurnitGetOtmlVisitor());
    if (otmlbytes != null) {
      params.setOtml(otmlbytes);
      Curnit copiedCurnit = curnitService.createCurnit(params);

      ProjectParameters projParams = new ProjectParameters();

      projParams.setCurnitId(copiedCurnit.getId());
      projParams.setJnlpId(project.getJnlp().getId());
      projParams.setOwners(owners);
      projParams.setProjectname(project.getName());
      projParams.setProjectType(project.getProjectType());

      projectService.createProject(projParams);

      outResponse =
          "Project "
              + project.getName()
              + " has been successfully "
              + "copied and can be found in My Customized Projects.";
    } else {
      outResponse = "This project is not of a type that can be copied.";
    }

    ModelAndView modelAndView = new ModelAndView();
    modelAndView.addObject(RESPONSE, outResponse);
    return modelAndView;
  }
  /**
   * @see org.springframework.validation.Validator#validate(java.lang.Object,
   *     org.springframework.validation.Errors)
   */
  public void validate(Object paramsIn, Errors errors) {
    FindProjectParameters param = (FindProjectParameters) paramsIn;

    /* there should be exactly on param field with data, reject
     * if less or more than one has data */
    int numWithData = 0;

    /* check and validate the project Id field */
    if (param.getProjectId() != null && !param.getProjectId().equals("")) {
      numWithData += 1;

      /* make sure project id is numeric */
      if (!StringUtils.isNumeric(param.getProjectId())) {
        errors.reject("error.projectId-not-numeric", "Project ID must be numeric.");
        return;
      }

      /* make sure that a project with the id exists */
      try {
        Project project = projectService.getById(Long.parseLong(param.getProjectId()));
        if (project == null) {
          errors.reject("error.projectId-not-found", "Project ID not found.");
          return;
        }
      } catch (Exception e) {
        errors.reject("error.projectId-not-found", "Project ID not found.");
        return;
      }
    }

    /* check and validate the userName field */
    if (param.getUserName() != null && !param.getUserName().equals("")) {
      numWithData += 1;

      /* make sure that a user with that name exists */
      if (userService.retrieveUserByUsername(param.getUserName()) == null) {
        errors.rejectValue("userName", "error.teacher-not-found");
      }
    }

    /* check and validate the runId field */
    if (param.getRunId() != null && !param.getRunId().equals("")) {
      numWithData += 1;

      /* make sure run id is numeric */
      if (!StringUtils.isNumeric(param.getRunId())) {
        errors.reject("error.runId-not-numeric", "Run ID must be numeric.");
        return;
      }

      /* make sure that a run with the given id exists */
      try {
        Run run = runService.retrieveById(Long.parseLong(param.getRunId()));
        if (run == null) {
          errors.reject("error.runId-not-found", "Run ID not found.");
          return;
        }
      } catch (ObjectNotFoundException e) {
        errors.reject("error.runId-not-found", "Run ID not found.");
        return;
      }
    }

    /* ensure that exactly one field was specified */
    if (numWithData != 1) {
      errors.reject("error.invalid-parameters", "Parameters passed to controller are invalid.");
    }
  }