protected InputStream getSmallImageInputStream(
      PortletDataContext portletDataContext, Element attachmentElement) {

    InputStream inputStream = null;

    String path = attachmentElement.attributeValue("path");

    FileEntry fileEntry = (FileEntry) portletDataContext.getZipEntryAsObject(path);

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

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

      try {
        inputStream = FileEntryUtil.getContentStream(fileEntry);
      } catch (Exception e) {
      }
    } else {
      inputStream = portletDataContext.getZipEntryAsInputStream(binPath);
    }

    if (inputStream == null) {
      if (_log.isWarnEnabled()) {
        _log.warn("Unable to import small image file entry " + fileEntry.getFileEntryId());
      }
    }

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

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

      FileVersion fileVersion = fileEntry.getFileVersion();

      if (existingFileEntry == null) {
        if (portletDataContext.isDataStrategyMirrorWithOverwriting()) {
          FileEntry existingTitleFileEntry =
              FileEntryUtil.fetchByR_F_T(
                  portletDataContext.getScopeGroupId(), folderId, fileEntry.getTitle());

          if (existingTitleFileEntry == null) {
            existingTitleFileEntry =
                FileEntryUtil.fetchByR_F_FN(
                    portletDataContext.getScopeGroupId(), folderId, fileEntry.getFileName());
          }

          if (existingTitleFileEntry != null) {
            DLAppLocalServiceUtil.deleteFileEntry(existingTitleFileEntry.getFileEntryId());
          }
        }

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

        String fileEntryTitle =
            DLFileEntryLocalServiceUtil.getUniqueTitle(
                portletDataContext.getScopeGroupId(),
                folderId,
                0,
                fileEntry.getTitle(),
                fileEntry.getExtension());

        importedFileEntry =
            DLAppLocalServiceUtil.addFileEntry(
                userId,
                portletDataContext.getScopeGroupId(),
                folderId,
                fileEntry.getFileName(),
                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());

            String fileEntryTitle =
                DLFileEntryLocalServiceUtil.getUniqueTitle(
                    portletDataContext.getScopeGroupId(),
                    existingFileEntry.getFolderId(),
                    existingFileEntry.getFileEntryId(),
                    fileEntry.getTitle(),
                    fileEntry.getExtension());

            importedFileEntry =
                DLAppLocalServiceUtil.updateFileEntry(
                    userId,
                    existingFileEntry.getFileEntryId(),
                    fileEntry.getFileName(),
                    fileEntry.getMimeType(),
                    fileEntryTitle,
                    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 {
      String fileEntryTitle =
          DLFileEntryLocalServiceUtil.getUniqueTitle(
              portletDataContext.getScopeGroupId(),
              folderId,
              0,
              fileEntry.getTitle(),
              fileEntry.getExtension());

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

    if (portletDataContext.getBooleanParameter("document_library", "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());
  }
  @Override
  protected void doImportStagedModel(PortletDataContext portletDataContext, WikiPage page)
      throws Exception {

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

    Element pageElement = portletDataContext.getImportDataStagedModelElement(page);

    String content =
        _wikiPageExportImportContentProcessor.replaceImportContentReferences(
            portletDataContext, page, page.getContent());

    page.setContent(content);

    ServiceContext serviceContext = portletDataContext.createServiceContext(page);

    serviceContext.setUuid(page.getUuid());

    Map<Long, Long> nodeIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(WikiNode.class);

    long nodeId = MapUtil.getLong(nodeIds, page.getNodeId(), page.getNodeId());

    WikiPage importedPage = null;

    WikiPage existingPage = _wikiPageLocalService.fetchPage(nodeId, page.getTitle());

    if (existingPage == null) {
      importedPage =
          _wikiPageLocalService.addPage(
              userId,
              nodeId,
              page.getTitle(),
              page.getVersion(),
              page.getContent(),
              page.getSummary(),
              page.isMinorEdit(),
              page.getFormat(),
              page.getHead(),
              page.getParentTitle(),
              page.getRedirectTitle(),
              serviceContext);

      WikiPageResource pageResource =
          _wikiPageResourceLocalService.getPageResource(importedPage.getResourcePrimKey());

      String pageResourceUuid =
          GetterUtil.getString(pageElement.attributeValue("page-resource-uuid"));

      if (Validator.isNotNull(pageResourceUuid)) {
        pageResource.setUuid(pageElement.attributeValue("page-resource-uuid"));

        _wikiPageResourceLocalService.updateWikiPageResource(pageResource);
      }
    } else {
      existingPage =
          fetchStagedModelByUuidAndGroupId(page.getUuid(), portletDataContext.getScopeGroupId());

      if (existingPage == null) {
        existingPage = _wikiPageLocalService.fetchPage(nodeId, page.getTitle(), page.getVersion());
      }

      if (existingPage == null) {
        importedPage =
            _wikiPageLocalService.updatePage(
                userId,
                nodeId,
                page.getTitle(),
                0.0,
                page.getContent(),
                page.getSummary(),
                page.isMinorEdit(),
                page.getFormat(),
                page.getParentTitle(),
                page.getRedirectTitle(),
                serviceContext);
      } else {
        importedPage = existingPage;
      }
    }

    if (page.isHead()) {
      List<Element> attachmentElements =
          portletDataContext.getReferenceDataElements(
              pageElement, DLFileEntry.class, PortletDataContext.REFERENCE_TYPE_WEAK);

      for (Element attachmentElement : attachmentElements) {
        String path = attachmentElement.attributeValue("path");

        FileEntry fileEntry = (FileEntry) portletDataContext.getZipEntryAsObject(path);

        InputStream inputStream = null;

        try {
          String binPath = attachmentElement.attributeValue("bin-path");

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

            try {
              inputStream = FileEntryUtil.getContentStream(fileEntry);
            } catch (NoSuchFileException nsfe) {
            }
          } else {
            inputStream = portletDataContext.getZipEntryAsInputStream(binPath);
          }

          if (inputStream == null) {
            if (_log.isWarnEnabled()) {
              _log.warn("Unable to import attachment for file entry " + fileEntry.getFileEntryId());
            }

            continue;
          }

          _wikiPageLocalService.addPageAttachment(
              userId,
              importedPage.getNodeId(),
              importedPage.getTitle(),
              fileEntry.getTitle(),
              inputStream,
              null);
        } finally {
          StreamUtil.cleanUp(inputStream);
        }
      }
    }

    portletDataContext.importClassedModel(page, importedPage);

    Map<Long, Long> pageIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(WikiPage.class + ".pageId");

    pageIds.put(page.getPageId(), importedPage.getPageId());
  }
  @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(
        _DL_PORTLET_DATA_HANDLER_NAMESPACE, "previews-and-thumbnails")) {

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

    exportMetaData(portletDataContext, fileEntryElement, fileEntry);

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