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