protected void deleteInstance(ActionRequest actionRequest) throws Exception {

    ThemeDisplay themeDisplay = (ThemeDisplay) actionRequest.getAttribute(WebKeys.THEME_DISPLAY);

    long workflowInstanceId = ParamUtil.getLong(actionRequest, "workflowInstanceId");

    WorkflowInstance workflowInstance =
        WorkflowInstanceManagerUtil.getWorkflowInstance(
            themeDisplay.getCompanyId(), workflowInstanceId);

    Map<String, Serializable> workflowContext = workflowInstance.getWorkflowContext();

    long companyId = GetterUtil.getLong(workflowContext.get(WorkflowConstants.CONTEXT_COMPANY_ID));
    long groupId = GetterUtil.getLong(workflowContext.get(WorkflowConstants.CONTEXT_GROUP_ID));
    String className =
        GetterUtil.getString(workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME));
    long classPK =
        GetterUtil.getLong(workflowContext.get(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK));

    WorkflowHandler workflowHandler = WorkflowHandlerRegistryUtil.getWorkflowHandler(className);

    workflowHandler.updateStatus(WorkflowConstants.STATUS_DRAFT, workflowContext);

    WorkflowInstanceLinkLocalServiceUtil.deleteWorkflowInstanceLink(
        companyId, groupId, className, classPK);
  }
  @Override
  public void updateClassPK(
      long companyId, long groupId, String className, long oldClassPK, long newClassPK)
      throws PortalException {

    if (!WorkflowThreadLocal.isEnabled()) {
      return;
    }

    List<WorkflowInstanceLink> workflowInstanceLinks =
        getWorkflowInstanceLinks(companyId, groupId, className, oldClassPK);

    for (WorkflowInstanceLink workflowInstanceLink : workflowInstanceLinks) {

      WorkflowInstance workflowInstance =
          WorkflowInstanceManagerUtil.getWorkflowInstance(
              workflowInstanceLink.getCompanyId(), workflowInstanceLink.getWorkflowInstanceId());

      workflowInstanceLink.setClassPK(newClassPK);

      workflowInstanceLinkPersistence.update(workflowInstanceLink);

      Map<String, Serializable> workflowContext =
          new HashMap<>(workflowInstance.getWorkflowContext());

      workflowContext.put(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK, String.valueOf(newClassPK));

      WorkflowInstanceManagerUtil.updateWorkflowContext(
          workflowInstanceLink.getCompanyId(),
          workflowInstanceLink.getWorkflowInstanceId(),
          workflowContext);
    }
  }
  @Override
  public String getState(long companyId, long groupId, String className, long classPK)
      throws PortalException {

    WorkflowInstanceLink workflowInstanceLink =
        getWorkflowInstanceLink(companyId, groupId, className, classPK);

    WorkflowInstance workflowInstance =
        WorkflowInstanceManagerUtil.getWorkflowInstance(
            companyId, workflowInstanceLink.getWorkflowInstanceId());

    return workflowInstance.getState();
  }
  @Override
  public boolean isEnded(long companyId, long groupId, String className, long classPK)
      throws PortalException {

    WorkflowInstanceLink workflowInstanceLink =
        fetchWorkflowInstanceLink(companyId, groupId, className, classPK);

    if (workflowInstanceLink == null) {
      return false;
    }

    WorkflowInstance workflowInstance =
        WorkflowInstanceManagerUtil.getWorkflowInstance(
            companyId, workflowInstanceLink.getWorkflowInstanceId());

    if (workflowInstance.getEndDate() != null) {
      return true;
    }

    return false;
  }