001    /**
002     * Copyright (c) 2000-2013 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.portlet.portletconfiguration.action;
016    
017    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.servlet.SessionMessages;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.model.Portlet;
024    import com.liferay.portal.model.PublicRenderParameter;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.theme.ThemeDisplay;
027    import com.liferay.portal.util.PortalUtil;
028    import com.liferay.portal.util.WebKeys;
029    import com.liferay.portlet.PortletPreferencesFactoryUtil;
030    import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterConfiguration;
031    
032    import java.util.Enumeration;
033    
034    import javax.portlet.ActionRequest;
035    import javax.portlet.ActionResponse;
036    import javax.portlet.PortletConfig;
037    import javax.portlet.PortletPreferences;
038    import javax.portlet.RenderRequest;
039    import javax.portlet.RenderResponse;
040    
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionForward;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Alberto Montero
047     */
048    public class EditPublicRenderParametersAction extends EditConfigurationAction {
049    
050            @Override
051            public void processAction(
052                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
053                            ActionRequest actionRequest, ActionResponse actionResponse)
054                    throws Exception {
055    
056                    Portlet portlet = null;
057    
058                    try {
059                            portlet = getPortlet(actionRequest);
060                    }
061                    catch (PrincipalException pe) {
062                            SessionErrors.add(
063                                    actionRequest, PrincipalException.class.getName());
064    
065                            setForward(actionRequest, "portlet.portlet_configuration.error");
066                    }
067    
068                    updatePreferences(actionRequest, portlet);
069    
070                    if (SessionErrors.isEmpty(actionRequest)) {
071                            LiferayPortletConfig liferayPortletConfig =
072                                    (LiferayPortletConfig)portletConfig;
073    
074                            String portletResource = ParamUtil.getString(
075                                    actionRequest, "portletResource");
076    
077                            SessionMessages.add(
078                                    actionRequest,
079                                    liferayPortletConfig.getPortletId() +
080                                            SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
081                                    portletResource);
082    
083                            SessionMessages.add(
084                                    actionRequest,
085                                    liferayPortletConfig.getPortletId() +
086                                            SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
087    
088                            String redirect = PortalUtil.escapeRedirect(
089                                    ParamUtil.getString(actionRequest, "redirect"));
090    
091                            if (Validator.isNotNull(redirect)) {
092                                    actionResponse.sendRedirect(redirect);
093                            }
094                    }
095            }
096    
097            @Override
098            public ActionForward render(
099                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
100                            RenderRequest renderRequest, RenderResponse renderResponse)
101                    throws Exception {
102    
103                    Portlet portlet = null;
104    
105                    try {
106                            portlet = getPortlet(renderRequest);
107                    }
108                    catch (PrincipalException pe) {
109                            SessionErrors.add(
110                                    renderRequest, PrincipalException.class.getName());
111    
112                            return mapping.findForward("portlet.portlet_configuration.error");
113                    }
114    
115                    ActionUtil.getLayoutPublicRenderParameters(renderRequest);
116    
117                    ActionUtil.getPublicRenderParameterConfigurationList(
118                            renderRequest, portlet);
119    
120                    renderResponse.setTitle(getTitle(portlet, renderRequest));
121    
122                    return mapping.findForward(
123                            getForward(
124                                    renderRequest,
125                                    "portlet.portlet_configuration.edit_public_render_parameters"));
126            }
127    
128            protected void updatePreferences(
129                            ActionRequest actionRequest, Portlet portlet)
130                    throws Exception {
131    
132                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
133                            WebKeys.THEME_DISPLAY);
134    
135                    Layout layout = themeDisplay.getLayout();
136    
137                    PortletPreferences preferences =
138                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
139                                    layout, portlet.getPortletId());
140    
141                    Enumeration<String> enu = preferences.getNames();
142    
143                    while (enu.hasMoreElements()) {
144                            String name = enu.nextElement();
145    
146                            if (name.startsWith(
147                                            PublicRenderParameterConfiguration.IGNORE_PREFIX) ||
148                                    name.startsWith(
149                                            PublicRenderParameterConfiguration.MAPPING_PREFIX)) {
150    
151                                    preferences.reset(name);
152                            }
153                    }
154    
155                    for (PublicRenderParameter publicRenderParameter :
156                                    portlet.getPublicRenderParameters()) {
157    
158                            String ignoreKey = PublicRenderParameterConfiguration.getIgnoreKey(
159                                    publicRenderParameter);
160    
161                            boolean ignoreValue = ParamUtil.getBoolean(
162                                    actionRequest, ignoreKey);
163    
164                            if (ignoreValue) {
165                                    preferences.setValue(ignoreKey, String.valueOf(Boolean.TRUE));
166                            }
167                            else {
168                                    String mappingKey =
169                                            PublicRenderParameterConfiguration.getMappingKey(
170                                                    publicRenderParameter);
171    
172                                    String mappingValue = ParamUtil.getString(
173                                            actionRequest, mappingKey);
174    
175                                    if (Validator.isNotNull(mappingValue)) {
176                                            preferences.setValue(mappingKey, mappingValue);
177                                    }
178                            }
179                    }
180    
181                    if (SessionErrors.isEmpty(actionRequest)) {
182                            preferences.store();
183                    }
184            }
185    
186    }