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