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