@Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, BlogsEntry entry)
      throws Exception {

    Element entryElement = portletDataContext.getExportDataElement(entry);

    if (entry.isSmallImage()) {
      Image smallImage = _imageLocalService.fetchImage(entry.getSmallImageId());

      if (smallImage != null) {
        String smallImagePath =
            ExportImportPathUtil.getModelPath(
                entry, smallImage.getImageId() + StringPool.PERIOD + smallImage.getType());

        entryElement.addAttribute("small-image-path", smallImagePath);

        entry.setSmallImageType(smallImage.getType());

        portletDataContext.addZipEntry(smallImagePath, smallImage.getTextObj());
      }
    }

    if (entry.getSmallImageFileEntryId() != 0) {
      FileEntry fileEntry =
          PortletFileRepositoryUtil.getPortletFileEntry(entry.getSmallImageFileEntryId());

      StagedModelDataHandlerUtil.exportReferenceStagedModel(
          portletDataContext, entry, fileEntry, PortletDataContext.REFERENCE_TYPE_WEAK);
    }

    if (entry.getCoverImageFileEntryId() != 0) {
      FileEntry fileEntry =
          PortletFileRepositoryUtil.getPortletFileEntry(entry.getCoverImageFileEntryId());

      StagedModelDataHandlerUtil.exportReferenceStagedModel(
          portletDataContext, entry, fileEntry, PortletDataContext.REFERENCE_TYPE_WEAK);
    }

    String content =
        _blogsEntryExportImportContentProcessor.replaceExportContentReferences(
            portletDataContext,
            entry,
            entry.getContent(),
            portletDataContext.getBooleanParameter("blogs", "referenced-content"),
            true);

    entry.setContent(content);

    portletDataContext.addClassedModel(
        entryElement, ExportImportPathUtil.getModelPath(entry), entry);
  }
  @Override
  protected void doExportStagedModel(
      PortletDataContext portletDataContext, DLFileEntryType fileEntryType) throws Exception {

    Element fileEntryTypeElement = portletDataContext.getExportDataElement(fileEntryType);

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

    for (DDMStructure ddmStructure : ddmStructures) {
      com.liferay.dynamic.data.mapping.model.DDMStructure structure =
          _ddmStructureLocalService.getStructure(ddmStructure.getStructureId());

      Element referenceElement =
          StagedModelDataHandlerUtil.exportReferenceStagedModel(
              portletDataContext,
              fileEntryType,
              structure,
              PortletDataContext.REFERENCE_TYPE_STRONG);

      referenceElement.addAttribute(
          "structure-id", StringUtil.valueOf(ddmStructure.getStructureId()));
    }

    long defaultUserId = UserLocalServiceUtil.getDefaultUserId(fileEntryType.getCompanyId());

    if (defaultUserId == fileEntryType.getUserId()) {
      fileEntryTypeElement.addAttribute("preloaded", "true");
    }

    portletDataContext.addClassedModel(
        fileEntryTypeElement, ExportImportPathUtil.getModelPath(fileEntryType), fileEntryType);
  }
  protected void readLocks(PortletDataContext portletDataContext) throws Exception {

    String xml =
        portletDataContext.getZipEntryAsString(
            ExportImportPathUtil.getSourceRootPath(portletDataContext) + "/locks.xml");

    if (xml == null) {
      return;
    }

    Document document = SAXReaderUtil.read(xml);

    Element rootElement = document.getRootElement();

    List<Element> assetElements = rootElement.elements("asset");

    for (Element assetElement : assetElements) {
      String path = assetElement.attributeValue("path");
      String className = assetElement.attributeValue("class-name");
      String key = assetElement.attributeValue("key");

      Lock lock = (Lock) portletDataContext.getZipEntryAsObject(path);

      if (lock != null) {
        portletDataContext.addLocks(className, key, lock);
      }
    }
  }
  @Override
  protected void doExportStagedModel(
      PortletDataContext portletDataContext,
      CalendarNotificationTemplate calendarNotificationTemplate)
      throws Exception {

    Calendar calendar =
        CalendarLocalServiceUtil.getCalendar(calendarNotificationTemplate.getCalendarId());

    StagedModelDataHandlerUtil.exportReferenceStagedModel(
        portletDataContext,
        calendarNotificationTemplate,
        calendar,
        PortletDataContext.REFERENCE_TYPE_STRONG);

    String body =
        ExportImportHelperUtil.replaceExportContentReferences(
            portletDataContext,
            calendarNotificationTemplate,
            calendarNotificationTemplate.getBody(),
            portletDataContext.getBooleanParameter(
                CalendarPortletDataHandler.NAMESPACE, "referenced-content"));

    calendarNotificationTemplate.setBody(body);

    Element calendarNotificationTemplateElement =
        portletDataContext.getExportDataElement(calendarNotificationTemplate);

    portletDataContext.addClassedModel(
        calendarNotificationTemplateElement,
        ExportImportPathUtil.getModelPath(calendarNotificationTemplate),
        calendarNotificationTemplate);
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, Layout layout)
      throws Exception {

    if (layout.isTypeSharedPortlet()) {
      return;
    }

    Element layoutElement = portletDataContext.getExportDataElement(layout);

    populateElementLayoutMetadata(layoutElement, layout);

    layoutElement.addAttribute(Constants.ACTION, Constants.ADD);

    portletDataContext.setPlid(layout.getPlid());

    long parentLayoutId = layout.getParentLayoutId();

    if (parentLayoutId != LayoutConstants.DEFAULT_PARENT_LAYOUT_ID) {
      Layout parentLayout =
          _layoutLocalService.fetchLayout(
              layout.getGroupId(), layout.isPrivateLayout(), parentLayoutId);

      if (parentLayout != null) {
        StagedModelDataHandlerUtil.exportReferenceStagedModel(
            portletDataContext, layout, parentLayout, PortletDataContext.REFERENCE_TYPE_PARENT);

        layoutElement.addAttribute("parent-layout-uuid", parentLayout.getUuid());
      }
    }

    List<LayoutFriendlyURL> layoutFriendlyURLs =
        _layoutFriendlyURLLocalService.getLayoutFriendlyURLs(layout.getPlid());

    for (LayoutFriendlyURL layoutFriendlyURL : layoutFriendlyURLs) {
      StagedModelDataHandlerUtil.exportReferenceStagedModel(
          portletDataContext,
          layout,
          layoutFriendlyURL,
          PortletDataContext.REFERENCE_TYPE_DEPENDENCY);
    }

    if (layout.isIconImage()) {
      exportLayoutIconImage(portletDataContext, layout, layoutElement);
    }

    if (layout.isTypeLinkToLayout()) {
      exportLinkedLayout(portletDataContext, layout, layoutElement);
    }

    fixExportTypeSettings(layout);

    exportTheme(portletDataContext, layout);

    portletDataContext.addClassedModel(
        layoutElement, ExportImportPathUtil.getModelPath(layout), layout);
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, AssetTag assetTag)
      throws Exception {

    Element assetTagElement = portletDataContext.getExportDataElement(assetTag);

    portletDataContext.addClassedModel(
        assetTagElement, ExportImportPathUtil.getModelPath(assetTag), assetTag);
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, UserGroup userGroup)
      throws Exception {

    Element userGroupElement = portletDataContext.getExportDataElement(userGroup);

    portletDataContext.addClassedModel(
        userGroupElement, ExportImportPathUtil.getModelPath(userGroup), userGroup);
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, Website website)
      throws Exception {

    Element websiteElement = portletDataContext.getExportDataElement(website);

    portletDataContext.addClassedModel(
        websiteElement, ExportImportPathUtil.getModelPath(website), website);
  }
  protected String getEventPath(PortletDataContext portletDataContext, CalEvent event) {

    StringBundler sb = new StringBundler(4);

    sb.append(ExportImportPathUtil.getPortletPath(portletDataContext, PortletKeys.CALENDAR));
    sb.append("/events/");
    sb.append(event.getEventId());
    sb.append(".xml");

    return sb.toString();
  }
  protected void exportMetaData(
      PortletDataContext portletDataContext, Element fileEntryElement, FileEntry fileEntry)
      throws Exception {

    LiferayFileEntry liferayFileEntry = (LiferayFileEntry) fileEntry;

    DLFileEntry dlFileEntry = liferayFileEntry.getDLFileEntry();

    long fileEntryTypeId = dlFileEntry.getFileEntryTypeId();

    DLFileEntryType dlFileEntryType =
        DLFileEntryTypeLocalServiceUtil.fetchFileEntryType(fileEntryTypeId);

    if ((dlFileEntryType == null) || !dlFileEntryType.isExportable()) {
      return;
    }

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

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

    for (DDMStructure ddmStructure : ddmStructures) {
      FileVersion fileVersion = fileEntry.getFileVersion();

      DLFileEntryMetadata dlFileEntryMetadata =
          DLFileEntryMetadataLocalServiceUtil.fetchFileEntryMetadata(
              ddmStructure.getStructureId(), fileVersion.getFileVersionId());

      if (dlFileEntryMetadata == null) {
        continue;
      }

      Element structureFields = fileEntryElement.addElement("structure-fields");

      String path =
          ExportImportPathUtil.getModelPath(
              ddmStructure, String.valueOf(dlFileEntryMetadata.getDDMStorageId()));

      structureFields.addAttribute("path", path);

      structureFields.addAttribute("structureUuid", ddmStructure.getUuid());

      DDMFormValues ddmFormValues =
          StorageEngineUtil.getDDMFormValues(dlFileEntryMetadata.getDDMStorageId());

      portletDataContext.addZipEntry(path, ddmFormValues);
    }
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, Calendar calendar)
      throws Exception {

    StagedModelDataHandlerUtil.exportReferenceStagedModel(
        portletDataContext,
        calendar,
        calendar.getCalendarResource(),
        PortletDataContext.REFERENCE_TYPE_STRONG);

    Element calendarElement = portletDataContext.getExportDataElement(calendar);

    portletDataContext.addClassedModel(
        calendarElement, ExportImportPathUtil.getModelPath(calendar), calendar);
  }
  protected Layout importLayoutFromLAR(StagedModel stagedModel)
      throws DocumentException, IOException {

    LayoutSetPrototypeStagedModelDataHandler layoutSetPrototypeStagedModelDataHandler =
        new LayoutSetPrototypeStagedModelDataHandler();

    String fileName =
        layoutSetPrototypeStagedModelDataHandler.getLayoutSetPrototypeLARFileName(
            (LayoutSetPrototype) stagedModel);

    String modelPath = ExportImportPathUtil.getModelPath(stagedModel, fileName);

    InputStream inputStream = portletDataContext.getZipEntryAsInputStream(modelPath);

    ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(inputStream);

    Document document = UnsecureSAXReaderUtil.read(zipReader.getEntryAsString("manifest.xml"));

    Element rootElement = document.getRootElement();

    Element layoutElement = rootElement.element("Layout");

    List<Element> elements = layoutElement.elements();

    List<Layout> importedLayouts = new ArrayList<>(elements.size());

    for (Element element : elements) {
      String layoutPrototypeUuid = element.attributeValue("layout-prototype-uuid");

      if (Validator.isNotNull(layoutPrototypeUuid)) {
        String path = element.attributeValue("path");

        Layout layout = (Layout) portletDataContext.fromXML(zipReader.getEntryAsString(path));

        importedLayouts.add(layout);
      }
    }

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

    try {
      return importedLayouts.get(0);
    } finally {
      zipReader.close();

      StreamUtil.cleanUp(inputStream);
    }
  }
  protected void exportLayoutIconImage(
      PortletDataContext portletDataContext, Layout layout, Element layoutElement)
      throws Exception {

    Image image = _imageLocalService.getImage(layout.getIconImageId());

    if (image != null) {
      String iconPath =
          ExportImportPathUtil.getModelPath(
              portletDataContext.getScopeGroupId(), Image.class.getName(), image.getImageId());

      Element iconImagePathElement = layoutElement.addElement("icon-image-path");

      iconImagePathElement.addText(iconPath);

      portletDataContext.addZipEntry(iconPath, image.getTextObj());
    }
  }
  @Override
  protected void doExportStagedModel(
      PortletDataContext portletDataContext, StagedAssetLink stagedAssetLink) throws Exception {

    Element stagedAssetLinkElement = portletDataContext.getExportDataElement(stagedAssetLink);

    AssetEntry assetEntry1 = _assetEntryLocalService.fetchEntry(stagedAssetLink.getEntryId1());

    addAssetReference(portletDataContext, stagedAssetLink, stagedAssetLinkElement, assetEntry1);

    AssetEntry assetEntry2 = _assetEntryLocalService.fetchEntry(stagedAssetLink.getEntryId2());

    addAssetReference(portletDataContext, stagedAssetLink, stagedAssetLinkElement, assetEntry2);

    portletDataContext.addClassedModel(
        stagedAssetLinkElement,
        ExportImportPathUtil.getModelPath(stagedAssetLink),
        stagedAssetLink);
  }
  protected void exportKBArticleAttachments(
      PortletDataContext portletDataContext, Element kbArticleElement, KBArticle kbArticle)
      throws Exception {

    List<FileEntry> attachmentsFileEntries = kbArticle.getAttachmentsFileEntries();

    for (FileEntry fileEntry : attachmentsFileEntries) {
      String path = ExportImportPathUtil.getModelPath(kbArticle, fileEntry.getTitle());

      Element fileEntryElement = portletDataContext.getExportDataElement(fileEntry);

      fileEntryElement.addAttribute("path", path);
      fileEntryElement.addAttribute("file-name", fileEntry.getTitle());

      portletDataContext.addZipEntry(path, fileEntry.getContentStream());

      portletDataContext.addReferenceElement(
          kbArticle, kbArticleElement, fileEntry, PortletDataContext.REFERENCE_TYPE_WEAK, false);
    }
  }
  @Override
  protected void doExportStagedModel(PortletDataContext portletDataContext, KBArticle kbArticle)
      throws Exception {

    if (kbArticle.getParentResourcePrimKey() != KBFolderConstants.DEFAULT_PARENT_FOLDER_ID) {

      long kbArticleClassNameId = PortalUtil.getClassNameId(KBArticleConstants.getClassName());

      if (kbArticle.getParentResourceClassNameId() == kbArticleClassNameId) {

        KBArticle parentKBArticle =
            KBArticleLocalServiceUtil.getLatestKBArticle(
                kbArticle.getParentResourcePrimKey(), WorkflowConstants.STATUS_APPROVED);

        StagedModelDataHandlerUtil.exportReferenceStagedModel(
            portletDataContext,
            kbArticle,
            parentKBArticle,
            PortletDataContext.REFERENCE_TYPE_PARENT);
      } else {
        KBFolder parentKBFolder =
            KBFolderLocalServiceUtil.getKBFolder(kbArticle.getParentResourcePrimKey());

        StagedModelDataHandlerUtil.exportReferenceStagedModel(
            portletDataContext,
            kbArticle,
            parentKBFolder,
            PortletDataContext.REFERENCE_TYPE_PARENT);
      }
    }

    Element kbArticleElement = portletDataContext.getExportDataElement(kbArticle);

    exportKBArticleAttachments(portletDataContext, kbArticleElement, kbArticle);

    portletDataContext.addClassedModel(
        kbArticleElement, ExportImportPathUtil.getModelPath(kbArticle), kbArticle);
  }
  @Ignore
  @Test
  public void testImportDLReferences() throws Exception {
    Element referrerStagedModelElement =
        _portletDataContextExport.getExportDataElement(_referrerStagedModel);

    String referrerStagedModelPath = ExportImportPathUtil.getModelPath(_referrerStagedModel);

    referrerStagedModelElement.addAttribute("path", referrerStagedModelPath);

    String content = replaceParameters(getContent("dl_references.txt"), _fileEntry);

    content =
        ExportImportHelperUtil.replaceExportContentReferences(
            _portletDataContextExport, _referrerStagedModel, content, true);

    _portletDataContextImport.setScopeGroupId(_fileEntry.getGroupId());

    content =
        ExportImportHelperUtil.replaceImportContentReferences(
            _portletDataContextImport, _referrerStagedModel, content);

    Assert.assertFalse(content.contains("[$dl-reference="));
  }
  protected void readExpandoTables(PortletDataContext portletDataContext) throws Exception {

    String xml =
        portletDataContext.getZipEntryAsString(
            ExportImportPathUtil.getSourceRootPath(portletDataContext) + "/expando-tables.xml");

    if (xml == null) {
      return;
    }

    Document document = SAXReaderUtil.read(xml);

    Element rootElement = document.getRootElement();

    List<Element> expandoTableElements = rootElement.elements("expando-table");

    for (Element expandoTableElement : expandoTableElements) {
      String className = expandoTableElement.attributeValue("class-name");

      ExpandoTable expandoTable = null;

      try {
        expandoTable =
            ExpandoTableLocalServiceUtil.getDefaultTable(
                portletDataContext.getCompanyId(), className);
      } catch (NoSuchTableException nste) {
        expandoTable =
            ExpandoTableLocalServiceUtil.addDefaultTable(
                portletDataContext.getCompanyId(), className);
      }

      List<Element> expandoColumnElements = expandoTableElement.elements("expando-column");

      for (Element expandoColumnElement : expandoColumnElements) {
        long columnId = GetterUtil.getLong(expandoColumnElement.attributeValue("column-id"));
        String name = expandoColumnElement.attributeValue("name");
        int type = GetterUtil.getInteger(expandoColumnElement.attributeValue("type"));
        String defaultData = expandoColumnElement.elementText("default-data");
        String typeSettings = expandoColumnElement.elementText("type-settings");

        Serializable defaultDataObject =
            ExpandoConverterUtil.getAttributeFromString(type, defaultData);

        ExpandoColumn expandoColumn =
            ExpandoColumnLocalServiceUtil.getColumn(expandoTable.getTableId(), name);

        if (expandoColumn != null) {
          ExpandoColumnLocalServiceUtil.updateColumn(
              expandoColumn.getColumnId(), name, type, defaultDataObject);
        } else {
          expandoColumn =
              ExpandoColumnLocalServiceUtil.addColumn(
                  expandoTable.getTableId(), name, type, defaultDataObject);
        }

        ExpandoColumnLocalServiceUtil.updateTypeSettings(expandoColumn.getColumnId(), typeSettings);

        portletDataContext.importPermissions(
            ExpandoColumn.class, columnId, expandoColumn.getColumnId());
      }
    }
  }
  protected void readAssetLinks(PortletDataContext portletDataContext) throws Exception {

    String xml =
        portletDataContext.getZipEntryAsString(
            ExportImportPathUtil.getSourceRootPath(portletDataContext) + "/links.xml");

    if (xml == null) {
      return;
    }

    Document document = SAXReaderUtil.read(xml);

    Element rootElement = document.getRootElement();

    List<Element> assetLinkGroupElements = rootElement.elements("asset-link-group");

    for (Element assetLinkGroupElement : assetLinkGroupElements) {
      String sourceUuid = assetLinkGroupElement.attributeValue("source-uuid");

      AssetEntry sourceAssetEntry =
          AssetEntryLocalServiceUtil.fetchEntry(portletDataContext.getScopeGroupId(), sourceUuid);

      if (sourceAssetEntry == null) {
        sourceAssetEntry =
            AssetEntryLocalServiceUtil.fetchEntry(
                portletDataContext.getCompanyGroupId(), sourceUuid);
      }

      if (sourceAssetEntry == null) {
        if (_log.isWarnEnabled()) {
          _log.warn("Unable to find asset entry with uuid " + sourceUuid);
        }

        continue;
      }

      List<Element> assetLinksElements = assetLinkGroupElement.elements("asset-link");

      for (Element assetLinkElement : assetLinksElements) {
        String path = assetLinkElement.attributeValue("path");

        if (!portletDataContext.isPathNotProcessed(path)) {
          continue;
        }

        String targetUuid = assetLinkElement.attributeValue("target-uuid");

        AssetEntry targetAssetEntry =
            AssetEntryLocalServiceUtil.fetchEntry(portletDataContext.getScopeGroupId(), targetUuid);

        if (targetAssetEntry == null) {
          targetAssetEntry =
              AssetEntryLocalServiceUtil.fetchEntry(
                  portletDataContext.getCompanyGroupId(), targetUuid);
        }

        if (targetAssetEntry == null) {
          if (_log.isWarnEnabled()) {
            _log.warn("Unable to find asset entry with uuid " + targetUuid);
          }

          continue;
        }

        AssetLink assetLink = (AssetLink) portletDataContext.getZipEntryAsObject(path);

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

        AssetLinkLocalServiceUtil.updateLink(
            userId,
            sourceAssetEntry.getEntryId(),
            targetAssetEntry.getEntryId(),
            assetLink.getType(),
            assetLink.getWeight());
      }
    }
  }
  @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);
  }