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.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.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 EditLayoutSetBranchAction 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                                    updateLayoutSetBranch(actionRequest);
069                            }
070                            else if (cmd.equals(Constants.DELETE)) {
071                                    deleteLayoutSetBranch(actionRequest, portletConfig);
072                            }
073                            else if (cmd.equals("merge_layout_set_branch")) {
074                                    mergeLayoutSetBranch(actionRequest);
075                            }
076    
077                            if (SessionErrors.isEmpty(actionRequest)) {
078                                    SessionMessages.add(
079                                            actionRequest,
080                                            portletConfig.getPortletName() +
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                                            portletConfig.getPortletName() +
091                                                    SessionMessages.KEY_SUFFIX_REFRESH_PORTLET_DATA,
092                                            data);
093                            }
094    
095                            sendRedirect(actionRequest, actionResponse);
096                    }
097                    catch (Exception e) {
098                            if (e instanceof LayoutSetBranchNameException) {
099                                    SessionErrors.add(actionRequest, e.getClass().getName(), e);
100    
101                                    sendRedirect(actionRequest, actionResponse);
102                            }
103                            else if (e instanceof PrincipalException ||
104                                             e instanceof SystemException) {
105    
106                                    SessionErrors.add(actionRequest, e.getClass().getName());
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().getName());
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_set_branch"));
151            }
152    
153            protected void deleteLayoutSetBranch(
154                            ActionRequest actionRequest, PortletConfig portletConfig)
155                    throws Exception {
156    
157                    long layoutSetBranchId = ParamUtil.getLong(
158                            actionRequest, "layoutSetBranchId");
159    
160                    long currentLayoutBranchId = ParamUtil.getLong(
161                            actionRequest, "currentLayoutBranchId");
162    
163                    if (layoutSetBranchId == currentLayoutBranchId) {
164                            SessionMessages.add(
165                                    actionRequest,
166                                    portletConfig.getPortletName() +
167                                            SessionMessages.KEY_SUFFIX_PORTLET_NOT_AJAXABLE);
168                    }
169    
170                    LayoutSetBranchServiceUtil.deleteLayoutSetBranch(layoutSetBranchId);
171    
172                    SessionMessages.add(actionRequest, "sitePageVariationDeleted");
173            }
174    
175            protected void mergeLayoutSetBranch(ActionRequest actionRequest)
176                    throws Exception {
177    
178                    long layoutSetBranchId = ParamUtil.getLong(
179                            actionRequest, "layoutSetBranchId");
180    
181                    long mergeLayoutSetBranchId = ParamUtil.getLong(
182                            actionRequest, "mergeLayoutSetBranchId");
183    
184                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
185                            actionRequest);
186    
187                    LayoutSetBranchServiceUtil.mergeLayoutSetBranch(
188                            layoutSetBranchId, mergeLayoutSetBranchId, serviceContext);
189    
190                    SessionMessages.add(actionRequest, "sitePageVariationMerged");
191            }
192    
193            protected void updateLayoutSetBranch(ActionRequest actionRequest)
194                    throws Exception {
195    
196                    long layoutSetBranchId = ParamUtil.getLong(
197                            actionRequest, "layoutSetBranchId");
198    
199                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
200                    boolean privateLayout = ParamUtil.getBoolean(
201                            actionRequest, "privateLayout");
202                    String name = ParamUtil.getString(actionRequest, "name");
203                    String description = ParamUtil.getString(actionRequest, "description");
204                    long copyLayoutSetBranchId = ParamUtil.getLong(
205                            actionRequest, "copyLayoutSetBranchId",
206                            LayoutSetBranchConstants.ALL_BRANCHES);
207    
208                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
209                            actionRequest);
210    
211                    if (layoutSetBranchId <= 0) {
212                            LayoutSetBranchServiceUtil.addLayoutSetBranch(
213                                    groupId, privateLayout, name, description, false,
214                                    copyLayoutSetBranchId, serviceContext);
215    
216                            SessionMessages.add(actionRequest, "sitePageVariationAdded");
217                    }
218                    else {
219                            LayoutSetBranchServiceUtil.updateLayoutSetBranch(
220                                    groupId, layoutSetBranchId, name, description, serviceContext);
221    
222                            SessionMessages.add(actionRequest, "sitePageVariationUpdated");
223                    }
224            }
225    
226    }