@Override
  public WorkflowInstanceLink deleteWorkflowInstanceLink(WorkflowInstanceLink workflowInstanceLink)
      throws PortalException {

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

    super.deleteWorkflowInstanceLink(workflowInstanceLink);

    WorkflowInstanceManagerUtil.deleteWorkflowInstance(
        workflowInstanceLink.getCompanyId(), workflowInstanceLink.getWorkflowInstanceId());

    return workflowInstanceLink;
  }
  @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;
  }