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 }
152
153 @Override
154 public String render(
155 PortletConfig portletConfig, RenderRequest renderRequest,
156 RenderResponse renderResponse)
157 throws Exception {
158
159 PortletConfig selPortletConfig = getSelPortletConfig(renderRequest);
160
161 String configTemplate = selPortletConfig.getInitParameter(
162 "config-template");
163
164 if (Validator.isNotNull(configTemplate)) {
165 return configTemplate;
166 }
167
168 String configJSP = selPortletConfig.getInitParameter("config-jsp");
169
170 if (Validator.isNotNull(configJSP)) {
171 return configJSP;
172 }
173
174 return "/configuration.jsp";
175 }
176
177 @Override
178 public void serveResource(
179 PortletConfig portletConfig, ResourceRequest resourceRequest,
180 ResourceResponse resourceResponse)
181 throws Exception {
182 }
183
184 public void setPreference(
185 PortletRequest portletRequest, String name, String value) {
186
187 setPreference(portletRequest, name, new String[] {value});
188 }
189
190 public void setPreference(
191 PortletRequest portletRequest, String name, String[] values) {
192
193 Map<String, String[]> portletPreferencesMap =
194 (Map<String, String[]>)portletRequest.getAttribute(
195 WebKeys.PORTLET_PREFERENCES_MAP);
196
197 if (portletPreferencesMap == null) {
198 portletPreferencesMap = new HashMap<String, String[]>();
199
200 portletRequest.setAttribute(
201 WebKeys.PORTLET_PREFERENCES_MAP, portletPreferencesMap);
202 }
203
204 portletPreferencesMap.put(name, values);
205 }
206
207 protected PortletConfig getSelPortletConfig(PortletRequest portletRequest)
208 throws SystemException {
209
210 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
211 WebKeys.THEME_DISPLAY);
212
213 String portletResource = ParamUtil.getString(
214 portletRequest, "portletResource");
215
216 Portlet selPortlet = PortletLocalServiceUtil.getPortletById(
217 themeDisplay.getCompanyId(), portletResource);
218
219 ServletContext servletContext =
220 (ServletContext)portletRequest.getAttribute(WebKeys.CTX);
221
222 PortletConfig selPortletConfig = PortletConfigFactoryUtil.create(
223 selPortlet, servletContext);
224
225 return selPortletConfig;
226 }
227
228 }