@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);
  }
  protected void copyPreferences(
      javax.portlet.PortletPreferences sourcePreferences,
      javax.portlet.PortletPreferences targetPreferences)
      throws SystemException {

    try {
      Iterator<String> itr = targetPreferences.getMap().keySet().iterator();

      while (itr.hasNext()) {
        try {
          String key = itr.next();

          targetPreferences.reset(key);
        } catch (ReadOnlyException roe) {
        }
      }

      itr = sourcePreferences.getMap().keySet().iterator();

      while (itr.hasNext()) {
        try {
          String key = itr.next();

          targetPreferences.setValues(key, sourcePreferences.getValues(key, new String[0]));
        } catch (ReadOnlyException roe) {
        }
      }

      targetPreferences.store();
    } catch (IOException ioe) {
      _log.error(ioe);
    } catch (ValidatorException ve) {
      throw new SystemException(ve);
    }
  }
  /**
   * Resets/restores the values in the portletPreferences.xhtml Facelet composition with portlet
   * preference default values.
   */
  public void reset() {

    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
    PortletPreferences portletPreferences = portletRequest.getPreferences();

    try {
      Enumeration<String> preferenceNames = portletPreferences.getNames();

      while (preferenceNames.hasMoreElements()) {
        String preferenceName = preferenceNames.nextElement();
        portletPreferences.reset(preferenceName);
      }

      portletPreferences.store();

      // Switch the portlet mode back to VIEW.
      ActionResponse actionResponse = (ActionResponse) externalContext.getResponse();
      actionResponse.setPortletMode(PortletMode.VIEW);
      actionResponse.setWindowState(WindowState.NORMAL);

      FacesContextHelperUtil.addGlobalSuccessInfoMessage();
    } catch (Exception e) {
      FacesContextHelperUtil.addGlobalUnexpectedErrorMessage();
    }
  }
  protected String updatePreferences(
      long companyId, long ownerId, int ownerType, long plid, String portletId, String xml)
      throws Exception {

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

    Map<String, String[]> preferencesMap = preferences.getMap();

    String rootPortletId = PortletConstants.getRootPortletId(portletId);

    for (String oldName : preferencesMap.keySet()) {
      String newName = getName(rootPortletId, oldName);
      String[] oldValues = preferencesMap.get(oldName);

      preferences.reset(oldName);

      if (newName != null) {
        preferences.setValues(newName, oldValues);
      }
    }

    Map<String, String> defaultPreferencesMap = getDefaultPreferencesMap(rootPortletId);

    for (String name : defaultPreferencesMap.keySet()) {
      if (preferences.getValues(name, null) == null) {
        preferences.setValues(name, StringUtil.split(defaultPreferencesMap.get(name)));
      }
    }

    return PortletPreferencesFactoryUtil.toXML(preferences);
  }
  public void editPublicRenderParameters(ActionRequest actionRequest, ActionResponse actionResponse)
      throws Exception {

    Portlet portlet = ActionUtil.getPortlet(actionRequest);

    PortletPreferences portletPreferences =
        ActionUtil.getLayoutPortletSetup(actionRequest, portlet);

    actionRequest = ActionUtil.getWrappedActionRequest(actionRequest, portletPreferences);

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

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

      if (name.startsWith(PublicRenderParameterConfiguration.IGNORE_PREFIX)
          || name.startsWith(PublicRenderParameterConfiguration.MAPPING_PREFIX)) {

        portletPreferences.reset(name);
      }
    }

    for (PublicRenderParameter publicRenderParameter : portlet.getPublicRenderParameters()) {

      String ignoreKey = PublicRenderParameterConfiguration.getIgnoreKey(publicRenderParameter);

      boolean ignoreValue = ParamUtil.getBoolean(actionRequest, ignoreKey);

      if (ignoreValue) {
        portletPreferences.setValue(ignoreKey, String.valueOf(Boolean.TRUE));
      } else {
        String mappingKey = PublicRenderParameterConfiguration.getMappingKey(publicRenderParameter);

        String mappingValue = ParamUtil.getString(actionRequest, mappingKey);

        if (Validator.isNotNull(mappingValue)) {
          portletPreferences.setValue(mappingKey, mappingValue);
        }
      }
    }

    if (SessionErrors.isEmpty(actionRequest)) {
      portletPreferences.store();
    }
  }
  @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);

    long linkToLayoutId =
        GetterUtil.getLong(portletPreferences.getValue("lfr-scope-layout-id", null));

    if (linkToLayoutId > 0) {
      String uuid = getLayoutUuid(plid, linkToLayoutId);

      if (uuid != null) {
        portletPreferences.setValue("lfr-scope-layout-uuid", uuid);
      }

      portletPreferences.reset("lfr-scope-layout-id");
    }

    return PortletPreferencesFactoryUtil.toXML(portletPreferences);
  }
 public void reset() throws ReadOnlyException {
   portletPreferences.reset(name);
 }
  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);
  }
  protected DDLRecordSet updateRecordSet(ActionRequest actionRequest) throws Exception {

    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);

    long recordSetId = ParamUtil.getLong(actionRequest, "recordSetId");

    long groupId = ParamUtil.getLong(actionRequest, "groupId");
    long ddmStructureId = ParamUtil.getLong(actionRequest, "ddmStructureId");
    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(actionRequest, "name");
    Map<Locale, String> descriptionMap =
        LocalizationUtil.getLocalizationMap(actionRequest, "description");
    int scope = ParamUtil.getInteger(actionRequest, "scope");

    ServiceContext serviceContext =
        ServiceContextFactory.getInstance(DDLRecordSet.class.getName(), actionRequest);

    DDLRecordSet recordSet = null;

    if (cmd.equals(Constants.ADD)) {
      recordSet =
          DDLRecordSetServiceUtil.addRecordSet(
              groupId,
              ddmStructureId,
              null,
              nameMap,
              descriptionMap,
              DDLRecordSetConstants.MIN_DISPLAY_ROWS_DEFAULT,
              scope,
              serviceContext);
    } else {
      recordSet =
          DDLRecordSetServiceUtil.updateRecordSet(
              recordSetId,
              ddmStructureId,
              nameMap,
              descriptionMap,
              DDLRecordSetConstants.MIN_DISPLAY_ROWS_DEFAULT,
              serviceContext);
    }

    String workflowDefinition = ParamUtil.getString(actionRequest, "workflowDefinition");

    WorkflowDefinitionLinkLocalServiceUtil.updateWorkflowDefinitionLink(
        serviceContext.getUserId(),
        serviceContext.getCompanyId(),
        groupId,
        DDLRecordSet.class.getName(),
        recordSet.getRecordSetId(),
        0,
        workflowDefinition);

    String portletResource = ParamUtil.getString(actionRequest, "portletResource");

    if (Validator.isNotNull(portletResource)) {
      PortletPreferences preferences =
          PortletPreferencesFactoryUtil.getPortletSetup(actionRequest, portletResource);

      preferences.reset("detailDDMTemplateId");
      preferences.reset("editable");
      preferences.reset("listDDMTemplateId");
      preferences.reset("spreadsheet");

      preferences.setValue("recordSetId", String.valueOf(recordSet.getRecordSetId()));

      preferences.store();
    }

    return recordSet;
  }