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 {
        Repository repository = RepositoryUtil.findByPrimaryKey(repositoryId);

        repository.setTypeSettingsProperties(typeSettingsProperties);

        RepositoryUtil.update(repository, false);
      } catch (Exception e) {
        throw new RepositoryException(e);
      }
    }

    parameters.put(
        SessionParameter.REPOSITORY_ID,
        getTypeSettingsValue(typeSettingsProperties, typeSettingsKey));
  }
  /** @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 Folder addAttachmentsFolder() throws PortalException, SystemException {

    if (_attachmentsFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {

      return PortletFileRepositoryUtil.getPortletFolder(_attachmentsFolderId);
    }

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);

    Repository repository =
        PortletFileRepositoryUtil.addPortletRepository(
            getGroupId(), PortletKeys.MESSAGE_BOARDS, serviceContext);

    MBThread thread = getThread();

    Folder threadFolder = thread.addAttachmentsFolder();

    Folder folder =
        PortletFileRepositoryUtil.addPortletFolder(
            getUserId(),
            repository.getRepositoryId(),
            threadFolder.getFolderId(),
            String.valueOf(getMessageId()),
            serviceContext);

    _attachmentsFolderId = folder.getFolderId();

    return folder;
  }
  @Override
  public Folder addAttachmentsFolder() throws PortalException {
    if (_attachmentsFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {

      return PortletFileRepositoryUtil.getPortletFolder(_attachmentsFolderId);
    }

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);

    Repository repository =
        PortletFileRepositoryUtil.addPortletRepository(
            getGroupId(), PortletKeys.BACKGROUND_TASK, serviceContext);

    Folder folder =
        PortletFileRepositoryUtil.addPortletFolder(
            getUserId(),
            repository.getRepositoryId(),
            DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
            String.valueOf(getBackgroundTaskId()),
            serviceContext);

    _attachmentsFolderId = folder.getFolderId();

    return folder;
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, Repository repository)
      throws Exception {

    Element repositoryElement = portletDataContext.getExportDataElement(repository);

    Folder folder = DLAppLocalServiceUtil.getFolder(repository.getDlFolderId());

    if (folder.getModel() instanceof DLFolder) {
      DLFolder dlFolder = (DLFolder) folder.getModel();

      repositoryElement.addAttribute("hidden", String.valueOf(dlFolder.isHidden()));
    }

    portletDataContext.addClassedModel(
        repositoryElement, ExportImportPathUtil.getModelPath(repository), repository);

    List<RepositoryEntry> repositoryEntries =
        RepositoryEntryLocalServiceUtil.getRepositoryEntries(repository.getRepositoryId());

    for (RepositoryEntry repositoryEntry : repositoryEntries) {
      StagedModelDataHandlerUtil.exportReferenceStagedModel(
          portletDataContext, repository, repositoryEntry, PortletDataContext.REFERENCE_TYPE_CHILD);
    }
  }
  @Override
  public long getAttachmentsFolderId() {
    if (_attachmentsFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {

      return _attachmentsFolderId;
    }

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);

    Repository repository =
        PortletFileRepositoryUtil.fetchPortletRepository(getGroupId(), PortletKeys.BACKGROUND_TASK);

    if (repository == null) {
      return DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
    }

    try {
      Folder folder =
          PortletFileRepositoryUtil.getPortletFolder(
              repository.getRepositoryId(),
              DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
              String.valueOf(getBackgroundTaskId()));

      _attachmentsFolderId = folder.getFolderId();
    } catch (Exception e) {
    }

    return _attachmentsFolderId;
  }
  @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());
    }
  }
  @Override
  public FileEntry addPortletFileEntry(
      long groupId,
      long userId,
      String className,
      long classPK,
      String portletId,
      long folderId,
      File file,
      String fileName,
      String mimeType,
      boolean indexingEnabled)
      throws PortalException, SystemException {

    if (Validator.isNull(fileName)) {
      return null;
    }

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);

    Repository repository = addPortletRepository(groupId, portletId, serviceContext);

    serviceContext.setAttribute("className", className);
    serviceContext.setAttribute("classPK", String.valueOf(classPK));
    serviceContext.setIndexingEnabled(indexingEnabled);

    if (Validator.isNull(mimeType) || mimeType.equals(ContentTypes.APPLICATION_OCTET_STREAM)) {

      mimeType = MimeTypesUtil.getContentType(file, fileName);
    }

    boolean dlAppHelperEnabled = DLAppHelperThreadLocal.isEnabled();

    try {
      DLAppHelperThreadLocal.setEnabled(false);

      return DLAppLocalServiceUtil.addFileEntry(
          userId,
          repository.getRepositoryId(),
          folderId,
          fileName,
          mimeType,
          fileName,
          StringPool.BLANK,
          StringPool.BLANK,
          file,
          serviceContext);
    } finally {
      DLAppHelperThreadLocal.setEnabled(dlAppHelperEnabled);
    }
  }
  protected void initByRepositoryId(long repositoryId) {
    _repositoryId = repositoryId;
    _groupId = repositoryId;

    try {
      Repository repository = repositoryService.getRepository(repositoryId);

      _repositoryId = repository.getRepositoryId();
      _groupId = repository.getGroupId();
      _dlFolderId = repository.getDlFolderId();
    } catch (Exception e) {
    }
  }
  @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
  protected void initByRepositoryId(long repositoryId) {
    setGroupId(repositoryId);
    setRepositoryId(repositoryId);

    try {
      com.liferay.portal.model.Repository repository =
          repositoryService.getRepository(repositoryId);

      setDlFolderId(repository.getDlFolderId());
      setGroupId(repository.getGroupId());
      setRepositoryId(repository.getRepositoryId());
    } catch (Exception e) {
    }
  }
  @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;
  }
  @Override
  public long getAttachmentsFolderId() throws PortalException, SystemException {

    if (_attachmentsFolderId != DLFolderConstants.DEFAULT_PARENT_FOLDER_ID) {

      return _attachmentsFolderId;
    }

    ServiceContext serviceContext = new ServiceContext();

    serviceContext.setAddGroupPermissions(true);
    serviceContext.setAddGuestPermissions(true);

    Repository repository =
        PortletFileRepositoryUtil.fetchPortletRepository(getGroupId(), PortletKeys.MESSAGE_BOARDS);

    long threadAttachmetsFolderId = getThreadAttachmentsFolderId();

    if ((repository == null)
        || (threadAttachmetsFolderId == DLFolderConstants.DEFAULT_PARENT_FOLDER_ID)) {

      return DLFolderConstants.DEFAULT_PARENT_FOLDER_ID;
    }

    try {
      Folder folder =
          PortletFileRepositoryUtil.getPortletFolder(
              getUserId(),
              repository.getRepositoryId(),
              threadAttachmetsFolderId,
              String.valueOf(getMessageId()),
              serviceContext);

      _attachmentsFolderId = folder.getFolderId();
    } catch (Exception e) {
    }

    return _attachmentsFolderId;
  }
 @Override
 public String getDisplayName(Repository repository) {
   return repository.getName();
 }
  @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);
  }
  @Indexable(type = IndexableType.REINDEX)
  public Album updateAlbum(
      long userId,
      long albumId,
      long artistId,
      String name,
      int year,
      InputStream inputStream,
      ServiceContext serviceContext)
      throws PortalException {

    // Event

    User user = userPersistence.findByPrimaryKey(userId);

    validate(name);

    Album album = albumPersistence.findByPrimaryKey(albumId);

    album.setModifiedDate(serviceContext.getModifiedDate(null));
    album.setArtistId(artistId);
    album.setName(name);
    album.setYear(year);
    album.setExpandoBridgeAttributes(serviceContext);

    albumPersistence.update(album);

    if (inputStream != null) {
      Repository repository =
          PortletFileRepositoryUtil.fetchPortletRepository(
              serviceContext.getScopeGroupId(), Constants.JUKEBOX_PORTLET_REPOSITORY);

      if (repository != null) {
        try {
          PortletFileRepositoryUtil.deletePortletFileEntry(
              repository.getRepositoryId(),
              DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
              String.valueOf(album.getAlbumId()));
        } catch (Exception e) {
          if (_log.isDebugEnabled()) {
            _log.debug("Cannot delete album cover");
          }
        }
      }

      PortletFileRepositoryUtil.addPortletFileEntry(
          serviceContext.getScopeGroupId(),
          userId,
          Album.class.getName(),
          album.getAlbumId(),
          Constants.JUKEBOX_PORTLET_REPOSITORY,
          DLFolderConstants.DEFAULT_PARENT_FOLDER_ID,
          inputStream,
          String.valueOf(album.getAlbumId()),
          StringPool.BLANK,
          true);
    }

    // Asset

    updateAsset(
        userId,
        album,
        serviceContext.getAssetCategoryIds(),
        serviceContext.getAssetTagNames(),
        serviceContext.getAssetLinkEntryIds());

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