@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 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 WorkflowInstanceLink addWorkflowInstanceLink(
      long userId,
      long companyId,
      long groupId,
      String className,
      long classPK,
      long workflowInstanceId)
      throws PortalException {

    User user = userPersistence.findByPrimaryKey(userId);
    long classNameId = classNameLocalService.getClassNameId(className);

    long workflowInstanceLinkId = counterLocalService.increment();

    WorkflowInstanceLink workflowInstanceLink =
        workflowInstanceLinkPersistence.create(workflowInstanceLinkId);

    workflowInstanceLink.setUserId(userId);
    workflowInstanceLink.setUserName(user.getFullName());
    workflowInstanceLink.setGroupId(groupId);
    workflowInstanceLink.setCompanyId(companyId);
    workflowInstanceLink.setClassNameId(classNameId);
    workflowInstanceLink.setClassPK(classPK);
    workflowInstanceLink.setWorkflowInstanceId(workflowInstanceId);

    workflowInstanceLinkPersistence.update(workflowInstanceLink);

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