Example #1
0
  /** 获取任务表单. */
  public FormDTO findTaskForm(String humanTaskId) {
    HumanTaskDTO humanTaskDto = this.findHumanTask(humanTaskId);

    FormDTO formDto = internalProcessConnector.findTaskForm(humanTaskDto.getTaskId());
    formDto.setTaskId(humanTaskId);

    FormDTO contentFormDto = formConnector.findForm(formDto.getCode());

    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 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;
  }
  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;
  }
  @POST
  @Path("completeTask")
  @Produces(MediaType.APPLICATION_JSON)
  public BaseDTO completeTask(
      @HeaderParam("sessionId") String sessionId,
      @FormParam("taskId") String taskId,
      @FormParam("data") String data)
      throws Exception {
    logger.info("start : {} {}", taskId, data);

    String humanTaskId = taskId;

    PimDevice pimDevice = pimDeviceManager.findUniqueBy("sessionId", sessionId);

    if (pimDevice == null) {
      BaseDTO result = new BaseDTO();
      result.setCode(401);
      result.setMessage("auth fail");

      return result;
    }

    String userId = pimDevice.getUserId();
    String tenantId = "1";

    Map<String, Object> map = jsonMapper.fromJson(data, Map.class);
    map.put("taskId", humanTaskId);

    Record record = null;
    FormParameter formParameter = null;
    HumanTaskDTO humanTaskDto = null;
    FormDTO formDto = null;

    formParameter = doSaveRecord(map, userId, tenantId);

    humanTaskId = formParameter.getHumanTaskId();
    operationService.saveDraft(userId, tenantId, formParameter);

    formDto = humanTaskConnector.findTaskForm(humanTaskId);

    humanTaskDto = humanTaskConnector.findHumanTask(humanTaskId);

    String processInstanceId = humanTaskDto.getProcessInstanceId();
    record = keyValueConnector.findByRef(processInstanceId);

    record = new RecordBuilder().build(record, formParameter.getMultiValueMap(), tenantId);

    keyValueConnector.save(record);

    Xform xform =
        new XformBuilder()
            .setStoreConnector(storeConnector)
            .setContent(formDto.getContent())
            .setRecord(record)
            .build();
    Map<String, Object> taskParameters = xform.getMapData();
    logger.info("taskParameters : {}", taskParameters);

    String comment = "";
    String action = "";

    try {
      humanTaskConnector.completeTask(humanTaskId, userId, action, comment, taskParameters);
    } catch (IllegalStateException ex) {
      logger.error(ex.getMessage(), ex);

      return null;
    }

    if (record == null) {
      record = new Record();
    }

    record = new RecordBuilder().build(record, STATUS_RUNNING, humanTaskDto.getProcessInstanceId());
    keyValueConnector.save(record);

    BaseDTO result = new BaseDTO();
    result.setCode(200);
    result.setData(data);
    logger.info("end");

    return result;
  }