/**
   * 根据流入任务集合,查询最近一次的流入任务节点
   *
   * @param procInst 流程实例
   * @param tempList 流入任务集合
   * @return
   */
  private ActivityImpl filterNewestActivity(ProcessInstance procInst, List<ActivityImpl> tempList) {
    while (tempList.size() > 0) {
      ActivityImpl activity_ = tempList.get(0);
      HistoricActivityInstance activityInstance_ =
          findHistoricUserTask(procInst, activity_.getId());
      if (activityInstance_ == null) {
        tempList.remove(activity_);
        continue;
      }

      if (tempList.size() > 0) {
        ActivityImpl activity_0 = tempList.get(0);
        HistoricActivityInstance activityInstance_0 =
            findHistoricUserTask(procInst, activity_0.getId());
        if (activityInstance_ == null) {
          tempList.remove(activity_);
          continue;
        }

        if (activityInstance_0.getEndTime().before(activityInstance_0.getEndTime())) {
          tempList.remove(activity_);
        } else {
          tempList.remove(activity_);
        }
      } else {
        break;
      }
    }
    if (tempList.size() > 0) {
      return tempList.get(0);
    }
    return null;
  }
  // Test to verify ACT-1755
  @Deployment
  public void testHistoryTables() {

    ProcessInstance pi = runtimeService.startProcessInstanceByKey("testHistoryRecords");

    List<HistoricActivityInstance> history =
        historyService.createHistoricActivityInstanceQuery().processInstanceId(pi.getId()).list();

    for (HistoricActivityInstance h : history) {
      if (h.getActivityId().equals("parallelgateway2")) {
        assertNotNull(h.getEndTime());
      }
    }
  }
 @Test
 @Deployment(resources = {"chapter4/bookorder.bpmn20.xml"})
 public void queryHistoricActivities() {
   startAndComplete();
   HistoryService historyService = activitiRule.getHistoryService();
   List<HistoricActivityInstance> activityList =
       historyService.createHistoricActivityInstanceQuery().list();
   assertEquals(4, activityList.size());
   for (HistoricActivityInstance historicActivityInstance : activityList) {
     assertNotNull(historicActivityInstance.getActivityId());
     System.out.println(
         "history activity "
             + historicActivityInstance.getActivityName()
             + ", type "
             + historicActivityInstance.getActivityType()
             + ", starttime "
             + historicActivityInstance.getStartTime()
             + ", endtime "
             + historicActivityInstance.getEndTime()
             + ", duration was "
             + historicActivityInstance.getDurationInMillis());
   }
 }
  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());
  }