/**
   * 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();
    }
  }
  /**
   * Saves the values in the portletPreferences.xhtml Facelet composition as portlet preferences.
   */
  public void submit() {

    // The JSR 329 specification defines an EL variable named mutablePortletPreferencesValues that
    // is being used in
    // the portletPreferences.xhtml Facelet composition. This object is of type Map<String,
    // Preference> and is
    // designed to be a model managed-bean (in a sense) that contain preference values. However the
    // only way to
    // access this from a Java class is to evaluate an EL expression (effectively self-injecting)
    // the map into
    // this backing bean.
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    String elExpression = "mutablePortletPreferencesValues";
    ELResolver elResolver = facesContext.getApplication().getELResolver();
    @SuppressWarnings("unchecked")
    Map<String, Preference> mutablePreferenceMap =
        (Map<String, Preference>)
            elResolver.getValue(facesContext.getELContext(), null, elExpression);

    // Get a list of portlet preference names.
    PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
    PortletPreferences portletPreferences = portletRequest.getPreferences();
    Enumeration<String> preferenceNames = portletPreferences.getNames();

    try {

      // For each portlet preference name:
      while (preferenceNames.hasMoreElements()) {

        // Get the value specified by the user.
        String preferenceName = preferenceNames.nextElement();
        String preferenceValue = mutablePreferenceMap.get(preferenceName).getValue();

        // Prepare to save the value.
        if (!portletPreferences.isReadOnly(preferenceName)) {
          portletPreferences.setValue(preferenceName, preferenceValue);
        }
      }

      // Save the preference values.
      portletPreferences.store();

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

      // Report a successful message back to the user as feedback.
      FacesContextHelperUtil.addGlobalSuccessInfoMessage();
    } catch (Exception e) {
      FacesContextHelperUtil.addGlobalUnexpectedErrorMessage();
    }
  }
  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();
    }
  }
  private Map<String, Preference> getPreferenceMap(PortletPreferences prefs) {

    Map<String, Preference> m;

    // construct a Map of PreferenceImpl objects for each preference
    Enumeration<String> e = prefs.getNames();

    if (e.hasMoreElements()) {
      m = new HashMap<String, Preference>();
      while (e.hasMoreElements()) {
        String name = e.nextElement();
        m.put(name, new PreferenceImpl(prefs, name));
      }
    } else {
      m = Collections.emptyMap();
    }

    return m;
  }
  protected void updatePortletPreferences(
      PortletDataContext portletDataContext,
      long ownerId,
      int ownerType,
      long plid,
      String portletId,
      String xml,
      boolean importData)
      throws Exception {

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

    if (portlet == null) {
      if (_log.isDebugEnabled()) {
        _log.debug(
            "Do not update portlet preferences for "
                + portletId
                + " because the portlet does not exist");
      }

      return;
    }

    PortletDataHandler portletDataHandler = portlet.getPortletDataHandlerInstance();

    if (importData || !MergeLayoutPrototypesThreadLocal.isInProgress()) {
      _portletPreferencesLocalService.updatePreferences(ownerId, ownerType, plid, portletId, xml);

      return;
    }

    // Portlet preferences to be updated only when importing data

    String[] dataPortletPreferences = portletDataHandler.getDataPortletPreferences();

    // 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);

    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);
  }
 @Override
 protected Enumeration<String> getPropertyNames() {
   return portletPreferences.getNames();
 }
  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);
  }