001    /**
002     * Copyright (c) 2000-present 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.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.LayoutSet;
025    import com.liferay.portal.model.LayoutSetBranch;
026    import com.liferay.portal.model.LayoutSetBranchConstants;
027    import com.liferay.portal.security.auth.PrincipalException;
028    import com.liferay.portal.service.LayoutSetBranchLocalServiceUtil;
029    import com.liferay.portal.service.LayoutSetBranchServiceUtil;
030    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
031    import com.liferay.portal.service.ServiceContext;
032    import com.liferay.portal.service.ServiceContextFactory;
033    import com.liferay.portal.struts.PortletAction;
034    import com.liferay.portal.util.PortalUtil;
035    import com.liferay.portal.util.PortletKeys;
036    import com.liferay.portlet.exportimport.staging.StagingUtil;
037    import com.liferay.portlet.sites.action.ActionUtil;
038    
039    import java.util.HashMap;
040    import java.util.Map;
041    
042    import javax.portlet.ActionRequest;
043    import javax.portlet.ActionResponse;
044    import javax.portlet.PortletConfig;
045    import javax.portlet.RenderRequest;
046    import javax.portlet.RenderResponse;
047    
048    import javax.servlet.http.HttpServletRequest;
049    
050    import org.apache.struts.action.ActionForm;
051    import org.apache.struts.action.ActionForward;
052    import org.apache.struts.action.ActionMapping;
053    
054    /**
055     * @author Brian Wing Shun Chan
056     * @author Julio Camarero
057     */
058    public class EditLayoutSetBranchAction extends PortletAction {
059    
060            @Override
061            public void processAction(
062                            ActionMapping actionMapping, ActionForm actionForm,
063                            PortletConfig portletConfig, ActionRequest actionRequest,
064                            ActionResponse actionResponse)
065                    throws Exception {
066    
067                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
068    
069                    try {
070                            if (cmd.equals(Constants.ADD) || cmd.equals(Constants.UPDATE)) {
071                                    updateLayoutSetBranch(actionRequest);
072                            }
073                            else if (cmd.equals(Constants.DELETE)) {
074                                    deleteLayoutSetBranch(actionRequest);
075                            }
076                            else if (cmd.equals("merge_layout_set_branch")) {
077                                    mergeLayoutSetBranch(actionRequest);
078                            }
079                            else if (cmd.equals("select_layout_set_branch")) {
080                                    selectLayoutSetBranch(actionRequest);
081                            }
082    
083                            if (SessionErrors.isEmpty(actionRequest)) {
084                                    SessionMessages.add(
085                                            actionRequest,
086                                            PortalUtil.getPortletId(actionRequest) +
087                                                    SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
088                                            PortletKeys.STAGING_BAR);
089    
090                                    Map<String, String> data = new HashMap<>();
091    
092                                    data.put("preventNotification", Boolean.TRUE.toString());
093    
094                                    SessionMessages.add(
095                                            actionRequest,
096                                            PortalUtil.getPortletId(actionRequest) +
097                                                    SessionMessages.KEY_SUFFIX_REFRESH_PORTLET_DATA,
098                                            data);
099                            }
100    
101                            sendRedirect(actionRequest, actionResponse);
102                    }
103                    catch (Exception e) {
104                            if (e instanceof LayoutSetBranchNameException) {
105                                    SessionErrors.add(actionRequest, e.getClass(), e);
106    
107                                    sendRedirect(actionRequest, actionResponse);
108                            }
109                            else if (e instanceof PrincipalException ||
110                                             e instanceof SystemException) {
111    
112                                    SessionErrors.add(actionRequest, e.getClass());
113    
114                                    setForward(actionRequest, "portlet.staging_bar.error");
115                            }
116                            else {
117                                    throw e;
118                            }
119                    }
120            }
121    
122            @Override
123            public ActionForward render(
124                            ActionMapping actionMapping, ActionForm actionForm,
125                            PortletConfig portletConfig, RenderRequest renderRequest,
126                            RenderResponse renderResponse)
127                    throws Exception {
128    
129                    try {
130                            ActionUtil.getGroup(renderRequest);
131                    }
132                    catch (Exception e) {
133                            if (e instanceof NoSuchGroupException ||
134                                    e instanceof PrincipalException) {
135    
136                                    SessionErrors.add(renderRequest, e.getClass());
137    
138                                    return actionMapping.findForward("portlet.staging_bar.error");
139                            }
140                            else {
141                                    throw e;
142                            }
143                    }
144    
145                    return actionMapping.findForward(
146                            getForward(
147                                    renderRequest, "portlet.staging_bar.edit_layout_set_branch"));
148            }
149    
150            protected void deleteLayoutSetBranch(ActionRequest actionRequest)
151                    throws Exception {
152    
153                    long layoutSetBranchId = ParamUtil.getLong(
154                            actionRequest, "layoutSetBranchId");
155    
156                    long currentLayoutBranchId = ParamUtil.getLong(
157                            actionRequest, "currentLayoutBranchId");
158    
159                    if (layoutSetBranchId == currentLayoutBranchId) {
160                            SessionMessages.add(
161                                    actionRequest,
162                                    PortalUtil.getPortletId(actionRequest) +
163                                            SessionMessages.KEY_SUFFIX_PORTLET_NOT_AJAXABLE);
164                    }
165    
166                    LayoutSetBranchServiceUtil.deleteLayoutSetBranch(layoutSetBranchId);
167    
168                    SessionMessages.add(actionRequest, "sitePageVariationDeleted");
169            }
170    
171            protected void mergeLayoutSetBranch(ActionRequest actionRequest)
172                    throws Exception {
173    
174                    long layoutSetBranchId = ParamUtil.getLong(
175                            actionRequest, "layoutSetBranchId");
176    
177                    long mergeLayoutSetBranchId = ParamUtil.getLong(
178                            actionRequest, "mergeLayoutSetBranchId");
179    
180                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
181                            actionRequest);
182    
183                    LayoutSetBranchServiceUtil.mergeLayoutSetBranch(
184                            layoutSetBranchId, mergeLayoutSetBranchId, serviceContext);
185    
186                    SessionMessages.add(actionRequest, "sitePageVariationMerged");
187            }
188    
189            protected void selectLayoutSetBranch(ActionRequest actionRequest)
190                    throws Exception {
191    
192                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
193                            actionRequest);
194    
195                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
196                    boolean privateLayout = ParamUtil.getBoolean(
197                            actionRequest, "privateLayout");
198    
199                    LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
200                            groupId, privateLayout);
201    
202                    long layoutSetBranchId = ParamUtil.getLong(
203                            actionRequest, "layoutSetBranchId");
204    
205                    // Ensure layout set branch exists
206    
207                    LayoutSetBranch layoutSetBranch =
208                            LayoutSetBranchLocalServiceUtil.getLayoutSetBranch(
209                                    layoutSetBranchId);
210    
211                    StagingUtil.setRecentLayoutSetBranchId(
212                            request, layoutSet.getLayoutSetId(),
213                            layoutSetBranch.getLayoutSetBranchId());
214            }
215    
216            protected void updateLayoutSetBranch(ActionRequest actionRequest)
217                    throws Exception {
218    
219                    long layoutSetBranchId = ParamUtil.getLong(
220                            actionRequest, "layoutSetBranchId");
221    
222                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
223                    boolean privateLayout = ParamUtil.getBoolean(
224                            actionRequest, "privateLayout");
225                    String name = ParamUtil.getString(actionRequest, "name");
226                    String description = ParamUtil.getString(actionRequest, "description");
227                    long copyLayoutSetBranchId = ParamUtil.getLong(
228                            actionRequest, "copyLayoutSetBranchId",
229                            LayoutSetBranchConstants.ALL_BRANCHES);
230    
231                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
232                            actionRequest);
233    
234                    if (layoutSetBranchId <= 0) {
235                            LayoutSetBranchServiceUtil.addLayoutSetBranch(
236                                    groupId, privateLayout, name, description, false,
237                                    copyLayoutSetBranchId, serviceContext);
238    
239                            SessionMessages.add(actionRequest, "sitePageVariationAdded");
240                    }
241                    else {
242                            LayoutSetBranchServiceUtil.updateLayoutSetBranch(
243                                    groupId, layoutSetBranchId, name, description, serviceContext);
244    
245                            SessionMessages.add(actionRequest, "sitePageVariationUpdated");
246                    }
247            }
248    
249    }