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