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