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.LayoutSetBranchNameException;
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.model.LayoutSetBranchConstants;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.service.LayoutSetBranchServiceUtil;
027    import com.liferay.portal.service.ServiceContext;
028    import com.liferay.portal.service.ServiceContextFactory;
029    import com.liferay.portal.util.PortletKeys;
030    import com.liferay.portlet.layoutsadmin.action.EditLayoutsAction;
031    
032    import javax.portlet.ActionRequest;
033    import javax.portlet.ActionResponse;
034    import javax.portlet.PortletConfig;
035    import javax.portlet.RenderRequest;
036    import javax.portlet.RenderResponse;
037    
038    import org.apache.struts.action.ActionForm;
039    import org.apache.struts.action.ActionForward;
040    import org.apache.struts.action.ActionMapping;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     * @author Julio Camarero
045     */
046    public class EditLayoutSetBranchAction extends EditLayoutsAction {
047    
048            @Override
049            public void processAction(
050                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
051                            ActionRequest actionRequest, ActionResponse actionResponse)
052                    throws Exception {
053    
054                    try {
055                            checkPermissions(actionRequest);
056                    }
057                    catch (PrincipalException pe) {
058                            return;
059                    }
060    
061                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
062    
063                    try {
064                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
065                                    updateLayoutSetBranch(actionRequest);
066                            }
067                            else if (cmd.equals(Constants.DELETE)) {
068                                    deleteLayoutSetBranch(actionRequest, portletConfig);
069                            }
070                            else if (cmd.equals("merge_layout_set_branch")) {
071                                    mergeLayoutSetBranch(actionRequest);
072                            }
073    
074                            if (SessionErrors.isEmpty(actionRequest)) {
075                                    SessionMessages.add(
076                                            actionRequest,
077                                            portletConfig.getPortletName() + ".doRefresh",
078                                            PortletKeys.STAGING_BAR);
079                            }
080    
081                            sendRedirect(actionRequest, actionResponse);
082                    }
083                    catch (Exception e) {
084                            if (e instanceof LayoutSetBranchNameException) {
085                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
086    
087                                    sendRedirect(actionRequest, actionResponse);
088                            }
089                            else if (e instanceof PrincipalException ||
090                                             e instanceof SystemException) {
091    
092                                    SessionErrors.add(actionRequest, e.getClass().getName());
093    
094                                    setForward(actionRequest, "portlet.staging_bar.error");
095                            }
096                            else {
097                                    throw e;
098                            }
099                    }
100            }
101    
102            @Override
103            public ActionForward render(
104                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
105                            RenderRequest renderRequest, RenderResponse renderResponse)
106                    throws Exception {
107    
108                    try {
109                            checkPermissions(renderRequest);
110                    }
111                    catch (PrincipalException pe) {
112                            SessionErrors.add(
113                                    renderRequest, PrincipalException.class.getName());
114    
115                            return mapping.findForward("portlet.staging_bar.error");
116                    }
117    
118                    try {
119                            getGroup(renderRequest);
120                    }
121                    catch (Exception e) {
122                            if (e instanceof NoSuchGroupException ||
123                                    e instanceof PrincipalException) {
124    
125                                    SessionErrors.add(renderRequest, e.getClass().getName());
126    
127                                    return mapping.findForward("portlet.staging_bar.error");
128                            }
129                            else {
130                                    throw e;
131                            }
132                    }
133    
134                    return mapping.findForward(
135                            getForward(
136                                    renderRequest, "portlet.staging_bar.edit_layout_set_branch"));
137            }
138    
139            protected void deleteLayoutSetBranch(
140                            ActionRequest actionRequest, PortletConfig portletConfig)
141                    throws Exception {
142    
143                    long layoutSetBranchId = ParamUtil.getLong(
144                            actionRequest, "layoutSetBranchId");
145    
146                    long currentLayoutBranchId = ParamUtil.getLong(
147                            actionRequest, "currentLayoutBranchId");
148    
149                    if (layoutSetBranchId == currentLayoutBranchId) {
150                            SessionMessages.add(
151                                    actionRequest,
152                                    portletConfig.getPortletName() + ".notAjaxable");
153                    }
154    
155                    LayoutSetBranchServiceUtil.deleteLayoutSetBranch(layoutSetBranchId);
156    
157                    SessionMessages.add(actionRequest, "sitePageVariationDeleted");
158            }
159    
160            protected void mergeLayoutSetBranch(ActionRequest actionRequest)
161                    throws Exception {
162    
163                    long layoutSetBranchId = ParamUtil.getLong(
164                            actionRequest, "layoutSetBranchId");
165    
166                    long mergeLayoutSetBranchId = ParamUtil.getLong(
167                            actionRequest, "mergeLayoutSetBranchId");
168    
169                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
170                            actionRequest);
171    
172                    LayoutSetBranchServiceUtil.mergeLayoutSetBranch(
173                            layoutSetBranchId, mergeLayoutSetBranchId, serviceContext);
174    
175                    SessionMessages.add(actionRequest, "sitePageVariationMerged");
176            }
177    
178            protected void updateLayoutSetBranch(ActionRequest actionRequest)
179                    throws Exception {
180    
181                    long layoutSetBranchId = ParamUtil.getLong(
182                            actionRequest, "layoutSetBranchId");
183    
184                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
185                    boolean privateLayout = ParamUtil.getBoolean(
186                            actionRequest, "privateLayout");
187                    String name = ParamUtil.getString(actionRequest, "name");
188                    String description = ParamUtil.getString(actionRequest, "description");
189                    long copyLayoutSetBranchId = ParamUtil.getLong(
190                            actionRequest, "copyLayoutSetBranchId",
191                            LayoutSetBranchConstants.ALL_BRANCHES);
192    
193                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
194                            actionRequest);
195    
196                    if (layoutSetBranchId <= 0) {
197                            LayoutSetBranchServiceUtil.addLayoutSetBranch(
198                                    groupId, privateLayout, name, description, false,
199                                    copyLayoutSetBranchId, serviceContext);
200    
201                            SessionMessages.add(actionRequest, "sitePageVariationAdded");
202                    }
203                    else {
204                            LayoutSetBranchServiceUtil.updateLayoutSetBranch(
205                                    groupId, layoutSetBranchId, name, description, serviceContext);
206    
207                            SessionMessages.add(actionRequest, "sitePageVariationUpdated");
208                    }
209            }
210    
211    }