001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.portletconfiguration.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.servlet.SessionMessages;
019    import com.liferay.portal.kernel.util.ParamUtil;
020    import com.liferay.portal.kernel.util.Validator;
021    import com.liferay.portal.model.Layout;
022    import com.liferay.portal.model.Portlet;
023    import com.liferay.portal.model.PublicRenderParameter;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portal.util.WebKeys;
028    import com.liferay.portlet.PortletPreferencesFactoryUtil;
029    import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterConfiguration;
030    
031    import java.util.Enumeration;
032    
033    import javax.portlet.ActionRequest;
034    import javax.portlet.ActionResponse;
035    import javax.portlet.PortletConfig;
036    import javax.portlet.PortletPreferences;
037    import javax.portlet.RenderRequest;
038    import javax.portlet.RenderResponse;
039    
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionForward;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Alberto Montero
046     */
047    public class EditPublicRenderParametersAction extends EditConfigurationAction {
048    
049            @Override
050            public void processAction(
051                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
052                            ActionRequest actionRequest, ActionResponse actionResponse)
053                    throws Exception {
054    
055                    Portlet portlet = null;
056    
057                    try {
058                            portlet = getPortlet(actionRequest);
059                    }
060                    catch (PrincipalException pe) {
061                            SessionErrors.add(
062                                    actionRequest, PrincipalException.class.getName());
063    
064                            setForward(actionRequest, "portlet.portlet_configuration.error");
065                    }
066    
067                    updatePreferences(actionRequest, portlet);
068    
069                    if (SessionErrors.isEmpty(actionRequest)) {
070                            SessionMessages.add(
071                                    actionRequest,
072                                    portletConfig.getPortletName() + ".doConfigure");
073    
074                            String portletResource = ParamUtil.getString(
075                                    actionRequest, "portletResource");
076    
077                            SessionMessages.add(
078                                    actionRequest, portletConfig.getPortletName() + ".doRefresh",
079                                    portletResource);
080    
081                            String redirect = PortalUtil.escapeRedirect(
082                                    ParamUtil.getString(actionRequest, "redirect"));
083    
084                            if (Validator.isNotNull(redirect)) {
085                                    actionResponse.sendRedirect(redirect);
086                            }
087                    }
088            }
089    
090            @Override
091            public ActionForward render(
092                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
093                            RenderRequest renderRequest, RenderResponse renderResponse)
094                    throws Exception {
095    
096                    Portlet portlet = null;
097    
098                    try {
099                            portlet = getPortlet(renderRequest);
100                    }
101                    catch (PrincipalException pe) {
102                            SessionErrors.add(
103                                    renderRequest, PrincipalException.class.getName());
104    
105                            return mapping.findForward("portlet.portlet_configuration.error");
106                    }
107    
108                    ActionUtil.getLayoutPublicRenderParameters(renderRequest);
109    
110                    ActionUtil.getPublicRenderParameterConfigurationList(
111                            renderRequest, portlet);
112    
113                    renderResponse.setTitle(getTitle(portlet, renderRequest));
114    
115                    return mapping.findForward(getForward(
116                            renderRequest,
117                            "portlet.portlet_configuration.edit_public_render_parameters"));
118            }
119    
120            protected void updatePreferences(
121                            ActionRequest actionRequest, Portlet portlet)
122                    throws Exception {
123    
124                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
125                            WebKeys.THEME_DISPLAY);
126    
127                    Layout layout = themeDisplay.getLayout();
128    
129                    PortletPreferences preferences =
130                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
131                                    layout, portlet.getPortletId());
132    
133                    Enumeration<String> enu = preferences.getNames();
134    
135                    while (enu.hasMoreElements()) {
136                            String name = enu.nextElement();
137    
138                            if (name.startsWith(
139                                            PublicRenderParameterConfiguration.IGNORE_PREFIX) ||
140                                    name.startsWith(
141                                            PublicRenderParameterConfiguration.MAPPING_PREFIX)) {
142    
143                                    preferences.reset(name);
144                            }
145                    }
146    
147                    for (PublicRenderParameter publicRenderParameter :
148                                    portlet.getPublicRenderParameters()) {
149    
150                            String ignoreKey = PublicRenderParameterConfiguration.getIgnoreKey(
151                                    publicRenderParameter);
152    
153                            boolean ignoreValue = ParamUtil.getBoolean(
154                                    actionRequest, ignoreKey);
155    
156                            if (ignoreValue) {
157                                    preferences.setValue(ignoreKey, String.valueOf(Boolean.TRUE));
158                            }
159                            else {
160                                    String mappingKey =
161                                            PublicRenderParameterConfiguration.getMappingKey(
162                                                    publicRenderParameter);
163    
164                                    String mappingValue = ParamUtil.getString(
165                                            actionRequest, mappingKey);
166    
167                                    if (Validator.isNotNull(mappingValue)) {
168                                            preferences.setValue(mappingKey, mappingValue);
169                                    }
170                            }
171                    }
172    
173                    if (SessionErrors.isEmpty(actionRequest)) {
174                            preferences.store();
175                    }
176            }
177    
178    }