@Override
  public void importMissingReference(
      PortletDataContext portletDataContext, Element referenceElement) throws PortletDataException {

    importMissingGroupReference(portletDataContext, referenceElement);

    String uuid = referenceElement.attributeValue("uuid");

    Map<Long, Long> groupIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(Group.class);

    long groupId = GetterUtil.getLong(referenceElement.attributeValue("group-id"));

    groupId = MapUtil.getLong(groupIds, groupId);

    String fileEntryTypeKey = referenceElement.attributeValue("file-entry-type-key");
    boolean preloaded = GetterUtil.getBoolean(referenceElement.attributeValue("preloaded"));

    DLFileEntryType existingFileEntryType = null;

    existingFileEntryType = fetchExistingFileEntryType(uuid, groupId, fileEntryTypeKey, preloaded);

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

    long fileEntryTypeId = GetterUtil.getLong(referenceElement.attributeValue("class-pk"));

    fileEntryTypeIds.put(fileEntryTypeId, existingFileEntryType.getFileEntryTypeId());
  }
  @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, 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);
      }
    }
  }
  protected PortletDataContext getPortletDataContext(
      ExportImportConfiguration exportImportConfiguration, File file) throws PortalException {

    Map<String, Serializable> settingsMap = exportImportConfiguration.getSettingsMap();

    Map<String, String[]> parameterMap = (Map<String, String[]>) settingsMap.get("parameterMap");
    String portletId = MapUtil.getString(settingsMap, "portletId");
    long targetPlid = MapUtil.getLong(settingsMap, "targetPlid");
    long targetGroupId = MapUtil.getLong(settingsMap, "targetGroupId");
    long userId = MapUtil.getLong(settingsMap, "userId");

    Layout layout = LayoutLocalServiceUtil.getLayout(targetPlid);

    String userIdStrategyString =
        MapUtil.getString(parameterMap, PortletDataHandlerKeys.USER_ID_STRATEGY);

    UserIdStrategy userIdStrategy =
        ExportImportHelperUtil.getUserIdStrategy(userId, userIdStrategyString);

    ZipReader zipReader = ZipReaderFactoryUtil.getZipReader(file);

    PortletDataContext portletDataContext =
        PortletDataContextFactoryUtil.createImportPortletDataContext(
            layout.getCompanyId(), targetGroupId, parameterMap, userIdStrategy, zipReader);

    portletDataContext.setOldPlid(targetPlid);
    portletDataContext.setPlid(targetPlid);
    portletDataContext.setPortletId(portletId);
    portletDataContext.setPrivateLayout(layout.isPrivateLayout());

    return portletDataContext;
  }
  @Override
  protected void doImportStagedModel(PortletDataContext portletDataContext, Website website)
      throws Exception {

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

    ServiceContext serviceContext = portletDataContext.createServiceContext(website);

    Website existingWebsite =
        _websiteLocalService.fetchWebsiteByUuidAndCompanyId(
            website.getUuid(), portletDataContext.getCompanyGroupId());

    Website importedWebsite = null;

    if (existingWebsite == null) {
      serviceContext.setUuid(website.getUuid());

      importedWebsite =
          _websiteLocalService.addWebsite(
              userId,
              website.getClassName(),
              website.getClassPK(),
              website.getUrl(),
              website.getTypeId(),
              website.isPrimary(),
              serviceContext);
    } else {
      importedWebsite =
          _websiteLocalService.updateWebsite(
              existingWebsite.getWebsiteId(), website.getUrl(),
              website.getTypeId(), website.isPrimary());
    }

    portletDataContext.importClassedModel(website, importedWebsite);
  }
  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, AssetTag assetTag)
      throws Exception {

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

    ServiceContext serviceContext = createServiceContext(portletDataContext, assetTag);

    AssetTag existingAssetTag =
        fetchStagedModelByUuidAndGroupId(assetTag.getUuid(), portletDataContext.getScopeGroupId());

    AssetTag importedAssetTag = null;

    if (existingAssetTag == null) {
      serviceContext.setUuid(assetTag.getUuid());

      importedAssetTag =
          AssetTagLocalServiceUtil.addTag(
              userId, portletDataContext.getScopeGroupId(), assetTag.getName(), serviceContext);
    } else {
      importedAssetTag =
          AssetTagLocalServiceUtil.updateTag(
              userId, existingAssetTag.getTagId(), assetTag.getName(), serviceContext);
    }

    portletDataContext.importClassedModel(assetTag, importedAssetTag);
  }
  @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, AssetTag assetTag)
      throws Exception {

    Element assetTagElement = portletDataContext.getExportDataElement(assetTag);

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

    Element websiteElement = portletDataContext.getExportDataElement(website);

    portletDataContext.addClassedModel(
        websiteElement, ExportImportPathUtil.getModelPath(website), website);
  }
  @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);
  }
  protected void importAssets(
      PortletDataContext portletDataContext, Layout layout, Layout importedLayout)
      throws Exception {

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

    long[] assetCategoryIds =
        portletDataContext.getAssetCategoryIds(Layout.class, layout.getPlid());
    String[] assetTagNames = portletDataContext.getAssetTagNames(Layout.class, layout.getPlid());

    _layoutLocalService.updateAsset(userId, importedLayout, assetCategoryIds, assetTagNames);
  }
  @Override
  public ExportActionableDynamicQuery getExportActionableDynamicQuery(
      final PortletDataContext portletDataContext) {
    final ExportActionableDynamicQuery exportActionableDynamicQuery =
        new ExportActionableDynamicQuery() {
          @Override
          public long performCount() throws PortalException {
            ManifestSummary manifestSummary = portletDataContext.getManifestSummary();

            StagedModelType stagedModelType = getStagedModelType();

            long modelAdditionCount = super.performCount();

            manifestSummary.addModelAdditionCount(stagedModelType.toString(), modelAdditionCount);

            long modelDeletionCount =
                ExportImportHelperUtil.getModelDeletionCount(portletDataContext, stagedModelType);

            manifestSummary.addModelDeletionCount(stagedModelType.toString(), modelDeletionCount);

            return modelAdditionCount;
          }
        };

    initActionableDynamicQuery(exportActionableDynamicQuery);

    exportActionableDynamicQuery.setAddCriteriaMethod(
        new ActionableDynamicQuery.AddCriteriaMethod() {
          @Override
          public void addCriteria(DynamicQuery dynamicQuery) {
            portletDataContext.addDateRangeCriteria(dynamicQuery, "modifiedDate");
          }
        });

    exportActionableDynamicQuery.setCompanyId(portletDataContext.getCompanyId());

    exportActionableDynamicQuery.setGroupId(portletDataContext.getScopeGroupId());

    exportActionableDynamicQuery.setPerformActionMethod(
        new ActionableDynamicQuery.PerformActionMethod() {
          @Override
          public void performAction(Object object) throws PortalException {
            CalendarNotificationTemplate stagedModel = (CalendarNotificationTemplate) object;

            StagedModelDataHandlerUtil.exportStagedModel(portletDataContext, stagedModel);
          }
        });
    exportActionableDynamicQuery.setStagedModelType(
        new StagedModelType(
            PortalUtil.getClassNameId(CalendarNotificationTemplate.class.getName())));

    return exportActionableDynamicQuery;
  }
  @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);
  }
  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);
    }
  }
  @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);
  }
  @Override
  public boolean validateReference(
      PortletDataContext portletDataContext, Element referenceElement) {

    validateMissingGroupReference(portletDataContext, referenceElement);

    String uuid = referenceElement.attributeValue("uuid");

    Map<Long, Long> groupIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(Group.class);

    long groupId = GetterUtil.getLong(referenceElement.attributeValue("group-id"));

    groupId = MapUtil.getLong(groupIds, groupId);

    String fileEntryTypeKey = referenceElement.attributeValue("file-entry-type-key");
    boolean preloaded = GetterUtil.getBoolean(referenceElement.attributeValue("preloaded"));

    DLFileEntryType existingFileEntryType =
        fetchExistingFileEntryType(uuid, groupId, fileEntryTypeKey, preloaded);

    if (existingFileEntryType == null) {
      return false;
    }

    return true;
  }
  @Override
  public boolean validateReference(
      PortletDataContext portletDataContext, Element referenceElement) {

    validateMissingGroupReference(portletDataContext, referenceElement);

    String uuid = referenceElement.attributeValue("uuid");

    Map<Long, Long> groupIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(Group.class);

    long groupId = GetterUtil.getLong(referenceElement.attributeValue("group-id"));

    groupId = MapUtil.getLong(groupIds, groupId);

    boolean privateLayout =
        GetterUtil.getBoolean(referenceElement.attributeValue("private-layout"));

    Layout existingLayout = fetchMissingReference(uuid, groupId, privateLayout);

    if (existingLayout == null) {
      return false;
    }

    return true;
  }
  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 void addAssetReference(
      PortletDataContext portletDataContext,
      StagedAssetLink stagedAssetLink,
      Element stagedAssetLinkElement,
      AssetEntry assetEntry) {

    AssetRenderer<? extends StagedModel> assetRenderer = null;
    StagedModel stagedModel = null;

    try {
      assetRenderer = (AssetRenderer<? extends StagedModel>) assetEntry.getAssetRenderer();

      stagedModel = assetRenderer.getAssetObject();
    } catch (Exception e) {
      return;
    }

    if (stagedModel == null) {
      return;
    }

    portletDataContext.addReferenceElement(
        stagedAssetLink,
        stagedAssetLinkElement,
        stagedModel,
        PortletDataContext.REFERENCE_TYPE_DEPENDENCY_DISPOSABLE,
        true);
  }
  protected void importServicePortletPreferences(
      PortletDataContext portletDataContext, Element serviceElement) throws PortalException {

    long ownerId = GetterUtil.getLong(serviceElement.attributeValue("owner-id"));
    int ownerType = GetterUtil.getInteger(serviceElement.attributeValue("owner-type"));
    String serviceName = serviceElement.attributeValue("service-name");

    PortletPreferences portletPreferences =
        getPortletPreferences(
            portletDataContext.getCompanyId(),
            ownerId,
            ownerType,
            LayoutConstants.DEFAULT_PLID,
            serviceName);

    for (Attribute attribute : serviceElement.attributes()) {
      serviceElement.remove(attribute);
    }

    String xml = serviceElement.asXML();

    portletPreferences.setPreferences(xml);

    PortletPreferencesLocalServiceUtil.updatePortletPreferences(portletPreferences);
  }
  @Override
  protected Long getImportPortletPreferencesNewPrimaryKey(
      PortletDataContext portletDataContext,
      Class<?> clazz,
      long companyGroupId,
      Map<Long, Long> primaryKeys,
      String uuid)
      throws Exception {

    if (Validator.isNumber(uuid)) {
      long oldPrimaryKey = GetterUtil.getLong(uuid);

      return MapUtil.getLong(primaryKeys, oldPrimaryKey, oldPrimaryKey);
    }

    String className = clazz.getName();

    if (className.equals(Organization.class.getName())) {
      Organization organization =
          OrganizationUtil.fetchByUuid_C_First(uuid, portletDataContext.getCompanyId(), null);

      if (organization != null) {
        return organization.getOrganizationId();
      }
    }

    return null;
  }
  protected void importTheme(
      PortletDataContext portletDataContext, Layout layout, Layout importedLayout)
      throws Exception {

    boolean importThemeSettings =
        MapUtil.getBoolean(
            portletDataContext.getParameterMap(), PortletDataHandlerKeys.THEME_REFERENCE);

    if (_log.isDebugEnabled()) {
      _log.debug("Import theme settings " + importThemeSettings);
    }

    if (importThemeSettings) {
      importedLayout.setColorSchemeId(layout.getColorSchemeId());
      importedLayout.setCss(layout.getCss());
      importedLayout.setThemeId(layout.getThemeId());
      importedLayout.setWapColorSchemeId(layout.getWapColorSchemeId());
      importedLayout.setWapThemeId(layout.getWapThemeId());
    } else {
      importedLayout.setColorSchemeId(StringPool.BLANK);
      importedLayout.setCss(StringPool.BLANK);
      importedLayout.setThemeId(StringPool.BLANK);
      importedLayout.setWapColorSchemeId(StringPool.BLANK);
      importedLayout.setWapThemeId(StringPool.BLANK);
    }
  }
  @Override
  protected PortletPreferences doDeleteData(
      PortletDataContext portletDataContext,
      String portletId,
      PortletPreferences portletPreferences)
      throws Exception {

    if (portletDataContext.addPrimaryKey(AdminPortletDataHandler.class, "deleteData")) {

      return portletPreferences;
    }

    GadgetLocalServiceUtil.deleteGadgets(portletDataContext.getCompanyId());

    return portletPreferences;
  }
  @Override
  protected PortletPreferences doDeleteData(
      PortletDataContext portletDataContext,
      String portletId,
      PortletPreferences portletPreferences)
      throws Exception {

    if (portletDataContext.addPrimaryKey(CalendarPortletDataHandler.class, "deleteData")) {

      return portletPreferences;
    }

    CalEventLocalServiceUtil.deleteEvents(portletDataContext.getScopeGroupId());

    return portletPreferences;
  }
  @Override
  protected void doImportMissingReference(
      PortletDataContext portletDataContext, String uuid, long groupId, long fileEntryId)
      throws Exception {

    FileEntry existingFileEntry = fetchMissingReference(uuid, groupId);

    Map<Long, Long> dlFileEntryIds =
        (Map<Long, Long>) portletDataContext.getNewPrimaryKeysMap(DLFileEntry.class);

    dlFileEntryIds.put(fileEntryId, existingFileEntry.getFileEntryId());

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

    fileEntryIds.put(fileEntryId, existingFileEntry.getFileEntryId());
  }
  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());
    }
  }
  protected void exportEvent(
      PortletDataContext portletDataContext, Element rootElement, CalEvent event)
      throws PortalException {

    if (!portletDataContext.isWithinDateRange(event.getModifiedDate())) {
      return;
    }

    String path = getEventPath(portletDataContext, event);

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

    Element eventElement = rootElement.addElement("event");

    portletDataContext.addClassedModel(eventElement, path, event);
  }
  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);
  }