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.portlet.LiferayPortletConfig;
021    import com.liferay.portal.kernel.servlet.SessionErrors;
022    import com.liferay.portal.kernel.servlet.SessionMessages;
023    import com.liferay.portal.kernel.util.Constants;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.security.auth.PrincipalException;
026    import com.liferay.portal.service.LayoutBranchServiceUtil;
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 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, portletConfig);
073                            }
074    
075                            if (SessionErrors.isEmpty(actionRequest)) {
076                                    LiferayPortletConfig liferayPortletConfig =
077                                            (LiferayPortletConfig)portletConfig;
078    
079                                    SessionMessages.add(
080                                            actionRequest,
081                                            liferayPortletConfig.getPortletId() +
082                                                    SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
083                                            PortletKeys.STAGING_BAR);
084    
085                                    Map<String, String> data = new HashMap<String, String>();
086    
087                                    data.put("preventNotification", Boolean.TRUE.toString());
088    
089                                    SessionMessages.add(
090                                            actionRequest,
091                                            liferayPortletConfig.getPortletId() +
092                                                    SessionMessages.KEY_SUFFIX_REFRESH_PORTLET_DATA,
093                                            data);
094                            }
095    
096                            sendRedirect(actionRequest, actionResponse);
097                    }
098                    catch (Exception e) {
099                            if (e instanceof LayoutBranchNameException) {
100                                    SessionErrors.add(actionRequest, e.getClass(), e);
101    
102                                    sendRedirect(actionRequest, actionResponse);
103                            }
104                            else if (e instanceof PrincipalException ||
105                                             e instanceof SystemException) {
106    
107                                    SessionErrors.add(actionRequest, e.getClass());
108    
109                                    setForward(actionRequest, "portlet.staging_bar.error");
110                            }
111                            else {
112                                    throw e;
113                            }
114                    }
115            }
116    
117            @Override
118            public ActionForward render(
119                            ActionMapping actionMapping, ActionForm actionForm,
120                            PortletConfig portletConfig, RenderRequest renderRequest,
121                            RenderResponse renderResponse)
122                    throws Exception {
123    
124                    try {
125                            checkPermissions(renderRequest);
126                    }
127                    catch (PrincipalException pe) {
128                            SessionErrors.add(
129                                    renderRequest, PrincipalException.class.getName());
130    
131                            return actionMapping.findForward("portlet.staging_bar.error");
132                    }
133    
134                    try {
135                            getGroup(renderRequest);
136                    }
137                    catch (Exception e) {
138                            if (e instanceof NoSuchGroupException ||
139                                    e instanceof PrincipalException) {
140    
141                                    SessionErrors.add(renderRequest, e.getClass());
142    
143                                    return actionMapping.findForward("portlet.staging_bar.error");
144                            }
145                            else {
146                                    throw e;
147                            }
148                    }
149    
150                    return actionMapping.findForward(
151                            getForward(
152                                    renderRequest, "portlet.staging_bar.edit_layout_branch"));
153            }
154    
155            protected void deleteLayoutBranch(
156                            ActionRequest actionRequest, PortletConfig portletConfig)
157                    throws Exception {
158    
159                    long layoutBranchId = ParamUtil.getLong(
160                            actionRequest, "layoutBranchId");
161    
162                    long currentLayoutBranchId = ParamUtil.getLong(
163                            actionRequest, "currentLayoutBranchId");
164    
165                    LayoutBranchServiceUtil.deleteLayoutBranch(layoutBranchId);
166    
167                    SessionMessages.add(actionRequest, "pageVariationDeleted");
168    
169                    if (layoutBranchId == currentLayoutBranchId) {
170                            LiferayPortletConfig liferayPortletConfig =
171                                    (LiferayPortletConfig)portletConfig;
172    
173                            SessionMessages.add(
174                                    actionRequest,
175                                    liferayPortletConfig.getPortletId() +
176                                            SessionMessages.KEY_SUFFIX_PORTLET_NOT_AJAXABLE);
177                    }
178            }
179    
180            protected void updateLayoutBranch(ActionRequest actionRequest)
181                    throws Exception {
182    
183                    long layoutBranchId = ParamUtil.getLong(
184                            actionRequest, "layoutBranchId");
185    
186                    long layoutRevisionId = ParamUtil.getLong(
187                            actionRequest, "copyLayoutRevisionId");
188                    String name = ParamUtil.getString(actionRequest, "name");
189                    String description = ParamUtil.getString(actionRequest, "description");
190    
191                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
192                            actionRequest);
193    
194                    if (layoutBranchId <= 0) {
195                            LayoutBranchServiceUtil.addLayoutBranch(
196                                    layoutRevisionId, name, description, false, serviceContext);
197    
198                            SessionMessages.add(actionRequest, "pageVariationAdded");
199                    }
200                    else {
201                            LayoutBranchServiceUtil.updateLayoutBranch(
202                                    layoutBranchId, name, description, serviceContext);
203    
204                            SessionMessages.add(actionRequest, "pageVariationUpdated");
205                    }
206            }
207    
208    }