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