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