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.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.model.LayoutSetBranchConstants;
026    import com.liferay.portal.security.auth.PrincipalException;
027    import com.liferay.portal.service.LayoutSetBranchServiceUtil;
028    import com.liferay.portal.service.ServiceContext;
029    import com.liferay.portal.service.ServiceContextFactory;
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, portletConfig);
074                            }
075                            else if (cmd.equals("merge_layout_set_branch")) {
076                                    mergeLayoutSetBranch(actionRequest);
077                            }
078    
079                            if (SessionErrors.isEmpty(actionRequest)) {
080                                    LiferayPortletConfig liferayPortletConfig =
081                                            (LiferayPortletConfig)portletConfig;
082    
083                                    SessionMessages.add(
084                                            actionRequest,
085                                            liferayPortletConfig.getPortletId() +
086                                                    SessionMessages.KEY_SUFFIX_REFRESH_PORTLET,
087                                            PortletKeys.STAGING_BAR);
088    
089                                    Map<String, String> data = new HashMap<String, String>();
090    
091                                    data.put("preventNotification", Boolean.TRUE.toString());
092    
093                                    SessionMessages.add(
094                                            actionRequest,
095                                            liferayPortletConfig.getPortletId() +
096                                                    SessionMessages.KEY_SUFFIX_REFRESH_PORTLET_DATA,
097                                            data);
098                            }
099    
100                            sendRedirect(actionRequest, actionResponse);
101                    }
102                    catch (Exception e) {
103                            if (e instanceof LayoutSetBranchNameException) {
104                                    SessionErrors.add(actionRequest, e.getClass(), e);
105    
106                                    sendRedirect(actionRequest, actionResponse);
107                            }
108                            else if (e instanceof PrincipalException ||
109                                             e instanceof SystemException) {
110    
111                                    SessionErrors.add(actionRequest, e.getClass());
112    
113                                    setForward(actionRequest, "portlet.staging_bar.error");
114                            }
115                            else {
116                                    throw e;
117                            }
118                    }
119            }
120    
121            @Override
122            public ActionForward render(
123                            ActionMapping actionMapping, ActionForm actionForm,
124                            PortletConfig portletConfig, RenderRequest renderRequest,
125                            RenderResponse renderResponse)
126                    throws Exception {
127    
128                    try {
129                            checkPermissions(renderRequest);
130                    }
131                    catch (PrincipalException pe) {
132                            SessionErrors.add(
133                                    renderRequest, PrincipalException.class.getName());
134    
135                            return actionMapping.findForward("portlet.staging_bar.error");
136                    }
137    
138                    try {
139                            getGroup(renderRequest);
140                    }
141                    catch (Exception e) {
142                            if (e instanceof NoSuchGroupException ||
143                                    e instanceof PrincipalException) {
144    
145                                    SessionErrors.add(renderRequest, e.getClass());
146    
147                                    return actionMapping.findForward("portlet.staging_bar.error");
148                            }
149                            else {
150                                    throw e;
151                            }
152                    }
153    
154                    return actionMapping.findForward(
155                            getForward(
156                                    renderRequest, "portlet.staging_bar.edit_layout_set_branch"));
157            }
158    
159            protected void deleteLayoutSetBranch(
160                            ActionRequest actionRequest, PortletConfig portletConfig)
161                    throws Exception {
162    
163                    long layoutSetBranchId = ParamUtil.getLong(
164                            actionRequest, "layoutSetBranchId");
165    
166                    long currentLayoutBranchId = ParamUtil.getLong(
167                            actionRequest, "currentLayoutBranchId");
168    
169                    if (layoutSetBranchId == currentLayoutBranchId) {
170                            LiferayPortletConfig liferayPortletConfig =
171                                    (LiferayPortletConfig)portletConfig;
172    
173                            SessionMessages.add(
174                                    actionRequest,
175                                    liferayPortletConfig.getPortletId() +
176                                            SessionMessages.KEY_SUFFIX_PORTLET_NOT_AJAXABLE);
177                    }
178    
179                    LayoutSetBranchServiceUtil.deleteLayoutSetBranch(layoutSetBranchId);
180    
181                    SessionMessages.add(actionRequest, "sitePageVariationDeleted");
182            }
183    
184            protected void mergeLayoutSetBranch(ActionRequest actionRequest)
185                    throws Exception {
186    
187                    long layoutSetBranchId = ParamUtil.getLong(
188                            actionRequest, "layoutSetBranchId");
189    
190                    long mergeLayoutSetBranchId = ParamUtil.getLong(
191                            actionRequest, "mergeLayoutSetBranchId");
192    
193                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
194                            actionRequest);
195    
196                    LayoutSetBranchServiceUtil.mergeLayoutSetBranch(
197                            layoutSetBranchId, mergeLayoutSetBranchId, serviceContext);
198    
199                    SessionMessages.add(actionRequest, "sitePageVariationMerged");
200            }
201    
202            protected void updateLayoutSetBranch(ActionRequest actionRequest)
203                    throws Exception {
204    
205                    long layoutSetBranchId = ParamUtil.getLong(
206                            actionRequest, "layoutSetBranchId");
207    
208                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
209                    boolean privateLayout = ParamUtil.getBoolean(
210                            actionRequest, "privateLayout");
211                    String name = ParamUtil.getString(actionRequest, "name");
212                    String description = ParamUtil.getString(actionRequest, "description");
213                    long copyLayoutSetBranchId = ParamUtil.getLong(
214                            actionRequest, "copyLayoutSetBranchId",
215                            LayoutSetBranchConstants.ALL_BRANCHES);
216    
217                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
218                            actionRequest);
219    
220                    if (layoutSetBranchId <= 0) {
221                            LayoutSetBranchServiceUtil.addLayoutSetBranch(
222                                    groupId, privateLayout, name, description, false,
223                                    copyLayoutSetBranchId, serviceContext);
224    
225                            SessionMessages.add(actionRequest, "sitePageVariationAdded");
226                    }
227                    else {
228                            LayoutSetBranchServiceUtil.updateLayoutSetBranch(
229                                    groupId, layoutSetBranchId, name, description, serviceContext);
230    
231                            SessionMessages.add(actionRequest, "sitePageVariationUpdated");
232                    }
233            }
234    
235    }