public Object execute(CommandContext commandContext) {
    if (processInstanceId == null) {
      throw new ActivitiException("processInstanceId is null");
    }
    // Check if process instance is still running
    HistoricProcessInstance instance =
        commandContext
            .getHistoricProcessInstanceManager()
            .findHistoricProcessInstance(processInstanceId);

    if (instance == null) {
      throw new ActivitiException(
          "No historic process instance found with id: " + processInstanceId);
    }
    if (instance.getEndTime() == null) {
      throw new ActivitiException(
          "Process instance is still running, cannot delete historic process instance: "
              + processInstanceId);
    }

    commandContext
        .getHistoricProcessInstanceManager()
        .deleteHistoricProcessInstanceById(processInstanceId);

    return null;
  }
  /**
   * Process variation over time i.e. tasks started and completed over the months
   *
   * @return array with the no. of processes started and completed over the months
   */
  @GET
  @Path("/processVariation/")
  @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  public ResponseHolder processVariationOverTime() {
    ResponseHolder response = new ResponseHolder();
    List list = new ArrayList();
    String[] MONTHS = {
      "Jan", "Feb", "March", "April", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"
    };
    SimpleDateFormat ft = new SimpleDateFormat("M");
    InstanceStatPerMonth[] processStatPerMonths = new InstanceStatPerMonth[12];
    for (int i = 0; i < processStatPerMonths.length; i++) {
      processStatPerMonths[i] = new InstanceStatPerMonth();
      processStatPerMonths[i].setMonth(MONTHS[i]);
      processStatPerMonths[i].setCompletedInstances(0);
      processStatPerMonths[i].setStartedInstances(0);
    }

    // Get completed process instances
    List<HistoricProcessInstance> completedProcesses =
        BPMNOSGIService.getHistoryService()
            .createHistoricProcessInstanceQuery()
            .processInstanceTenantId(str)
            .finished()
            .list();
    for (HistoricProcessInstance instance : completedProcesses) {
      int startTime = Integer.parseInt(ft.format(instance.getStartTime()));
      int endTime = Integer.parseInt(ft.format(instance.getEndTime()));
      processStatPerMonths[startTime - 1].setStartedInstances(
          processStatPerMonths[startTime - 1].getStartedInstances() + 1);
      processStatPerMonths[endTime - 1].setCompletedInstances(
          processStatPerMonths[endTime - 1].getCompletedInstances() + 1);
    }
    // Get active process instances
    List<HistoricProcessInstance> activeProcesses =
        BPMNOSGIService.getHistoryService()
            .createHistoricProcessInstanceQuery()
            .processInstanceTenantId(str)
            .unfinished()
            .list();
    for (HistoricProcessInstance instance : activeProcesses) {
      int startTime = Integer.parseInt(ft.format(instance.getStartTime()));
      processStatPerMonths[startTime - 1].setStartedInstances(
          processStatPerMonths[startTime - 1].getStartedInstances() + 1);
    }

    for (int i = 0; i < processStatPerMonths.length; i++) {
      list.add(processStatPerMonths[i]);
    }
    response.setData(list);
    return response;
  }
 /**
  * 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());
 }
Example #5
0
  /**
   * @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);
  }