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