protected void importPortletPreferences(
      PortletDataContext portletDataContext,
      long companyId,
      long groupId,
      Layout layout,
      Element parentElement,
      boolean preserveScopeLayoutId,
      boolean importPortletArchivedSetups,
      boolean importPortletData,
      boolean importPortletSetup,
      boolean importPortletUserPreferences)
      throws Exception {

    long plid = LayoutConstants.DEFAULT_PLID;
    String scopeType = StringPool.BLANK;
    String scopeLayoutUuid = StringPool.BLANK;

    if (layout != null) {
      plid = layout.getPlid();

      if (preserveScopeLayoutId) {
        javax.portlet.PortletPreferences jxPortletPreferences =
            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
                layout, portletDataContext.getPortletId());

        scopeType = GetterUtil.getString(jxPortletPreferences.getValue("lfrScopeType", null));
        scopeLayoutUuid =
            GetterUtil.getString(jxPortletPreferences.getValue("lfrScopeLayoutUuid", null));

        portletDataContext.setScopeType(scopeType);
        portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);
      }
    }

    List<Element> portletPreferencesElements = parentElement.elements("portlet-preferences");

    for (Element portletPreferencesElement : portletPreferencesElements) {
      String path = portletPreferencesElement.attributeValue("path");

      if (portletDataContext.isPathNotProcessed(path)) {
        String xml = null;

        Element element = null;

        try {
          xml = portletDataContext.getZipEntryAsString(path);

          Document preferencesDocument = SAXReaderUtil.read(xml);

          element = preferencesDocument.getRootElement();
        } catch (DocumentException de) {
          throw new SystemException(de);
        }

        long ownerId = GetterUtil.getLong(element.attributeValue("owner-id"));
        int ownerType = GetterUtil.getInteger(element.attributeValue("owner-type"));

        if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_COMPANY) || !importPortletSetup) {

          continue;
        }

        if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) && !importPortletArchivedSetups) {

          continue;
        }

        if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_USER)
            && (ownerId != PortletKeys.PREFS_OWNER_ID_DEFAULT)
            && !importPortletUserPreferences) {

          continue;
        }

        long curPlid = plid;
        String curPortletId = portletDataContext.getPortletId();

        if (ownerType == PortletKeys.PREFS_OWNER_TYPE_GROUP) {
          curPlid = PortletKeys.PREFS_PLID_SHARED;
          curPortletId = portletDataContext.getRootPortletId();
          ownerId = portletDataContext.getScopeGroupId();
        }

        if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
          String userUuid = element.attributeValue("archive-user-uuid");

          long userId = portletDataContext.getUserId(userUuid);

          String name = element.attributeValue("archive-name");

          curPortletId = portletDataContext.getRootPortletId();

          PortletItem portletItem =
              _portletItemLocalService.updatePortletItem(
                  userId, groupId, name, curPortletId, PortletPreferences.class.getName());

          curPlid = LayoutConstants.DEFAULT_PLID;
          ownerId = portletItem.getPortletItemId();
        }

        if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
          String userUuid = element.attributeValue("user-uuid");

          ownerId = portletDataContext.getUserId(userUuid);
        }

        boolean defaultUser = GetterUtil.getBoolean(element.attributeValue("default-user"));

        if (defaultUser) {
          ownerId = _userLocalService.getDefaultUserId(companyId);
        }

        javax.portlet.PortletPreferences jxPortletPreferences =
            PortletPreferencesFactoryUtil.fromXML(
                companyId, ownerId, ownerType, curPlid, curPortletId, xml);

        Element importDataRootElement = portletDataContext.getImportDataRootElement();

        try {
          Element preferenceDataElement = portletPreferencesElement.element("preference-data");

          if (preferenceDataElement != null) {
            portletDataContext.setImportDataRootElement(preferenceDataElement);
          }

          Portlet portlet =
              _portletLocalService.getPortletById(portletDataContext.getCompanyId(), curPortletId);

          ExportImportPortletPreferencesProcessor exportImportPortletPreferencesProcessor =
              ExportImportPortletPreferencesProcessorRegistryUtil
                  .getExportImportPortletPreferencesProcessor(portlet.getRootPortletId());

          if (exportImportPortletPreferencesProcessor != null) {
            List<Capability> importCapabilities =
                exportImportPortletPreferencesProcessor.getImportCapabilities();

            if (ListUtil.isNotEmpty(importCapabilities)) {
              for (Capability importCapability : importCapabilities) {

                importCapability.process(portletDataContext, jxPortletPreferences);
              }
            }

            exportImportPortletPreferencesProcessor.processImportPortletPreferences(
                portletDataContext, jxPortletPreferences);
          } else {
            PortletDataHandler portletDataHandler = portlet.getPortletDataHandlerInstance();

            jxPortletPreferences =
                portletDataHandler.processImportPortletPreferences(
                    portletDataContext, curPortletId, jxPortletPreferences);
          }
        } finally {
          portletDataContext.setImportDataRootElement(importDataRootElement);
        }

        updatePortletPreferences(
            portletDataContext,
            ownerId,
            ownerType,
            curPlid,
            curPortletId,
            PortletPreferencesFactoryUtil.toXML(jxPortletPreferences),
            importPortletData);
      }
    }

    if (preserveScopeLayoutId && (layout != null)) {
      javax.portlet.PortletPreferences jxPortletPreferences =
          PortletPreferencesFactoryUtil.getLayoutPortletSetup(
              layout, portletDataContext.getPortletId());

      try {
        jxPortletPreferences.setValue("lfrScopeType", scopeType);
        jxPortletPreferences.setValue("lfrScopeLayoutUuid", scopeLayoutUuid);

        jxPortletPreferences.store();
      } finally {
        portletDataContext.setScopeType(scopeType);
        portletDataContext.setScopeLayoutUuid(scopeLayoutUuid);
      }
    }
  }
  protected void exportPortletPreference(
      PortletDataContext portletDataContext,
      long ownerId,
      int ownerType,
      boolean defaultUser,
      PortletPreferences portletPreferences,
      String portletId,
      long plid,
      Element parentElement)
      throws Exception {

    String preferencesXML = portletPreferences.getPreferences();

    if (Validator.isNull(preferencesXML)) {
      preferencesXML = PortletConstants.DEFAULT_PREFERENCES;
    }

    javax.portlet.PortletPreferences jxPortletPreferences =
        PortletPreferencesFactoryUtil.fromDefaultXML(preferencesXML);

    Portlet portlet =
        _portletLocalService.getPortletById(portletDataContext.getCompanyId(), portletId);

    Element portletPreferencesElement = parentElement.addElement("portlet-preferences");

    if ((portlet != null) && (portlet.getPortletDataHandlerInstance() != null)) {

      Element exportDataRootElement = portletDataContext.getExportDataRootElement();

      try {
        portletDataContext.clearScopedPrimaryKeys();

        Element preferenceDataElement = portletPreferencesElement.addElement("preference-data");

        portletDataContext.setExportDataRootElement(preferenceDataElement);

        ExportImportPortletPreferencesProcessor exportImportPortletPreferencesProcessor =
            ExportImportPortletPreferencesProcessorRegistryUtil
                .getExportImportPortletPreferencesProcessor(portlet.getRootPortletId());

        if (exportImportPortletPreferencesProcessor != null) {
          List<Capability> exportCapabilities =
              exportImportPortletPreferencesProcessor.getExportCapabilities();

          if (ListUtil.isNotEmpty(exportCapabilities)) {
            for (Capability exportCapability : exportCapabilities) {
              exportCapability.process(portletDataContext, jxPortletPreferences);
            }
          }

          exportImportPortletPreferencesProcessor.processExportPortletPreferences(
              portletDataContext, jxPortletPreferences);
        } else {
          PortletDataHandler portletDataHandler = portlet.getPortletDataHandlerInstance();

          jxPortletPreferences =
              portletDataHandler.processExportPortletPreferences(
                  portletDataContext, portletId, jxPortletPreferences);
        }
      } finally {
        portletDataContext.setExportDataRootElement(exportDataRootElement);
      }
    }

    Document document =
        SAXReaderUtil.read(PortletPreferencesFactoryUtil.toXML(jxPortletPreferences));

    Element rootElement = document.getRootElement();

    rootElement.addAttribute("owner-id", String.valueOf(ownerId));
    rootElement.addAttribute("owner-type", String.valueOf(ownerType));
    rootElement.addAttribute("default-user", String.valueOf(defaultUser));
    rootElement.addAttribute("plid", String.valueOf(plid));
    rootElement.addAttribute("portlet-id", portletId);

    if (ownerType == PortletKeys.PREFS_OWNER_TYPE_ARCHIVED) {
      PortletItem portletItem = _portletItemLocalService.getPortletItem(ownerId);

      rootElement.addAttribute("archive-user-uuid", portletItem.getUserUuid());
      rootElement.addAttribute("archive-name", portletItem.getName());
    } else if (ownerType == PortletKeys.PREFS_OWNER_TYPE_USER) {
      User user = _userLocalService.fetchUserById(ownerId);

      if (user == null) {
        return;
      }

      rootElement.addAttribute("user-uuid", user.getUserUuid());
    }

    List<Node> nodes =
        document.selectNodes(
            "/portlet-preferences/preference[name/text() = " + "'last-publish-date']");

    for (Node node : nodes) {
      document.remove(node);
    }

    String path =
        ExportImportPathUtil.getPortletPreferencesPath(
            portletDataContext, portletId, ownerId, ownerType, plid);

    portletPreferencesElement.addAttribute("path", path);

    portletDataContext.addZipEntry(path, document.formattedString(StringPool.TAB, false, false));
  }