protected void cascadeFileEntryTypes(
      long groupId,
      long folderId,
      long defaultFileEntryTypeId,
      List<Long> fileEntryTypeIds,
      ServiceContext serviceContext)
      throws PortalException, SystemException {

    List<DLFileEntry> dlFileEntries = dlFileEntryPersistence.findByG_F(groupId, folderId);

    for (DLFileEntry dlFileEntry : dlFileEntries) {
      Long fileEntryTypeId = dlFileEntry.getFileEntryTypeId();

      if (fileEntryTypeIds.contains(fileEntryTypeId)) {
        continue;
      }

      DLFileVersion dlFileVersion =
          dlFileVersionLocalService.getLatestFileVersion(dlFileEntry.getFileEntryId(), true);

      if (dlFileVersion.isPending()) {
        workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
            dlFileVersion.getCompanyId(),
            groupId,
            DLFileEntry.class.getName(),
            dlFileVersion.getFileVersionId());
      }

      dlFileEntryService.updateFileEntry(
          dlFileEntry.getFileEntryId(),
          null,
          null,
          null,
          null,
          null,
          false,
          defaultFileEntryTypeId,
          null,
          null,
          null,
          0,
          serviceContext);
    }

    List<DLFolder> subFolders = dlFolderPersistence.findByG_M_P_H(groupId, false, folderId, false);

    for (DLFolder subFolder : subFolders) {
      long subFolderId = subFolder.getFolderId();

      if (subFolder.isOverrideFileEntryTypes()) {
        continue;
      }

      cascadeFileEntryTypes(
          groupId, subFolderId, defaultFileEntryTypeId, fileEntryTypeIds, serviceContext);
    }
  }
  protected void cascadeFileEntryTypes(
      long userId,
      long groupId,
      long folderId,
      long defaultFileEntryTypeId,
      List<Long> fileEntryTypeIds,
      ServiceContext serviceContext)
      throws PortalException {

    List<DLFileEntry> dlFileEntries = dlFileEntryPersistence.findByG_F(groupId, folderId);

    for (DLFileEntry dlFileEntry : dlFileEntries) {
      Long fileEntryTypeId = dlFileEntry.getFileEntryTypeId();

      if (fileEntryTypeIds.contains(fileEntryTypeId)) {
        continue;
      }

      DLFileVersion dlFileVersion =
          dlFileVersionLocalService.getLatestFileVersion(dlFileEntry.getFileEntryId(), true);

      if (dlFileVersion.isPending()) {
        workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
            dlFileVersion.getCompanyId(),
            groupId,
            DLFileEntry.class.getName(),
            dlFileVersion.getFileVersionId());
      }

      dlFileEntryLocalService.updateFileEntryType(
          userId, dlFileEntry.getFileEntryId(), defaultFileEntryTypeId, serviceContext);

      dlAppHelperLocalService.updateAsset(
          userId,
          new LiferayFileEntry(dlFileEntry),
          new LiferayFileVersion(dlFileVersion),
          serviceContext.getAssetCategoryIds(),
          serviceContext.getAssetTagNames(),
          serviceContext.getAssetLinkEntryIds());
    }

    List<DLFolder> subFolders = dlFolderPersistence.findByG_M_P_H(groupId, false, folderId, false);

    for (DLFolder subFolder : subFolders) {
      long subFolderId = subFolder.getFolderId();

      if (subFolder.getRestrictionType() == DLFolderConstants.RESTRICTION_TYPE_INHERIT) {

        continue;
      }

      cascadeFileEntryTypes(
          userId, groupId, subFolderId, defaultFileEntryTypeId, fileEntryTypeIds, serviceContext);
    }
  }
  @Override
  public DLFileEntry updateFileEntryFileEntryType(
      DLFileEntry dlFileEntry, ServiceContext serviceContext) throws PortalException {

    long groupId = serviceContext.getScopeGroupId();
    long folderId = DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;

    DLFolder dlFolder = dlFolderPersistence.fetchByPrimaryKey(dlFileEntry.getFolderId());

    if (dlFolder != null) {
      groupId = dlFolder.getGroupId();
      folderId = dlFolder.getFolderId();
    }

    List<DLFileEntryType> dlFileEntryTypes =
        getFolderFileEntryTypes(
            PortalUtil.getCurrentAndAncestorSiteGroupIds(groupId), folderId, true);

    List<Long> fileEntryTypeIds = getFileEntryTypeIds(dlFileEntryTypes);

    if (fileEntryTypeIds.contains(dlFileEntry.getFileEntryTypeId())) {
      return dlFileEntry;
    }

    long defaultFileEntryTypeId = getDefaultFileEntryTypeId(folderId);

    DLFileVersion dlFileVersion =
        dlFileVersionLocalService.getLatestFileVersion(dlFileEntry.getFileEntryId(), true);

    if (dlFileVersion.isPending()) {
      workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
          dlFileVersion.getCompanyId(), dlFileEntry.getGroupId(),
          DLFileEntry.class.getName(), dlFileVersion.getFileVersionId());
    }

    return dlFileEntryLocalService.updateFileEntry(
        serviceContext.getUserId(),
        dlFileEntry.getFileEntryId(),
        null,
        null,
        null,
        null,
        null,
        false,
        defaultFileEntryTypeId,
        null,
        null,
        null,
        0,
        serviceContext);
  }
  public void updateStatuses(User user, List<Object> dlFileEntriesAndDLFolders, int status)
      throws PortalException, SystemException {

    for (Object object : dlFileEntriesAndDLFolders) {
      if (object instanceof DLFileEntry) {
        DLFileEntry dlFileEntry = (DLFileEntry) object;

        List<DLFileVersion> dlFileVersions =
            dlFileVersionLocalService.getFileVersions(
                dlFileEntry.getFileEntryId(), WorkflowConstants.STATUS_ANY);

        dlFileVersions = ListUtil.copy(dlFileVersions);

        Collections.sort(dlFileVersions, new FileVersionVersionComparator());

        DLFileVersion latestDlFileVersion = dlFileVersions.get(0);

        if ((status == WorkflowConstants.STATUS_APPROVED)
            && (latestDlFileVersion.getStatus() == WorkflowConstants.STATUS_IN_TRASH)) {

          continue;
        }

        // Asset

        if (status == WorkflowConstants.STATUS_APPROVED) {
          if (latestDlFileVersion.isApproved()) {
            assetEntryLocalService.updateVisible(
                DLFileEntryConstants.getClassName(), dlFileEntry.getFileEntryId(), true);
          }
        } else {
          assetEntryLocalService.moveEntryToTrash(
              DLFileEntryConstants.getClassName(), dlFileEntry.getFileEntryId());
        }

        // Social

        if (status == WorkflowConstants.STATUS_APPROVED) {
          socialActivityCounterLocalService.enableActivityCounters(
              DLFileEntryConstants.getClassName(), dlFileEntry.getFileEntryId());

          socialActivityLocalService.addActivity(
              user.getUserId(),
              dlFileEntry.getGroupId(),
              DLFileEntryConstants.getClassName(),
              dlFileEntry.getFileEntryId(),
              SocialActivityConstants.TYPE_RESTORE_FROM_TRASH,
              StringPool.BLANK,
              0);
        } else if (latestDlFileVersion.getStatus() == WorkflowConstants.STATUS_APPROVED) {

          socialActivityLocalService.addActivity(
              user.getUserId(),
              dlFileEntry.getGroupId(),
              DLFileEntryConstants.getClassName(),
              dlFileEntry.getFileEntryId(),
              SocialActivityConstants.TYPE_MOVE_TO_TRASH,
              StringPool.BLANK,
              0);
        }

        // Index

        Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(DLFileEntry.class);

        if (status == WorkflowConstants.STATUS_APPROVED) {
          indexer.reindex(dlFileEntry);
        } else {
          indexer.delete(dlFileEntry);
        }

        // Workflow

        if (status != WorkflowConstants.STATUS_APPROVED) {
          for (DLFileVersion dlFileVersion : dlFileVersions) {
            if (!dlFileVersion.isPending()) {
              continue;
            }

            dlFileVersion.setStatus(WorkflowConstants.STATUS_DRAFT);

            dlFileVersionPersistence.update(dlFileVersion, false);

            workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
                dlFileVersion.getCompanyId(),
                dlFileVersion.getGroupId(),
                DLFileEntryConstants.getClassName(),
                dlFileVersion.getFileVersionId());
          }
        }
      } else if (object instanceof DLFolder) {
        DLFolder dlFolder = (DLFolder) object;

        if (dlFolder.isInTrash()) {
          continue;
        }

        QueryDefinition queryDefinition = new QueryDefinition(WorkflowConstants.STATUS_ANY);

        List<Object> foldersAndFileEntriesAndFileShortcuts =
            dlFolderLocalService.getFoldersAndFileEntriesAndFileShortcuts(
                dlFolder.getGroupId(), dlFolder.getFolderId(), null, false, queryDefinition);

        updateStatuses(user, foldersAndFileEntriesAndFileShortcuts, status);
      }
    }
  }