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