@Override
  public SyncDLObjectUpdate getSyncDLObjectUpdate(
      long companyId, long repositoryId, long lastAccessTime) throws PortalException {

    try {
      SyncUtil.checkSyncEnabled(repositoryId);

      repositoryService.checkRepository(repositoryId);

      List<SyncDLObject> syncDLObjects =
          syncDLObjectFinder.filterFindByC_M_R_P(companyId, lastAccessTime, repositoryId, -1);

      if (!InlineSQLHelperUtil.isEnabled(repositoryId)) {
        syncDLObjects = checkSyncDLObjects(syncDLObjects);
      }

      for (SyncDLObject syncDLObject : syncDLObjects) {
        if (syncDLObject.getModifiedTime() > lastAccessTime) {
          lastAccessTime = syncDLObject.getModifiedTime();
        }
      }

      return new SyncDLObjectUpdate(syncDLObjects, lastAccessTime);
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  /** @deprecated As of 7.0.0, with no direct replacement */
  @Deprecated
  @Override
  public SyncDLObjectUpdate getAllSyncDLObjects(long repositoryId, long folderId)
      throws PortalException {

    try {
      SyncUtil.checkSyncEnabled(repositoryId);

      long lastAccessTime = System.currentTimeMillis();

      long companyId = 0;

      Repository repository = repositoryLocalService.fetchRepository(repositoryId);

      if (repository != null) {
        companyId = repository.getCompanyId();
      } else {
        Group group = groupLocalService.getGroup(repositoryId);

        companyId = group.getCompanyId();
      }

      List<SyncDLObject> syncDLObjects =
          syncDLObjectPersistence.findByC_M_R(companyId, 0, repositoryId);

      return new SyncDLObjectUpdate(syncDLObjects, lastAccessTime);
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public SyncDLObjectUpdate getSyncDLObjectUpdate(
      long companyId, long repositoryId, long parentFolderId, long lastAccessTime)
      throws PortalException {

    try {
      SyncUtil.checkSyncEnabled(repositoryId);

      repositoryService.checkRepository(repositoryId);

      List<SyncDLObject> syncDLObjects = new ArrayList<>();

      if (parentFolderId > 0) {
        PermissionChecker permissionChecker = getPermissionChecker();

        if (permissionChecker.hasPermission(
            repositoryId, DLFolderConstants.getClassName(), parentFolderId, ActionKeys.VIEW)) {

          SyncDLObject syncDLObject =
              syncDLObjectPersistence.fetchByT_T(SyncConstants.TYPE_FOLDER, parentFolderId);

          if (syncDLObject != null) {
            syncDLObjects.add(syncDLObject);
          }
        }
      }

      syncDLObjects =
          getSyncDLObjects(syncDLObjects, companyId, repositoryId, parentFolderId, lastAccessTime);

      return new SyncDLObjectUpdate(syncDLObjects, lastAccessTime);
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public List<SyncDLObject> getFolderSyncDLObjects(long repositoryId, long parentFolderId)
      throws PortalException {

    try {
      SyncUtil.checkSyncEnabled(repositoryId);

      List<Folder> folders = dlAppService.getFolders(repositoryId, parentFolderId);

      List<SyncDLObject> syncDLObjects = new ArrayList<>(folders.size());

      for (Folder folder : folders) {
        if (!SyncUtil.isSupportedFolder(folder)) {
          continue;
        }

        SyncDLObject syncDLObject = toSyncDLObject(folder, SyncConstants.EVENT_GET);

        syncDLObjects.add(syncDLObject);
      }

      return syncDLObjects;
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public Group getGroup(long groupId) throws PortalException {
    try {
      SyncUtil.checkSyncEnabled(groupId);

      return groupService.getGroup(groupId);
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public SyncDLObject getFileEntrySyncDLObject(long groupId, long folderId, String title)
      throws PortalException {

    try {
      SyncUtil.checkSyncEnabled(groupId);

      FileEntry fileEntry = dlAppService.getFileEntry(groupId, folderId, title);

      return toSyncDLObject(fileEntry, SyncConstants.EVENT_GET);
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public SyncDLObject moveFileEntry(
      long fileEntryId, long newFolderId, ServiceContext serviceContext) throws PortalException {

    try {
      FileEntry fileEntry = dlAppLocalService.getFileEntry(fileEntryId);

      SyncUtil.checkSyncEnabled(fileEntry.getGroupId());

      fileEntry = dlAppService.moveFileEntry(fileEntryId, newFolderId, serviceContext);

      return toSyncDLObject(fileEntry, SyncConstants.EVENT_MOVE);
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public SyncDLObject moveFolder(long folderId, long parentFolderId, ServiceContext serviceContext)
      throws PortalException {

    try {
      Folder folder = dlAppLocalService.getFolder(folderId);

      SyncUtil.checkSyncEnabled(folder.getGroupId());

      folder = dlAppService.moveFolder(folderId, parentFolderId, serviceContext);

      return toSyncDLObject(folder, SyncConstants.EVENT_MOVE);
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public SyncDLObject updateFolder(
      long folderId, String name, String description, ServiceContext serviceContext)
      throws PortalException {

    try {
      Folder folder = dlAppLocalService.getFolder(folderId);

      SyncUtil.checkSyncEnabled(folder.getGroupId());

      folder = dlAppService.updateFolder(folderId, name, description, serviceContext);

      return toSyncDLObject(folder, SyncConstants.EVENT_UPDATE);
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public SyncDLObject cancelCheckOut(long fileEntryId) throws PortalException {

    try {
      FileEntry fileEntry = dlAppLocalService.getFileEntry(fileEntryId);

      SyncUtil.checkSyncEnabled(fileEntry.getGroupId());

      dlAppService.cancelCheckOut(fileEntryId);

      fileEntry = dlAppLocalService.getFileEntry(fileEntryId);

      return toSyncDLObject(fileEntry, SyncConstants.EVENT_CANCEL_CHECK_OUT);
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public SyncDLObject restoreFolderFromTrash(long folderId) throws PortalException {

    try {
      Folder folder = dlAppLocalService.getFolder(folderId);

      SyncUtil.checkSyncEnabled(folder.getGroupId());

      dlAppService.restoreFolderFromTrash(folderId);

      folder = dlAppLocalService.getFolder(folderId);

      return toSyncDLObject(folder, SyncConstants.EVENT_RESTORE);
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public SyncDLObject checkOutFileEntry(
      long fileEntryId, String owner, long expirationTime, ServiceContext serviceContext)
      throws PortalException {

    try {
      FileEntry fileEntry = dlAppLocalService.getFileEntry(fileEntryId);

      SyncUtil.checkSyncEnabled(fileEntry.getGroupId());

      fileEntry =
          dlAppService.checkOutFileEntry(fileEntryId, owner, expirationTime, serviceContext);

      return toSyncDLObject(fileEntry, SyncConstants.EVENT_CHECK_OUT);
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public SyncDLObject getFolderSyncDLObject(long repositoryId, long parentFolderId, String name)
      throws PortalException {

    try {
      SyncUtil.checkSyncEnabled(repositoryId);

      Folder folder = dlAppService.getFolder(repositoryId, parentFolderId, name);

      if (!SyncUtil.isSupportedFolder(folder)) {
        return null;
      }

      return toSyncDLObject(folder, SyncConstants.EVENT_GET);
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public SyncDLObject checkInFileEntry(
      long fileEntryId, boolean majorVersion, String changeLog, ServiceContext serviceContext)
      throws PortalException {

    try {
      FileEntry fileEntry = dlAppLocalService.getFileEntry(fileEntryId);

      SyncUtil.checkSyncEnabled(fileEntry.getGroupId());

      dlAppService.checkInFileEntry(fileEntryId, majorVersion, changeLog, serviceContext);

      fileEntry = dlAppLocalService.getFileEntry(fileEntryId);

      return toSyncDLObject(fileEntry, SyncConstants.EVENT_CHECK_IN);
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public SyncDLObject getFolderSyncDLObject(long folderId) throws PortalException {

    try {
      Folder folder = dlAppLocalService.getFolder(folderId);

      SyncUtil.checkSyncEnabled(folder.getGroupId());

      folder = dlAppService.getFolder(folderId);

      if (!SyncUtil.isSupportedFolder(folder)) {
        return null;
      }

      return toSyncDLObject(folder, SyncConstants.EVENT_GET);
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public List<SyncDLObject> getAllFolderSyncDLObjects(long companyId, long repositoryId)
      throws PortalException {

    try {
      SyncUtil.checkSyncEnabled(repositoryId);

      repositoryService.checkRepository(repositoryId);

      List<SyncDLObject> syncDLObjects =
          syncDLObjectFinder.filterFindByC_R(companyId, repositoryId);

      if (!InlineSQLHelperUtil.isEnabled(repositoryId)) {
        return checkSyncDLObjects(syncDLObjects);
      }

      return syncDLObjects;
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public SyncDLObject addFolder(
      long repositoryId,
      long parentFolderId,
      String name,
      String description,
      ServiceContext serviceContext)
      throws PortalException {

    try {
      Group group = groupLocalService.getGroup(repositoryId);

      SyncUtil.checkSyncEnabled(group.getGroupId());

      if (!group.isUser() && (serviceContext.getGroupPermissions() == null)) {

        SyncUtil.setFilePermissions(group, true, serviceContext);
      }

      Folder folder =
          dlAppService.addFolder(repositoryId, parentFolderId, name, description, serviceContext);

      return toSyncDLObject(folder, SyncConstants.EVENT_ADD);
    } catch (PortalException pe) {
      if (pe instanceof DuplicateFolderNameException) {
        if (GetterUtil.getBoolean(serviceContext.getAttribute("overwrite"))) {

          Folder folder = dlAppService.getFolder(repositoryId, parentFolderId, name);

          folder =
              dlAppService.updateFolder(folder.getFolderId(), name, description, serviceContext);

          return toSyncDLObject(folder, SyncConstants.EVENT_UPDATE);
        }
      }

      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public SyncDLObject moveFolderToTrash(long folderId) throws PortalException {

    try {
      Folder folder = dlAppLocalService.getFolder(folderId);

      SyncUtil.checkSyncEnabled(folder.getGroupId());

      if (TrashUtil.isInTrash(DLFolderConstants.getClassName(), folderId)) {

        return null;
      }

      folder = dlAppService.moveFolderToTrash(folderId);

      return toSyncDLObject(folder, SyncConstants.EVENT_TRASH);
    } catch (NoSuchFolderException nsfe) {
      return null;
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public SyncDLObject moveFileEntryToTrash(long fileEntryId) throws PortalException {

    try {
      FileEntry fileEntry = dlAppLocalService.getFileEntry(fileEntryId);

      SyncUtil.checkSyncEnabled(fileEntry.getGroupId());

      if (TrashUtil.isInTrash(DLFileEntryConstants.getClassName(), fileEntryId)) {

        return null;
      }

      fileEntry = dlAppService.moveFileEntryToTrash(fileEntryId);

      return toSyncDLObject(fileEntry, SyncConstants.EVENT_TRASH);
    } catch (NoSuchFileEntryException nsfee) {
      return null;
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public List<SyncDLObject> getFileEntrySyncDLObjects(long repositoryId, long folderId)
      throws PortalException {

    try {
      SyncUtil.checkSyncEnabled(repositoryId);

      List<FileEntry> fileEntries = dlAppService.getFileEntries(repositoryId, folderId);

      List<SyncDLObject> syncDLObjects = new ArrayList<>(fileEntries.size());

      for (FileEntry fileEntry : fileEntries) {
        SyncDLObject syncDLObject = toSyncDLObject(fileEntry, SyncConstants.EVENT_GET);

        syncDLObjects.add(syncDLObject);
      }

      return syncDLObjects;
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public SyncDLObject updateFileEntry(
      long fileEntryId,
      String sourceFileName,
      String mimeType,
      String title,
      String description,
      String changeLog,
      boolean majorVersion,
      File file,
      String checksum,
      ServiceContext serviceContext)
      throws PortalException {

    try {
      FileEntry fileEntry = dlAppLocalService.getFileEntry(fileEntryId);

      SyncUtil.checkSyncEnabled(fileEntry.getGroupId());

      fileEntry =
          dlAppService.updateFileEntry(
              fileEntryId,
              sourceFileName,
              mimeType,
              title,
              description,
              changeLog,
              majorVersion,
              file,
              serviceContext);

      return toSyncDLObject(fileEntry, SyncConstants.EVENT_UPDATE);
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public SyncDLObject addFileEntry(
      long repositoryId,
      long folderId,
      String sourceFileName,
      String mimeType,
      String title,
      String description,
      String changeLog,
      File file,
      String checksum,
      ServiceContext serviceContext)
      throws PortalException {

    try {
      Group group = groupLocalService.getGroup(repositoryId);

      SyncUtil.checkSyncEnabled(group.getGroupId());

      if (!group.isUser() && (serviceContext.getGroupPermissions() == null)) {

        SyncUtil.setFilePermissions(group, false, serviceContext);
      }

      FileEntry fileEntry =
          dlAppService.addFileEntry(
              repositoryId,
              folderId,
              sourceFileName,
              mimeType,
              title,
              description,
              changeLog,
              file,
              serviceContext);

      return toSyncDLObject(fileEntry, SyncConstants.EVENT_ADD);
    } catch (PortalException pe) {
      if (pe instanceof DuplicateFileException) {
        if (GetterUtil.getBoolean(serviceContext.getAttribute("overwrite"))) {

          FileEntry fileEntry = dlAppService.getFileEntry(repositoryId, folderId, title);

          fileEntry =
              dlAppService.updateFileEntry(
                  fileEntry.getFileEntryId(),
                  sourceFileName,
                  mimeType,
                  title,
                  description,
                  changeLog,
                  false,
                  file,
                  serviceContext);

          return toSyncDLObject(fileEntry, SyncConstants.EVENT_UPDATE);
        }
      }

      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    }
  }
  @Override
  public SyncDLObject patchFileEntry(
      long fileEntryId,
      long sourceVersionId,
      String sourceFileName,
      String mimeType,
      String title,
      String description,
      String changeLog,
      boolean majorVersion,
      File deltaFile,
      String checksum,
      ServiceContext serviceContext)
      throws PortalException {

    File patchedFile = null;

    try {
      FileEntry fileEntry = dlAppLocalService.getFileEntry(fileEntryId);

      SyncUtil.checkSyncEnabled(fileEntry.getGroupId());

      DLFileVersion dlFileVersion = dlFileVersionLocalService.getDLFileVersion(sourceVersionId);

      File sourceFile =
          dlFileEntryLocalService.getFile(fileEntryId, dlFileVersion.getVersion(), false);

      patchedFile = FileUtil.createTempFile();

      SyncUtil.patchFile(sourceFile, deltaFile, patchedFile);

      SyncDLObject syncDLObject =
          updateFileEntry(
              fileEntryId,
              sourceFileName,
              mimeType,
              title,
              description,
              changeLog,
              majorVersion,
              patchedFile,
              checksum,
              serviceContext);

      if (PortletPropsValues.SYNC_FILE_DIFF_CACHE_ENABLED
          && (sourceVersionId != syncDLObject.getVersionId())) {

        DLFileVersion targetDLFileVersion =
            dlFileVersionLocalService.getFileVersion(syncDLObject.getVersionId());

        syncDLFileVersionDiffLocalService.addSyncDLFileVersionDiff(
            fileEntryId, sourceVersionId, targetDLFileVersion.getFileVersionId(), deltaFile);
      }

      return syncDLObject;
    } catch (PortalException pe) {
      throw new PortalException(SyncUtil.buildExceptionMessage(pe), pe);
    } finally {
      FileUtil.delete(patchedFile);
    }
  }