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);
      }
    }
  }
  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();
  }
  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);
    }
  }
  @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());
  }
  @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 DLFileVersion updateDLFileVersion(DLFileVersion model) throws SystemException {
    DLFileVersion dlFileVersion = new DLFileVersionImpl();

    dlFileVersion.setNew(false);

    dlFileVersion.setFileVersionId(model.getFileVersionId());
    dlFileVersion.setCompanyId(model.getCompanyId());
    dlFileVersion.setUserId(model.getUserId());
    dlFileVersion.setUserName(model.getUserName());
    dlFileVersion.setCreateDate(model.getCreateDate());
    dlFileVersion.setFolderId(model.getFolderId());
    dlFileVersion.setName(model.getName());
    dlFileVersion.setVersion(model.getVersion());
    dlFileVersion.setSize(model.getSize());

    return dlFileVersionPersistence.update(dlFileVersion);
  }
  @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 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) {
        }
      }
    }
  }
  @Test
  public void testUpdateExisting() throws Exception {
    long pk = RandomTestUtil.nextLong();

    DLFileVersion newDLFileVersion = _persistence.create(pk);

    newDLFileVersion.setUuid(RandomTestUtil.randomString());

    newDLFileVersion.setGroupId(RandomTestUtil.nextLong());

    newDLFileVersion.setCompanyId(RandomTestUtil.nextLong());

    newDLFileVersion.setUserId(RandomTestUtil.nextLong());

    newDLFileVersion.setUserName(RandomTestUtil.randomString());

    newDLFileVersion.setCreateDate(RandomTestUtil.nextDate());

    newDLFileVersion.setModifiedDate(RandomTestUtil.nextDate());

    newDLFileVersion.setRepositoryId(RandomTestUtil.nextLong());

    newDLFileVersion.setFolderId(RandomTestUtil.nextLong());

    newDLFileVersion.setFileEntryId(RandomTestUtil.nextLong());

    newDLFileVersion.setTreePath(RandomTestUtil.randomString());

    newDLFileVersion.setExtension(RandomTestUtil.randomString());

    newDLFileVersion.setMimeType(RandomTestUtil.randomString());

    newDLFileVersion.setTitle(RandomTestUtil.randomString());

    newDLFileVersion.setDescription(RandomTestUtil.randomString());

    newDLFileVersion.setChangeLog(RandomTestUtil.randomString());

    newDLFileVersion.setExtraSettings(RandomTestUtil.randomString());

    newDLFileVersion.setFileEntryTypeId(RandomTestUtil.nextLong());

    newDLFileVersion.setVersion(RandomTestUtil.randomString());

    newDLFileVersion.setSize(RandomTestUtil.nextLong());

    newDLFileVersion.setChecksum(RandomTestUtil.randomString());

    newDLFileVersion.setStatus(RandomTestUtil.nextInt());

    newDLFileVersion.setStatusByUserId(RandomTestUtil.nextLong());

    newDLFileVersion.setStatusByUserName(RandomTestUtil.randomString());

    newDLFileVersion.setStatusDate(RandomTestUtil.nextDate());

    _persistence.update(newDLFileVersion);

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

    Assert.assertEquals(existingDLFileVersion.getUuid(), newDLFileVersion.getUuid());
    Assert.assertEquals(
        existingDLFileVersion.getFileVersionId(), newDLFileVersion.getFileVersionId());
    Assert.assertEquals(existingDLFileVersion.getGroupId(), newDLFileVersion.getGroupId());
    Assert.assertEquals(existingDLFileVersion.getCompanyId(), newDLFileVersion.getCompanyId());
    Assert.assertEquals(existingDLFileVersion.getUserId(), newDLFileVersion.getUserId());
    Assert.assertEquals(existingDLFileVersion.getUserName(), newDLFileVersion.getUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingDLFileVersion.getCreateDate()),
        Time.getShortTimestamp(newDLFileVersion.getCreateDate()));
    Assert.assertEquals(
        Time.getShortTimestamp(existingDLFileVersion.getModifiedDate()),
        Time.getShortTimestamp(newDLFileVersion.getModifiedDate()));
    Assert.assertEquals(
        existingDLFileVersion.getRepositoryId(), newDLFileVersion.getRepositoryId());
    Assert.assertEquals(existingDLFileVersion.getFolderId(), newDLFileVersion.getFolderId());
    Assert.assertEquals(existingDLFileVersion.getFileEntryId(), newDLFileVersion.getFileEntryId());
    Assert.assertEquals(existingDLFileVersion.getTreePath(), newDLFileVersion.getTreePath());
    Assert.assertEquals(existingDLFileVersion.getExtension(), newDLFileVersion.getExtension());
    Assert.assertEquals(existingDLFileVersion.getMimeType(), newDLFileVersion.getMimeType());
    Assert.assertEquals(existingDLFileVersion.getTitle(), newDLFileVersion.getTitle());
    Assert.assertEquals(existingDLFileVersion.getDescription(), newDLFileVersion.getDescription());
    Assert.assertEquals(existingDLFileVersion.getChangeLog(), newDLFileVersion.getChangeLog());
    Assert.assertEquals(
        existingDLFileVersion.getExtraSettings(), newDLFileVersion.getExtraSettings());
    Assert.assertEquals(
        existingDLFileVersion.getFileEntryTypeId(), newDLFileVersion.getFileEntryTypeId());
    Assert.assertEquals(existingDLFileVersion.getVersion(), newDLFileVersion.getVersion());
    Assert.assertEquals(existingDLFileVersion.getSize(), newDLFileVersion.getSize());
    Assert.assertEquals(existingDLFileVersion.getChecksum(), newDLFileVersion.getChecksum());
    Assert.assertEquals(existingDLFileVersion.getStatus(), newDLFileVersion.getStatus());
    Assert.assertEquals(
        existingDLFileVersion.getStatusByUserId(), newDLFileVersion.getStatusByUserId());
    Assert.assertEquals(
        existingDLFileVersion.getStatusByUserName(), newDLFileVersion.getStatusByUserName());
    Assert.assertEquals(
        Time.getShortTimestamp(existingDLFileVersion.getStatusDate()),
        Time.getShortTimestamp(newDLFileVersion.getStatusDate()));
  }
  @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);
    }
  }
  @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, FileEntry fileEntry)
      throws Exception {

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

    if (!fileEntry.isDefaultRepository()) {

      // References has been automatically imported, nothing to do here

      return;
    }

    Map<Long, Long> folderIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(Folder.class);

    long folderId = MapUtil.getLong(folderIds, fileEntry.getFolderId(), fileEntry.getFolderId());

    long[] assetCategoryIds =
        portletDataContext.getAssetCategoryIds(DLFileEntry.class, fileEntry.getFileEntryId());
    String[] assetTagNames =
        portletDataContext.getAssetTagNames(DLFileEntry.class, fileEntry.getFileEntryId());

    ServiceContext serviceContext =
        portletDataContext.createServiceContext(fileEntry, DLFileEntry.class);

    serviceContext.setAttribute("sourceFileName", "A." + fileEntry.getExtension());
    serviceContext.setUserId(userId);

    Element fileEntryElement = portletDataContext.getImportDataElement(fileEntry);

    String binPath = fileEntryElement.attributeValue("bin-path");

    InputStream is = null;

    if (Validator.isNull(binPath) && portletDataContext.isPerformDirectBinaryImport()) {

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

        return;
      }
    } else {
      is = portletDataContext.getZipEntryAsInputStream(binPath);
    }

    if (is == null) {
      if (_log.isWarnEnabled()) {
        _log.warn("No file found for file entry " + fileEntry.getFileEntryId());
      }

      return;
    }

    importMetaData(portletDataContext, fileEntryElement, fileEntry, serviceContext);

    FileEntry importedFileEntry = null;

    String titleWithExtension = DLUtil.getTitleWithExtension(fileEntry);
    String extension = fileEntry.getExtension();

    String periodAndExtension = StringPool.PERIOD.concat(extension);

    if (portletDataContext.isDataStrategyMirror()) {
      FileEntry existingFileEntry =
          fetchStagedModelByUuidAndGroupId(
              fileEntry.getUuid(), portletDataContext.getScopeGroupId());

      FileVersion fileVersion = fileEntry.getFileVersion();

      if (existingFileEntry == null) {
        String fileEntryTitle = fileEntry.getTitle();

        FileEntry existingTitleFileEntry =
            FileEntryUtil.fetchByR_F_T(
                portletDataContext.getScopeGroupId(), folderId, fileEntryTitle);

        if (existingTitleFileEntry != null) {
          if ((fileEntry.getGroupId() == portletDataContext.getSourceGroupId())
              && portletDataContext.isDataStrategyMirrorWithOverwriting()) {

            DLAppLocalServiceUtil.deleteFileEntry(existingTitleFileEntry.getFileEntryId());
          } else {
            boolean titleHasExtension = false;

            if (fileEntryTitle.endsWith(periodAndExtension)) {
              fileEntryTitle = FileUtil.stripExtension(fileEntryTitle);

              titleHasExtension = true;
            }

            for (int i = 1; ; i++) {
              fileEntryTitle += StringPool.SPACE + i;

              titleWithExtension = fileEntryTitle + periodAndExtension;

              existingTitleFileEntry =
                  FileEntryUtil.fetchByR_F_T(
                      portletDataContext.getScopeGroupId(), folderId, titleWithExtension);

              if (existingTitleFileEntry == null) {
                if (titleHasExtension) {
                  fileEntryTitle += periodAndExtension;
                }

                break;
              }
            }
          }
        }

        serviceContext.setAttribute("fileVersionUuid", fileVersion.getUuid());
        serviceContext.setUuid(fileEntry.getUuid());

        importedFileEntry =
            DLAppLocalServiceUtil.addFileEntry(
                userId,
                portletDataContext.getScopeGroupId(),
                folderId,
                titleWithExtension,
                fileEntry.getMimeType(),
                fileEntryTitle,
                fileEntry.getDescription(),
                null,
                is,
                fileEntry.getSize(),
                serviceContext);

        if (fileEntry.isInTrash()) {
          importedFileEntry =
              DLAppServiceUtil.moveFileEntryToTrash(importedFileEntry.getFileEntryId());
        }
      } else {
        FileVersion latestExistingFileVersion = existingFileEntry.getLatestFileVersion(true);

        boolean indexEnabled = serviceContext.isIndexingEnabled();

        boolean deleteFileEntry = false;
        boolean updateFileEntry = false;

        if (!Validator.equals(fileVersion.getUuid(), latestExistingFileVersion.getUuid())) {

          deleteFileEntry = true;
          updateFileEntry = true;
        } else {
          InputStream existingFileVersionInputStream = null;

          try {
            existingFileVersionInputStream = latestExistingFileVersion.getContentStream(false);
          } catch (Exception e) {
            if (_log.isDebugEnabled()) {
              _log.debug(e, e);
            }
          } finally {
            if (existingFileVersionInputStream != null) {
              existingFileVersionInputStream.close();
            }
          }

          if (existingFileVersionInputStream == null) {
            updateFileEntry = true;
          }
        }

        try {
          serviceContext.setIndexingEnabled(false);

          if (updateFileEntry) {
            DLFileVersion alreadyExistingFileVersion =
                DLFileVersionLocalServiceUtil.getFileVersionByUuidAndGroupId(
                    fileVersion.getUuid(), existingFileEntry.getGroupId());

            if (alreadyExistingFileVersion != null) {
              serviceContext.setAttribute(
                  "existingDLFileVersionId", alreadyExistingFileVersion.getFileVersionId());
            }

            serviceContext.setUuid(fileVersion.getUuid());

            importedFileEntry =
                DLAppLocalServiceUtil.updateFileEntry(
                    userId,
                    existingFileEntry.getFileEntryId(),
                    titleWithExtension,
                    fileEntry.getMimeType(),
                    fileEntry.getTitle(),
                    fileEntry.getDescription(),
                    null,
                    false,
                    is,
                    fileEntry.getSize(),
                    serviceContext);
          } else {
            DLAppLocalServiceUtil.updateAsset(
                userId, existingFileEntry,
                latestExistingFileVersion, assetCategoryIds,
                assetTagNames, null);

            importedFileEntry = existingFileEntry;
          }

          if (importedFileEntry.getFolderId() != folderId) {
            importedFileEntry =
                DLAppLocalServiceUtil.moveFileEntry(
                    userId, importedFileEntry.getFileEntryId(), folderId, serviceContext);
          }

          if (importedFileEntry instanceof LiferayFileEntry) {
            LiferayFileEntry liferayFileEntry = (LiferayFileEntry) importedFileEntry;

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

            indexer.reindex((DLFileEntry) liferayFileEntry.getModel());
          }

          if (deleteFileEntry && ExportImportThreadLocal.isStagingInProcess()) {

            DLAppServiceUtil.deleteFileVersion(
                latestExistingFileVersion.getFileEntryId(), latestExistingFileVersion.getVersion());
          }
        } finally {
          serviceContext.setIndexingEnabled(indexEnabled);
        }
      }
    } else {
      try {
        importedFileEntry =
            DLAppLocalServiceUtil.addFileEntry(
                userId,
                portletDataContext.getScopeGroupId(),
                folderId,
                titleWithExtension,
                fileEntry.getMimeType(),
                fileEntry.getTitle(),
                fileEntry.getDescription(),
                null,
                is,
                fileEntry.getSize(),
                serviceContext);
      } catch (DuplicateFileException dfe) {
        String title = fileEntry.getTitle();

        String[] titleParts = title.split("\\.", 2);

        title = titleParts[0] + StringUtil.randomString();

        if (titleParts.length > 1) {
          title += StringPool.PERIOD + titleParts[1];
        }

        if (!title.endsWith(periodAndExtension)) {
          title += periodAndExtension;
        }

        importedFileEntry =
            DLAppLocalServiceUtil.addFileEntry(
                userId,
                portletDataContext.getScopeGroupId(),
                folderId,
                title,
                fileEntry.getMimeType(),
                title,
                fileEntry.getDescription(),
                null,
                is,
                fileEntry.getSize(),
                serviceContext);
      }
    }

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

      DLProcessorRegistryUtil.importGeneratedFiles(
          portletDataContext, fileEntry, importedFileEntry, fileEntryElement);
    }

    portletDataContext.importClassedModel(fileEntry, importedFileEntry, DLFileEntry.class);

    Map<Long, Long> fileEntryIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(FileEntry.class);

    fileEntryIds.put(fileEntry.getFileEntryId(), importedFileEntry.getFileEntryId());
  }
  public FileEntry moveFileEntryToTrash(long userId, FileEntry fileEntry)
      throws PortalException, SystemException {

    // File entry

    DLFileEntry dlFileEntry = (DLFileEntry) fileEntry.getModel();

    dlFileEntry.setTitle(DLAppUtil.appendTrashNamespace(dlFileEntry.getTitle()));

    dlFileEntryPersistence.update(dlFileEntry, false);

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

    dlFileVersions = ListUtil.sort(dlFileVersions, new FileVersionVersionComparator());

    FileVersion fileVersion = new LiferayFileVersion(dlFileVersions.get(0));

    int oldStatus = fileVersion.getStatus();

    // File version

    dlFileEntryLocalService.updateStatus(
        userId,
        fileVersion.getFileVersionId(),
        WorkflowConstants.STATUS_IN_TRASH,
        new HashMap<String, Serializable>(),
        new ServiceContext());

    // File shortcut

    dlFileShortcutLocalService.disableFileShortcuts(fileEntry.getFileEntryId());

    // File rank

    dlFileRankLocalService.disableFileRanks(fileEntry.getFileEntryId());

    // Social

    socialActivityLocalService.addActivity(
        userId,
        fileEntry.getGroupId(),
        DLFileEntryConstants.getClassName(),
        fileEntry.getFileEntryId(),
        SocialActivityConstants.TYPE_MOVE_TO_TRASH,
        StringPool.BLANK,
        0);

    // Trash

    List<ObjectValuePair<Long, Integer>> fileVersionStatuses =
        new ArrayList<ObjectValuePair<Long, Integer>>(dlFileVersions.size());

    for (DLFileVersion dlFileVersion : dlFileVersions) {
      ObjectValuePair<Long, Integer> fileVersionStatus = new ObjectValuePair<Long, Integer>();

      fileVersionStatus.setKey(dlFileVersion.getFileVersionId());

      int status = dlFileVersion.getStatus();

      if (status == WorkflowConstants.STATUS_PENDING) {
        status = WorkflowConstants.STATUS_DRAFT;
      }

      fileVersionStatus.setValue(status);

      fileVersionStatuses.add(fileVersionStatus);
    }

    trashEntryLocalService.addTrashEntry(
        userId,
        fileEntry.getGroupId(),
        DLFileEntryConstants.getClassName(),
        fileEntry.getFileEntryId(),
        oldStatus,
        fileVersionStatuses,
        null);

    // Workflow

    if (oldStatus == WorkflowConstants.STATUS_PENDING) {
      workflowInstanceLinkLocalService.deleteWorkflowInstanceLink(
          fileVersion.getCompanyId(),
          fileVersion.getGroupId(),
          DLFileEntryConstants.getClassName(),
          fileVersion.getFileVersionId());
    }

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