001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.portlet;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.settings.PortletPreferencesSettings;
020    import com.liferay.portal.kernel.settings.Settings;
021    import com.liferay.portal.kernel.util.StringUtil;
022    
023    import javax.portlet.ActionRequest;
024    import javax.portlet.PortletPreferences;
025    import javax.portlet.PortletRequest;
026    import javax.portlet.ReadOnlyException;
027    
028    /**
029     * @author Iv??n Zaera
030     */
031    public class DefaultConfigurationAction
032            extends BaseJSPSettingsConfigurationAction
033            implements ConfigurationAction, ResourceServingConfigurationAction {
034    
035            public DefaultConfigurationAction() {
036                    setParameterNamePrefix("preferences--");
037            }
038    
039            @Override
040            protected Settings getSettings(ActionRequest actionRequest) {
041                    return new PortletPreferencesSettings(actionRequest.getPreferences());
042            }
043    
044            @SuppressWarnings("unused")
045            protected void postProcess(
046                            long companyId, PortletRequest portletRequest,
047                            PortletPreferences portletPreferences)
048                    throws PortalException {
049            }
050    
051            @Override
052            protected void postProcess(
053                            long companyId, PortletRequest portletRequest, Settings settings)
054                    throws PortalException {
055    
056                    PortletPreferencesSettings portletPreferencesSettings =
057                            (PortletPreferencesSettings)settings;
058    
059                    postProcess(
060                            companyId, portletRequest,
061                            portletPreferencesSettings.getPortletPreferences());
062            }
063    
064            protected void removeDefaultValue(
065                    PortletRequest portletRequest, PortletPreferences portletPreferences,
066                    String key, String defaultValue) {
067    
068                    String value = getParameter(portletRequest, key);
069    
070                    if (defaultValue.equals(value) ||
071                            StringUtil.equalsIgnoreBreakLine(defaultValue, value)) {
072    
073                            try {
074                                    portletPreferences.reset(key);
075                            }
076                            catch (ReadOnlyException roe) {
077                                    throw new SystemException(roe);
078                            }
079                    }
080            }
081    
082            @Override
083            protected void updateMultiValuedKeys(ActionRequest actionRequest) {
084    
085                    // Legacy configuration actions that are not based on Settings must
086                    // ignore this method to avoid failures due to multi valued keys not
087                    // registering with SettingsConfigurationAction
088    
089            }
090    
091    }