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 LiferayPortletConfig liferayPortletConfig =
129 (LiferayPortletConfig)portletConfig;
130
131 SessionMessages.add(
132 actionRequest,
133 liferayPortletConfig.getPortletId() +
134 SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
135 portletResource);
136
137 SessionMessages.add(
138 actionRequest,
139 liferayPortletConfig.getPortletId() +
140 SessionMessages.KEY_SUFFIX_UPDATED_CONFIGURATION);
141 }
142 }
143
144 public String render(
145 PortletConfig portletConfig, RenderRequest renderRequest,
146 RenderResponse renderResponse)
147 throws Exception {
148
149 PortletConfig selPortletConfig = getSelPortletConfig(renderRequest);
150
151 String configTemplate = selPortletConfig.getInitParameter(
152 "config-template");
153
154 if (Validator.isNotNull(configTemplate)) {
155 return configTemplate;
156 }
157
158 String configJSP = selPortletConfig.getInitParameter("config-jsp");
159
160 if (Validator.isNotNull(configJSP)) {
161 return configJSP;
162 }
163
164 return "/configuration.jsp";
165 }
166
167 public void serveResource(
168 PortletConfig portletConfig, ResourceRequest resourceRequest,
169 ResourceResponse resourceResponse)
170 throws Exception {
171 }
172
173 public void setPreference(
174 PortletRequest portletRequest, String name, String value) {
175
176 setPreference(portletRequest, name, new String[] {value});
177 }
178
179 public void setPreference(
180 PortletRequest portletRequest, String name, String[] values) {
181
182 Map<String, String[]> portletPreferencesMap =
183 (Map<String, String[]>)portletRequest.getAttribute(
184 WebKeys.PORTLET_PREFERENCES_MAP);
185
186 if (portletPreferencesMap == null) {
187 portletPreferencesMap = new HashMap<String, String[]>();
188
189 portletRequest.setAttribute(
190 WebKeys.PORTLET_PREFERENCES_MAP, portletPreferencesMap);
191 }
192
193 portletPreferencesMap.put(name, values);
194 }
195
196 protected PortletConfig getSelPortletConfig(PortletRequest portletRequest)
197 throws SystemException {
198
199 ThemeDisplay themeDisplay = (ThemeDisplay)portletRequest.getAttribute(
200 WebKeys.THEME_DISPLAY);
201
202 String portletResource = ParamUtil.getString(
203 portletRequest, "portletResource");
204
205 Portlet selPortlet = PortletLocalServiceUtil.getPortletById(
206 themeDisplay.getCompanyId(), portletResource);
207
208 ServletContext servletContext =
209 (ServletContext)portletRequest.getAttribute(WebKeys.CTX);
210
211 PortletConfig selPortletConfig = PortletConfigFactoryUtil.create(
212 selPortlet, servletContext);
213
214 return selPortletConfig;
215 }
216
217 }