/*
   * ############################
   * Form events
   * ############################
   */
  public void doSubmit(StaplerRequest req, StaplerResponse rsp)
      throws ServletException, IOException {
    checkPermission(PERMISSION);

    FormData formData = FormData.parse(req.getSubmittedForm());
    ParsedFormData parsedFormData = isValid(formData);
    if (parsedFormData.getStatus() == ParsedFormData.Status.OK) {
      BootstrapExecutor executor;
      executor = new BootstrapExecutor(parsedFormData);
      Log log = executor.execute();

      Flash flash = Flash.getForRequest(req);
      flash.put(FlashKeys.BOOTSTRAP_LOG, log);

      rsp.sendRedirect("/" + URL + "/" + BootstrapResultAction.URL);
    } else {
      req.setAttribute("error", true);
      formData.setFormDataAsAttributesOn(req);
      // leading and training slash as Jenkins otherwise issues an
      // HTTP status code 301 (moved permanently), which would then
      // result in the loss of the request attributes.
      req.getRequestDispatcher("/" + URL + "/").forward(req, rsp);
    }
  }
  private ParsedFormData isValid(FormData formData) {
    ParsedFormData result;
    result = new ParsedFormData(ParsedFormData.Status.ERROR);

    if (!isValidName(formData.getName())
        || !isValidPckg(formData.getPckg())
        || !isValidDescription(formData.getDescription())) {
      return result;
    }

    Project project = new Project();
    project.setName(formData.getName());
    project.setDescription(formData.getDescription());
    project.setPckg(formData.getPckg());
    result.setProject(project);

    VCSConfiguration vcsConfig;
    try {
      vcsConfig = getVCSConfig(formData);
    } catch (IllegalArgumentException ex) {
      return result;
    }
    result.setVcsConfiguration(vcsConfig);

    CIConfiguration ciConfig;
    try {
      ciConfig = getCIConfig(formData);
    } catch (IllegalArgumentException ex) {
      return result;
    }
    result.setCiConfiguration(ciConfig);

    JiraConfiguration jiraConfig;
    try {
      jiraConfig = getJiraConfig(formData);
    } catch (IllegalArgumentException ex) {
      return result;
    }
    result.setJiraConfiguration(jiraConfig);

    if (isValidGroupName(formData.getJiraGroupName()) != null) {
      return result;
    }
    result.setJiraGroupName(formData.getJiraGroupName());

    if (!isValidJiraPermissionScheme(jiraConfig, formData.getJiraPermissionScheme())) {
      return result;
    }
    result.setJiraPermissionScheme(formData.getJiraPermissionScheme());

    if (isValidJIRAProjectKey(formData.getJiraConfigName(), formData.getJiraProjectKey()) != null) {
      return result;
    }
    result.setJiraProjectKey(formData.getJiraProjectKey());

    if (formData.getUsers().isEmpty()) {
      return result;
    }
    result.setJiraUsers(formData.getUsers());

    CatalogEntry scaffold;
    try {
      scaffold = getScaffold(formData);
    } catch (IllegalArgumentException ex) {
      return result;
    }
    result.setScaffold(scaffold);

    if (isRequiredContextFilledIn(scaffold, formData)) {
      result.setContext(formData.getContextParameters());
      result.setStatus(ParsedFormData.Status.OK);
    }

    return result;
  }