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.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 actionMapping, ActionForm actionForm,
043                            PortletConfig portletConfig, ActionRequest actionRequest,
044                            ActionResponse actionResponse)
045                    throws Exception {
046    
047                    try {
048                            moveCategory(actionRequest, actionResponse);
049    
050                            String redirect = PortalUtil.escapeRedirect(
051                                    ParamUtil.getString(actionRequest, "redirect"));
052    
053                            if (Validator.isNotNull(redirect)) {
054                                    actionResponse.sendRedirect(redirect);
055                            }
056                    }
057                    catch (Exception e) {
058                            if (e instanceof PrincipalException) {
059                                    SessionErrors.add(actionRequest, e.getClass());
060                            }
061                            else {
062                                    throw e;
063                            }
064                    }
065            }
066    
067            @Override
068            public ActionForward render(
069                            ActionMapping actionMapping, ActionForm actionForm,
070                            PortletConfig portletConfig, RenderRequest renderRequest,
071                            RenderResponse renderResponse)
072                    throws Exception {
073    
074                    try {
075                            ActionUtil.getCategory(renderRequest);
076                    }
077                    catch (Exception e) {
078                            if (e instanceof PrincipalException) {
079                                    SessionErrors.add(renderRequest, e.getClass());
080    
081                                    return actionMapping.findForward(
082                                            "portlet.message_boards.error");
083                            }
084                            else {
085                                    throw e;
086                            }
087                    }
088    
089                    return actionMapping.findForward(
090                            getForward(renderRequest, "portlet.message_boards.move_category"));
091            }
092    
093            protected void moveCategory(
094                            ActionRequest actionRequest, ActionResponse actionResponse)
095                    throws Exception {
096    
097                    long categoryId = ParamUtil.getLong(actionRequest, "mbCategoryId");
098    
099                    long parentCategoryId = ParamUtil.getLong(
100                            actionRequest, "parentCategoryId");
101    
102                    boolean mergeWithParentCategory = ParamUtil.getBoolean(
103                            actionRequest, "mergeWithParentCategory");
104    
105                    MBCategoryServiceUtil.moveCategory(
106                            categoryId, parentCategoryId, mergeWithParentCategory);
107            }
108    
109    }