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.messageboards.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    import com.liferay.portal.security.auth.PrincipalException;
021    import com.liferay.portal.struts.PortletAction;
022    import com.liferay.portal.util.PortalUtil;
023    import com.liferay.portlet.messageboards.service.MBCategoryServiceUtil;
024    
025    import javax.portlet.ActionRequest;
026    import javax.portlet.ActionResponse;
027    import javax.portlet.PortletConfig;
028    import javax.portlet.RenderRequest;
029    import javax.portlet.RenderResponse;
030    
031    import org.apache.struts.action.ActionForm;
032    import org.apache.struts.action.ActionForward;
033    import org.apache.struts.action.ActionMapping;
034    
035    /**
036     * @author Eduardo Garcia
037     */
038    public class MoveCategoryAction extends PortletAction {
039    
040            @Override
041            public void processAction(
042                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
043                            ActionRequest actionRequest, ActionResponse actionResponse)
044                    throws Exception {
045    
046                    try {
047                            moveCategory(actionRequest, actionResponse);
048    
049                            String redirect = PortalUtil.escapeRedirect(
050                                    ParamUtil.getString(actionRequest, "redirect"));
051    
052                            if (Validator.isNotNull(redirect)) {
053                                    actionResponse.sendRedirect(redirect);
054                            }
055                    }
056                    catch (Exception e) {
057                            if (e instanceof PrincipalException) {
058                                    SessionErrors.add(actionRequest, e.getClass());
059                            }
060                            else {
061                                    throw e;
062                            }
063                    }
064            }
065    
066            @Override
067            public ActionForward render(
068                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
069                            RenderRequest renderRequest, RenderResponse renderResponse)
070                    throws Exception {
071    
072                    try {
073                            ActionUtil.getCategory(renderRequest);
074                    }
075                    catch (Exception e) {
076                            if (e instanceof PrincipalException) {
077                                    SessionErrors.add(renderRequest, e.getClass());
078    
079                                    return mapping.findForward("portlet.message_boards.error");
080                            }
081                            else {
082                                    throw e;
083                            }
084                    }
085    
086                    return mapping.findForward(
087                            getForward(renderRequest, "portlet.message_boards.move_category"));
088            }
089    
090            protected void moveCategory(
091                            ActionRequest actionRequest, ActionResponse actionResponse)
092                    throws Exception {
093    
094                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
095    
096                    long parentCategoryId = ParamUtil.getLong(
097                            actionRequest, "parentCategoryId");
098    
099                    boolean mergeWithParentCategory = ParamUtil.getBoolean(
100                            actionRequest, "mergeWithParentCategory");
101    
102                    MBCategoryServiceUtil.moveCategory(
103                            categoryId, parentCategoryId, mergeWithParentCategory);
104            }
105    
106    }