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