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.Portlet;
028 import com.liferay.portal.security.permission.ActionKeys;
029 import com.liferay.portal.service.PortletLocalServiceUtil;
030 import com.liferay.portal.service.permission.PortletPermissionUtil;
031 import com.liferay.portal.theme.ThemeDisplay;
032 import com.liferay.portlet.PortletConfigFactoryUtil;
033 import com.liferay.portlet.PortletPreferencesFactoryUtil;
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
048 import javax.servlet.ServletContext;
049
050
054 public class DefaultConfigurationAction
055 implements ConfigurationAction, ResourceServingConfigurationAction {
056
057 public final static String PREFERENCES_PREFIX = "preferences--";
058
059 public String getLocalizedParameter(
060 PortletRequest portletRequest, String name) {
061
062 String languageId = ParamUtil.getString(portletRequest, "languageId");
063
064 return getParameter(
065 portletRequest,
066 name.concat(StringPool.UNDERLINE).concat(languageId));
067 }
068
069 public String getParameter(PortletRequest portletRequest, String name) {
070 name = PREFERENCES_PREFIX.concat(name).concat(StringPool.DOUBLE_DASH);
071
072 return ParamUtil.getString(portletRequest, name);
073 }
074
075 public void processAction(
076 PortletConfig portletConfig, ActionRequest actionRequest,
077 ActionResponse actionResponse)
078 throws Exception {
079
080 String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
081
082 if (!cmd.equals(Constants.UPDATE)) {
083 return;
084 }
085
086 ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
087 WebKeys.THEME_DISPLAY);
088
089 UnicodeProperties properties = PropertiesParamUtil.getProperties(
090 actionRequest, PREFERENCES_PREFIX);
091
092 String portletResource = ParamUtil.getString(
093 actionRequest, "portletResource");
094
095 PortletPermissionUtil.check(
096 themeDisplay.getPermissionChecker(), themeDisplay.getLayout(),
097 portletResource, ActionKeys.CONFIGURATION);
098
099 PortletPreferences portletPreferences =
100 PortletPreferencesFactoryUtil.getPortletSetup(
101 actionRequest, portletResource);
102
103 for (Map.Entry<String, String> entry : properties.entrySet()) {
104 String name = entry.getKey();
105 String value = entry.getValue();
106
107 portletPreferences.setValue(name, value);
108 }
109
110 Map<String, String[]> portletPreferencesMap =
111 (Map<String, String[]>)actionRequest.getAttribute(
112 WebKeys.PORTLET_PREFERENCES_MAP);
113
114 if (portletPreferencesMap != null) {
115 for (Map.Entry<String, String[]> entry :
116 portletPreferencesMap.entrySet()) {
117
118 String name = entry.getKey();
119 String[] values = entry.getValue();
120
121 portletPreferences.setValues(name, values);
122 }
123 }
124
125 if (SessionErrors.isEmpty(actionRequest)) {
126 portletPreferences.store();
127
128 SessionMessages.add(
129 actionRequest,
130 portletConfig.getPortletName() +
131 SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
132 portletResource);
133
134 SessionMessages.add(
135 actionRequest,
136 portletConfig.getPortletName() +
137 SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
138 }
139 }
140
141 public String render(
142 PortletConfig portletConfig, RenderRequest renderRequest,
143 RenderResponse renderResponse)
144 throws Exception {
145
146 PortletConfig selPortletConfig = getSelPortletConfig(renderRequest);
147
148 String configJSP = selPortletConfig.getInitParameter("config-jsp");
149
150 if (Validator.isNotNull(configJSP)) {
151 return configJSP;
152 }
153
154 return "/configuration.jsp";
155 }
156
157 public void serveResource(
158 PortletConfig portletConfig, ResourceRequest resourceRequest,
159 ResourceResponse resourceResponse)
160 throws Exception {
161 }
162
163 public void setPreference(
164 PortletRequest portletRequest, String name, String value) {
165
166 setPreference(portletRequest, name, new String[] {value});
167 }
168
169 public void setPreference(
170 PortletRequest portletRequest, String name, String[] values) {
171
172 Map<String, String[]> portletPreferencesMap =
173 (Map<String, String[]>)portletRequest.getAttribute(
174 WebKeys.PORTLET_PREFERENCES_MAP);
175
176 if (portletPreferencesMap == null) {
177 portletPreferencesMap = new HashMap<String, String[]>();
178
179 portletRequest.setAttribute(
180 WebKeys.PORTLET_PREFERENCES_MAP, portletPreferencesMap);
181 }
182
183 portletPreferencesMap.put(name, values);
184 }
185
186 protected PortletConfig getSelPortletConfig(PortletRequest portletRequest)
187 throws SystemException {
188
189 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
190 WebKeys.THEME_DISPLAY);
191
192 String portletResource = ParamUtil.getString(
193 portletRequest, "portletResource");
194
195 Portlet selPortlet = PortletLocalServiceUtil.getPortletById(
196 themeDisplay.getCompanyId(), portletResource);
197
198 ServletContext servletContext =
199 (ServletContext)portletRequest.getAttribute(WebKeys.CTX);
200
201 PortletConfig selPortletConfig = PortletConfigFactoryUtil.create(
202 selPortlet, servletContext);
203
204 return selPortletConfig;
205 }
206
207 }