001    /**
002     * Copyright (c) 2000-present 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.asset.kernel.model.AssetEntry;
018    import com.liferay.exportimport.kernel.staging.StagingUtil;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.model.Layout;
022    import com.liferay.portal.kernel.model.LayoutRevision;
023    import com.liferay.portal.kernel.model.LayoutTypePortlet;
024    import com.liferay.portal.kernel.model.Portlet;
025    import com.liferay.portal.kernel.portlet.AddPortletProvider;
026    import com.liferay.portal.kernel.portlet.PortletJSONUtil;
027    import com.liferay.portal.kernel.portlet.PortletPreferencesFactoryUtil;
028    import com.liferay.portal.kernel.security.permission.ActionKeys;
029    import com.liferay.portal.kernel.security.permission.PermissionChecker;
030    import com.liferay.portal.kernel.service.LayoutRevisionLocalServiceUtil;
031    import com.liferay.portal.kernel.service.LayoutServiceUtil;
032    import com.liferay.portal.kernel.service.PortletLocalServiceUtil;
033    import com.liferay.portal.kernel.service.ServiceContext;
034    import com.liferay.portal.kernel.service.ServiceContextFactory;
035    import com.liferay.portal.kernel.service.permission.LayoutPermissionUtil;
036    import com.liferay.portal.kernel.servlet.BufferCacheServletResponse;
037    import com.liferay.portal.kernel.servlet.DynamicServletRequest;
038    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
039    import com.liferay.portal.kernel.theme.ThemeDisplay;
040    import com.liferay.portal.kernel.util.Constants;
041    import com.liferay.portal.kernel.util.ContentTypes;
042    import com.liferay.portal.kernel.util.GetterUtil;
043    import com.liferay.portal.kernel.util.InstancePool;
044    import com.liferay.portal.kernel.util.ParamUtil;
045    import com.liferay.portal.kernel.util.PortalUtil;
046    import com.liferay.portal.kernel.util.PropertiesParamUtil;
047    import com.liferay.portal.kernel.util.StringBundler;
048    import com.liferay.portal.kernel.util.StringPool;
049    import com.liferay.portal.kernel.util.StringUtil;
050    import com.liferay.portal.kernel.util.UnicodeProperties;
051    import com.liferay.portal.kernel.util.Validator;
052    import com.liferay.portal.kernel.util.WebKeys;
053    import com.liferay.portal.kernel.workflow.WorkflowConstants;
054    import com.liferay.portal.servlet.NamespaceServletRequest;
055    import com.liferay.portal.struts.JSONAction;
056    import com.liferay.portal.util.LayoutClone;
057    import com.liferay.portal.util.LayoutCloneFactory;
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    /**
071     * @author Brian Wing Shun Chan
072     */
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("update_type_settings")) {
217                            UnicodeProperties layoutTypeSettingsProperties =
218                                    layout.getTypeSettingsProperties();
219    
220                            UnicodeProperties formTypeSettingsProperties =
221                                    PropertiesParamUtil.getProperties(
222                                            request, "TypeSettingsProperties--");
223    
224                            layoutTypeSettingsProperties.putAll(formTypeSettingsProperties);
225                    }
226                    else if (cmd.equals("undo_layout_revision")) {
227                            long layoutRevisionId = ParamUtil.getLong(
228                                    request, "layoutRevisionId");
229                            long layoutSetBranchId = ParamUtil.getLong(
230                                    request, "layoutSetBranchId");
231    
232                            ServiceContext serviceContext = ServiceContextFactory.getInstance(
233                                    request);
234    
235                            LayoutRevision layoutRevision =
236                                    LayoutRevisionLocalServiceUtil.updateStatus(
237                                            userId, layoutRevisionId, WorkflowConstants.STATUS_INACTIVE,
238                                            serviceContext);
239    
240                            StagingUtil.setRecentLayoutRevisionId(
241                                    request, layoutSetBranchId, layout.getPlid(),
242                                    layoutRevision.getParentLayoutRevisionId());
243    
244                            updateLayout = false;
245                    }
246    
247                    if (updateLayout) {
248    
249                            // LEP-3648
250    
251                            layoutTypePortlet.resetModes();
252                            layoutTypePortlet.resetStates();
253    
254                            layout = LayoutServiceUtil.updateLayout(
255                                    layout.getGroupId(), layout.isPrivateLayout(),
256                                    layout.getLayoutId(), layout.getTypeSettings());
257                    }
258                    else {
259                            LayoutClone layoutClone = LayoutCloneFactory.getInstance();
260    
261                            if (layoutClone != null) {
262                                    layoutClone.update(
263                                            request, layout.getPlid(), layout.getTypeSettings());
264                            }
265                    }
266    
267                    if (cmd.equals(Constants.ADD) && (portletId != null)) {
268                            addPortlet(actionMapping, actionForm, request, response, portletId);
269                    }
270    
271                    return StringPool.BLANK;
272            }
273    
274            protected void addPortlet(
275                            ActionMapping actionMapping, ActionForm actionForm,
276                            HttpServletRequest request, HttpServletResponse response,
277                            String portletId)
278                    throws Exception {
279    
280                    // Run the render portlet action to add a portlet without refreshing.
281    
282                    Action renderPortletAction = (Action)InstancePool.get(
283                            RenderPortletAction.class.getName());
284    
285                    // Pass in the portlet id because the portlet id may be the instance id.
286                    // Namespace the request if necessary. See LEP-4644.
287    
288                    long companyId = PortalUtil.getCompanyId(request);
289    
290                    Portlet portlet = PortletLocalServiceUtil.getPortletById(
291                            companyId, portletId);
292    
293                    DynamicServletRequest dynamicRequest = null;
294    
295                    if (portlet.isPrivateRequestAttributes()) {
296                            String portletNamespace = PortalUtil.getPortletNamespace(
297                                    portlet.getPortletId());
298    
299                            dynamicRequest = new NamespaceServletRequest(
300                                    request, portletNamespace, portletNamespace);
301                    }
302                    else {
303                            dynamicRequest = new DynamicServletRequest(request);
304                    }
305    
306                    dynamicRequest.setParameter("p_p_id", portletId);
307    
308                    String dataType = StringUtil.toLowerCase(
309                            ParamUtil.getString(request, "dataType"));
310    
311                    if (dataType.equals("json")) {
312                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
313    
314                            BufferCacheServletResponse bufferCacheServletResponse =
315                                    new BufferCacheServletResponse(response);
316    
317                            renderPortletAction.execute(
318                                    actionMapping, actionForm, dynamicRequest,
319                                    bufferCacheServletResponse);
320    
321                            String portletHTML = bufferCacheServletResponse.getString();
322    
323                            portletHTML = portletHTML.trim();
324    
325                            PortletJSONUtil.populatePortletJSONObject(
326                                    request, portletHTML, portlet, jsonObject);
327    
328                            response.setContentType(ContentTypes.APPLICATION_JSON);
329    
330                            ServletResponseUtil.write(response, jsonObject.toString());
331                    }
332                    else {
333                            renderPortletAction.execute(
334                                    actionMapping, actionForm, dynamicRequest, response);
335                    }
336            }
337    
338            protected void storeAddContentPortletPreferences(
339                            HttpServletRequest request, Layout layout, String portletId,
340                            ThemeDisplay themeDisplay)
341                    throws Exception {
342    
343                    // We need to get the portlet setup before doing anything else to ensure
344                    // that it is created in the database
345    
346                    PortletPreferences portletSetup =
347                            PortletPreferencesFactoryUtil.getLayoutPortletSetup(
348                                    layout, portletId);
349    
350                    String[] portletData = StringUtil.split(
351                            ParamUtil.getString(request, "portletData"));
352    
353                    if (portletData.length == 0) {
354                            return;
355                    }
356    
357                    long classPK = GetterUtil.getLong(portletData[0]);
358    
359                    String className = GetterUtil.getString(portletData[1]);
360    
361                    if ((classPK <= 0) || Validator.isNull(className)) {
362                            return;
363                    }
364    
365                    AddPortletProvider addPortletProvider = _serviceTrackerMap.getService(
366                            className);
367    
368                    if (addPortletProvider == null) {
369                            addPortletProvider = _serviceTrackerMap.getService(
370                                    AssetEntry.class.getName());
371                    }
372    
373                    if (addPortletProvider != null) {
374                            addPortletProvider.updatePortletPreferences(
375                                    portletSetup, portletId, className, classPK, themeDisplay);
376                    }
377    
378                    portletSetup.store();
379            }
380    
381            private static final ServiceTrackerMap<String, AddPortletProvider>
382                    _serviceTrackerMap = ServiceTrackerCollections.openSingleValueMap(
383                            AddPortletProvider.class, "model.class.name");
384    
385    }