public ActionForward createParticipator(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws FenixServiceException {

    ResearchResult result = getResultFromRequest(request);
    ResultParticipationCreationBean bean =
        (ResultParticipationCreationBean)
            RenderUtils.getViewState("beanForExternalPerson").getMetaObject().getObject();

    if (bean.getOrganization() != null) {

      ExternalContract contract =
          InsertExternalPerson.run(
              new InsertExternalPerson.ServiceArguments(
                  bean.getParticipatorName(), bean.getOrganization()));
      bean.setParticipator(contract.getPerson().getPersonName());
      createParticipation(bean);
    } else {
      request.setAttribute("createUnit", true);
      request.setAttribute("duringCreation", true);
      request.setAttribute("bean", bean);
      setResParticipationRequestAttributes(request, result, bean);
      RenderUtils.invalidateViewState();
      return mapping.findForward("editParticipation");
    }
    return prepareEdit(mapping, form, request, response);
  }
  private void schemasStateAutomaton(
      HttpServletRequest request,
      ResearchResult result,
      ResultParticipationCreationBean bean,
      boolean automatonIsInUnitSelection) {

    String resultParticipationsSchema =
        result.getIsPossibleSelectPersonRole()
            ? "resultParticipation.full"
            : "resultParticipation.withoutRole";
    request.setAttribute("listSchema", resultParticipationsSchema);

    if (!automatonIsInUnitSelection) {
      String createResultParticipationSchema = "resultParticipation.creation";
      if (result.getIsPossibleSelectPersonRole()) {
        createResultParticipationSchema += "WithRole";
      }
      createResultParticipationSchema += (bean.isBeanExternal()) ? ".external" : ".internal";
      request.setAttribute("createSchema", createResultParticipationSchema);
    } else {
      bean.setUnitParticipationType(ParticipationType.EXTERNAL);
      String createResultParticipationSchema = "resultParticipation.fullCreation";
      if (result.getIsPossibleSelectPersonRole()) {
        createResultParticipationSchema += "WithRole";
      }
      createResultParticipationSchema += ".external.readOnly";
      request.setAttribute("createSchema", createResultParticipationSchema);
    }
  }
 public ActionForward changeUnitType(
     ActionMapping mapping,
     ActionForm form,
     HttpServletRequest request,
     HttpServletResponse response)
     throws FenixServiceException {
   final ResultParticipationCreationBean bean = getRenderedObject("bean");
   ResearchResult result = bean.getResult();
   request.setAttribute("bean", bean);
   request.setAttribute("result", result);
   schemasStateAutomaton(request, result, bean, true);
   checkRolesInCreation(request);
   RenderUtils.invalidateViewState("bean");
   return mapping.findForward("editParticipation");
 }
  public ActionForward unitWrapper(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws FenixServiceException {

    ResultParticipationCreationBean bean =
        (ResultParticipationCreationBean)
            RenderUtils.getViewState("beanForExternalPerson").getMetaObject().getObject();

    if (bean.getOrganization() == null) {
      return createUnit(mapping, form, request, response);
    } else {
      return createParticipator(mapping, form, request, response);
    }
  }
  public ActionForward createUnit(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws FenixServiceException {

    getResultFromRequest(request);

    ResultParticipationCreationBean bean =
        (ResultParticipationCreationBean)
            RenderUtils.getViewState("beanForExternalPerson").getMetaObject().getObject();

    Unit unit = CreateExternalUnitByName.run(bean.getOrganizationName());
    bean.setOrganization(unit);
    createParticipation(bean);

    return prepareEdit(mapping, form, request, response);
  }
  public ActionForward prepareCreateParticipator(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws FenixServiceException {

    ResearchResult result = getResultFromRequest(request);
    ResultParticipationCreationBean bean =
        (ResultParticipationCreationBean)
            RenderUtils.getViewState("bean").getMetaObject().getObject();
    bean.setUnitParticipationType(ParticipationType.EXTERNAL);

    setResParticipationRequestAttributes(request, result, bean);
    request.setAttribute("bean", bean);
    if (bean.getParticipator() != null) {
      request.setAttribute("needToCreatePerson", true);
      addActionMessage(request, "label.cannot.select.person.to.create.new");
      return mapping.findForward("editParticipation");
    }
    request.setAttribute("duringCreation", true);
    request.setAttribute("createUnit", true);
    return mapping.findForward("editParticipation");
  }
  public ActionForward create(
      ActionMapping mapping,
      ActionForm form,
      HttpServletRequest request,
      HttpServletResponse response)
      throws FenixServiceException {
    final ResultParticipationCreationBean bean = getRenderedObject("bean");

    request.setAttribute("bean", bean);
    try {
      if (bean.getParticipator() == null) {
        if (bean.isBeanExternal()) {
          request.setAttribute("name", bean.getParticipatorName());
          request.setAttribute("needToCreatePerson", true);
          schemasStateAutomaton(request, bean.getResult(), bean, false);
        } else {
          throw new DomainException("error.label.invalidNameForPersonInSelection");
        }
      } else {
        createParticipation(bean);
        schemasStateAutomaton(request, bean.getResult(), bean, false);
      }
    } catch (Exception e) {
      final ActionForward defaultForward = backToResultList(mapping, form, request, response);
      if (bean != null) {
        bean.reset();
      }
      return processException(request, mapping, defaultForward, e);
    }

    // bean.reset();
    checkRolesInCreation(request);
    request.setAttribute("bean", bean);
    request.setAttribute("result", bean.getResult());
    RenderUtils.invalidateViewState();
    return mapping.findForward("editParticipation");
  }
  private void checkNeededSchemas(
      HttpServletRequest request, ResearchResult result, ResultParticipationCreationBean bean) {
    String resultParticipationsSchema = "resultParticipation.withoutRole";
    String createResultParticipationSchema = "resultParticipation.creation";

    if ((bean.getParticipator() != null
            || (bean.getParticipatorName() != null && bean.getParticipatorName().length() > 0))
        && (bean.isBeanExternal() || bean.isUnitExternal())) {
      createResultParticipationSchema = "resultParticipation.fullCreation";
    }

    // Defining schemas with roles
    if (result.getIsPossibleSelectPersonRole()) {
      resultParticipationsSchema = "resultParticipation.full";
      if (!bean.isBeanExternal()) {
        createResultParticipationSchema = "resultParticipation.creationWithRole";
      } else {
        if (bean.getParticipator() == null) {
          if (bean.getParticipatorName().length() == 0) {
            createResultParticipationSchema = "resultParticipation.creationWithRole";
          } else {
            createResultParticipationSchema = "resultParticipation.fullCreationWithRole";
          }
        } else {
          bean.setOrganization(
              bean.getParticipator().getPerson().getExternalContract().getInstitutionUnit());
          createResultParticipationSchema = "resultParticipation.creationWithRole";
        }
      }
    }

    createResultParticipationSchema += (bean.isBeanExternal()) ? ".external" : ".internal";

    request.setAttribute("listSchema", resultParticipationsSchema);
    request.setAttribute("createSchema", createResultParticipationSchema);
  }