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 startWorkflowInstance(
      long companyId,
      long groupId,
      long userId,
      String className,
      long classPK,
      Map<String, Serializable> workflowContext)
      throws PortalException {

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

    if (userId == 0) {
      userId = userLocalService.getDefaultUserId(companyId);
    }

    WorkflowHandler<?> workflowHandler = WorkflowHandlerRegistryUtil.getWorkflowHandler(className);

    WorkflowDefinitionLink workflowDefinitionLink =
        workflowHandler.getWorkflowDefinitionLink(companyId, groupId, classPK);

    String workflowDefinitionName = workflowDefinitionLink.getWorkflowDefinitionName();
    int workflowDefinitionVersion = workflowDefinitionLink.getWorkflowDefinitionVersion();

    if (workflowContext != null) {
      workflowContext = new HashMap<>(workflowContext);
    } else {
      workflowContext = new HashMap<>();
    }

    workflowContext.put(WorkflowConstants.CONTEXT_COMPANY_ID, String.valueOf(companyId));
    workflowContext.put(WorkflowConstants.CONTEXT_GROUP_ID, String.valueOf(groupId));
    workflowContext.put(WorkflowConstants.CONTEXT_ENTRY_CLASS_NAME, className);
    workflowContext.put(WorkflowConstants.CONTEXT_ENTRY_CLASS_PK, String.valueOf(classPK));
    workflowContext.put(
        WorkflowConstants.CONTEXT_ENTRY_TYPE, workflowHandler.getType(LocaleUtil.getDefault()));

    WorkflowInstance workflowInstance =
        WorkflowInstanceManagerUtil.startWorkflowInstance(
            companyId,
            groupId,
            userId,
            workflowDefinitionName,
            workflowDefinitionVersion,
            null,
            workflowContext);

    addWorkflowInstanceLink(
        userId, companyId, groupId, className, classPK, workflowInstance.getWorkflowInstanceId());
  }
  @Override
  protected String getLink(
      UserNotificationEvent userNotificationEvent, ServiceContext serviceContext) throws Exception {

    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(userNotificationEvent.getPayload());

    String entryClassName = jsonObject.getString("entryClassName");

    WorkflowHandler<?> workflowHandler =
        WorkflowHandlerRegistryUtil.getWorkflowHandler(entryClassName);

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

    long workflowTaskId = jsonObject.getLong("workflowTaskId");

    return workflowHandler.getURLEditWorkflowTask(workflowTaskId, serviceContext);
  }
  public void addFileEntry(
      long userId, FileEntry fileEntry, FileVersion fileVersion, ServiceContext serviceContext)
      throws PortalException, SystemException {

    updateAsset(
        userId,
        fileEntry,
        fileVersion,
        serviceContext.getAssetCategoryIds(),
        serviceContext.getAssetTagNames(),
        serviceContext.getAssetLinkEntryIds());

    if (PropsValues.DL_FILE_ENTRY_COMMENTS_ENABLED) {
      mbMessageLocalService.addDiscussionMessage(
          fileEntry.getUserId(), fileEntry.getUserName(),
          fileEntry.getGroupId(), DLFileEntryConstants.getClassName(),
          fileEntry.getFileEntryId(), WorkflowConstants.ACTION_PUBLISH);
    }

    if (fileVersion instanceof LiferayFileVersion) {
      DLFileVersion dlFileVersion = (DLFileVersion) fileVersion.getModel();

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

      workflowContext.put("event", DLSyncConstants.EVENT_ADD);

      WorkflowHandlerRegistryUtil.startWorkflowInstance(
          dlFileVersion.getCompanyId(),
          dlFileVersion.getGroupId(),
          userId,
          DLFileEntryConstants.getClassName(),
          dlFileVersion.getFileVersionId(),
          dlFileVersion,
          serviceContext,
          workflowContext);
    }

    registerDLProcessorCallback(fileEntry, null);
  }
  public KBArticle updateKBArticle(
      long userId,
      long resourcePrimKey,
      String title,
      String content,
      String description,
      String[] sections,
      String dirName,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    // KB article

    User user = userPersistence.findByPrimaryKey(userId);
    int version = KBArticleConstants.DEFAULT_VERSION;
    int status = WorkflowConstants.STATUS_DRAFT;

    validate(title, content);

    KBArticle oldKBArticle = getLatestKBArticle(resourcePrimKey, WorkflowConstants.STATUS_ANY);

    long oldResourcePrimKey = oldKBArticle.getResourcePrimKey();
    long oldGroupId = oldKBArticle.getGroupId();
    Date oldCreateDate = oldKBArticle.getCreateDate();
    long oldRootResourcePrimKey = oldKBArticle.getRootResourcePrimKey();
    long oldParentResourcePrimKey = oldKBArticle.getParentResourcePrimKey();
    int oldVersion = oldKBArticle.getVersion();
    double oldPriority = oldKBArticle.getPriority();
    int oldViewCount = oldKBArticle.getViewCount();
    int oldStatus = oldKBArticle.getStatus();

    KBArticle kbArticle = null;

    if (oldStatus == WorkflowConstants.STATUS_APPROVED) {
      long kbArticleId = counterLocalService.increment();

      kbArticle = kbArticlePersistence.create(kbArticleId);
      version = oldVersion + 1;
    } else {
      kbArticle = oldKBArticle;
      version = oldVersion;
    }

    if (oldStatus == WorkflowConstants.STATUS_PENDING) {
      status = WorkflowConstants.STATUS_PENDING;
    }

    kbArticle.setResourcePrimKey(oldResourcePrimKey);
    kbArticle.setGroupId(oldGroupId);
    kbArticle.setCompanyId(user.getCompanyId());
    kbArticle.setUserId(user.getUserId());
    kbArticle.setUserName(user.getFullName());
    kbArticle.setCreateDate(oldCreateDate);
    kbArticle.setModifiedDate(serviceContext.getModifiedDate(null));
    kbArticle.setRootResourcePrimKey(oldRootResourcePrimKey);
    kbArticle.setParentResourcePrimKey(oldParentResourcePrimKey);
    kbArticle.setVersion(version);
    kbArticle.setTitle(title);
    kbArticle.setContent(content);
    kbArticle.setDescription(description);
    kbArticle.setPriority(oldPriority);
    kbArticle.setSections(StringUtil.merge(AdminUtil.escapeSections(sections)));
    kbArticle.setViewCount(oldViewCount);
    kbArticle.setLatest(true);
    kbArticle.setMain(false);
    kbArticle.setStatus(status);

    kbArticlePersistence.update(kbArticle, false);

    if (oldVersion < version) {
      oldKBArticle.setLatest(false);

      kbArticlePersistence.update(oldKBArticle, false);
    }

    // Resources

    if ((serviceContext.getGroupPermissions() != null)
        || (serviceContext.getGuestPermissions() != null)) {

      updateKBArticleResources(
          kbArticle, serviceContext.getGroupPermissions(), serviceContext.getGuestPermissions());
    }

    // Asset

    updateKBArticleAsset(
        userId, kbArticle, serviceContext.getAssetCategoryIds(), serviceContext.getAssetTagNames());

    // Attachments

    updateKBArticleAttachments(kbArticle, oldVersion, dirName, serviceContext);

    // Workflow

    WorkflowHandlerRegistryUtil.startWorkflowInstance(
        user.getCompanyId(),
        kbArticle.getGroupId(),
        userId,
        KBArticle.class.getName(),
        resourcePrimKey,
        kbArticle,
        serviceContext);

    return kbArticle;
  }
  public KBArticle addKBArticle(
      long userId,
      long parentResourcePrimKey,
      String title,
      String content,
      String description,
      String[] sections,
      String dirName,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    // KB article

    User user = userPersistence.findByPrimaryKey(userId);
    long groupId = serviceContext.getScopeGroupId();
    double priority = getPriority(groupId, parentResourcePrimKey);
    Date now = new Date();

    validate(title, content);

    long kbArticleId = counterLocalService.increment();

    long resourcePrimKey = counterLocalService.increment();

    long rootResourcePrimKey = getRootResourcePrimKey(resourcePrimKey, parentResourcePrimKey);

    KBArticle kbArticle = kbArticlePersistence.create(kbArticleId);

    kbArticle.setUuid(serviceContext.getUuid());
    kbArticle.setResourcePrimKey(resourcePrimKey);
    kbArticle.setGroupId(groupId);
    kbArticle.setCompanyId(user.getCompanyId());
    kbArticle.setUserId(user.getUserId());
    kbArticle.setUserName(user.getFullName());
    kbArticle.setCreateDate(serviceContext.getCreateDate(now));
    kbArticle.setModifiedDate(serviceContext.getModifiedDate(now));
    kbArticle.setRootResourcePrimKey(rootResourcePrimKey);
    kbArticle.setParentResourcePrimKey(parentResourcePrimKey);
    kbArticle.setVersion(KBArticleConstants.DEFAULT_VERSION);
    kbArticle.setTitle(title);
    kbArticle.setContent(content);
    kbArticle.setDescription(description);
    kbArticle.setPriority(priority);
    kbArticle.setSections(StringUtil.merge(AdminUtil.escapeSections(sections)));
    kbArticle.setViewCount(0);
    kbArticle.setLatest(true);
    kbArticle.setMain(false);
    kbArticle.setStatus(WorkflowConstants.STATUS_DRAFT);

    kbArticlePersistence.update(kbArticle, false);

    // Resources

    resourceLocalService.addModelResources(kbArticle, serviceContext);

    // Asset

    updateKBArticleAsset(
        userId, kbArticle, serviceContext.getAssetCategoryIds(), serviceContext.getAssetTagNames());

    // Attachments

    addKBArticleAttachments(kbArticle, dirName, serviceContext);

    // Workflow

    WorkflowHandlerRegistryUtil.startWorkflowInstance(
        user.getCompanyId(),
        groupId,
        userId,
        KBArticle.class.getName(),
        resourcePrimKey,
        kbArticle,
        serviceContext);

    return kbArticle;
  }
  public sample.lrdev05.database.model.Books addBooks(
      Books booksParam, long userId, ServiceContext serviceContext) {
    sample.lrdev05.database.model.Books booksVar;

    try {
      booksVar = booksPersistence.create(counterLocalService.increment(Books.class.toString()));
    } catch (SystemException e) {
      e.printStackTrace();
      return booksVar = null;
    }

    booksVar.setTitle(booksParam.getTitle());
    booksVar.setAuthor(booksParam.getAuthor());
    booksVar.setIsbn(booksParam.getIsbn());
    booksVar.setSummary(booksParam.getSummary());
    booksVar.setCompanyId(booksParam.getCompanyId());
    booksVar.setGroupId(booksParam.getGroupId());
    booksVar.setTitle(booksParam.getTitle());
    booksVar.setStatus(WorkflowConstants.STATUS_DRAFT);

    try {
      resourceLocalService.addResources(
          booksParam.getCompanyId(),
          booksParam.getGroupId(),
          userId,
          Books.class.getName(),
          booksParam.getPrimaryKey(),
          false,
          true,
          true);
      assetEntryLocalService.updateEntry(
          userId,
          booksVar.getGroupId(),
          Books.class.getName(),
          booksVar.getBookId(),
          serviceContext.getAssetCategoryIds(),
          serviceContext.getAssetTagNames());
      // Workflow
      WorkflowHandlerRegistryUtil.startWorkflowInstance(
          booksVar.getCompanyId(),
          booksVar.getGroupId(),
          userId,
          Books.class.getName(),
          booksVar.getPrimaryKey(),
          booksVar,
          serviceContext);

      sendMessage(booksVar, serviceContext);

    } catch (PortalException e) {
      e.printStackTrace();
      return booksVar = null;
    } catch (SystemException e) {
      e.printStackTrace();
      return booksVar = null;
    }

    try {
      return booksPersistence.update(booksVar, false);
    } catch (SystemException e) {
      e.printStackTrace();
      return booksVar = null;
    }
  }