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.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.GroupPermissionUtil;
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                    String cmd = ParamUtil.getString(request, Constants.CMD);
067    
068                    JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
069    
070                    if (cmd.equals("add")) {
071                            String[] array = addPage(themeDisplay, request, response);
072    
073                            jsonObj.put("deletable", Boolean.valueOf(array[2]));
074                            jsonObj.put("layoutId", array[0]);
075                            jsonObj.put("sortable", Boolean.valueOf(array[3]));
076                            jsonObj.put("updateable", Boolean.valueOf(array[4]));
077                            jsonObj.put("url", array[1]);
078                    }
079                    else if (cmd.equals("delete")) {
080                            SitesUtil.deleteLayout(request, response);
081                    }
082                    else if (cmd.equals("display_order")) {
083                            updateDisplayOrder(request);
084                    }
085                    else if (cmd.equals("name")) {
086                            updateName(request);
087                    }
088                    else if (cmd.equals("parent_layout_id")) {
089                            updateParentLayoutId(request);
090                    }
091                    else if (cmd.equals("priority")) {
092                            updatePriority(request);
093                    }
094    
095                    return jsonObj.toString();
096            }
097    
098            protected String[] addPage(
099                            ThemeDisplay themeDisplay, HttpServletRequest request,
100                            HttpServletResponse response)
101                    throws Exception {
102    
103                    String doAsUserId = ParamUtil.getString(request, "doAsUserId");
104                    String doAsUserLanguageId = ParamUtil.getString(
105                            request, "doAsUserLanguageId");
106    
107                    long groupId = ParamUtil.getLong(request, "groupId");
108                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
109                    long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
110                    String name = ParamUtil.getString(request, "name", "New Page");
111                    String title = StringPool.BLANK;
112                    String description = StringPool.BLANK;
113                    String type = LayoutConstants.TYPE_PORTLET;
114                    boolean hidden = false;
115                    String friendlyURL = StringPool.BLANK;
116                    long layoutPrototypeId = ParamUtil.getLong(
117                            request, "layoutPrototypeId");
118    
119                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
120                            request);
121    
122                    Layout layout = null;
123    
124                    if (layoutPrototypeId > 0) {
125                            LayoutPrototype layoutPrototype =
126                                    LayoutPrototypeServiceUtil.getLayoutPrototype(
127                                            layoutPrototypeId);
128    
129                            serviceContext.setAttribute(
130                                    "layoutPrototypeUuid", layoutPrototype.getUuid());
131    
132                            layout = LayoutServiceUtil.addLayout(
133                                    groupId, privateLayout, parentLayoutId, name, title,
134                                    description, LayoutConstants.TYPE_PORTLET, false, friendlyURL,
135                                    serviceContext);
136                    }
137                    else {
138                            layout = LayoutServiceUtil.addLayout(
139                                    groupId, privateLayout, parentLayoutId, name, title,
140                                    description, type, hidden, friendlyURL, serviceContext);
141                    }
142    
143                    LayoutSettings layoutSettings = LayoutSettings.getInstance(layout);
144    
145                    EventsProcessorUtil.process(
146                            PropsKeys.LAYOUT_CONFIGURATION_ACTION_UPDATE,
147                            layoutSettings.getConfigurationActionUpdate(), request, response);
148    
149                    String layoutURL = PortalUtil.getLayoutURL(layout, themeDisplay);
150    
151                    if (Validator.isNotNull(doAsUserId)) {
152                            layoutURL = HttpUtil.addParameter(
153                                    layoutURL, "doAsUserId", themeDisplay.getDoAsUserId());
154                    }
155    
156                    if (Validator.isNotNull(doAsUserLanguageId)) {
157                            layoutURL = HttpUtil.addParameter(
158                                    layoutURL, "doAsUserLanguageId",
159                                    themeDisplay.getDoAsUserLanguageId());
160                    }
161    
162                    boolean deleteable = LayoutPermissionUtil.contains(
163                            themeDisplay.getPermissionChecker(), layout, ActionKeys.DELETE);
164                    boolean sortable =
165                            GroupPermissionUtil.contains(
166                                    themeDisplay.getPermissionChecker(), layout.getGroupId(),
167                                    ActionKeys.MANAGE_LAYOUTS) &&
168                            SitesUtil.isLayoutSortable(layout);
169                    boolean updateable = LayoutPermissionUtil.contains(
170                            themeDisplay.getPermissionChecker(), layout, ActionKeys.UPDATE);
171    
172                    return new String[] {
173                            String.valueOf(layout.getLayoutId()), layoutURL,
174                            String.valueOf(deleteable), String.valueOf(sortable),
175                            String.valueOf(updateable)
176                    };
177            }
178    
179            protected void updateDisplayOrder(HttpServletRequest request)
180                    throws Exception {
181    
182                    long groupId = ParamUtil.getLong(request, "groupId");
183                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
184                    long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
185                    long[] layoutIds = StringUtil.split(
186                            ParamUtil.getString(request, "layoutIds"), 0L);
187    
188                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
189                            request);
190    
191                    LayoutServiceUtil.setLayouts(
192                            groupId, privateLayout, parentLayoutId, layoutIds, serviceContext);
193            }
194    
195            protected void updateName(HttpServletRequest request) throws Exception {
196                    long plid = ParamUtil.getLong(request, "plid");
197    
198                    long groupId = ParamUtil.getLong(request, "groupId");
199                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
200                    long layoutId = ParamUtil.getLong(request, "layoutId");
201                    String name = ParamUtil.getString(request, "name");
202                    String languageId = ParamUtil.getString(request, "languageId");
203    
204                    LayoutLocalServiceUtil.updateScopedPortletNames(
205                            groupId, privateLayout, layoutId, name, languageId);
206    
207                    if (plid <= 0) {
208                            LayoutServiceUtil.updateName(
209                                    groupId, privateLayout, layoutId, name, languageId);
210                    }
211                    else {
212                            LayoutServiceUtil.updateName(plid, name, languageId);
213                    }
214            }
215    
216            protected void updateParentLayoutId(HttpServletRequest request)
217                    throws Exception {
218    
219                    long plid = ParamUtil.getLong(request, "plid");
220    
221                    long parentPlid = ParamUtil.getLong(request, "parentPlid");
222    
223                    long groupId = ParamUtil.getLong(request, "groupId");
224                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
225                    long layoutId = ParamUtil.getLong(request, "layoutId");
226                    long parentLayoutId = ParamUtil.getLong(
227                            request, "parentLayoutId",
228                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
229    
230                    if (plid <= 0) {
231                            LayoutServiceUtil.updateParentLayoutId(
232                                    groupId, privateLayout, layoutId, parentLayoutId);
233                    }
234                    else {
235                            LayoutServiceUtil.updateParentLayoutId(plid, parentPlid);
236                    }
237    
238                    updatePriority(request);
239            }
240    
241            protected void updatePriority(HttpServletRequest request) throws Exception {
242                    long plid = ParamUtil.getLong(request, "plid");
243    
244                    long groupId = ParamUtil.getLong(request, "groupId");
245                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
246                    long layoutId = ParamUtil.getLong(request, "layoutId");
247                    int priority = ParamUtil.getInteger(request, "priority");
248    
249                    if (plid <= 0) {
250                            LayoutServiceUtil.updatePriority(
251                                    groupId, privateLayout, layoutId, priority);
252                    }
253                    else {
254                            LayoutServiceUtil.updatePriority(plid, priority);
255                    }
256            }
257    
258    }