1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.portletconfiguration.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.ParamUtil;
19  import com.liferay.portal.kernel.util.Validator;
20  import com.liferay.portal.model.Layout;
21  import com.liferay.portal.model.Portlet;
22  import com.liferay.portal.model.PublicRenderParameter;
23  import com.liferay.portal.security.auth.PrincipalException;
24  import com.liferay.portal.theme.ThemeDisplay;
25  import com.liferay.portal.util.WebKeys;
26  import com.liferay.portlet.PortletPreferencesFactoryUtil;
27  import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterConfiguration;
28  
29  import java.util.Enumeration;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.ActionResponse;
33  import javax.portlet.PortletConfig;
34  import javax.portlet.PortletPreferences;
35  import javax.portlet.RenderRequest;
36  import javax.portlet.RenderResponse;
37  
38  import org.apache.struts.action.ActionForm;
39  import org.apache.struts.action.ActionForward;
40  import org.apache.struts.action.ActionMapping;
41  
42  /**
43   * <a href="EditPublicRenderParametersAction.java.html"><b><i>View Source</i>
44   * </b></a>
45   *
46   * @author Alberto Montero
47   */
48  public class EditPublicRenderParametersAction extends EditConfigurationAction {
49  
50      public void processAction(
51              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
52              ActionRequest actionRequest, ActionResponse actionResponse)
53          throws Exception {
54  
55          Portlet portlet = null;
56  
57          try {
58              portlet = getPortlet(actionRequest);
59          }
60          catch (PrincipalException pe) {
61              SessionErrors.add(
62                  actionRequest, PrincipalException.class.getName());
63  
64              setForward(actionRequest, "portlet.portlet_configuration.error");
65          }
66  
67          updatePreferences(actionRequest, portlet);
68  
69          if (SessionErrors.isEmpty(actionRequest)) {
70              sendRedirect(actionRequest, actionResponse);
71          }
72      }
73  
74      public ActionForward render(
75              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
76              RenderRequest renderRequest, RenderResponse renderResponse)
77          throws Exception {
78  
79          Portlet portlet = null;
80  
81          try {
82              portlet = getPortlet(renderRequest);
83          }
84          catch (PrincipalException pe) {
85              SessionErrors.add(
86                  renderRequest, PrincipalException.class.getName());
87  
88              return mapping.findForward("portlet.portlet_configuration.error");
89          }
90  
91          ActionUtil.getLayoutPublicRenderParameters(renderRequest);
92  
93          ActionUtil.getPublicRenderParameterConfigurationList(
94              renderRequest, portlet);
95  
96          renderResponse.setTitle(getTitle(portlet, renderRequest));
97  
98          return mapping.findForward(getForward(
99              renderRequest,
100             "portlet.portlet_configuration.edit_public_render_parameters"));
101     }
102 
103     protected void updatePreferences(
104             ActionRequest actionRequest, Portlet portlet)
105         throws Exception {
106 
107         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
108             WebKeys.THEME_DISPLAY);
109 
110         Layout layout = themeDisplay.getLayout();
111 
112         PortletPreferences preferences =
113             PortletPreferencesFactoryUtil.getLayoutPortletSetup(
114                 layout, portlet.getPortletId());
115 
116         Enumeration<String> enu = preferences.getNames();
117 
118         while (enu.hasMoreElements()) {
119             String name = enu.nextElement();
120 
121             if (name.startsWith(
122                     PublicRenderParameterConfiguration.IGNORE_PREFIX) ||
123                 name.startsWith(
124                     PublicRenderParameterConfiguration.MAPPING_PREFIX)) {
125 
126                 preferences.reset(name);
127             }
128         }
129 
130         for (PublicRenderParameter publicRenderParameter :
131                 portlet.getPublicRenderParameters()) {
132 
133             String ignoreKey = PublicRenderParameterConfiguration.getIgnoreKey(
134                 publicRenderParameter);
135 
136             boolean ignoreValue = ParamUtil.getBoolean(
137                 actionRequest, ignoreKey);
138 
139             if (ignoreValue) {
140                 preferences.setValue(ignoreKey, String.valueOf(Boolean.TRUE));
141             }
142             else {
143                 String mappingKey =
144                     PublicRenderParameterConfiguration.getMappingKey(
145                         publicRenderParameter);
146 
147                 String mappingValue = ParamUtil.getString(
148                     actionRequest, mappingKey);
149 
150                 if (Validator.isNotNull(mappingValue)) {
151                     preferences.setValue(mappingKey, mappingValue);
152                 }
153             }
154         }
155 
156         if (SessionErrors.isEmpty(actionRequest)) {
157             preferences.store();
158         }
159     }
160 
161 }