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(
160                                    "layoutPrototypeUuid", layoutPrototype.getUuid());
161    
162                            layout = LayoutServiceUtil.addLayout(
163                                    groupId, privateLayout, parentLayoutId, name, title,
164                                    description, LayoutConstants.TYPE_PORTLET, false, friendlyURL,
165                                    serviceContext);
166                    }
167                    else {
168                            layout = LayoutServiceUtil.addLayout(
169                                    groupId, privateLayout, parentLayoutId, name, title,
170                                    description, type, hidden, friendlyURL, serviceContext);
171                    }
172    
173                    LayoutSettings layoutSettings = LayoutSettings.getInstance(layout);
174    
175                    EventsProcessorUtil.process(
176                            PropsKeys.LAYOUT_CONFIGURATION_ACTION_UPDATE,
177                            layoutSettings.getConfigurationActionUpdate(), request, response);
178    
179                    String layoutURL = PortalUtil.getLayoutURL(layout, themeDisplay);
180    
181                    if (Validator.isNotNull(doAsUserId)) {
182                            layoutURL = HttpUtil.addParameter(
183                                    layoutURL, "doAsUserId", themeDisplay.getDoAsUserId());
184                    }
185    
186                    if (Validator.isNotNull(doAsUserLanguageId)) {
187                            layoutURL = HttpUtil.addParameter(
188                                    layoutURL, "doAsUserLanguageId",
189                                    themeDisplay.getDoAsUserLanguageId());
190                    }
191    
192                    boolean updateable = SitesUtil.isLayoutUpdateable(layout);
193                    boolean deleteable =
194                            updateable &&
195                            LayoutPermissionUtil.contains(
196                                    themeDisplay.getPermissionChecker(), layout, ActionKeys.DELETE);
197    
198                    return new String[] {
199                            String.valueOf(layout.getLayoutId()), layoutURL,
200                            String.valueOf(deleteable), String.valueOf(updateable)
201                    };
202            }
203    
204            protected void updateDisplayOrder(HttpServletRequest request)
205                    throws Exception {
206    
207                    long groupId = ParamUtil.getLong(request, "groupId");
208                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
209                    long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
210                    long[] layoutIds = StringUtil.split(
211                            ParamUtil.getString(request, "layoutIds"), 0L);
212    
213                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
214                            request);
215    
216                    LayoutServiceUtil.setLayouts(
217                            groupId, privateLayout, parentLayoutId, layoutIds, serviceContext);
218            }
219    
220            protected void updateName(HttpServletRequest request) throws Exception {
221                    long plid = ParamUtil.getLong(request, "plid");
222    
223                    long groupId = ParamUtil.getLong(request, "groupId");
224                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
225                    long layoutId = ParamUtil.getLong(request, "layoutId");
226                    String name = ParamUtil.getString(request, "name");
227                    String languageId = ParamUtil.getString(request, "languageId");
228    
229                    LayoutLocalServiceUtil.updateScopedPortletNames(
230                            groupId, privateLayout, layoutId, name, languageId);
231    
232                    if (plid <= 0) {
233                            LayoutServiceUtil.updateName(
234                                    groupId, privateLayout, layoutId, name, languageId);
235                    }
236                    else {
237                            LayoutServiceUtil.updateName(plid, name, languageId);
238                    }
239            }
240    
241            protected void updateParentLayoutId(HttpServletRequest request)
242                    throws Exception {
243    
244                    long plid = ParamUtil.getLong(request, "plid");
245    
246                    long parentPlid = ParamUtil.getLong(request, "parentPlid");
247    
248                    long groupId = ParamUtil.getLong(request, "groupId");
249                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
250                    long layoutId = ParamUtil.getLong(request, "layoutId");
251                    long parentLayoutId = ParamUtil.getLong(
252                            request, "parentLayoutId",
253                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
254    
255                    if (plid <= 0) {
256                            LayoutServiceUtil.updateParentLayoutId(
257                                    groupId, privateLayout, layoutId, parentLayoutId);
258                    }
259                    else {
260                            LayoutServiceUtil.updateParentLayoutId(plid, parentPlid);
261                    }
262    
263                    updatePriority(request);
264            }
265    
266            protected void updatePriority(HttpServletRequest request) throws Exception {
267                    long plid = ParamUtil.getLong(request, "plid");
268    
269                    long groupId = ParamUtil.getLong(request, "groupId");
270                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
271                    long layoutId = ParamUtil.getLong(request, "layoutId");
272                    int priority = ParamUtil.getInteger(request, "priority");
273    
274                    if (plid <= 0) {
275                            LayoutServiceUtil.updatePriority(
276                                    groupId, privateLayout, layoutId, priority);
277                    }
278                    else {
279                            LayoutServiceUtil.updatePriority(plid, priority);
280                    }
281            }
282    
283    }