/**
  * 根据流程实例ID查询流程定义对象{@link ProcessDefinition}
  *
  * @param processInstanceId 流程实例ID
  * @return 流程定义对象{@link ProcessDefinition}
  */
 public ProcessDefinition findProcessDefinitionByPid(String processInstanceId) {
   HistoricProcessInstance historicProcessInstance =
       historyService
           .createHistoricProcessInstanceQuery()
           .processInstanceId(processInstanceId)
           .singleResult();
   String processDefinitionId = historicProcessInstance.getProcessDefinitionId();
   ProcessDefinition processDefinition = findProcessDefinition(processDefinitionId);
   return processDefinition;
 }
  protected ProcessInstanceItem createItem(HistoricProcessInstance processInstance) {
    ProcessInstanceItem item = new ProcessInstanceItem();
    item.addItemProperty("id", new ObjectProperty<String>(processInstance.getId(), String.class));

    ProcessDefinition processDefinition =
        getProcessDefinition(processInstance.getProcessDefinitionId());

    String itemName =
        getProcessDisplayName(processDefinition) + " (" + processInstance.getId() + ")";
    item.addItemProperty("name", new ObjectProperty<String>(itemName, String.class));
    return item;
  }
  public void drawHistoryFlow(BufferedImage image, String processInstanceId) {
    HistoricProcessInstance historicProcessInstance =
        Context.getCommandContext()
            .getHistoricProcessInstanceEntityManager()
            .findHistoricProcessInstance(processInstanceId);
    String processDefinitionId = historicProcessInstance.getProcessDefinitionId();
    Graph graph = new ActivitiHistoryGraphBuilder(processInstanceId).build();

    for (Edge edge : graph.getEdges()) {
      drawSequenceFlow(image, processDefinitionId, edge.getName());
    }
  }
Exemple #4
0
  public Page<Leave> find(Page<Leave> page, Leave leave) {

    leave.getSqlMap().put("dsf", dataScopeFilter(leave.getCurrentUser(), "o", "u"));

    leave.setPage(page);
    page.setList(leaveDao.findList(leave));

    for (Leave item : page.getList()) {
      String processInstanceId = item.getProcessInstanceId();
      Task task =
          taskService
              .createTaskQuery()
              .processInstanceId(processInstanceId)
              .active()
              .singleResult();
      item.setTask(task);
      HistoricProcessInstance historicProcessInstance =
          historyService
              .createHistoricProcessInstanceQuery()
              .processInstanceId(processInstanceId)
              .singleResult();
      if (historicProcessInstance != null) {
        item.setHistoricProcessInstance(historicProcessInstance);
        item.setProcessDefinition(
            repositoryService
                .createProcessDefinitionQuery()
                .processDefinitionId(historicProcessInstance.getProcessDefinitionId())
                .singleResult());
      } else {
        ProcessInstance processInstance =
            runtimeService
                .createProcessInstanceQuery()
                .processInstanceId(processInstanceId)
                .active()
                .singleResult();
        if (processInstance != null) {
          item.setProcessInstance(processInstance);
          item.setProcessDefinition(
              repositoryService
                  .createProcessDefinitionQuery()
                  .processDefinitionId(processInstance.getProcessDefinitionId())
                  .singleResult());
        }
      }
    }
    return page;
  }
 /**
  * Convert historic process instances to BPMN process instances
  *
  * @param historicProcessInstanceList List of historic process instances
  * @return BPMNProcessInstance array
  */
 private BPMNProcessInstance[] getBPMNProcessInstances(
     List<HistoricProcessInstance> historicProcessInstanceList) {
   BPMNProcessInstance bpmnProcessInstance;
   List<BPMNProcessInstance> bpmnProcessInstances = new ArrayList<>();
   for (HistoricProcessInstance instance : historicProcessInstanceList) {
     bpmnProcessInstance = new BPMNProcessInstance();
     bpmnProcessInstance.setProcessDefinitionId(instance.getProcessDefinitionId());
     bpmnProcessInstance.setTenantId(instance.getTenantId());
     bpmnProcessInstance.setName(instance.getName());
     bpmnProcessInstance.setInstanceId(instance.getId());
     bpmnProcessInstance.setBusinessKey(instance.getBusinessKey());
     bpmnProcessInstance.setStartTime(instance.getStartTime());
     bpmnProcessInstance.setEndTime(instance.getEndTime());
     bpmnProcessInstance.setDuration(instance.getDurationInMillis());
     bpmnProcessInstance.setStartUserId(instance.getStartUserId());
     bpmnProcessInstance.setStartActivityId(instance.getStartActivityId());
     bpmnProcessInstance.setVariables(formatVariables(instance.getProcessVariables()));
     bpmnProcessInstances.add(bpmnProcessInstance);
   }
   return bpmnProcessInstances.toArray(new BPMNProcessInstance[bpmnProcessInstances.size()]);
 }
 @Test
 @Deployment(resources = {"chapter4/bookorder.bpmn20.xml"})
 public void queryHistoricInstances() {
   String processInstanceID = startAndComplete();
   HistoryService historyService = activitiRule.getHistoryService();
   HistoricProcessInstance historicProcessInstance =
       historyService
           .createHistoricProcessInstanceQuery()
           .processInstanceId(processInstanceID)
           .singleResult();
   assertNotNull(historicProcessInstance);
   assertEquals(processInstanceID, historicProcessInstance.getId());
   System.out.println(
       "history process with definition id "
           + historicProcessInstance.getProcessDefinitionId()
           + ", started at "
           + historicProcessInstance.getStartTime()
           + ", ended at "
           + historicProcessInstance.getEndTime()
           + ", duration was "
           + historicProcessInstance.getDurationInMillis());
 }
  /**
   * 流程列表
   *
   * @param user 操作用户
   * @param processStatus 流程状态 unfinished finished involve
   * @param searchable
   * @param model
   * @return
   */
  @RequestMapping(method = RequestMethod.GET)
  public String list(
      @CurrentUser User user,
      @RequestParam("processstatus") String processStatus,
      Searchable searchable,
      Model model) {

    //		if (permissionList != null) {
    //			this.permissionList.assertHasViewPermission();
    //		}

    if (null != processStatus && "involve".equals(processStatus)) {

      Page<HistoricProcessInstance> processInstances =
          userProcessService.findInvolvedProcessInstances(user.getUsername(), searchable.getPage());
      Map<String, BpmProcess> processMap = new HashMap<String, BpmProcess>();
      for (HistoricProcessInstance processInstance : processInstances) {
        String processDefinitionId = processInstance.getProcessDefinitionId();
        BpmProcess bpmProcess =
            bpmProcessService.findByBpmConfBase(
                bpmConfBaseService.findByProcessDefinitionId(processDefinitionId));
        if (!processMap.containsKey(processDefinitionId)) {
          processMap.put(processDefinitionId, bpmProcess);
        }
      }
      model.addAttribute("bpmProcessMap", processMap);
      model.addAttribute("page", processInstances);
    } else if (null != processStatus && "unfinished".equals(processStatus)) {
      Page<HistoricProcessInstance> processInstances =
          userProcessService.findRunningProcessInstances(user.getUsername(), searchable.getPage());
      Map<String, BpmProcess> processMap = new HashMap<String, BpmProcess>();
      for (HistoricProcessInstance processInstance : processInstances) {
        String processDefinitionId = processInstance.getProcessDefinitionId();
        BpmProcess bpmProcess =
            bpmProcessService.findByBpmConfBase(
                bpmConfBaseService.findByProcessDefinitionId(processDefinitionId));
        if (!processMap.containsKey(processDefinitionId)) {
          processMap.put(processDefinitionId, bpmProcess);
        }
      }
      model.addAttribute("bpmProcessMap", processMap);
      model.addAttribute("page", processInstances);
    } else if (null != processStatus && "finished".equals(processStatus)) {
      Page<HistoricProcessInstance> processInstances =
          userProcessService.findCompletedProcessInstances(
              user.getUsername(), searchable.getPage());
      Map<String, BpmProcess> processMap = new HashMap<String, BpmProcess>();
      for (HistoricProcessInstance processInstance : processInstances) {
        String processDefinitionId = processInstance.getProcessDefinitionId();
        BpmProcess bpmProcess =
            bpmProcessService.findByBpmConfBase(
                bpmConfBaseService.findByProcessDefinitionId(processDefinitionId));
        if (!processMap.containsKey(processDefinitionId)) {
          processMap.put(processDefinitionId, bpmProcess);
        }
      }
      model.addAttribute("bpmProcessMap", processMap);
      model.addAttribute("page", processInstances);
    }
    if (listAlsoSetCommonData) {
      setCommonData(model);
    }

    return "bpm/userprocess/list";
  }
  public InputStream generateDiagram(String processInstanceId) throws IOException {
    HistoricProcessInstance historicProcessInstance =
        Context.getCommandContext()
            .getHistoricProcessInstanceEntityManager()
            .findHistoricProcessInstance(processInstanceId);
    String processDefinitionId = historicProcessInstance.getProcessDefinitionId();
    GetBpmnModelCmd getBpmnModelCmd = new GetBpmnModelCmd(processDefinitionId);
    BpmnModel bpmnModel = getBpmnModelCmd.execute(Context.getCommandContext());
    Point point = getMinXAndMinY(bpmnModel);
    this.minX = point.x;
    this.minY = point.y;
    this.minX = (this.minX <= 5) ? 5 : this.minX;
    this.minY = (this.minY <= 5) ? 5 : this.minY;
    this.minX -= 5;
    this.minY -= 5;

    ProcessDefinitionEntity definition =
        new GetDeploymentProcessDefinitionCmd(processDefinitionId)
            .execute(Context.getCommandContext());
    String diagramResourceName = definition.getDiagramResourceName();
    String deploymentId = definition.getDeploymentId();
    byte[] bytes =
        Context.getCommandContext()
            .getResourceEntityManager()
            .findResourceByDeploymentIdAndResourceName(deploymentId, diagramResourceName)
            .getBytes();
    InputStream originDiagram = new ByteArrayInputStream(bytes);
    BufferedImage image = ImageIO.read(originDiagram);

    HistoricActivityInstanceQueryImpl historicActivityInstanceQueryImpl =
        new HistoricActivityInstanceQueryImpl();
    historicActivityInstanceQueryImpl
        .processInstanceId(processInstanceId)
        .orderByHistoricActivityInstanceStartTime()
        .asc();

    Page page = new Page(0, 100);
    List<HistoricActivityInstance> activityInstances =
        Context.getCommandContext()
            .getHistoricActivityInstanceEntityManager()
            .findHistoricActivityInstancesByQueryCriteria(historicActivityInstanceQueryImpl, page);

    this.drawHistoryFlow(image, processInstanceId);

    for (HistoricActivityInstance historicActivityInstance : activityInstances) {
      String historicActivityId = historicActivityInstance.getActivityId();
      ActivityImpl activity = definition.findActivity(historicActivityId);

      if (activity != null) {
        if (historicActivityInstance.getEndTime() == null) {
          // 节点正在运行中
          signRunningNode(
              image,
              activity.getX() - this.minX,
              activity.getY() - this.minY,
              activity.getWidth(),
              activity.getHeight(),
              historicActivityInstance.getActivityType());
        } else {
          String deleteReason = null;

          if (historicActivityInstance.getTaskId() != null) {
            deleteReason =
                Context.getCommandContext()
                    .getHistoricTaskInstanceEntityManager()
                    .findHistoricTaskInstanceById(historicActivityInstance.getTaskId())
                    .getDeleteReason();
          }

          // 节点已经结束
          if ("跳过".equals(deleteReason)) {
            signSkipNode(
                image,
                activity.getX() - this.minX,
                activity.getY() - this.minY,
                activity.getWidth(),
                activity.getHeight(),
                historicActivityInstance.getActivityType());
          } else {
            signHistoryNode(
                image,
                activity.getX() - this.minX,
                activity.getY() - this.minY,
                activity.getWidth(),
                activity.getHeight(),
                historicActivityInstance.getActivityType());
          }
        }
      }
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    String formatName = getDiagramExtension(diagramResourceName);
    ImageIO.write(image, formatName, out);

    return new ByteArrayInputStream(out.toByteArray());
  }
  /**
   * @Title: myWorkTaskData @Description: TODO
   *
   * @param request
   * @param response
   * @param dataGrid void
   * @throws
   * @exception
   * @author fly
   * @date 2015年6月23日 上午10:20:42
   */
  @RequestMapping(params = "myWorkTaskData")
  public void myWorkTaskData(
      HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {

    String userId = ResourceUtil.getSessionUserName().getId();
    // involvedUser 当前用户相关的
    HistoricProcessInstanceQuery query =
        historyService.createHistoricProcessInstanceQuery().involvedUser(userId);

    List<HistoricProcessInstance> historicTasks =
        query
            .orderByProcessInstanceStartTime()
            .desc()
            .listPage(dataGrid.getStart(), dataGrid.getEnd());
    long total = query.count();
    System.out.println(dataGrid.getStart() + " end: " + dataGrid.getEnd());
    StringBuffer rows = new StringBuffer();
    for (HistoricProcessInstance t : historicTasks) {
      ProcessDefinition processDefinition =
          repositoryService.getProcessDefinition(t.getProcessDefinitionId());

      rows.append(
          "{'id':'"
              + t.getId()
              + "','key':'"
              + processDefinition.getName()
              + "-"
              + DateUtils.date_sdf.format(t.getStartTime())
              + "','taskId':'"
              + t.getId()
              + "'");
      // 流程详细查看
      WorkFlowSetEntity workFlowSet =
          systemService.findUniqueByProperty(
              WorkFlowSetEntity.class, "deploymentId", processDefinition.getDeploymentId());
      if (workFlowSet != null) {
        rows.append(",'action':'" + workFlowSet.getDetailUrl() + "'");
      }
      // 流程用户处理
      if (t.getStartUserId() != null) {
        TSUser user = systemService.get(TSUser.class, t.getStartUserId());
        rows.append(",'username':'******'");
      }

      // 流程开始结束时间处理
      if (t.getStartTime() == null) {
        rows.append(",'beginDate':'无'");
      } else {
        rows.append(",'beginDate':'" + DateUtils.datetimeFormat.format(t.getStartTime()) + "'");
      }
      if (t.getEndTime() == null) {
        rows.append(",'endDate':'无','stateType':'办理中'");

      } else {
        rows.append(
            ",'endDate':'"
                + DateUtils.datetimeFormat.format(t.getEndTime())
                + "','stateType':'已完成'");
      }
      rows.append("},");
    }
    String rowStr = StringUtils.substringBeforeLast(rows.toString(), ",");

    JSONObject jObject = JSONObject.fromObject("{'total':" + total + ",'rows':[" + rowStr + "]}");
    responseDatagrid(response, jObject);
  }