001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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.PortletJSONUtil;
020    import com.liferay.portal.kernel.servlet.BufferCacheServletResponse;
021    import com.liferay.portal.kernel.servlet.DynamicServletRequest;
022    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
023    import com.liferay.portal.kernel.staging.StagingUtil;
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.workflow.WorkflowConstants;
036    import com.liferay.portal.model.Layout;
037    import com.liferay.portal.model.LayoutRevision;
038    import com.liferay.portal.model.LayoutTypePortlet;
039    import com.liferay.portal.model.Portlet;
040    import com.liferay.portal.security.permission.ActionKeys;
041    import com.liferay.portal.security.permission.PermissionChecker;
042    import com.liferay.portal.service.LayoutRevisionLocalServiceUtil;
043    import com.liferay.portal.service.LayoutServiceUtil;
044    import com.liferay.portal.service.PortletLocalServiceUtil;
045    import com.liferay.portal.service.ServiceContext;
046    import com.liferay.portal.service.ServiceContextFactory;
047    import com.liferay.portal.service.permission.LayoutPermissionUtil;
048    import com.liferay.portal.servlet.NamespaceServletRequest;
049    import com.liferay.portal.struts.JSONAction;
050    import com.liferay.portal.theme.ThemeDisplay;
051    import com.liferay.portal.util.LayoutClone;
052    import com.liferay.portal.util.LayoutCloneFactory;
053    import com.liferay.portal.util.PortalUtil;
054    import com.liferay.portal.util.WebKeys;
055    import com.liferay.portlet.PortletPreferencesFactoryUtil;
056    import com.liferay.portlet.asset.AssetRendererFactoryRegistryUtil;
057    import com.liferay.portlet.asset.model.AssetRenderer;
058    import com.liferay.portlet.asset.model.AssetRendererFactory;
059    
060    import javax.portlet.PortletPreferences;
061    
062    import javax.servlet.http.HttpServletRequest;
063    import javax.servlet.http.HttpServletResponse;
064    
065    import org.apache.struts.action.Action;
066    import org.apache.struts.action.ActionForm;
067    import org.apache.struts.action.ActionMapping;
068    
069    /**
070     * @author Brian Wing Shun Chan
071     */
072    public class UpdateLayoutAction extends JSONAction {
073    
074            @Override
075            public String getJSON(
076                            ActionMapping actionMapping, ActionForm actionForm,
077                            HttpServletRequest request, HttpServletResponse response)
078                    throws Exception {
079    
080                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
081                            WebKeys.THEME_DISPLAY);
082    
083                    long userId = themeDisplay.getUserId();
084    
085                    Layout layout = themeDisplay.getLayout();
086                    LayoutTypePortlet layoutTypePortlet =
087                            themeDisplay.getLayoutTypePortlet();
088    
089                    PermissionChecker permissionChecker =
090                            themeDisplay.getPermissionChecker();
091    
092                    String cmd = ParamUtil.getString(request, Constants.CMD);
093    
094                    String portletId = ParamUtil.getString(request, "p_p_id");
095    
096                    boolean updateLayout = true;
097    
098                    if (cmd.equals(Constants.ADD)) {
099                            String columnId = ParamUtil.getString(request, "p_p_col_id", null);
100                            int columnPos = ParamUtil.getInteger(request, "p_p_col_pos", -1);
101    
102                            portletId = layoutTypePortlet.addPortletId(
103                                    userId, portletId, columnId, columnPos);
104    
105                            storeAddContentPortletPreferences(
106                                    request, layout, portletId, themeDisplay);
107    
108                            if (layoutTypePortlet.isCustomizable() &&
109                                    layoutTypePortlet.isCustomizedView() &&
110                                    !layoutTypePortlet.isColumnDisabled(columnId)) {
111    
112                                    updateLayout = false;
113                            }
114                    }
115                    else if (cmd.equals(Constants.DELETE)) {
116                            if (layoutTypePortlet.hasPortletId(portletId)) {
117                                    layoutTypePortlet.removePortletId(userId, portletId);
118    
119                                    if (layoutTypePortlet.isCustomizable() &&
120                                            layoutTypePortlet.isCustomizedView()) {
121    
122                                            updateLayout = false;
123                                    }
124                            }
125                    }
126                    else if (cmd.equals("drag")) {
127                            if (LayoutPermissionUtil.contains(
128                                            permissionChecker, layout, ActionKeys.UPDATE)) {
129    
130                                    String height = ParamUtil.getString(request, "height");
131                                    String width = ParamUtil.getString(request, "width");
132                                    String top = ParamUtil.getString(request, "top");
133                                    String left = ParamUtil.getString(request, "left");
134    
135                                    PortletPreferences portletPreferences =
136                                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
137                                                    layout, portletId);
138    
139                                    StringBundler sb = new StringBundler(12);
140    
141                                    sb.append("height=");
142                                    sb.append(height);
143                                    sb.append("\n");
144                                    sb.append("width=");
145                                    sb.append(width);
146                                    sb.append("\n");
147                                    sb.append("top=");
148                                    sb.append(top);
149                                    sb.append("\n");
150                                    sb.append("left=");
151                                    sb.append(left);
152                                    sb.append("\n");
153    
154                                    portletPreferences.setValue(
155                                            "portlet-freeform-styles", sb.toString());
156    
157                                    portletPreferences.store();
158                            }
159                    }
160                    else if (cmd.equals("minimize")) {
161                            boolean restore = ParamUtil.getBoolean(request, "p_p_restore");
162    
163                            if (restore) {
164                                    layoutTypePortlet.removeStateMinPortletId(portletId);
165                            }
166                            else {
167                                    layoutTypePortlet.addStateMinPortletId(portletId);
168                            }
169    
170                            updateLayout = false;
171                    }
172                    else if (cmd.equals("move")) {
173                            String columnId = ParamUtil.getString(request, "p_p_col_id");
174                            int columnPos = ParamUtil.getInteger(request, "p_p_col_pos");
175    
176                            layoutTypePortlet.movePortletId(
177                                    userId, portletId, columnId, columnPos);
178    
179                            if (layoutTypePortlet.isCustomizable() &&
180                                    layoutTypePortlet.isCustomizedView() &&
181                                    !layoutTypePortlet.isColumnDisabled(columnId)) {
182    
183                                    updateLayout = false;
184                            }
185                    }
186                    else if (cmd.equals("redo_layout_revision")) {
187                            long layoutRevisionId = ParamUtil.getLong(
188                                    request, "layoutRevisionId");
189                            long layoutSetBranchId = ParamUtil.getLong(
190                                    request, "layoutSetBranchId");
191    
192                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
193                                    request);
194    
195                            LayoutRevisionLocalServiceUtil.updateStatus(
196                                    userId, layoutRevisionId, WorkflowConstants.STATUS_DRAFT,
197                                    serviceContext);
198    
199                            StagingUtil.setRecentLayoutRevisionId(
200                                    request, layoutSetBranchId, layout.getPlid(), layoutRevisionId);
201    
202                            updateLayout = false;
203                    }
204                    else if (cmd.equals("select_layout_revision")) {
205                            long layoutRevisionId = ParamUtil.getLong(
206                                    request, "layoutRevisionId");
207                            long layoutSetBranchId = ParamUtil.getLong(
208                                    request, "layoutSetBranchId");
209    
210                            StagingUtil.setRecentLayoutRevisionId(
211                                    request, layoutSetBranchId, layout.getPlid(), layoutRevisionId);
212    
213                            updateLayout = false;
214                    }
215                    else if (cmd.equals("toggle_customized_view")) {
216                            updateLayout = false;
217                    }
218                    else if (cmd.equals("update_type_settings")) {
219                            UnicodeProperties layoutTypeSettingsProperties =
220                                    layout.getTypeSettingsProperties();
221    
222                            UnicodeProperties formTypeSettingsProperties =
223                                    PropertiesParamUtil.getProperties(
224                                            request, "TypeSettingsProperties--");
225    
226                            layoutTypeSettingsProperties.putAll(formTypeSettingsProperties);
227                    }
228                    else if (cmd.equals("undo_layout_revision")) {
229                            long layoutRevisionId = ParamUtil.getLong(
230                                    request, "layoutRevisionId");
231                            long layoutSetBranchId = ParamUtil.getLong(
232                                    request, "layoutSetBranchId");
233    
234                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
235                                    request);
236    
237                            LayoutRevision layoutRevision =
238                                    LayoutRevisionLocalServiceUtil.updateStatus(
239                                            userId, layoutRevisionId, WorkflowConstants.STATUS_INACTIVE,
240                                            serviceContext);
241    
242                            StagingUtil.setRecentLayoutRevisionId(
243                                    request, layoutSetBranchId, layout.getPlid(),
244                                    layoutRevision.getParentLayoutRevisionId());
245    
246                            updateLayout = false;
247                    }
248    
249                    if (updateLayout) {
250    
251                            // LEP-3648
252    
253                            layoutTypePortlet.resetModes();
254                            layoutTypePortlet.resetStates();
255    
256                            layout = LayoutServiceUtil.updateLayout(
257                                    layout.getGroupId(), layout.isPrivateLayout(),
258                                    layout.getLayoutId(), layout.getTypeSettings());
259                    }
260                    else {
261                            LayoutClone layoutClone = LayoutCloneFactory.getInstance();
262    
263                            if (layoutClone != null) {
264                                    layoutClone.update(
265                                            request, layout.getPlid(), layout.getTypeSettings());
266                            }
267                    }
268    
269                    if (cmd.equals(Constants.ADD) && (portletId != null)) {
270                            addPortlet(actionMapping, actionForm, request, response, portletId);
271                    }
272    
273                    return StringPool.BLANK;
274            }
275    
276            protected void addPortlet(
277                            ActionMapping actionMapping, ActionForm actionForm,
278                            HttpServletRequest request, HttpServletResponse response,
279                            String portletId)
280                    throws Exception {
281    
282                    // Run the render portlet action to add a portlet without refreshing.
283    
284                    Action renderPortletAction = (Action)InstancePool.get(
285                            RenderPortletAction.class.getName());
286    
287                    // Pass in the portlet id because the portlet id may be the instance id.
288                    // Namespace the request if necessary. See LEP-4644.
289    
290                    long companyId = PortalUtil.getCompanyId(request);
291    
292                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
293                            companyId, portletId);
294    
295                    DynamicServletRequest dynamicRequest = null;
296    
297                    if (portlet.isPrivateRequestAttributes()) {
298                            String portletNamespace = PortalUtil.getPortletNamespace(
299                                    portlet.getPortletId());
300    
301                            dynamicRequest = new NamespaceServletRequest(
302                                    request, portletNamespace, portletNamespace);
303                    }
304                    else {
305                            dynamicRequest = new DynamicServletRequest(request);
306                    }
307    
308                    dynamicRequest.setParameter("p_p_id", portletId);
309    
310                    String dataType = ParamUtil.getString(request, "dataType");
311    
312                    if (dataType.equals("json")) {
313                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
314    
315                            BufferCacheServletResponse bufferCacheServletResponse =
316                                    new BufferCacheServletResponse(response);
317    
318                            renderPortletAction.execute(
319                                    actionMapping, actionForm, dynamicRequest,
320                                    bufferCacheServletResponse);
321    
322                            String portletHTML = bufferCacheServletResponse.getString();
323    
324                            portletHTML = portletHTML.trim();
325    
326                            PortletJSONUtil.populatePortletJSONObject(
327                                    request, portletHTML, portlet, jsonObject);
328    
329                            response.setContentType(ContentTypes.APPLICATION_JSON);
330    
331                            ServletResponseUtil.write(response, jsonObject.toString());
332                    }
333                    else {
334                            renderPortletAction.execute(
335                                    actionMapping, actionForm, dynamicRequest, response);
336                    }
337            }
338    
339            protected void storeAddContentPortletPreferences(
340                            HttpServletRequest request, Layout layout, String portletId,
341                            ThemeDisplay themeDisplay)
342                    throws Exception {
343    
344                    // We need to get the portlet setup before doing anything else to ensure
345                    // that it is created in the database
346    
347                    PortletPreferences portletSetup =
348                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
349                                    layout, portletId);
350    
351                    String[] portletData = StringUtil.split(
352                            ParamUtil.getString(request, "portletData"));
353    
354                    if (portletData.length == 0) {
355                            return;
356                    }
357    
358                    long classPK = GetterUtil.getLong(portletData[0]);
359    
360                    String className = GetterUtil.getString(portletData[1]);
361    
362                    if ((classPK <= 0) || Validator.isNull(className)) {
363                            return;
364                    }
365    
366                    AssetRendererFactory assetRendererFactory =
367                            AssetRendererFactoryRegistryUtil.getAssetRendererFactoryByClassName(
368                                    className);
369    
370                    AssetRenderer assetRenderer = assetRendererFactory.getAssetRenderer(
371                            classPK);
372    
373                    assetRenderer.setAddToPagePreferences(
374                            portletSetup, portletId, themeDisplay);
375    
376                    portletSetup.store();
377            }
378    
379    }