@Override
  public void deletePortletRepository(long groupId, String portletId)
      throws PortalException, SystemException {

    Repository repository = RepositoryLocalServiceUtil.fetchRepository(groupId, portletId);

    if (repository != null) {
      RepositoryLocalServiceUtil.deleteRepository(repository.getRepositoryId());
    }
  }
  @Override
  public void deleteStagedModel(String uuid, long groupId, String className, String extraData)
      throws PortalException {

    Repository repository =
        RepositoryLocalServiceUtil.fetchRepositoryByUuidAndGroupId(uuid, groupId);

    if (repository != null) {
      RepositoryLocalServiceUtil.deleteRepository(repository.getRepositoryId());
    }
  }
  @Before
  public void setUp() throws Exception {
    ServiceTestUtil.setUser(TestPropsValues.getUser());

    ServiceContext serviceContext = ServiceContextTestUtil.getServiceContext();

    _repository =
        RepositoryLocalServiceUtil.addRepository(
            TestPropsValues.getUserId(),
            TestPropsValues.getGroupId(),
            ClassNameLocalServiceUtil.getClassNameId(_REPOSITORY_CLASS_NAME),
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            StringUtil.randomString(),
            StringUtil.randomString(),
            StringUtil.randomString(),
            new UnicodeProperties(),
            true,
            serviceContext);

    _repositoryEntry =
        RepositoryEntryLocalServiceUtil.createRepositoryEntry(_repository.getDlFolderId());

    _repositoryEntry.setUuid(serviceContext.getUuid());
    _repositoryEntry.setGroupId(serviceContext.getScopeGroupId());
    _repositoryEntry.setCompanyId(serviceContext.getCompanyId());
    _repositoryEntry.setUserId(serviceContext.getUserId());
    _repositoryEntry.setUserName(StringUtil.randomString());
    _repositoryEntry.setCreateDate(serviceContext.getCreateDate(null));
    _repositoryEntry.setModifiedDate(serviceContext.getModifiedDate(null));
    _repositoryEntry.setRepositoryId(_repository.getRepositoryId());
    _repositoryEntry.setMappedId(_MAPPED_ID);

    RepositoryEntryLocalServiceUtil.addRepositoryEntry(_repositoryEntry);
  }
  @Override
  public Repository addPortletRepository(
      long groupId, String portletId, ServiceContext serviceContext)
      throws PortalException, SystemException {

    Repository repository = RepositoryLocalServiceUtil.fetchRepository(groupId, portletId);

    if (repository != null) {
      return repository;
    }

    Group group = GroupLocalServiceUtil.getGroup(groupId);

    User user = UserLocalServiceUtil.getDefaultUser(group.getCompanyId());

    long classNameId = PortalUtil.getClassNameId(LiferayRepository.class.getName());

    UnicodeProperties typeSettingsProperties = new UnicodeProperties();

    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();

    try {
      DLAppHelperThreadLocal.setEnabled(false);

      return RepositoryLocalServiceUtil.addRepository(
          user.getUserId(),
          groupId,
          classNameId,
          DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
          portletId,
          StringPool.BLANK,
          portletId,
          typeSettingsProperties,
          true,
          serviceContext);
    } finally {
      DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
    }
  }
  @Override
  public boolean isInHiddenFolder() {
    try {
      Repository repository = RepositoryLocalServiceUtil.getRepository(getRepositoryId());

      long dlFolderId = repository.getDlFolderId();

      DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(dlFolderId);

      return dlFolder.isHidden();
    } catch (Exception e) {
    }

    return false;
  }
  @Override
  public boolean isInHiddenFolder() {
    try {
      long repositoryId = getRepositoryId();

      Repository repository = RepositoryLocalServiceUtil.getRepository(repositoryId);

      long dlFolderId = repository.getDlFolderId();

      DLFolder dlFolder = DLFolderLocalServiceUtil.getFolder(dlFolderId);

      return dlFolder.isHidden();
    } catch (Exception e) {
      if (_log.isWarnEnabled()) {
        _log.warn(e, e);
      }
    }

    return false;
  }
  public static void checkRepository(
      long repositoryId,
      Map<String, String> parameters,
      UnicodeProperties typeSettingsProperties,
      String typeSettingsKey)
      throws PortalException, RepositoryException {

    if (!typeSettingsProperties.containsKey(typeSettingsKey)) {
      org.apache.chemistry.opencmis.client.api.Repository cmisRepository =
          _sessionFactory.getRepositories(parameters).get(0);

      typeSettingsProperties.setProperty(typeSettingsKey, cmisRepository.getId());

      try {
        RepositoryLocalServiceUtil.updateRepository(repositoryId, typeSettingsProperties);
      } catch (PortalException | SystemException e) {
        throw new RepositoryException(e);
      }
    }

    parameters.put(
        SessionParameter.REPOSITORY_ID,
        getTypeSettingsValue(typeSettingsProperties, typeSettingsKey));
  }
  @Override
  public Repository getPortletRepository(long groupId, String portletId)
      throws PortalException, SystemException {

    return RepositoryLocalServiceUtil.getRepository(groupId, portletId);
  }
  @Override
  public Repository fetchPortletRepository(long groupId, String portletId) throws SystemException {

    return RepositoryLocalServiceUtil.fetchRepository(groupId, portletId);
  }
  @Override
  protected Document doGetDocument(Object obj) throws Exception {
    DLFileEntry dlFileEntry = (DLFileEntry) obj;

    if (_log.isDebugEnabled()) {
      _log.debug("Indexing document " + dlFileEntry);
    }

    boolean indexContent = true;

    InputStream is = null;

    try {
      if (PropsValues.DL_FILE_INDEXING_MAX_SIZE == 0) {
        indexContent = false;
      } else if (PropsValues.DL_FILE_INDEXING_MAX_SIZE != -1) {
        if (dlFileEntry.getSize() > PropsValues.DL_FILE_INDEXING_MAX_SIZE) {

          indexContent = false;
        }
      }

      if (indexContent) {
        String[] ignoreExtensions =
            PrefsPropsUtil.getStringArray(
                PropsKeys.DL_FILE_INDEXING_IGNORE_EXTENSIONS, StringPool.COMMA);

        if (ArrayUtil.contains(ignoreExtensions, StringPool.PERIOD + dlFileEntry.getExtension())) {

          indexContent = false;
        }
      }

      if (indexContent) {
        is = dlFileEntry.getFileVersion().getContentStream(false);
      }
    } catch (Exception e) {
    }

    DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();

    try {
      Document document = getBaseModelDocument(PORTLET_ID, dlFileEntry, dlFileVersion);

      if (indexContent) {
        if (is != null) {
          try {
            document.addFile(Field.CONTENT, is, dlFileEntry.getTitle());
          } catch (IOException ioe) {
            throw new SearchException("Cannot extract text from file" + dlFileEntry);
          }
        } else if (_log.isDebugEnabled()) {
          _log.debug("Document " + dlFileEntry + " does not have any content");
        }
      }

      document.addKeyword(Field.CLASS_TYPE_ID, dlFileEntry.getFileEntryTypeId());
      document.addText(Field.DESCRIPTION, dlFileEntry.getDescription());
      document.addKeyword(Field.FOLDER_ID, dlFileEntry.getFolderId());
      document.addKeyword(Field.HIDDEN, dlFileEntry.isInHiddenFolder());
      document.addText(Field.PROPERTIES, dlFileEntry.getLuceneProperties());
      document.addText(Field.TITLE, dlFileEntry.getTitle());

      document.addKeyword("dataRepositoryId", dlFileEntry.getDataRepositoryId());
      document.addKeyword("extension", dlFileEntry.getExtension());
      document.addKeyword("fileEntryTypeId", dlFileEntry.getFileEntryTypeId());
      document.addKeyword(
          "mimeType",
          StringUtil.replace(
              dlFileEntry.getMimeType(), CharPool.FORWARD_SLASH, CharPool.UNDERLINE));
      document.addKeyword("path", dlFileEntry.getTitle());
      document.addKeyword("readCount", dlFileEntry.getReadCount());
      document.addKeyword("size", dlFileEntry.getSize());

      ExpandoBridge expandoBridge =
          ExpandoBridgeFactoryUtil.getExpandoBridge(
              dlFileEntry.getCompanyId(),
              DLFileEntry.class.getName(),
              dlFileVersion.getFileVersionId());

      ExpandoBridgeIndexerUtil.addAttributes(document, expandoBridge);

      addFileEntryTypeAttributes(document, dlFileVersion);

      if (dlFileEntry.isInHiddenFolder()) {
        try {
          Repository repository =
              RepositoryLocalServiceUtil.getRepository(dlFileEntry.getRepositoryId());

          String portletId = repository.getPortletId();

          for (Indexer indexer : IndexerRegistryUtil.getIndexers()) {
            if (portletId.equals(indexer.getPortletId())) {
              indexer.addRelatedEntryFields(document, obj);
            }
          }
        } catch (Exception e) {
        }
      }

      if (!dlFileVersion.isInTrash() && dlFileVersion.isInTrashContainer()) {

        DLFolder folder = dlFileVersion.getTrashContainer();

        addTrashFields(
            document,
            DLFolder.class.getName(),
            folder.getFolderId(),
            null,
            null,
            DLFileEntryAssetRendererFactory.TYPE);

        document.addKeyword(Field.ROOT_ENTRY_CLASS_NAME, DLFolder.class.getName());
        document.addKeyword(Field.ROOT_ENTRY_CLASS_PK, folder.getFolderId());
        document.addKeyword(Field.STATUS, WorkflowConstants.STATUS_IN_TRASH);
      }

      if (_log.isDebugEnabled()) {
        _log.debug("Document " + dlFileEntry + " indexed successfully");
      }

      return document;
    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (IOException ioe) {
        }
      }
    }
  }
  @Override
  protected void doImportStagedModel(PortletDataContext portletDataContext, Repository repository)
      throws Exception {

    long userId = portletDataContext.getUserId(repository.getUserUuid());

    ServiceContext serviceContext = portletDataContext.createServiceContext(repository);

    Repository importedRepository = null;

    Element repositoryElement = portletDataContext.getImportDataStagedModelElement(repository);

    try {
      boolean hidden = GetterUtil.getBoolean(repositoryElement.attributeValue("hidden"));

      if (portletDataContext.isDataStrategyMirror()) {
        Repository existingRepository =
            RepositoryLocalServiceUtil.fetchRepositoryByUuidAndGroupId(
                repository.getUuid(), portletDataContext.getScopeGroupId());

        if (existingRepository == null) {
          existingRepository =
              RepositoryLocalServiceUtil.fetchRepository(
                  portletDataContext.getScopeGroupId(), repository.getName());
        }

        long classNameId = 0;

        if (existingRepository != null) {
          classNameId = existingRepository.getClassNameId();
        }

        if ((existingRepository == null)
            || (classNameId != PortalUtil.getClassNameId(LiferayRepository.class))) {

          serviceContext.setUuid(repository.getUuid());

          importedRepository =
              RepositoryLocalServiceUtil.addRepository(
                  userId,
                  portletDataContext.getScopeGroupId(),
                  repository.getClassNameId(),
                  DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
                  repository.getName(),
                  repository.getDescription(),
                  repository.getPortletId(),
                  repository.getTypeSettingsProperties(),
                  hidden,
                  serviceContext);
        } else {
          RepositoryLocalServiceUtil.updateRepository(
              existingRepository.getRepositoryId(),
              repository.getName(),
              repository.getDescription());

          importedRepository = existingRepository;
        }
      } else {
        importedRepository =
            RepositoryLocalServiceUtil.addRepository(
                userId,
                portletDataContext.getScopeGroupId(),
                repository.getClassNameId(),
                DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
                repository.getName(),
                repository.getDescription(),
                repository.getPortletId(),
                repository.getTypeSettingsProperties(),
                hidden,
                serviceContext);
      }
    } catch (Exception e) {
      if (_log.isWarnEnabled()) {
        _log.warn(
            "Unable to connect to repository {name="
                + repository.getName()
                + ", typeSettings="
                + repository.getTypeSettingsProperties()
                + "}",
            e);
      }
    }

    portletDataContext.importClassedModel(repository, importedRepository);

    StagedModelDataHandlerUtil.importReferenceStagedModels(
        portletDataContext, repository, RepositoryEntry.class);
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, FileEntry fileEntry)
      throws Exception {

    Element fileEntryElement = portletDataContext.getExportDataElement(fileEntry);

    String fileEntryPath = ExportImportPathUtil.getModelPath(fileEntry);

    if (!fileEntry.isDefaultRepository()) {
      Repository repository = RepositoryLocalServiceUtil.getRepository(fileEntry.getRepositoryId());

      StagedModelDataHandlerUtil.exportReferenceStagedModel(
          portletDataContext, fileEntry, repository, PortletDataContext.REFERENCE_TYPE_STRONG);

      portletDataContext.addClassedModel(fileEntryElement, fileEntryPath, fileEntry);

      long portletRepositoryClassNameId =
          PortalUtil.getClassNameId(PortletRepository.class.getName());

      if (repository.getClassNameId() != portletRepositoryClassNameId) {
        return;
      }
    }

    if (fileEntry.getFolderId() != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {

      StagedModelDataHandlerUtil.exportReferenceStagedModel(
          portletDataContext,
          fileEntry,
          fileEntry.getFolder(),
          PortletDataContext.REFERENCE_TYPE_PARENT);
    }

    LiferayFileEntry liferayFileEntry = (LiferayFileEntry) fileEntry;

    liferayFileEntry.setCachedFileVersion(fileEntry.getFileVersion());

    if (!portletDataContext.isPerformDirectBinaryImport()) {
      InputStream is = null;

      try {
        is = FileEntryUtil.getContentStream(fileEntry);
      } catch (Exception e) {
        if (_log.isWarnEnabled()) {
          _log.warn("Unable to retrieve content for file entry " + fileEntry.getFileEntryId(), e);
        }
      }

      if (is == null) {
        fileEntryElement.detach();

        return;
      }

      try {
        String binPath = ExportImportPathUtil.getModelPath(fileEntry, fileEntry.getVersion());

        portletDataContext.addZipEntry(binPath, is);

        fileEntryElement.addAttribute("bin-path", binPath);
      } finally {
        try {
          is.close();
        } catch (IOException ioe) {
          _log.error(ioe, ioe);
        }
      }
    }

    if (portletDataContext.getBooleanParameter(
        DLPortletDataHandler.NAMESPACE, "previews-and-thumbnails")) {

      DLProcessorRegistryUtil.exportGeneratedFiles(portletDataContext, fileEntry, fileEntryElement);
    }

    exportMetaData(portletDataContext, fileEntryElement, fileEntry);

    portletDataContext.addClassedModel(
        fileEntryElement, fileEntryPath, liferayFileEntry, DLFileEntry.class);
  }