001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.util.PortletKeys;
029    import com.liferay.portlet.layoutsadmin.action.EditLayoutsAction;
030    
031    import javax.portlet.ActionRequest;
032    import javax.portlet.ActionResponse;
033    import javax.portlet.PortletConfig;
034    import javax.portlet.RenderRequest;
035    import javax.portlet.RenderResponse;
036    
037    import org.apache.struts.action.ActionForm;
038    import org.apache.struts.action.ActionForward;
039    import org.apache.struts.action.ActionMapping;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     * @author Julio Camarero
044     */
045    public class EditLayoutBranchAction extends EditLayoutsAction {
046    
047            @Override
048            public void processAction(
049                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
050                            ActionRequest actionRequest, ActionResponse actionResponse)
051                    throws Exception {
052    
053                    try {
054                            checkPermissions(actionRequest);
055                    }
056                    catch (PrincipalException pe) {
057                            return;
058                    }
059    
060                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
061    
062                    try {
063                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
064                                    updateLayoutSetBranch(actionRequest);
065                            }
066                            else if (cmd.equals(Constants.DELETE)) {
067                                    deleteLayoutBranch(actionRequest, portletConfig);
068                            }
069    
070                            if (SessionErrors.isEmpty(actionRequest)) {
071                                    SessionMessages.add(
072                                            actionRequest,
073                                            portletConfig.getPortletName() + ".doRefresh",
074                                            PortletKeys.STAGING_BAR);
075                            }
076    
077                            sendRedirect(actionRequest, actionResponse);
078                    }
079                    catch (Exception e) {
080                            if (e instanceof LayoutBranchNameException) {
081                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
082    
083                                    sendRedirect(actionRequest, actionResponse);
084                            }
085                            else if (e instanceof PrincipalException ||
086                                    e instanceof SystemException) {
087    
088                                    SessionErrors.add(actionRequest, e.getClass().getName());
089    
090                                    setForward(actionRequest, "portlet.staging_bar.error");
091                            }
092                            else {
093                                    throw e;
094                            }
095                    }
096            }
097    
098            @Override
099            public ActionForward render(
100                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
101                            RenderRequest renderRequest, RenderResponse renderResponse)
102                    throws Exception {
103    
104                    try {
105                            checkPermissions(renderRequest);
106                    }
107                    catch (PrincipalException pe) {
108                            SessionErrors.add(
109                                    renderRequest, PrincipalException.class.getName());
110    
111                            return mapping.findForward("portlet.staging_bar.error");
112                    }
113    
114                    try {
115                            getGroup(renderRequest);
116                    }
117                    catch (Exception e) {
118                            if (e instanceof NoSuchGroupException ||
119                                    e instanceof PrincipalException) {
120    
121                                    SessionErrors.add(renderRequest, e.getClass().getName());
122    
123                                    return mapping.findForward("portlet.staging_bar.error");
124                            }
125                            else {
126                                    throw e;
127                            }
128                    }
129    
130                    return mapping.findForward(
131                            getForward(
132                                    renderRequest, "portlet.staging_bar.edit_layout_branch"));
133            }
134    
135            protected void deleteLayoutBranch(
136                            ActionRequest actionRequest, PortletConfig portletConfig)
137                    throws Exception {
138    
139                    long layoutBranchId = ParamUtil.getLong(
140                            actionRequest, "layoutBranchId");
141    
142                    long currentLayoutBranchId = ParamUtil.getLong(
143                            actionRequest, "currentLayoutBranchId");
144    
145                    LayoutBranchServiceUtil.deleteLayoutBranch(layoutBranchId);
146    
147                    SessionMessages.add(actionRequest, "pageVariationDeleted");
148    
149                    if (layoutBranchId == currentLayoutBranchId) {
150                            SessionMessages.add(
151                                    actionRequest,
152                                    portletConfig.getPortletName() + ".notAjaxable");
153                    }
154            }
155    
156            protected void updateLayoutSetBranch(ActionRequest actionRequest)
157                    throws Exception {
158    
159                    long layoutBranchId = ParamUtil.getLong(
160                            actionRequest, "layoutBranchId");
161    
162                    long layoutRevisionId = ParamUtil.getLong(
163                            actionRequest, "mergeLayoutRevisionId");
164                    String name = ParamUtil.getString(actionRequest, "name");
165                    String description = ParamUtil.getString(actionRequest, "description");
166    
167                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
168                            actionRequest);
169    
170                    if (layoutBranchId <= 0) {
171                            LayoutBranchServiceUtil.addLayoutBranch(
172                                    layoutRevisionId, name, description, false, serviceContext);
173    
174                            SessionMessages.add(actionRequest, "pageVariationAdded");
175                    }
176                    else {
177                            LayoutBranchServiceUtil.updateLayoutBranch(
178                                    layoutBranchId, name, description, serviceContext);
179    
180                            SessionMessages.add(actionRequest, "pageVariationUpdated");
181                    }
182            }
183    
184    }