@Override
  public Map<String, DDMFormValues> getDDMFormValuesMap(long fileVersionId) throws PortalException {

    Map<String, DDMFormValues> ddmFormValuesMap = new HashMap<>();

    DLFileVersion dlFileVersion = DLFileVersionLocalServiceUtil.getFileVersion(fileVersionId);

    long fileEntryTypeId = dlFileVersion.getFileEntryTypeId();

    if (fileEntryTypeId <= 0) {
      return ddmFormValuesMap;
    }

    DLFileEntryType dlFileEntryType = getDLFileEntryType();

    List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();

    for (DDMStructure ddmStructure : ddmStructures) {
      DLFileEntryMetadata dlFileEntryMetadata =
          DLFileEntryMetadataLocalServiceUtil.fetchFileEntryMetadata(
              ddmStructure.getStructureId(), fileVersionId);

      if (dlFileEntryMetadata != null) {
        DDMFormValues ddmFormValues =
            StorageEngineUtil.getDDMFormValues(dlFileEntryMetadata.getDDMStorageId());

        ddmFormValuesMap.put(ddmStructure.getStructureKey(), ddmFormValues);
      }
    }

    return ddmFormValuesMap;
  }
  protected void assertLatestFileVersionTitle(String fileName) throws PortalException {

    DLFileVersion latestDLFileVersion =
        DLFileVersionLocalServiceUtil.getLatestFileVersion(_fileEntry.getFileEntryId(), false);

    Assert.assertEquals(fileName, latestDLFileVersion.getTitle());
  }
  protected void addFileEntryTypeAttributes(Document document, DLFileVersion dlFileVersion)
      throws PortalException, SystemException {

    DLFileEntryType dlFileEntryType =
        DLFileEntryTypeLocalServiceUtil.getDLFileEntryType(dlFileVersion.getFileEntryTypeId());

    List<DDMStructure> ddmStructures = dlFileEntryType.getDDMStructures();

    for (DDMStructure ddmStructure : ddmStructures) {
      Fields fields = null;

      try {
        DLFileEntryMetadata fileEntryMetadata =
            DLFileEntryMetadataLocalServiceUtil.getFileEntryMetadata(
                ddmStructure.getStructureId(), dlFileVersion.getFileVersionId());

        fields = StorageEngineUtil.getFields(fileEntryMetadata.getDDMStorageId());
      } catch (Exception e) {
      }

      if (fields != null) {
        DDMIndexerUtil.addAttributes(document, ddmStructure, fields);
      }
    }
  }
  @Test
  public void testResetOriginalValues() throws Exception {
    if (!PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
      return;
    }

    DLFileVersion newDLFileVersion = addDLFileVersion();

    _persistence.clearCache();

    DLFileVersionModelImpl existingDLFileVersionModelImpl =
        (DLFileVersionModelImpl) _persistence.findByPrimaryKey(newDLFileVersion.getPrimaryKey());

    Assert.assertTrue(
        Validator.equals(
            existingDLFileVersionModelImpl.getUuid(),
            existingDLFileVersionModelImpl.getOriginalUuid()));
    Assert.assertEquals(
        existingDLFileVersionModelImpl.getGroupId(),
        existingDLFileVersionModelImpl.getOriginalGroupId());

    Assert.assertEquals(
        existingDLFileVersionModelImpl.getFileEntryId(),
        existingDLFileVersionModelImpl.getOriginalFileEntryId());
    Assert.assertTrue(
        Validator.equals(
            existingDLFileVersionModelImpl.getVersion(),
            existingDLFileVersionModelImpl.getOriginalVersion()));
  }
  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);
    }
  }
  @Test
  public void testFetchByPrimaryKeyExisting() throws Exception {
    DLFileVersion newDLFileVersion = addDLFileVersion();

    DLFileVersion existingDLFileVersion =
        _persistence.fetchByPrimaryKey(newDLFileVersion.getPrimaryKey());

    Assert.assertEquals(existingDLFileVersion, newDLFileVersion);
  }
  @Test
  public void testCreate() throws Exception {
    long pk = RandomTestUtil.nextLong();

    DLFileVersion dlFileVersion = _persistence.create(pk);

    Assert.assertNotNull(dlFileVersion);

    Assert.assertEquals(dlFileVersion.getPrimaryKey(), pk);
  }
  @Override
  public int getStatus() {
    try {
      DLFileVersion dlFileVersion = getFileVersion();

      return dlFileVersion.getStatus();
    } catch (Exception e) {
      return WorkflowConstants.STATUS_APPROVED;
    }
  }
  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);
    }
  }
  @Test
  public void testRemove() throws Exception {
    DLFileVersion newDLFileVersion = addDLFileVersion();

    _persistence.remove(newDLFileVersion);

    DLFileVersion existingDLFileVersion =
        _persistence.fetchByPrimaryKey(newDLFileVersion.getPrimaryKey());

    Assert.assertNull(existingDLFileVersion);
  }
  @Test
  public void testCreateRepositoryFromExistingFileVersionId() throws Exception {

    DLFolder dlFolder = DLTestUtil.addDLFolder(_group.getGroupId());

    DLFileEntry dlFileEntry = DLTestUtil.addDLFileEntry(dlFolder.getFolderId());

    DLFileVersion dlFileVersion = dlFileEntry.getLatestFileVersion(true);

    RepositoryFactoryUtil.create(0, 0, dlFileVersion.getFileVersionId());
  }
  @Test
  public void testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception {
    DLFileVersion newDLFileVersion = addDLFileVersion();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newDLFileVersion.getPrimaryKey());

    Map<Serializable, DLFileVersion> dlFileVersions = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(1, dlFileVersions.size());
    Assert.assertEquals(newDLFileVersion, dlFileVersions.get(newDLFileVersion.getPrimaryKey()));
  }
  @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);
  }
  @Override
  public ExpandoBridge getExpandoBridge() {
    try {
      DLFileVersion dlFileVersion = getFileVersion();

      return dlFileVersion.getExpandoBridge();
    } catch (Exception e) {
      if (_log.isWarnEnabled()) {
        _log.warn(e, e);
      }
    }

    return null;
  }
  protected void initByFileVersionId(long fileVersionId) {
    try {
      DLFileVersion dlFileVersion = dlFileVersionLocalService.getFileVersion(fileVersionId);

      initByRepositoryId(dlFileVersion.getRepositoryId());
    } catch (Exception e) {
      if (_log.isTraceEnabled()) {
        if (e instanceof NoSuchFileVersionException) {
          _log.trace(e.getMessage());
        } else {
          _log.trace(e, e);
        }
      }
    }
  }
  /** @deprecated As of 6.2.0, replaced by {@link DLFileVersion#getUserUuid()} */
  @Deprecated
  @Override
  public String getVersionUserUuid() {
    String versionUserUuid = StringPool.BLANK;

    try {
      DLFileVersion dlFileVersion = getFileVersion();

      versionUserUuid = dlFileVersion.getUserUuid();
    } catch (Exception e) {
      _log.error(e, e);
    }

    return versionUserUuid;
  }
  @Override
  protected void doReindex(DLFileEntry dlFileEntry) throws Exception {
    DLFileVersion dlFileVersion = dlFileEntry.getFileVersion();

    if (!dlFileVersion.isApproved() && !dlFileEntry.isInTrash()) {
      return;
    }

    Document document = getDocument(dlFileEntry);

    if (document != null) {
      SearchEngineUtil.updateDocument(
          getSearchEngineId(), dlFileEntry.getCompanyId(), document, isCommitImmediately());
    }
  }
  protected String extractDDMContent(DLFileVersion dlFileVersion, Locale locale) throws Exception {

    List<DLFileEntryMetadata> dlFileEntryMetadatas =
        DLFileEntryMetadataLocalServiceUtil.getFileVersionFileEntryMetadatas(
            dlFileVersion.getFileVersionId());

    StringBundler sb = new StringBundler(dlFileEntryMetadatas.size());

    for (DLFileEntryMetadata dlFileEntryMetadata : dlFileEntryMetadatas) {
      Fields fields = null;

      try {
        fields = StorageEngineUtil.getFields(dlFileEntryMetadata.getDDMStorageId());
      } catch (Exception e) {
      }

      if (fields != null) {
        DDMStructure ddmStructure =
            DDMStructureLocalServiceUtil.getStructure(dlFileEntryMetadata.getDDMStructureId());

        sb.append(DDMIndexerUtil.extractAttributes(ddmStructure, fields, locale));
      }
    }

    return sb.toString();
  }
  /** @deprecated As of 6.2.0, replaced by {@link DLFileVersion#getUserName()} */
  @Deprecated
  @Override
  public String getVersionUserName() {
    String versionUserName = StringPool.BLANK;

    try {
      DLFileVersion dlFileVersion = getFileVersion();

      versionUserName = dlFileVersion.getUserName();
    } catch (Exception e) {
      if (_log.isWarnEnabled()) {
        _log.warn(e, e);
      }
    }

    return versionUserName;
  }
  /** @deprecated As of 6.2.0, replaced by {@link DLFileVersion#getUserId()} */
  @Deprecated
  @Override
  public long getVersionUserId() {
    long versionUserId = 0;

    try {
      DLFileVersion dlFileVersion = getFileVersion();

      versionUserId = dlFileVersion.getUserId();
    } catch (Exception e) {
      if (_log.isWarnEnabled()) {
        _log.warn(e, e);
      }
    }

    return versionUserId;
  }
  @Test
  public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist()
      throws Exception {
    DLFileVersion newDLFileVersion = addDLFileVersion();

    long pk = RandomTestUtil.nextLong();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newDLFileVersion.getPrimaryKey());
    primaryKeys.add(pk);

    Map<Serializable, DLFileVersion> dlFileVersions = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(1, dlFileVersions.size());
    Assert.assertEquals(newDLFileVersion, dlFileVersions.get(newDLFileVersion.getPrimaryKey()));
  }
  protected void migrateDLFileEntry(
      long companyId, String portletId, long groupId, long repositoryId, DLFileEntry fileEntry)
      throws Exception {

    String fileName = fileEntry.getName();
    long fileEntryId = fileEntry.getFileEntryId();
    String properties = fileEntry.getLuceneProperties();

    List<DLFileVersion> dlFileVersions =
        DLFileVersionLocalServiceUtil.getFileVersions(
            groupId, repositoryId, fileName, WorkflowConstants.STATUS_ANY);

    if (dlFileVersions.isEmpty()) {
      String versionNumber = Hook.DEFAULT_VERSION;
      Date modifiedDate = fileEntry.getModifiedDate();

      migrateFile(
          companyId,
          portletId,
          groupId,
          repositoryId,
          fileName,
          versionNumber,
          fileEntryId,
          properties,
          modifiedDate);

      return;
    }

    for (DLFileVersion dlFileVersion : dlFileVersions) {
      String versionNumber = dlFileVersion.getVersion();
      Date modifiedDate = dlFileVersion.getCreateDate();

      migrateFile(
          companyId,
          portletId,
          groupId,
          repositoryId,
          fileName,
          versionNumber,
          fileEntryId,
          properties,
          modifiedDate);
    }
  }
  @Test
  public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
    DLFileVersion newDLFileVersion = addDLFileVersion();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(DLFileVersion.class, DLFileVersion.class.getClassLoader());

    dynamicQuery.add(
        RestrictionsFactoryUtil.eq("fileVersionId", newDLFileVersion.getFileVersionId()));

    List<DLFileVersion> result = _persistence.findWithDynamicQuery(dynamicQuery);

    Assert.assertEquals(1, result.size());

    DLFileVersion existingDLFileVersion = result.get(0);

    Assert.assertEquals(existingDLFileVersion, newDLFileVersion);
  }
  @Test
  public void testDynamicQueryByProjectionExisting() throws Exception {
    DLFileVersion newDLFileVersion = addDLFileVersion();

    DynamicQuery dynamicQuery =
        DynamicQueryFactoryUtil.forClass(DLFileVersion.class, DLFileVersion.class.getClassLoader());

    dynamicQuery.setProjection(ProjectionFactoryUtil.property("fileVersionId"));

    Object newFileVersionId = newDLFileVersion.getFileVersionId();

    dynamicQuery.add(RestrictionsFactoryUtil.in("fileVersionId", new Object[] {newFileVersionId}));

    List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);

    Assert.assertEquals(1, result.size());

    Object existingFileVersionId = result.get(0);

    Assert.assertEquals(existingFileVersionId, newFileVersionId);
  }
  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);
  }
  protected void updateThumbnails(long fileEntryId) throws Exception {
    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
      con = DataAccess.getUpgradeOptimizedConnection();

      ps =
          con.prepareStatement(
              "select fileVersionId, userId, extension, mimeType, version "
                  + "from DLFileVersion where fileEntryId = "
                  + fileEntryId
                  + " order by version asc");

      rs = ps.executeQuery();

      while (rs.next()) {
        long fileVersionId = rs.getLong("fileVersionId");
        long userId = rs.getLong("userId");
        String extension = rs.getString("extension");
        String mimeType = rs.getString("mimeType");
        String version = rs.getString("version");

        if (_imageMimeTypes.contains(mimeType)) {
          DLFileVersion dlFileVersion = new DLFileVersionImpl();

          dlFileVersion.setFileVersionId(fileVersionId);
          dlFileVersion.setUserId(userId);
          dlFileVersion.setFileEntryId(fileEntryId);
          dlFileVersion.setExtension(extension);
          dlFileVersion.setMimeType(mimeType);
          dlFileVersion.setVersion(version);

          FileVersion fileVersion = new LiferayFileVersion(dlFileVersion);

          try {
            ImageProcessorUtil.generateImages(null, fileVersion);
          } catch (Exception e) {
            if (_log.isWarnEnabled()) {
              _log.warn("Unable to generate thumbnails for " + fileVersion.getFileVersionId(), e);
            }
          }
        }
      }
    } finally {
      DataAccess.cleanUp(con, ps, rs);
    }
  }
  @Test
  public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist()
      throws Exception {
    DLFileVersion newDLFileVersion1 = addDLFileVersion();
    DLFileVersion newDLFileVersion2 = addDLFileVersion();

    Set<Serializable> primaryKeys = new HashSet<Serializable>();

    primaryKeys.add(newDLFileVersion1.getPrimaryKey());
    primaryKeys.add(newDLFileVersion2.getPrimaryKey());

    Map<Serializable, DLFileVersion> dlFileVersions = _persistence.fetchByPrimaryKeys(primaryKeys);

    Assert.assertEquals(2, dlFileVersions.size());
    Assert.assertEquals(newDLFileVersion1, dlFileVersions.get(newDLFileVersion1.getPrimaryKey()));
    Assert.assertEquals(newDLFileVersion2, dlFileVersions.get(newDLFileVersion2.getPrimaryKey()));
  }
  protected void addFileEntryTypeAttributes(Document document, DLFileVersion dlFileVersion)
      throws PortalException {

    List<DLFileEntryMetadata> dlFileEntryMetadatas =
        DLFileEntryMetadataLocalServiceUtil.getFileVersionFileEntryMetadatas(
            dlFileVersion.getFileVersionId());

    for (DLFileEntryMetadata dlFileEntryMetadata : dlFileEntryMetadatas) {
      Fields fields = null;

      try {
        fields = StorageEngineUtil.getFields(dlFileEntryMetadata.getDDMStorageId());
      } catch (Exception e) {
      }

      if (fields != null) {
        DDMStructure ddmStructure =
            DDMStructureLocalServiceUtil.getStructure(dlFileEntryMetadata.getDDMStructureId());

        DDMIndexerUtil.addAttributes(document, ddmStructure, fields);
      }
    }
  }
  @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(
          Field.TREE_PATH, StringUtil.split(dlFileEntry.getTreePath(), CharPool.SLASH));

      document.addKeyword("dataRepositoryId", dlFileEntry.getDataRepositoryId());
      document.addText("ddmContent", extractDDMContent(dlFileVersion, LocaleUtil.getSiteDefault()));
      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()) {
        Indexer indexer = IndexerRegistryUtil.getIndexer(dlFileEntry.getClassName());

        if (indexer != null) {
          indexer.addRelatedEntryFields(document, obj);

          DocumentHelper documentHelper = new DocumentHelper(document);

          documentHelper.setAttachmentOwnerKey(
              PortalUtil.getClassNameId(dlFileEntry.getClassName()), dlFileEntry.getClassPK());

          document.addKeyword(Field.RELATED_ENTRY, true);
        }
      }

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

      return document;
    } finally {
      if (is != null) {
        try {
          is.close();
        } catch (IOException ioe) {
        }
      }
    }
  }
  protected DLFileVersion addDLFileVersion() throws Exception {
    long pk = RandomTestUtil.nextLong();

    DLFileVersion dlFileVersion = _persistence.create(pk);

    dlFileVersion.setUuid(RandomTestUtil.randomString());

    dlFileVersion.setGroupId(RandomTestUtil.nextLong());

    dlFileVersion.setCompanyId(RandomTestUtil.nextLong());

    dlFileVersion.setUserId(RandomTestUtil.nextLong());

    dlFileVersion.setUserName(RandomTestUtil.randomString());

    dlFileVersion.setCreateDate(RandomTestUtil.nextDate());

    dlFileVersion.setModifiedDate(RandomTestUtil.nextDate());

    dlFileVersion.setRepositoryId(RandomTestUtil.nextLong());

    dlFileVersion.setFolderId(RandomTestUtil.nextLong());

    dlFileVersion.setFileEntryId(RandomTestUtil.nextLong());

    dlFileVersion.setTreePath(RandomTestUtil.randomString());

    dlFileVersion.setExtension(RandomTestUtil.randomString());

    dlFileVersion.setMimeType(RandomTestUtil.randomString());

    dlFileVersion.setTitle(RandomTestUtil.randomString());

    dlFileVersion.setDescription(RandomTestUtil.randomString());

    dlFileVersion.setChangeLog(RandomTestUtil.randomString());

    dlFileVersion.setExtraSettings(RandomTestUtil.randomString());

    dlFileVersion.setFileEntryTypeId(RandomTestUtil.nextLong());

    dlFileVersion.setVersion(RandomTestUtil.randomString());

    dlFileVersion.setSize(RandomTestUtil.nextLong());

    dlFileVersion.setChecksum(RandomTestUtil.randomString());

    dlFileVersion.setStatus(RandomTestUtil.nextInt());

    dlFileVersion.setStatusByUserId(RandomTestUtil.nextLong());

    dlFileVersion.setStatusByUserName(RandomTestUtil.randomString());

    dlFileVersion.setStatusDate(RandomTestUtil.nextDate());

    _persistence.update(dlFileVersion);

    return dlFileVersion;
  }