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