001
014
015 package com.liferay.portal.kernel.portlet;
016
017 import com.liferay.portal.kernel.exception.SystemException;
018 import com.liferay.portal.kernel.servlet.SessionErrors;
019 import com.liferay.portal.kernel.servlet.SessionMessages;
020 import com.liferay.portal.kernel.util.Constants;
021 import com.liferay.portal.kernel.util.ParamUtil;
022 import com.liferay.portal.kernel.util.PropertiesParamUtil;
023 import com.liferay.portal.kernel.util.StringPool;
024 import com.liferay.portal.kernel.util.UnicodeProperties;
025 import com.liferay.portal.kernel.util.Validator;
026 import com.liferay.portal.kernel.util.WebKeys;
027 import com.liferay.portal.model.Layout;
028 import com.liferay.portal.model.Portlet;
029 import com.liferay.portal.security.permission.ActionKeys;
030 import com.liferay.portal.service.PortletLocalServiceUtil;
031 import com.liferay.portal.service.permission.PortletPermissionUtil;
032 import com.liferay.portal.theme.ThemeDisplay;
033 import com.liferay.portal.util.PortalUtil;
034 import com.liferay.portlet.PortletConfigFactoryUtil;
035
036 import java.util.HashMap;
037 import java.util.Map;
038
039 import javax.portlet.ActionRequest;
040 import javax.portlet.ActionResponse;
041 import javax.portlet.PortletConfig;
042 import javax.portlet.PortletPreferences;
043 import javax.portlet.PortletRequest;
044 import javax.portlet.RenderRequest;
045 import javax.portlet.RenderResponse;
046 import javax.portlet.ResourceRequest;
047 import javax.portlet.ResourceResponse;
048 import javax.portlet.ValidatorException;
049
050 import javax.servlet.ServletContext;
051
052
057 public class DefaultConfigurationAction
058 implements ConfigurationAction, ResourceServingConfigurationAction {
059
060 public static final String PREFERENCES_PREFIX = "preferences--";
061
062 public String getLocalizedParameter(
063 PortletRequest portletRequest, String name) {
064
065 String languageId = ParamUtil.getString(portletRequest, "languageId");
066
067 return getParameter(
068 portletRequest,
069 name.concat(StringPool.UNDERLINE).concat(languageId));
070 }
071
072 public String getParameter(PortletRequest portletRequest, String name) {
073 name = PREFERENCES_PREFIX.concat(name).concat(StringPool.DOUBLE_DASH);
074
075 return ParamUtil.getString(portletRequest, name);
076 }
077
078 @Override
079 public void processAction(
080 PortletConfig portletConfig, ActionRequest actionRequest,
081 ActionResponse actionResponse)
082 throws Exception {
083
084 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
085
086 if (!cmd.equals(Constants.UPDATE)) {
087 return;
088 }
089
090 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
091 WebKeys.THEME_DISPLAY);
092
093 UnicodeProperties properties = PropertiesParamUtil.getProperties(
094 actionRequest, PREFERENCES_PREFIX);
095
096 String portletResource = ParamUtil.getString(
097 actionRequest, "portletResource");
098
099 Layout layout = PortletConfigurationLayoutUtil.getLayout(themeDisplay);
100
101 PortletPermissionUtil.check(
102 themeDisplay.getPermissionChecker(), themeDisplay.getScopeGroupId(),
103 layout, portletResource, ActionKeys.CONFIGURATION);
104
105 PortletPreferences portletPreferences = actionRequest.getPreferences();
106
107 for (Map.Entry<String, String> entry : properties.entrySet()) {
108 String name = entry.getKey();
109 String value = entry.getValue();
110
111 portletPreferences.setValue(name, value);
112 }
113
114 Map<String, String[]> portletPreferencesMap =
115 (Map<String, String[]>)actionRequest.getAttribute(
116 WebKeys.PORTLET_PREFERENCES_MAP);
117
118 if (portletPreferencesMap != null) {
119 for (Map.Entry<String, String[]> entry :
120 portletPreferencesMap.entrySet()) {
121
122 String name = entry.getKey();
123 String[] values = entry.getValue();
124
125 portletPreferences.setValues(name, values);
126 }
127 }
128
129 if (SessionErrors.isEmpty(actionRequest)) {
130 try {
131 portletPreferences.store();
132 }
133 catch (ValidatorException ve) {
134 SessionErrors.add(
135 actionRequest, ValidatorException.class.getName(), ve);
136
137 return;
138 }
139
140 SessionMessages.add(
141 actionRequest,
142 PortalUtil.getPortletId(actionRequest) +
143 SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
144 portletResource);
145
146 SessionMessages.add(
147 actionRequest,
148 PortalUtil.getPortletId(actionRequest) +
149 SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
150
151 String redirect = PortalUtil.escapeRedirect(
152 ParamUtil.getString(actionRequest, "redirect"));
153
154 if (Validator.isNotNull(redirect)) {
155 actionResponse.sendRedirect(redirect);
156 }
157 }
158 }
159
160 @Override
161 public String render(
162 PortletConfig portletConfig, RenderRequest renderRequest,
163 RenderResponse renderResponse)
164 throws Exception {
165
166 PortletConfig selPortletConfig = getSelPortletConfig(renderRequest);
167
168 String configTemplate = selPortletConfig.getInitParameter(
169 "config-template");
170
171 if (Validator.isNotNull(configTemplate)) {
172 return configTemplate;
173 }
174
175 String configJSP = selPortletConfig.getInitParameter("config-jsp");
176
177 if (Validator.isNotNull(configJSP)) {
178 return configJSP;
179 }
180
181 return "/configuration.jsp";
182 }
183
184 @Override
185 public void serveResource(
186 PortletConfig portletConfig, ResourceRequest resourceRequest,
187 ResourceResponse resourceResponse)
188 throws Exception {
189 }
190
191 public void setPreference(
192 PortletRequest portletRequest, String name, String value) {
193
194 setPreference(portletRequest, name, new String[] {value});
195 }
196
197 public void setPreference(
198 PortletRequest portletRequest, String name, String[] values) {
199
200 Map<String, String[]> portletPreferencesMap =
201 (Map<String, String[]>)portletRequest.getAttribute(
202 WebKeys.PORTLET_PREFERENCES_MAP);
203
204 if (portletPreferencesMap == null) {
205 portletPreferencesMap = new HashMap<String, String[]>();
206
207 portletRequest.setAttribute(
208 WebKeys.PORTLET_PREFERENCES_MAP, portletPreferencesMap);
209 }
210
211 portletPreferencesMap.put(name, values);
212 }
213
214 protected PortletConfig getSelPortletConfig(PortletRequest portletRequest)
215 throws SystemException {
216
217 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
218 WebKeys.THEME_DISPLAY);
219
220 String portletResource = ParamUtil.getString(
221 portletRequest, "portletResource");
222
223 Portlet selPortlet = PortletLocalServiceUtil.getPortletById(
224 themeDisplay.getCompanyId(), portletResource);
225
226 ServletContext servletContext =
227 (ServletContext)portletRequest.getAttribute(WebKeys.CTX);
228
229 PortletConfig selPortletConfig = PortletConfigFactoryUtil.create(
230 selPortlet, servletContext);
231
232 return selPortletConfig;
233 }
234
235 }