001
014
015 package com.liferay.portal.action;
016
017 import com.liferay.portal.kernel.json.JSONFactoryUtil;
018 import com.liferay.portal.kernel.json.JSONObject;
019 import com.liferay.portal.kernel.portlet.AddPortletProvider;
020 import com.liferay.portal.kernel.portlet.PortletJSONUtil;
021 import com.liferay.portal.kernel.servlet.BufferCacheServletResponse;
022 import com.liferay.portal.kernel.servlet.DynamicServletRequest;
023 import com.liferay.portal.kernel.servlet.ServletResponseUtil;
024 import com.liferay.portal.kernel.util.Constants;
025 import com.liferay.portal.kernel.util.ContentTypes;
026 import com.liferay.portal.kernel.util.GetterUtil;
027 import com.liferay.portal.kernel.util.InstancePool;
028 import com.liferay.portal.kernel.util.ParamUtil;
029 import com.liferay.portal.kernel.util.PropertiesParamUtil;
030 import com.liferay.portal.kernel.util.StringBundler;
031 import com.liferay.portal.kernel.util.StringPool;
032 import com.liferay.portal.kernel.util.StringUtil;
033 import com.liferay.portal.kernel.util.UnicodeProperties;
034 import com.liferay.portal.kernel.util.Validator;
035 import com.liferay.portal.kernel.util.WebKeys;
036 import com.liferay.portal.kernel.workflow.WorkflowConstants;
037 import com.liferay.portal.model.Layout;
038 import com.liferay.portal.model.LayoutRevision;
039 import com.liferay.portal.model.LayoutTypePortlet;
040 import com.liferay.portal.model.Portlet;
041 import com.liferay.portal.security.permission.ActionKeys;
042 import com.liferay.portal.security.permission.PermissionChecker;
043 import com.liferay.portal.service.LayoutRevisionLocalServiceUtil;
044 import com.liferay.portal.service.LayoutServiceUtil;
045 import com.liferay.portal.service.PortletLocalServiceUtil;
046 import com.liferay.portal.service.ServiceContext;
047 import com.liferay.portal.service.ServiceContextFactory;
048 import com.liferay.portal.service.permission.LayoutPermissionUtil;
049 import com.liferay.portal.servlet.NamespaceServletRequest;
050 import com.liferay.portal.struts.JSONAction;
051 import com.liferay.portal.theme.ThemeDisplay;
052 import com.liferay.portal.util.LayoutClone;
053 import com.liferay.portal.util.LayoutCloneFactory;
054 import com.liferay.portal.util.PortalUtil;
055 import com.liferay.portlet.PortletPreferencesFactoryUtil;
056 import com.liferay.portlet.asset.model.AssetEntry;
057 import com.liferay.portlet.exportimport.staging.StagingUtil;
058 import com.liferay.registry.collections.ServiceTrackerCollections;
059 import com.liferay.registry.collections.ServiceTrackerMap;
060
061 import javax.portlet.PortletPreferences;
062
063 import javax.servlet.http.HttpServletRequest;
064 import javax.servlet.http.HttpServletResponse;
065
066 import org.apache.struts.action.Action;
067 import org.apache.struts.action.ActionForm;
068 import org.apache.struts.action.ActionMapping;
069
070
073 public class UpdateLayoutAction extends JSONAction {
074
075 @Override
076 public String getJSON(
077 ActionMapping actionMapping, ActionForm actionForm,
078 HttpServletRequest request, HttpServletResponse response)
079 throws Exception {
080
081 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
082 WebKeys.THEME_DISPLAY);
083
084 long userId = themeDisplay.getUserId();
085
086 Layout layout = themeDisplay.getLayout();
087 LayoutTypePortlet layoutTypePortlet =
088 themeDisplay.getLayoutTypePortlet();
089
090 PermissionChecker permissionChecker =
091 themeDisplay.getPermissionChecker();
092
093 String cmd = ParamUtil.getString(request, Constants.CMD);
094
095 String portletId = ParamUtil.getString(request, "p_p_id");
096
097 boolean updateLayout = true;
098
099 if (cmd.equals(Constants.ADD)) {
100 String columnId = ParamUtil.getString(request, "p_p_col_id", null);
101 int columnPos = ParamUtil.getInteger(request, "p_p_col_pos", -1);
102
103 portletId = layoutTypePortlet.addPortletId(
104 userId, portletId, columnId, columnPos);
105
106 storeAddContentPortletPreferences(
107 request, layout, portletId, themeDisplay);
108
109 if (layoutTypePortlet.isCustomizable() &&
110 layoutTypePortlet.isCustomizedView() &&
111 !layoutTypePortlet.isColumnDisabled(columnId)) {
112
113 updateLayout = false;
114 }
115 }
116 else if (cmd.equals(Constants.DELETE)) {
117 if (layoutTypePortlet.hasPortletId(portletId)) {
118 layoutTypePortlet.removePortletId(userId, portletId);
119
120 if (layoutTypePortlet.isCustomizable() &&
121 layoutTypePortlet.isCustomizedView()) {
122
123 updateLayout = false;
124 }
125 }
126 }
127 else if (cmd.equals("drag")) {
128 if (LayoutPermissionUtil.contains(
129 permissionChecker, layout, ActionKeys.UPDATE)) {
130
131 String height = ParamUtil.getString(request, "height");
132 String width = ParamUtil.getString(request, "width");
133 String top = ParamUtil.getString(request, "top");
134 String left = ParamUtil.getString(request, "left");
135
136 PortletPreferences portletPreferences =
137 PortletPreferencesFactoryUtil.getLayoutPortletSetup(
138 layout, portletId);
139
140 StringBundler sb = new StringBundler(12);
141
142 sb.append("height=");
143 sb.append(height);
144 sb.append("\n");
145 sb.append("width=");
146 sb.append(width);
147 sb.append("\n");
148 sb.append("top=");
149 sb.append(top);
150 sb.append("\n");
151 sb.append("left=");
152 sb.append(left);
153 sb.append("\n");
154
155 portletPreferences.setValue(
156 "portlet-freeform-styles", sb.toString());
157
158 portletPreferences.store();
159 }
160 }
161 else if (cmd.equals("minimize")) {
162 boolean restore = ParamUtil.getBoolean(request, "p_p_restore");
163
164 if (restore) {
165 layoutTypePortlet.removeStateMinPortletId(portletId);
166 }
167 else {
168 layoutTypePortlet.addStateMinPortletId(portletId);
169 }
170
171 updateLayout = false;
172 }
173 else if (cmd.equals("move")) {
174 String columnId = ParamUtil.getString(request, "p_p_col_id");
175 int columnPos = ParamUtil.getInteger(request, "p_p_col_pos");
176
177 layoutTypePortlet.movePortletId(
178 userId, portletId, columnId, columnPos);
179
180 if (layoutTypePortlet.isCustomizable() &&
181 layoutTypePortlet.isCustomizedView() &&
182 !layoutTypePortlet.isColumnDisabled(columnId)) {
183
184 updateLayout = false;
185 }
186 }
187 else if (cmd.equals("redo_layout_revision")) {
188 long layoutRevisionId = ParamUtil.getLong(
189 request, "layoutRevisionId");
190 long layoutSetBranchId = ParamUtil.getLong(
191 request, "layoutSetBranchId");
192
193 ServiceContext serviceContext = ServiceContextFactory.getInstance(
194 request);
195
196 LayoutRevisionLocalServiceUtil.updateStatus(
197 userId, layoutRevisionId, WorkflowConstants.STATUS_DRAFT,
198 serviceContext);
199
200 StagingUtil.setRecentLayoutRevisionId(
201 request, layoutSetBranchId, layout.getPlid(), layoutRevisionId);
202
203 updateLayout = false;
204 }
205 else if (cmd.equals("select_layout_revision")) {
206 long layoutRevisionId = ParamUtil.getLong(
207 request, "layoutRevisionId");
208 long layoutSetBranchId = ParamUtil.getLong(
209 request, "layoutSetBranchId");
210
211 StagingUtil.setRecentLayoutRevisionId(
212 request, layoutSetBranchId, layout.getPlid(), layoutRevisionId);
213
214 updateLayout = false;
215 }
216 else if (cmd.equals("toggle_customized_view")) {
217 updateLayout = false;
218 }
219 else if (cmd.equals("update_type_settings")) {
220 UnicodeProperties layoutTypeSettingsProperties =
221 layout.getTypeSettingsProperties();
222
223 UnicodeProperties formTypeSettingsProperties =
224 PropertiesParamUtil.getProperties(
225 request, "TypeSettingsProperties--");
226
227 layoutTypeSettingsProperties.putAll(formTypeSettingsProperties);
228 }
229 else if (cmd.equals("undo_layout_revision")) {
230 long layoutRevisionId = ParamUtil.getLong(
231 request, "layoutRevisionId");
232 long layoutSetBranchId = ParamUtil.getLong(
233 request, "layoutSetBranchId");
234
235 ServiceContext serviceContext = ServiceContextFactory.getInstance(
236 request);
237
238 LayoutRevision layoutRevision =
239 LayoutRevisionLocalServiceUtil.updateStatus(
240 userId, layoutRevisionId, WorkflowConstants.STATUS_INACTIVE,
241 serviceContext);
242
243 StagingUtil.setRecentLayoutRevisionId(
244 request, layoutSetBranchId, layout.getPlid(),
245 layoutRevision.getParentLayoutRevisionId());
246
247 updateLayout = false;
248 }
249
250 if (updateLayout) {
251
252
253
254 layoutTypePortlet.resetModes();
255 layoutTypePortlet.resetStates();
256
257 layout = LayoutServiceUtil.updateLayout(
258 layout.getGroupId(), layout.isPrivateLayout(),
259 layout.getLayoutId(), layout.getTypeSettings());
260 }
261 else {
262 LayoutClone layoutClone = LayoutCloneFactory.getInstance();
263
264 if (layoutClone != null) {
265 layoutClone.update(
266 request, layout.getPlid(), layout.getTypeSettings());
267 }
268 }
269
270 if (cmd.equals(Constants.ADD) && (portletId != null)) {
271 addPortlet(actionMapping, actionForm, request, response, portletId);
272 }
273
274 return StringPool.BLANK;
275 }
276
277 protected void addPortlet(
278 ActionMapping actionMapping, ActionForm actionForm,
279 HttpServletRequest request, HttpServletResponse response,
280 String portletId)
281 throws Exception {
282
283
284
285 Action renderPortletAction = (Action)InstancePool.get(
286 RenderPortletAction.class.getName());
287
288
289
290
291 long companyId = PortalUtil.getCompanyId(request);
292
293 Portlet portlet = PortletLocalServiceUtil.getPortletById(
294 companyId, portletId);
295
296 DynamicServletRequest dynamicRequest = null;
297
298 if (portlet.isPrivateRequestAttributes()) {
299 String portletNamespace = PortalUtil.getPortletNamespace(
300 portlet.getPortletId());
301
302 dynamicRequest = new NamespaceServletRequest(
303 request, portletNamespace, portletNamespace);
304 }
305 else {
306 dynamicRequest = new DynamicServletRequest(request);
307 }
308
309 dynamicRequest.setParameter("p_p_id", portletId);
310
311 String dataType = StringUtil.toLowerCase(
312 ParamUtil.getString(request, "dataType"));
313
314 if (dataType.equals("json")) {
315 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
316
317 BufferCacheServletResponse bufferCacheServletResponse =
318 new BufferCacheServletResponse(response);
319
320 renderPortletAction.execute(
321 actionMapping, actionForm, dynamicRequest,
322 bufferCacheServletResponse);
323
324 String portletHTML = bufferCacheServletResponse.getString();
325
326 portletHTML = portletHTML.trim();
327
328 PortletJSONUtil.populatePortletJSONObject(
329 request, portletHTML, portlet, jsonObject);
330
331 response.setContentType(ContentTypes.APPLICATION_JSON);
332
333 ServletResponseUtil.write(response, jsonObject.toString());
334 }
335 else {
336 renderPortletAction.execute(
337 actionMapping, actionForm, dynamicRequest, response);
338 }
339 }
340
341 protected void storeAddContentPortletPreferences(
342 HttpServletRequest request, Layout layout, String portletId,
343 ThemeDisplay themeDisplay)
344 throws Exception {
345
346
347
348
349 PortletPreferences portletSetup =
350 PortletPreferencesFactoryUtil.getLayoutPortletSetup(
351 layout, portletId);
352
353 String[] portletData = StringUtil.split(
354 ParamUtil.getString(request, "portletData"));
355
356 if (portletData.length == 0) {
357 return;
358 }
359
360 long classPK = GetterUtil.getLong(portletData[0]);
361
362 String className = GetterUtil.getString(portletData[1]);
363
364 if ((classPK <= 0) || Validator.isNull(className)) {
365 return;
366 }
367
368 AddPortletProvider addPortletProvider = _serviceTrackerMap.getService(
369 className);
370
371 if (addPortletProvider == null) {
372 addPortletProvider = _serviceTrackerMap.getService(
373 AssetEntry.class.getName());
374 }
375
376 if (addPortletProvider != null) {
377 addPortletProvider.updatePortletPreferences(
378 portletSetup, portletId, className, classPK, themeDisplay);
379 }
380
381 portletSetup.store();
382 }
383
384 private static final ServiceTrackerMap<String, AddPortletProvider>
385 _serviceTrackerMap = ServiceTrackerCollections.openSingleValueMap(
386 AddPortletProvider.class, "model.class.name");
387
388 }