001    /**
002     * Copyright (c) 2000-2010 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.journal.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.security.auth.PrincipalException;
020    import com.liferay.portal.struts.PortletAction;
021    import com.liferay.portlet.journal.DuplicateStructureIdException;
022    import com.liferay.portlet.journal.NoSuchStructureException;
023    import com.liferay.portlet.journal.StructureIdException;
024    import com.liferay.portlet.journal.service.JournalStructureServiceUtil;
025    
026    import javax.portlet.ActionRequest;
027    import javax.portlet.ActionResponse;
028    import javax.portlet.PortletConfig;
029    import javax.portlet.RenderRequest;
030    import javax.portlet.RenderResponse;
031    
032    import org.apache.struts.action.ActionForm;
033    import org.apache.struts.action.ActionForward;
034    import org.apache.struts.action.ActionMapping;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class CopyStructureAction extends PortletAction {
040    
041            public void processAction(
042                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
043                            ActionRequest actionRequest, ActionResponse actionResponse)
044                    throws Exception {
045    
046                    try {
047                            copyStructure(actionRequest);
048    
049                            sendRedirect(actionRequest, actionResponse);
050                    }
051                    catch (Exception e) {
052                            if (e instanceof NoSuchStructureException ||
053                                    e instanceof PrincipalException) {
054    
055                                    SessionErrors.add(actionRequest, e.getClass().getName());
056    
057                                    setForward(actionRequest, "portlet.journal.error");
058                            }
059                            else if (e instanceof DuplicateStructureIdException ||
060                                             e instanceof StructureIdException) {
061    
062                                    SessionErrors.add(actionRequest, e.getClass().getName());
063                            }
064                            else {
065                                    throw e;
066                            }
067                    }
068            }
069    
070            public ActionForward render(
071                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
072                            RenderRequest renderRequest, RenderResponse renderResponse)
073                    throws Exception {
074    
075                    return mapping.findForward(
076                            getForward(renderRequest, "portlet.journal.copy_structure"));
077            }
078    
079            protected void copyStructure(ActionRequest actionRequest) throws Exception {
080                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
081                    String oldStructureId = ParamUtil.getString(
082                            actionRequest, "oldStructureId");
083                    String newStructureId = ParamUtil.getString(
084                            actionRequest, "newStructureId");
085                    boolean autoStructureId = ParamUtil.getBoolean(
086                            actionRequest, "autoStructureId");
087    
088                    JournalStructureServiceUtil.copyStructure(
089                            groupId, oldStructureId, newStructureId, autoStructureId);
090            }
091    
092    }