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.StringPool;
20  import com.liferay.portal.kernel.util.Validator;
21  import com.liferay.portal.model.Layout;
22  import com.liferay.portal.model.Portlet;
23  import com.liferay.portal.model.PublicRenderParameter;
24  import com.liferay.portal.security.auth.PrincipalException;
25  import com.liferay.portal.theme.ThemeDisplay;
26  import com.liferay.portal.util.WebKeys;
27  import com.liferay.portlet.PortletPreferencesFactoryUtil;
28  import com.liferay.portlet.portletconfiguration.util.PublicRenderParameterConfiguration;
29  
30  import java.util.Enumeration;
31  
32  import javax.portlet.ActionRequest;
33  import javax.portlet.ActionResponse;
34  import javax.portlet.PortletConfig;
35  import javax.portlet.PortletPreferences;
36  import javax.portlet.RenderRequest;
37  import javax.portlet.RenderResponse;
38  
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionForward;
41  import org.apache.struts.action.ActionMapping;
42  
43  /**
44   * <a href="EditPublicRenderParametersAction.java.html"><b><i>View Source</i>
45   * </b></a>
46   *
47   * @author Alberto Montero
48   */
49  public class EditPublicRenderParametersAction extends EditConfigurationAction {
50  
51      public void processAction(
52              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
53              ActionRequest actionRequest, ActionResponse actionResponse)
54          throws Exception {
55  
56          Portlet portlet = null;
57  
58          try {
59              portlet = getPortlet(actionRequest);
60          }
61          catch (PrincipalException pe) {
62              SessionErrors.add(
63                  actionRequest, PrincipalException.class.getName());
64  
65              setForward(actionRequest, "portlet.portlet_configuration.error");
66          }
67  
68          updatePreferences(actionRequest, portlet);
69  
70          if (SessionErrors.isEmpty(actionRequest)) {
71              sendRedirect(actionRequest, actionResponse);
72          }
73      }
74  
75      public ActionForward render(
76              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
77              RenderRequest renderRequest, RenderResponse renderResponse)
78          throws Exception {
79  
80          Portlet portlet = null;
81  
82          try {
83              portlet = getPortlet(renderRequest);
84          }
85          catch (PrincipalException pe) {
86              SessionErrors.add(
87                  renderRequest, PrincipalException.class.getName());
88  
89              return mapping.findForward("portlet.portlet_configuration.error");
90          }
91  
92          ActionUtil.getLayoutPublicRenderParameters(renderRequest);
93  
94          ActionUtil.getPublicRenderParameterConfigurationList(
95              renderRequest, portlet);
96  
97          renderResponse.setTitle(getTitle(portlet, renderRequest));
98  
99          return mapping.findForward(getForward(
100             renderRequest,
101             "portlet.portlet_configuration.edit_public_render_parameters"));
102     }
103 
104     protected void updatePreferences(
105             ActionRequest actionRequest, Portlet portlet)
106         throws Exception {
107 
108         ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
109             WebKeys.THEME_DISPLAY);
110 
111         Layout layout = themeDisplay.getLayout();
112 
113         PortletPreferences preferences =
114             PortletPreferencesFactoryUtil.getLayoutPortletSetup(
115                 layout, portlet.getPortletId());
116 
117         Enumeration<String> enu = preferences.getNames();
118 
119         while (enu.hasMoreElements()) {
120             String name = enu.nextElement();
121 
122             if (name.startsWith(_IGNORE_PREFIX) ||
123                 name.startsWith(_MAPPING_PREFIX)) {
124 
125                 preferences.reset(name);
126             }
127         }
128 
129         for (PublicRenderParameter publicRenderParameter :
130                 portlet.getPublicRenderParameters()) {
131 
132             String ignoreName =
133                 _IGNORE_PREFIX + publicRenderParameter.getIdentifier();
134 
135             if (Validator.isNotNull(
136                     ParamUtil.getString(actionRequest, ignoreName))) {
137 
138                 preferences.setValue(ignoreName, StringPool.TRUE);
139             }
140             else {
141                 String mappingName =
142                     _MAPPING_PREFIX + publicRenderParameter.getIdentifier();
143 
144                 String mappingValue = ParamUtil.getString(
145                     actionRequest, mappingName);
146 
147                 if (Validator.isNotNull(mappingValue)) {
148                     String mappingKey = _MAPPING_PREFIX + mappingValue;
149 
150                     if (Validator.isNull(
151                             preferences.getValue(mappingKey, null))) {
152 
153                         preferences.setValue(
154                             mappingKey, publicRenderParameter.getIdentifier());
155                     }
156                     else {
157                         SessionErrors.add(actionRequest, "duplicateMapping");
158 
159                         break;
160                     }
161                 }
162             }
163         }
164 
165         if (SessionErrors.isEmpty(actionRequest)) {
166             preferences.store();
167         }
168     }
169 
170     private static final String _IGNORE_PREFIX =
171         PublicRenderParameterConfiguration.IGNORE_PREFIX;
172 
173     private static final String _MAPPING_PREFIX =
174         PublicRenderParameterConfiguration.MAPPING_PREFIX;
175 
176 }