001    /**
002     * Copyright (c) 2000-present 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.dockbar.action;
016    
017    import com.liferay.portal.kernel.exception.SystemException;
018    import com.liferay.portal.kernel.servlet.MultiSessionMessages;
019    import com.liferay.portal.kernel.servlet.SessionErrors;
020    import com.liferay.portal.kernel.util.Constants;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.model.LayoutTypePortlet;
023    import com.liferay.portal.security.auth.PrincipalException;
024    import com.liferay.portal.security.permission.ActionKeys;
025    import com.liferay.portal.service.permission.LayoutPermissionUtil;
026    import com.liferay.portal.struts.PortletAction;
027    import com.liferay.portal.theme.ThemeDisplay;
028    import com.liferay.portal.util.PortalUtil;
029    import com.liferay.portal.util.WebKeys;
030    import com.liferay.portlet.sites.util.SitesUtil;
031    
032    import javax.portlet.ActionRequest;
033    import javax.portlet.ActionResponse;
034    import javax.portlet.PortletConfig;
035    
036    import org.apache.struts.action.ActionForm;
037    import org.apache.struts.action.ActionMapping;
038    
039    /**
040     * @author Eudaldo Alonso
041     */
042    public class EditLayoutsAction extends PortletAction {
043    
044            @Override
045            public void processAction(
046                            ActionMapping actionMapping, ActionForm actionForm,
047                            PortletConfig portletConfig, ActionRequest actionRequest,
048                            ActionResponse actionResponse)
049                    throws Exception {
050    
051                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
052                            WebKeys.THEME_DISPLAY);
053    
054                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
055    
056                    try {
057                            if (cmd.equals("reset_customized_view")) {
058                                    if (!LayoutPermissionUtil.contains(
059                                                    themeDisplay.getPermissionChecker(),
060                                                    themeDisplay.getLayout(), ActionKeys.CUSTOMIZE)) {
061    
062                                            throw new PrincipalException();
063                                    }
064    
065                                    LayoutTypePortlet layoutTypePortlet =
066                                            themeDisplay.getLayoutTypePortlet();
067    
068                                    if ((layoutTypePortlet != null) &&
069                                            layoutTypePortlet.isCustomizable() &&
070                                            layoutTypePortlet.isCustomizedView()) {
071    
072                                            layoutTypePortlet.resetUserPreferences();
073                                    }
074                            }
075                            else if (cmd.equals("reset_prototype")) {
076                                    SitesUtil.resetPrototype(themeDisplay.getLayout());
077                            }
078    
079                            MultiSessionMessages.add(
080                                    actionRequest,
081                                    PortalUtil.getPortletId(actionRequest) + "requestProcessed");
082    
083                            sendRedirect(actionRequest, actionResponse);
084                    }
085                    catch (Exception e) {
086                            if (e instanceof SystemException) {
087                                    SessionErrors.add(actionRequest, e.getClass(), e);
088    
089                                    sendRedirect(actionRequest, actionResponse);
090                            }
091                            else {
092                                    throw e;
093                            }
094                    }
095            }
096    
097    }