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