001    /**
002     * Copyright (c) 2000-2012 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.portlet.layoutsadmin.action;
016    
017    import com.liferay.portal.events.EventsProcessorUtil;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.PropsKeys;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.model.Layout;
028    import com.liferay.portal.model.LayoutConstants;
029    import com.liferay.portal.model.LayoutPrototype;
030    import com.liferay.portal.security.permission.ActionKeys;
031    import com.liferay.portal.service.LayoutLocalServiceUtil;
032    import com.liferay.portal.service.LayoutPrototypeServiceUtil;
033    import com.liferay.portal.service.LayoutServiceUtil;
034    import com.liferay.portal.service.ServiceContext;
035    import com.liferay.portal.service.ServiceContextFactory;
036    import com.liferay.portal.service.permission.LayoutPermissionUtil;
037    import com.liferay.portal.struts.JSONAction;
038    import com.liferay.portal.theme.ThemeDisplay;
039    import com.liferay.portal.util.LayoutSettings;
040    import com.liferay.portal.util.PortalUtil;
041    import com.liferay.portal.util.WebKeys;
042    import com.liferay.portlet.sites.util.SitesUtil;
043    
044    import javax.servlet.http.HttpServletRequest;
045    import javax.servlet.http.HttpServletResponse;
046    
047    import org.apache.struts.action.ActionForm;
048    import org.apache.struts.action.ActionMapping;
049    
050    /**
051     * @author Ming-Gih Lam
052     * @author Hugo Huijser
053     */
054    public class UpdateLayoutAction extends JSONAction {
055    
056            @Override
057            public String getJSON(
058                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
059                            HttpServletResponse response)
060                    throws Exception {
061    
062                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
063                            WebKeys.THEME_DISPLAY);
064    
065                    String cmd = ParamUtil.getString(request, Constants.CMD);
066    
067                    JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
068    
069                    if (cmd.equals("add")) {
070                            String[] array = addPage(themeDisplay, request, response);
071    
072                            jsonObj.put("deletable", Boolean.valueOf(array[2]));
073                            jsonObj.put("layoutId", array[0]);
074                            jsonObj.put("updateable", Boolean.valueOf(array[3]));
075                            jsonObj.put("url", array[1]);
076                    }
077                    else if (cmd.equals("delete")) {
078                            SitesUtil.deleteLayout(request, response);
079                    }
080                    else if (cmd.equals("display_order")) {
081                            updateDisplayOrder(request);
082                    }
083                    else if (cmd.equals("name")) {
084                            updateName(request);
085                    }
086                    else if (cmd.equals("parent_layout_id")) {
087                            updateParentLayoutId(request);
088                    }
089                    else if (cmd.equals("priority")) {
090                            updatePriority(request);
091                    }
092    
093                    return jsonObj.toString();
094            }
095    
096            protected String[] addPage(
097                            ThemeDisplay themeDisplay, HttpServletRequest request,
098                            HttpServletResponse response)
099                    throws Exception {
100    
101                    String doAsUserId = ParamUtil.getString(request, "doAsUserId");
102                    String doAsUserLanguageId = ParamUtil.getString(
103                            request, "doAsUserLanguageId");
104    
105                    long groupId = ParamUtil.getLong(request, "groupId");
106                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
107                    long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
108                    String name = ParamUtil.getString(request, "name", "New Page");
109                    String title = StringPool.BLANK;
110                    String description = StringPool.BLANK;
111                    String type = LayoutConstants.TYPE_PORTLET;
112                    boolean hidden = false;
113                    String friendlyURL = StringPool.BLANK;
114                    long layoutPrototypeId = ParamUtil.getLong(
115                            request, "layoutPrototypeId");
116    
117                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
118                            request);
119    
120                    Layout layout = null;
121    
122                    if (layoutPrototypeId > 0) {
123                            LayoutPrototype layoutPrototype =
124                                    LayoutPrototypeServiceUtil.getLayoutPrototype(
125                                            layoutPrototypeId);
126    
127                            serviceContext.setAttribute(
128                                    "layoutPrototypeUuid", layoutPrototype.getUuid());
129    
130                            layout = LayoutServiceUtil.addLayout(
131                                    groupId, privateLayout, parentLayoutId, name, title,
132                                    description, LayoutConstants.TYPE_PORTLET, false, friendlyURL,
133                                    serviceContext);
134                    }
135                    else {
136                            layout = LayoutServiceUtil.addLayout(
137                                    groupId, privateLayout, parentLayoutId, name, title,
138                                    description, type, hidden, friendlyURL, serviceContext);
139                    }
140    
141                    LayoutSettings layoutSettings = LayoutSettings.getInstance(layout);
142    
143                    EventsProcessorUtil.process(
144                            PropsKeys.LAYOUT_CONFIGURATION_ACTION_UPDATE,
145                            layoutSettings.getConfigurationActionUpdate(), request, response);
146    
147                    String layoutURL = PortalUtil.getLayoutURL(layout, themeDisplay);
148    
149                    if (Validator.isNotNull(doAsUserId)) {
150                            layoutURL = HttpUtil.addParameter(
151                                    layoutURL, "doAsUserId", themeDisplay.getDoAsUserId());
152                    }
153    
154                    if (Validator.isNotNull(doAsUserLanguageId)) {
155                            layoutURL = HttpUtil.addParameter(
156                                    layoutURL, "doAsUserLanguageId",
157                                    themeDisplay.getDoAsUserLanguageId());
158                    }
159    
160                    boolean updateable = SitesUtil.isLayoutUpdateable(layout);
161                    boolean deleteable =
162                            updateable &&
163                            LayoutPermissionUtil.contains(
164                                    themeDisplay.getPermissionChecker(), layout, ActionKeys.DELETE);
165    
166                    return new String[] {
167                            String.valueOf(layout.getLayoutId()), layoutURL,
168                            String.valueOf(deleteable), String.valueOf(updateable)
169                    };
170            }
171    
172            protected void updateDisplayOrder(HttpServletRequest request)
173                    throws Exception {
174    
175                    long groupId = ParamUtil.getLong(request, "groupId");
176                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
177                    long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
178                    long[] layoutIds = StringUtil.split(
179                            ParamUtil.getString(request, "layoutIds"), 0L);
180    
181                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
182                            request);
183    
184                    LayoutServiceUtil.setLayouts(
185                            groupId, privateLayout, parentLayoutId, layoutIds, serviceContext);
186            }
187    
188            protected void updateName(HttpServletRequest request) throws Exception {
189                    long plid = ParamUtil.getLong(request, "plid");
190    
191                    long groupId = ParamUtil.getLong(request, "groupId");
192                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
193                    long layoutId = ParamUtil.getLong(request, "layoutId");
194                    String name = ParamUtil.getString(request, "name");
195                    String languageId = ParamUtil.getString(request, "languageId");
196    
197                    LayoutLocalServiceUtil.updateScopedPortletNames(
198                            groupId, privateLayout, layoutId, name, languageId);
199    
200                    if (plid <= 0) {
201                            LayoutServiceUtil.updateName(
202                                    groupId, privateLayout, layoutId, name, languageId);
203                    }
204                    else {
205                            LayoutServiceUtil.updateName(plid, name, languageId);
206                    }
207            }
208    
209            protected void updateParentLayoutId(HttpServletRequest request)
210                    throws Exception {
211    
212                    long plid = ParamUtil.getLong(request, "plid");
213    
214                    long parentPlid = ParamUtil.getLong(request, "parentPlid");
215    
216                    long groupId = ParamUtil.getLong(request, "groupId");
217                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
218                    long layoutId = ParamUtil.getLong(request, "layoutId");
219                    long parentLayoutId = ParamUtil.getLong(
220                            request, "parentLayoutId",
221                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
222    
223                    if (plid <= 0) {
224                            LayoutServiceUtil.updateParentLayoutId(
225                                    groupId, privateLayout, layoutId, parentLayoutId);
226                    }
227                    else {
228                            LayoutServiceUtil.updateParentLayoutId(plid, parentPlid);
229                    }
230    
231                    updatePriority(request);
232            }
233    
234            protected void updatePriority(HttpServletRequest request) throws Exception {
235                    long plid = ParamUtil.getLong(request, "plid");
236    
237                    long groupId = ParamUtil.getLong(request, "groupId");
238                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
239                    long layoutId = ParamUtil.getLong(request, "layoutId");
240                    int priority = ParamUtil.getInteger(request, "priority");
241    
242                    if (plid <= 0) {
243                            LayoutServiceUtil.updatePriority(
244                                    groupId, privateLayout, layoutId, priority);
245                    }
246                    else {
247                            LayoutServiceUtil.updatePriority(plid, priority);
248                    }
249            }
250    
251    }