@Override
  protected String upgradePreferences(
      long companyId, long ownerId, int ownerType, long plid, String portletId, String xml)
      throws Exception {

    PortletPreferences preferences =
        PortletPreferencesFactoryUtil.fromXML(companyId, ownerId, ownerType, plid, portletId, xml);

    if (portletId.startsWith(_PORTLET_ID_ASSET_PUBLISHER)) {
      updatePreferencesClassPKs(preferences, "anyClassTypeJournalArticleAssetRendererFactory");
      updatePreferencesClassPKs(preferences, "classTypeIds");
      updatePreferencesClassPKs(preferences, "classTypeIdsJournalArticleAssetRendererFactory");
    } else if (portletId.startsWith(_PORTLET_ID_JOURNAL_CONTENT)) {
      String templateId = preferences.getValue("templateId", StringPool.BLANK);

      if (Validator.isNotNull(templateId)) {
        preferences.reset("templateId");

        preferences.setValue("ddmTemplateKey", templateId);
      }
    } else if (portletId.startsWith(_PORTLET_ID_JOURNAL_CONTENT_LIST)) {
      String structureId = preferences.getValue("structureId", StringPool.BLANK);

      if (Validator.isNotNull(structureId)) {
        preferences.reset("structureId");

        preferences.setValue("ddmStructureKey", structureId);
      }
    }

    return PortletPreferencesFactoryUtil.toXML(preferences);
  }
  @Override
  protected String upgradePreferences(
      long companyId, long ownerId, int ownerType, long plid, String portletId, String xml)
      throws Exception {

    PortletPreferences portletPreferences =
        PortletPreferencesFactoryUtil.fromXML(companyId, ownerId, ownerType, plid, portletId, xml);

    String selectionStyle = portletPreferences.getValue("selection-style", null);

    if (Validator.isNotNull(selectionStyle) && !selectionStyle.equals("dynamic")) {

      String[] assetEntryXmls = portletPreferences.getValues("asset-entry-xml", new String[0]);

      String[] newAssetEntryXmls = getAssetEntryXmls(assetEntryXmls);

      portletPreferences.setValues("asset-entry-xml", newAssetEntryXmls);
    }

    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
  }
  public 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();
        }

        long elementPlid = GetterUtil.getLong(element.attributeValue("plid"));

        if ((ownerType == PortletKeys.PREFS_OWNER_TYPE_LAYOUT)
            && (ownerId != PortletKeys.PREFS_OWNER_ID_DEFAULT)
            && (elementPlid == PortletKeys.PREFS_PLID_SHARED)) {

          curPlid = PortletKeys.PREFS_PLID_SHARED;
          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);
          }

          ExportImportPortletPreferencesProcessor exportImportPortletPreferencesProcessor =
              ExportImportPortletPreferencesProcessorRegistryUtil
                  .getExportImportPortletPreferencesProcessor(
                      PortletConstants.getRootPortletId(curPortletId));

          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 =
                _portletDataHandlerProvider.provide(
                    portletDataContext.getCompanyId(), curPortletId);

            if (portletDataHandler != null) {
              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 updatePortletPreferences(
      PortletDataContext portletDataContext,
      long ownerId,
      int ownerType,
      long plid,
      String portletId,
      String xml,
      boolean importData)
      throws Exception {

    PortletDataHandler portletDataHandler =
        _portletDataHandlerProvider.provide(portletDataContext.getCompanyId(), portletId);

    // Current portlet preferences

    javax.portlet.PortletPreferences portletPreferences =
        _portletPreferencesLocalService.getPreferences(
            portletDataContext.getCompanyId(), ownerId, ownerType, plid, portletId);

    // New portlet preferences

    javax.portlet.PortletPreferences jxPortletPreferences =
        PortletPreferencesFactoryUtil.fromXML(
            portletDataContext.getCompanyId(), ownerId, ownerType, plid, portletId, xml);

    if (importData || !MergeLayoutPrototypesThreadLocal.isInProgress()) {
      String currentLastPublishDate = portletPreferences.getValue("last-publish-date", null);
      String newLastPublishDate = jxPortletPreferences.getValue("last-publish-date", null);

      if (Validator.isNotNull(currentLastPublishDate)) {
        jxPortletPreferences.setValue("last-publish-date", currentLastPublishDate);
      } else if (Validator.isNotNull(newLastPublishDate)) {
        jxPortletPreferences.reset("last-publish-date");
      }

      _portletPreferencesLocalService.updatePreferences(
          ownerId,
          ownerType,
          plid,
          portletId,
          PortletPreferencesFactoryUtil.toXML(jxPortletPreferences));

      return;
    }

    // Portlet preferences will be updated only when importing data

    String[] dataPortletPreferences = portletDataHandler.getDataPortletPreferences();

    Enumeration<String> enu = jxPortletPreferences.getNames();

    while (enu.hasMoreElements()) {
      String name = enu.nextElement();

      String scopeLayoutUuid = portletDataContext.getScopeLayoutUuid();
      String scopeType = portletDataContext.getScopeType();

      if (!ArrayUtil.contains(dataPortletPreferences, name)
          || (Validator.isNull(scopeLayoutUuid) && scopeType.equals("company"))) {

        String[] values = jxPortletPreferences.getValues(name, null);

        portletPreferences.setValues(name, values);
      }
    }

    _portletPreferencesLocalService.updatePreferences(
        ownerId, ownerType, plid, portletId, portletPreferences);
  }