001    /**
002     * Copyright (c) 2000-2012 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.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                            String portletResource = ParamUtil.getString(
071                                    actionRequest, "portletResource");
072    
073                            SessionMessages.add(
074                                    actionRequest,
075                                    portletConfig.getPortletName() +
076                                            SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
077                                    portletResource);
078    
079                            SessionMessages.add(
080                                    actionRequest,
081                                    portletConfig.getPortletName() +
082                                            SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
083    
084                            String redirect = PortalUtil.escapeRedirect(
085                                    ParamUtil.getString(actionRequest, "redirect"));
086    
087                            if (Validator.isNotNull(redirect)) {
088                                    actionResponse.sendRedirect(redirect);
089                            }
090                    }
091            }
092    
093            @Override
094            public ActionForward render(
095                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
096                            RenderRequest renderRequest, RenderResponse renderResponse)
097                    throws Exception {
098    
099                    Portlet portlet = null;
100    
101                    try {
102                            portlet = getPortlet(renderRequest);
103                    }
104                    catch (PrincipalException pe) {
105                            SessionErrors.add(
106                                    renderRequest, PrincipalException.class.getName());
107    
108                            return mapping.findForward("portlet.portlet_configuration.error");
109                    }
110    
111                    ActionUtil.getLayoutPublicRenderParameters(renderRequest);
112    
113                    ActionUtil.getPublicRenderParameterConfigurationList(
114                            renderRequest, portlet);
115    
116                    renderResponse.setTitle(getTitle(portlet, renderRequest));
117    
118                    return mapping.findForward(getForward(
119                            renderRequest,
120                            "portlet.portlet_configuration.edit_public_render_parameters"));
121            }
122    
123            protected void updatePreferences(
124                            ActionRequest actionRequest, Portlet portlet)
125                    throws Exception {
126    
127                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
128                            WebKeys.THEME_DISPLAY);
129    
130                    Layout layout = themeDisplay.getLayout();
131    
132                    PortletPreferences preferences =
133                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
134                                    layout, portlet.getPortletId());
135    
136                    Enumeration<String> enu = preferences.getNames();
137    
138                    while (enu.hasMoreElements()) {
139                            String name = enu.nextElement();
140    
141                            if (name.startsWith(
142                                            PublicRenderParameterConfiguration.IGNORE_PREFIX) ||
143                                    name.startsWith(
144                                            PublicRenderParameterConfiguration.MAPPING_PREFIX)) {
145    
146                                    preferences.reset(name);
147                            }
148                    }
149    
150                    for (PublicRenderParameter publicRenderParameter :
151                                    portlet.getPublicRenderParameters()) {
152    
153                            String ignoreKey = PublicRenderParameterConfiguration.getIgnoreKey(
154                                    publicRenderParameter);
155    
156                            boolean ignoreValue = ParamUtil.getBoolean(
157                                    actionRequest, ignoreKey);
158    
159                            if (ignoreValue) {
160                                    preferences.setValue(ignoreKey, String.valueOf(Boolean.TRUE));
161                            }
162                            else {
163                                    String mappingKey =
164                                            PublicRenderParameterConfiguration.getMappingKey(
165                                                    publicRenderParameter);
166    
167                                    String mappingValue = ParamUtil.getString(
168                                            actionRequest, mappingKey);
169    
170                                    if (Validator.isNotNull(mappingValue)) {
171                                            preferences.setValue(mappingKey, mappingValue);
172                                    }
173                            }
174                    }
175    
176                    if (SessionErrors.isEmpty(actionRequest)) {
177                            preferences.store();
178                    }
179            }
180    
181    }