public FormDTO findStartEventForm(FirstTaskForm firstTaskForm) {
    ProcessDefinition processDefinition =
        processEngine
            .getRepositoryService()
            .createProcessDefinitionQuery()
            .processDefinitionId(firstTaskForm.getProcessDefinitionId())
            .singleResult();
    List<BpmConfForm> bpmConfForms =
        bpmConfFormManager.find(
            "from BpmConfForm where bpmConfNode.bpmConfBase.processDefinitionId=? and bpmConfNode.code=?",
            firstTaskForm.getProcessDefinitionId(),
            firstTaskForm.getActivityId());
    FormDTO formDto = new FormDTO();
    formDto.setProcessDefinitionId(firstTaskForm.getProcessDefinitionId());
    formDto.setActivityId(firstTaskForm.getActivityId());
    formDto.setCode(firstTaskForm.getFormKey());

    if (!bpmConfForms.isEmpty()) {
      BpmConfForm bpmConfForm = bpmConfForms.get(0);

      if (!Integer.valueOf(2).equals(bpmConfForm.getStatus())) {
        if (Integer.valueOf(1).equals(bpmConfForm.getType())) {
          formDto.setRedirect(true);
          formDto.setUrl(bpmConfForm.getValue());
        } else {
          formDto.setCode(bpmConfForm.getValue());
        }
      }
    } else {
      logger.info(
          "cannot find bpmConfForm : {}, {}",
          firstTaskForm.getProcessDefinitionId(),
          formDto.getActivityId());
    }

    FormDTO contentFormDto =
        formConnector.findForm(formDto.getCode(), processDefinition.getTenantId());

    if (contentFormDto == null) {
      logger.error("cannot find form : {}", formDto.getCode());

      return formDto;
    }

    formDto.setRedirect(contentFormDto.isRedirect());
    formDto.setUrl(contentFormDto.getUrl());
    formDto.setContent(contentFormDto.getContent());

    return formDto;
  }
  public FirstTaskForm findStartEventForm(ProcessDefinitionEntity processDefinitionEntity) {
    FirstTaskForm firstTaskForm = new FirstTaskForm();
    firstTaskForm.setExists(true);
    firstTaskForm.setProcessDefinitionId(processDefinitionId);
    firstTaskForm.setTaskForm(false);

    DefaultFormHandler formHandler =
        (DefaultFormHandler) processDefinitionEntity.getStartFormHandler();

    if (formHandler.getFormKey() != null) {
      String formKey = formHandler.getFormKey().getExpressionText();
      firstTaskForm.setFormKey(formKey);
      firstTaskForm.setActivityId(processDefinitionEntity.getInitial().getId());
    }

    return firstTaskForm;
  }
  public FirstTaskForm execute(CommandContext commandContext) {
    ProcessDefinitionEntity processDefinitionEntity =
        Context.getProcessEngineConfiguration()
            .getDeploymentManager()
            .findDeployedProcessDefinitionById(processDefinitionId);

    if (processDefinitionEntity == null) {
      throw new IllegalArgumentException("cannot find processDefinition : " + processDefinitionId);
    }

    if (processDefinitionEntity.hasStartFormKey()) {
      return this.findStartEventForm(processDefinitionEntity);
    }

    ActivityImpl startActivity = processDefinitionEntity.getInitial();

    if (startActivity.getOutgoingTransitions().size() != 1) {
      throw new IllegalStateException(
          "start activity outgoing transitions cannot more than 1, now is : "
              + startActivity.getOutgoingTransitions().size());
    }

    PvmTransition pvmTransition = startActivity.getOutgoingTransitions().get(0);
    PvmActivity targetActivity = pvmTransition.getDestination();

    if (!"userTask".equals(targetActivity.getProperty("type"))) {
      logger.info("first activity is not userTask, just skip");

      return new FirstTaskForm();
    }

    FirstTaskForm firstTaskForm = new FirstTaskForm();
    firstTaskForm.setProcessDefinitionId(processDefinitionId);
    firstTaskForm.setExists(true);
    firstTaskForm.setTaskForm(true);

    String taskDefinitionKey = targetActivity.getId();
    logger.debug("activityId : {}", targetActivity.getId());
    firstTaskForm.setActivityId(taskDefinitionKey);

    TaskDefinition taskDefinition =
        processDefinitionEntity.getTaskDefinitions().get(taskDefinitionKey);

    Expression expression = taskDefinition.getAssigneeExpression();

    if (expression != null) {
      String expressionText = expression.getExpressionText();
      logger.debug("{}", expressionText);
      logger.debug("{}", startActivity.getProperties());
      logger.debug("{}", processDefinitionEntity.getProperties());
      firstTaskForm.setAssignee(expressionText);
    } else {
      logger.info("cannot find expression : {}, {}", processDefinitionId, taskDefinitionKey);
    }

    String initiatorVariableName =
        (String)
            processDefinitionEntity.getProperty(BpmnParse.PROPERTYNAME_INITIATOR_VARIABLE_NAME);
    firstTaskForm.setInitiatorName(initiatorVariableName);

    DefaultFormHandler formHandler = (DefaultFormHandler) taskDefinition.getTaskFormHandler();

    if (formHandler.getFormKey() != null) {
      String formKey = formHandler.getFormKey().getExpressionText();
      firstTaskForm.setFormKey(formKey);
    } else {
      logger.info("cannot formKey : {}, {}", processDefinitionId, taskDefinitionKey);
    }

    return firstTaskForm;
  }
  /** 获得启动表单. */
  public FormDTO findStartForm(String processDefinitionId) {
    ProcessDefinition processDefinition =
        processEngine
            .getRepositoryService()
            .createProcessDefinitionQuery()
            .processDefinitionId(processDefinitionId)
            .singleResult();
    FirstTaskForm firstTaskForm =
        processEngine
            .getManagementService()
            .executeCommand(new FindFirstTaskFormCmd(processDefinitionId));

    if ((!firstTaskForm.isExists()) && (firstTaskForm.getActivityId() != null)) {
      // 再从数据库里找一遍配置
      com.mossle.spi.humantask.FormDTO humantaskFormDto =
          taskDefinitionConnector.findForm(firstTaskForm.getActivityId(), processDefinitionId);

      if (humantaskFormDto != null) {
        firstTaskForm.setFormKey(humantaskFormDto.getKey());
      }
    }

    if (!firstTaskForm.isExists()) {
      logger.info("cannot find startForm : {}", processDefinitionId);

      return new FormDTO();
    }

    if (!firstTaskForm.isTaskForm()) {
      logger.info("find startEventForm : {}", processDefinitionId);

      return this.findStartEventForm(firstTaskForm);
    }

    List<TaskUserDTO> taskUserDtos =
        taskDefinitionConnector.findTaskUsers(
            firstTaskForm.getActivityId(), firstTaskForm.getProcessDefinitionId());
    String assignee = firstTaskForm.getAssignee();
    logger.debug("assignee : {}", assignee);

    for (TaskUserDTO taskUserDto : taskUserDtos) {
      logger.debug("catalog : {}, user : {}", taskUserDto.getCatalog(), taskUserDto.getValue());

      if ("assignee".equals(taskUserDto.getCatalog())) {
        assignee = taskUserDto.getValue();

        break;
      }
    }

    logger.debug("assignee : {}", assignee);

    boolean exists = assignee != null;

    if ((("${" + firstTaskForm.getInitiatorName() + "}").equals(assignee))
        || "流程发起人".equals(assignee)
        || ((assignee != null) && assignee.equals(Authentication.getAuthenticatedUserId()))) {
      exists = true;
    }

    if (!exists) {
      logger.info(
          "cannot find taskForm : {}, {}", processDefinitionId, firstTaskForm.getActivityId());

      return new FormDTO();
    }

    com.mossle.spi.humantask.FormDTO taskFormDto =
        taskDefinitionConnector.findForm(
            firstTaskForm.getActivityId(), firstTaskForm.getProcessDefinitionId());

    List<BpmConfForm> bpmConfForms =
        bpmConfFormManager.find(
            "from BpmConfForm where bpmConfNode.bpmConfBase.processDefinitionId=? and bpmConfNode.code=?",
            firstTaskForm.getProcessDefinitionId(),
            firstTaskForm.getActivityId());

    if (taskFormDto == null) {
      logger.info(
          "cannot find bpmConfForm : {}, {}", processDefinitionId, firstTaskForm.getActivityId());

      return new FormDTO();
    }

    FormDTO formDto = new FormDTO();
    formDto.setProcessDefinitionId(firstTaskForm.getProcessDefinitionId());
    formDto.setActivityId(firstTaskForm.getActivityId());

    FormDTO contentFormDto =
        formConnector.findForm(taskFormDto.getKey(), processDefinition.getTenantId());

    if (contentFormDto == null) {
      logger.error("cannot find form : {}", formDto.getCode());

      return formDto;
    }

    formDto.setCode(taskFormDto.getKey());
    formDto.setRedirect(contentFormDto.isRedirect());
    formDto.setUrl(contentFormDto.getUrl());
    formDto.setContent(contentFormDto.getContent());

    return formDto;
  }