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.LayoutTypeException;
018    import com.liferay.portal.events.EventsProcessorUtil;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.util.Constants;
022    import com.liferay.portal.kernel.util.HttpUtil;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.kernel.util.PropsKeys;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.StringUtil;
027    import com.liferay.portal.kernel.util.Validator;
028    import com.liferay.portal.model.Layout;
029    import com.liferay.portal.model.LayoutConstants;
030    import com.liferay.portal.model.LayoutPrototype;
031    import com.liferay.portal.security.permission.ActionKeys;
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 actionMapping, ActionForm actionForm,
060                            HttpServletRequest request, 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 jsonObject = JSONFactoryUtil.createJSONObject();
069    
070                    try {
071                            if (cmd.equals("add")) {
072                                    String[] array = addPage(themeDisplay, request, response);
073    
074                                    jsonObject.put("deletable", Boolean.valueOf(array[2]));
075                                    jsonObject.put("layoutId", array[0]);
076                                    jsonObject.put("sortable", Boolean.valueOf(array[3]));
077                                    jsonObject.put("updateable", Boolean.valueOf(array[4]));
078                                    jsonObject.put("url", array[1]);
079                            }
080                            else if (cmd.equals("delete")) {
081                                    SitesUtil.deleteLayout(request, response);
082                            }
083                            else if (cmd.equals("display_order")) {
084                                    updateDisplayOrder(request);
085                            }
086                            else if (cmd.equals("name")) {
087                                    updateName(request);
088                            }
089                            else if (cmd.equals("parent_layout_id")) {
090                                    updateParentLayoutId(request);
091                            }
092                            else if (cmd.equals("priority")) {
093                                    updatePriority(request);
094                            }
095    
096                            jsonObject.put("status", HttpServletResponse.SC_OK);
097                    }
098                    catch (LayoutTypeException lte) {
099                            jsonObject.put(
100                                    "message",
101                                    getLayoutTypeExceptionMessage(themeDisplay, lte, cmd));
102                            jsonObject.put("status", HttpServletResponse.SC_BAD_REQUEST);
103                    }
104    
105                    return jsonObject.toString();
106            }
107    
108            protected String[] addPage(
109                            ThemeDisplay themeDisplay, HttpServletRequest request,
110                            HttpServletResponse response)
111                    throws Exception {
112    
113                    String doAsUserId = ParamUtil.getString(request, "doAsUserId");
114                    String doAsUserLanguageId = ParamUtil.getString(
115                            request, "doAsUserLanguageId");
116    
117                    long groupId = ParamUtil.getLong(request, "groupId");
118                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
119                    long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
120                    String name = ParamUtil.getString(request, "name", "New Page");
121                    String title = StringPool.BLANK;
122                    String description = StringPool.BLANK;
123                    String type = LayoutConstants.TYPE_PORTLET;
124                    boolean hidden = false;
125                    String friendlyURL = StringPool.BLANK;
126                    long layoutPrototypeId = ParamUtil.getLong(
127                            request, "layoutPrototypeId");
128    
129                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
130                            request);
131    
132                    Layout layout = null;
133    
134                    if (layoutPrototypeId > 0) {
135                            LayoutPrototype layoutPrototype =
136                                    LayoutPrototypeServiceUtil.getLayoutPrototype(
137                                            layoutPrototypeId);
138    
139                            serviceContext.setAttribute(
140                                    "layoutPrototypeUuid", layoutPrototype.getUuid());
141    
142                            layout = LayoutServiceUtil.addLayout(
143                                    groupId, privateLayout, parentLayoutId, name, title,
144                                    description, LayoutConstants.TYPE_PORTLET, false, friendlyURL,
145                                    serviceContext);
146                    }
147                    else {
148                            layout = LayoutServiceUtil.addLayout(
149                                    groupId, privateLayout, parentLayoutId, name, title,
150                                    description, type, hidden, friendlyURL, serviceContext);
151                    }
152    
153                    LayoutSettings layoutSettings = LayoutSettings.getInstance(layout);
154    
155                    EventsProcessorUtil.process(
156                            PropsKeys.LAYOUT_CONFIGURATION_ACTION_UPDATE,
157                            layoutSettings.getConfigurationActionUpdate(), request, response);
158    
159                    String layoutURL = PortalUtil.getLayoutURL(layout, themeDisplay);
160    
161                    if (Validator.isNotNull(doAsUserId)) {
162                            layoutURL = HttpUtil.addParameter(
163                                    layoutURL, "doAsUserId", themeDisplay.getDoAsUserId());
164                    }
165    
166                    if (Validator.isNotNull(doAsUserLanguageId)) {
167                            layoutURL = HttpUtil.addParameter(
168                                    layoutURL, "doAsUserLanguageId",
169                                    themeDisplay.getDoAsUserLanguageId());
170                    }
171    
172                    boolean deleteable = LayoutPermissionUtil.contains(
173                            themeDisplay.getPermissionChecker(), layout, ActionKeys.DELETE);
174                    boolean sortable =
175                            GroupPermissionUtil.contains(
176                                    themeDisplay.getPermissionChecker(), layout.getGroupId(),
177                                    ActionKeys.MANAGE_LAYOUTS) &&
178                            SitesUtil.isLayoutSortable(layout);
179                    boolean updateable = LayoutPermissionUtil.contains(
180                            themeDisplay.getPermissionChecker(), layout, ActionKeys.UPDATE);
181    
182                    return new String[] {
183                            String.valueOf(layout.getLayoutId()), layoutURL,
184                            String.valueOf(deleteable), String.valueOf(sortable),
185                            String.valueOf(updateable)
186                    };
187            }
188    
189            protected String getLayoutTypeExceptionMessage(
190                    ThemeDisplay themeDisplay, LayoutTypeException lte, String cmd) {
191    
192                    if (Validator.isNotNull(cmd)) {
193                            if (cmd.equals("delete") &&
194                                    (lte.getType() == LayoutTypeException.FIRST_LAYOUT)) {
195    
196                                    return themeDisplay.translate(
197                                            "you-cannot-delete-this-page-because-the-next-page-is-of-" +
198                                                    "type-x-and-so-cannot-be-the-first-page",
199                                            "layout.types." + lte.getLayoutType());
200                            }
201    
202                            if (cmd.equals("delete") &&
203                                    (lte.getType() ==
204                                            LayoutTypeException.FIRST_LAYOUT_PERMISSION)) {
205    
206                                    return themeDisplay.translate(
207                                            "you-cannot-delete-this-page-because-the-next-page-is-" +
208                                                    "not-vieweable-by-unathenticated-users-and-so-cannot-" +
209                                                            "be-the-first-page");
210                            }
211    
212                            if ((cmd.equals("display_order") || cmd.equals("priority")) &&
213                                    (lte.getType() == LayoutTypeException.FIRST_LAYOUT)) {
214    
215                                    return themeDisplay.translate(
216                                            "you-cannot-move-this-page-because-the-resulting-order-" +
217                                                    "would-place-a-page-of-type-x-as-the-first-page",
218                                            "layout.types." + lte.getLayoutType());
219                            }
220                    }
221    
222                    if (lte.getType() == LayoutTypeException.FIRST_LAYOUT ) {
223                            return themeDisplay.translate(
224                                    "the-first-page-cannot-be-of-type-x",
225                                    "layout.types." + lte.getLayoutType());
226                    }
227                    else if (lte.getType() == LayoutTypeException.NOT_PARENTABLE) {
228                            return themeDisplay.translate(
229                                    "a-page-cannot-become-a-child-of-a-page-that-is-not-" +
230                                            "parentable");
231                    }
232    
233                    return StringPool.BLANK;
234            }
235    
236            protected void updateDisplayOrder(HttpServletRequest request)
237                    throws Exception {
238    
239                    long groupId = ParamUtil.getLong(request, "groupId");
240                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
241                    long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
242                    long[] layoutIds = StringUtil.split(
243                            ParamUtil.getString(request, "layoutIds"), 0L);
244    
245                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
246                            request);
247    
248                    LayoutServiceUtil.setLayouts(
249                            groupId, privateLayout, parentLayoutId, layoutIds, serviceContext);
250            }
251    
252            protected void updateName(HttpServletRequest request) throws Exception {
253                    long plid = ParamUtil.getLong(request, "plid");
254    
255                    long groupId = ParamUtil.getLong(request, "groupId");
256                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
257                    long layoutId = ParamUtil.getLong(request, "layoutId");
258                    String name = ParamUtil.getString(request, "name");
259                    String languageId = ParamUtil.getString(request, "languageId");
260    
261                    if (plid <= 0) {
262                            LayoutServiceUtil.updateName(
263                                    groupId, privateLayout, layoutId, name, languageId);
264                    }
265                    else {
266                            LayoutServiceUtil.updateName(plid, name, languageId);
267                    }
268            }
269    
270            protected void updateParentLayoutId(HttpServletRequest request)
271                    throws Exception {
272    
273                    long plid = ParamUtil.getLong(request, "plid");
274    
275                    long parentPlid = ParamUtil.getLong(request, "parentPlid");
276    
277                    long groupId = ParamUtil.getLong(request, "groupId");
278                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
279                    long layoutId = ParamUtil.getLong(request, "layoutId");
280                    long parentLayoutId = ParamUtil.getLong(
281                            request, "parentLayoutId",
282                            LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
283    
284                    if (plid <= 0) {
285                            LayoutServiceUtil.updateParentLayoutId(
286                                    groupId, privateLayout, layoutId, parentLayoutId);
287                    }
288                    else {
289                            LayoutServiceUtil.updateParentLayoutId(plid, parentPlid);
290                    }
291    
292                    updatePriority(request);
293            }
294    
295            protected void updatePriority(HttpServletRequest request) throws Exception {
296                    long plid = ParamUtil.getLong(request, "plid");
297    
298                    long groupId = ParamUtil.getLong(request, "groupId");
299                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
300                    long layoutId = ParamUtil.getLong(request, "layoutId");
301                    long nextLayoutId = ParamUtil.getLong(request, "nextLayoutId");
302                    long previousLayoutId = ParamUtil.getLong(request, "previousLayoutId");
303                    int priority = ParamUtil.getInteger(request, "priority");
304    
305                    if (plid <= 0) {
306                            LayoutServiceUtil.updatePriority(
307                                    groupId, privateLayout, layoutId, nextLayoutId,
308                                    previousLayoutId);
309                    }
310                    else {
311                            LayoutServiceUtil.updatePriority(plid, priority);
312                    }
313            }
314    
315    }