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