private ImageSelector _getImageSelector(
      PortletDataContext portletDataContext, long fileEntryId, List<Element> attachmentElements)
      throws Exception {

    FileEntry oldImageFileEntry = PortletFileRepositoryUtil.getPortletFileEntry(fileEntryId);

    for (Element attachmentElement : attachmentElements) {
      Attribute uuidAttribute = attachmentElement.attribute("uuid");

      String uuidAttributeValue = uuidAttribute.getValue();

      if (uuidAttributeValue.equals(oldImageFileEntry.getUuid())) {
        String path = attachmentElement.attributeValue("path");

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

        return new ImageSelector(
            FileUtil.getBytes(fileEntry.getContentStream()),
            fileEntry.getFileName(),
            fileEntry.getMimeType(),
            null);
      }
    }

    return null;
  }
  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 PortletPreferences doImportData(
      PortletDataContext portletDataContext,
      String portletId,
      PortletPreferences portletPreferences,
      String data)
      throws Exception {

    portletDataContext.importPermissions(
        CalendarPermission.RESOURCE_NAME,
        portletDataContext.getSourceGroupId(),
        portletDataContext.getScopeGroupId());

    Element rootElement = portletDataContext.getImportDataRootElement();

    for (Element eventElement : rootElement.elements("event")) {
      String path = eventElement.attributeValue("path");

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

      CalEvent event = (CalEvent) portletDataContext.getZipEntryAsObject(path);

      importEvent(portletDataContext, eventElement, event);
    }

    return null;
  }
  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);
      }
    }
  }
  protected void importMetaData(
      PortletDataContext portletDataContext,
      Element fileEntryElement,
      FileEntry fileEntry,
      ServiceContext serviceContext)
      throws Exception {

    LiferayFileEntry liferayFileEntry = (LiferayFileEntry) fileEntry;

    DLFileEntry dlFileEntry = liferayFileEntry.getDLFileEntry();

    Map<Long, Long> dlFileEntryTypeIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(DLFileEntryType.class);

    long dlFileEntryTypeId =
        MapUtil.getLong(
            dlFileEntryTypeIds, dlFileEntry.getFileEntryTypeId(), dlFileEntry.getFileEntryTypeId());

    DLFileEntryType existingDLFileEntryType =
        DLFileEntryTypeLocalServiceUtil.fetchDLFileEntryType(dlFileEntryTypeId);

    if (existingDLFileEntryType == null) {
      serviceContext.setAttribute("fileEntryTypeId", -1);

      return;
    }

    serviceContext.setAttribute("fileEntryTypeId", existingDLFileEntryType.getFileEntryTypeId());

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

    for (DDMStructure ddmStructure : ddmStructures) {
      Element structureFieldsElement =
          (Element)
              fileEntryElement.selectSingleNode(
                  "structure-fields[@structureUuid='".concat(ddmStructure.getUuid()).concat("']"));

      if (structureFieldsElement == null) {
        continue;
      }

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

      DDMFormValues ddmFormValues = (DDMFormValues) portletDataContext.getZipEntryAsObject(path);

      serviceContext.setAttribute(
          DDMFormValues.class.getName() + ddmStructure.getStructureId(), ddmFormValues);
    }
  }
  protected void importLayoutFriendlyURLs(
      PortletDataContext portletDataContext, Layout layout, Layout importedLayout)
      throws Exception {

    List<Element> layoutFriendlyURLElements =
        portletDataContext.getReferenceDataElements(layout, LayoutFriendlyURL.class);

    for (Element layoutFriendlyURLElement : layoutFriendlyURLElements) {
      String layoutFriendlyURLPath = layoutFriendlyURLElement.attributeValue("path");

      LayoutFriendlyURL layoutFriendlyURL =
          (LayoutFriendlyURL) portletDataContext.getZipEntryAsObject(layoutFriendlyURLPath);

      StagedModelDataHandlerUtil.importStagedModel(portletDataContext, layoutFriendlyURL);
    }

    deleteMissingLayoutFriendlyURLs(portletDataContext, importedLayout);
  }
  protected void importLinkedLayout(
      PortletDataContext portletDataContext,
      Layout layout,
      Layout importedLayout,
      Element layoutElement,
      Map<Long, Layout> layouts)
      throws Exception {

    UnicodeProperties typeSettingsProperties = layout.getTypeSettingsProperties();

    long linkToLayoutId =
        GetterUtil.getLong(typeSettingsProperties.getProperty("linkToLayoutId", StringPool.BLANK));

    String linkedToLayoutUuid = layoutElement.attributeValue("linked-to-layout-uuid");

    if (Validator.isNull(linkedToLayoutUuid)) {
      return;
    }

    if (linkToLayoutId <= 0) {
      updateTypeSettings(importedLayout, layout);

      return;
    }

    Element linkedToLayoutElement =
        portletDataContext.getReferenceDataElement(
            layout, Layout.class, layout.getGroupId(), linkedToLayoutUuid);

    if (linkedToLayoutElement != null) {
      String linkedToLayoutPath = linkedToLayoutElement.attributeValue("path");

      Layout linkedToLayout = (Layout) portletDataContext.getZipEntryAsObject(linkedToLayoutPath);

      StagedModelDataHandlerUtil.importStagedModel(portletDataContext, linkedToLayout);

      Layout importedLinkedLayout = layouts.get(linkToLayoutId);

      if (importedLinkedLayout == null) {
        throw new NoSuchLayoutException(
            "Layout with layout id "
                + linkToLayoutId
                + " that is linked from layout with layout id "
                + layout.getLayoutId()
                + " does not exist");
      }

      typeSettingsProperties.setProperty(
          "privateLayout", String.valueOf(importedLinkedLayout.isPrivateLayout()));
      typeSettingsProperties.setProperty(
          "linkToLayoutId", String.valueOf(importedLinkedLayout.getLayoutId()));
    } else {
      if (_log.isWarnEnabled()) {
        StringBundler sb = new StringBundler(6);

        sb.append("Unable to link layout with friendly URL ");
        sb.append(layout.getFriendlyURL());
        sb.append(" and layout id ");
        sb.append(layout.getLayoutId());
        sb.append(" to layout with layout id ");
        sb.append(linkToLayoutId);

        _log.warn(sb.toString());
      }
    }

    updateTypeSettings(importedLayout, layout);
  }
  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());
      }
    }
  }