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.stagingbar.action;
016    
017    import com.liferay.portal.LayoutBranchNameException;
018    import com.liferay.portal.NoSuchGroupException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.servlet.SessionErrors;
021    import com.liferay.portal.kernel.servlet.SessionMessages;
022    import com.liferay.portal.kernel.util.Constants;
023    import com.liferay.portal.kernel.util.ParamUtil;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.service.LayoutBranchServiceUtil;
026    import com.liferay.portal.service.ServiceContext;
027    import com.liferay.portal.service.ServiceContextFactory;
028    import com.liferay.portal.struts.PortletAction;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.PortletKeys;
032    import com.liferay.portal.util.WebKeys;
033    import com.liferay.portlet.exportimport.staging.StagingUtil;
034    import com.liferay.portlet.sites.action.ActionUtil;
035    
036    import java.util.HashMap;
037    import java.util.Map;
038    
039    import javax.portlet.ActionRequest;
040    import javax.portlet.ActionResponse;
041    import javax.portlet.PortletConfig;
042    import javax.portlet.RenderRequest;
043    import javax.portlet.RenderResponse;
044    
045    import javax.servlet.http.HttpServletRequest;
046    
047    import org.apache.struts.action.ActionForm;
048    import org.apache.struts.action.ActionForward;
049    import org.apache.struts.action.ActionMapping;
050    
051    /**
052     * @author Brian Wing Shun Chan
053     * @author Julio Camarero
054     */
055    public class EditLayoutBranchAction extends PortletAction {
056    
057            @Override
058            public void processAction(
059                            ActionMapping actionMapping, ActionForm actionForm,
060                            PortletConfig portletConfig, ActionRequest actionRequest,
061                            ActionResponse actionResponse)
062                    throws Exception {
063    
064                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
065    
066                    try {
067                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
068                                    updateLayoutBranch(actionRequest);
069                            }
070                            else if (cmd.equals(Constants.DELETE)) {
071                                    deleteLayoutBranch(actionRequest);
072                            }
073                            else if (cmd.equals("select_layout_branch")) {
074                                    selectLayoutBranch(actionRequest);
075                            }
076    
077                            if (SessionErrors.isEmpty(actionRequest)) {
078                                    SessionMessages.add(
079                                            actionRequest,
080                                            PortalUtil.getPortletId(actionRequest) +
081                                                    SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
082                                            PortletKeys.STAGING_BAR);
083    
084                                    Map<String, String> data = new HashMap<>();
085    
086                                    data.put("preventNotification", Boolean.TRUE.toString());
087    
088                                    SessionMessages.add(
089                                            actionRequest,
090                                            PortalUtil.getPortletId(actionRequest) +
091                                                    SessionMessages.KEY_SUFFIX_REFRESH_PORTLET_DATA,
092                                            data);
093                            }
094    
095                            sendRedirect(actionRequest, actionResponse);
096                    }
097                    catch (Exception e) {
098                            if (e instanceof LayoutBranchNameException) {
099                                    SessionErrors.add(actionRequest, e.getClass(), e);
100    
101                                    sendRedirect(actionRequest, actionResponse);
102                            }
103                            else if (e instanceof PrincipalException ||
104                                             e instanceof SystemException) {
105    
106                                    SessionErrors.add(actionRequest, e.getClass());
107    
108                                    setForward(actionRequest, "portlet.staging_bar.error");
109                            }
110                            else {
111                                    throw e;
112                            }
113                    }
114            }
115    
116            @Override
117            public ActionForward render(
118                            ActionMapping actionMapping, ActionForm actionForm,
119                            PortletConfig portletConfig, RenderRequest renderRequest,
120                            RenderResponse renderResponse)
121                    throws Exception {
122    
123                    try {
124                            ActionUtil.getGroup(renderRequest);
125                    }
126                    catch (Exception e) {
127                            if (e instanceof NoSuchGroupException ||
128                                    e instanceof PrincipalException) {
129    
130                                    SessionErrors.add(renderRequest, e.getClass());
131    
132                                    return actionMapping.findForward("portlet.staging_bar.error");
133                            }
134                            else {
135                                    throw e;
136                            }
137                    }
138    
139                    return actionMapping.findForward(
140                            getForward(
141                                    renderRequest, "portlet.staging_bar.edit_layout_branch"));
142            }
143    
144            protected void deleteLayoutBranch(ActionRequest actionRequest)
145                    throws Exception {
146    
147                    long layoutBranchId = ParamUtil.getLong(
148                            actionRequest, "layoutBranchId");
149    
150                    long currentLayoutBranchId = ParamUtil.getLong(
151                            actionRequest, "currentLayoutBranchId");
152    
153                    LayoutBranchServiceUtil.deleteLayoutBranch(layoutBranchId);
154    
155                    SessionMessages.add(actionRequest, "pageVariationDeleted");
156    
157                    if (layoutBranchId == currentLayoutBranchId) {
158                            SessionMessages.add(
159                                    actionRequest,
160                                    PortalUtil.getPortletId(actionRequest) +
161                                            SessionMessages.KEY_SUFFIX_PORTLET_NOT_AJAXABLE);
162                    }
163            }
164    
165            protected void selectLayoutBranch(ActionRequest actionRequest) {
166                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
167                            actionRequest);
168    
169                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
170                            WebKeys.THEME_DISPLAY);
171    
172                    long layoutSetBranchId = ParamUtil.getLong(
173                            actionRequest, "layoutSetBranchId");
174    
175                    long layoutBranchId = ParamUtil.getLong(
176                            actionRequest, "layoutBranchId");
177    
178                    StagingUtil.setRecentLayoutBranchId(
179                            request, layoutSetBranchId, themeDisplay.getPlid(), layoutBranchId);
180            }
181    
182            protected void updateLayoutBranch(ActionRequest actionRequest)
183                    throws Exception {
184    
185                    long layoutBranchId = ParamUtil.getLong(
186                            actionRequest, "layoutBranchId");
187    
188                    long layoutRevisionId = ParamUtil.getLong(
189                            actionRequest, "copyLayoutRevisionId");
190                    String name = ParamUtil.getString(actionRequest, "name");
191                    String description = ParamUtil.getString(actionRequest, "description");
192    
193                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
194                            actionRequest);
195    
196                    if (layoutBranchId <= 0) {
197                            LayoutBranchServiceUtil.addLayoutBranch(
198                                    layoutRevisionId, name, description, false, serviceContext);
199    
200                            SessionMessages.add(actionRequest, "pageVariationAdded");
201                    }
202                    else {
203                            LayoutBranchServiceUtil.updateLayoutBranch(
204                                    layoutBranchId, name, description, serviceContext);
205    
206                            SessionMessages.add(actionRequest, "pageVariationUpdated");
207                    }
208            }
209    
210    }