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.ArticleIdException;
022    import com.liferay.portlet.journal.DuplicateArticleIdException;
023    import com.liferay.portlet.journal.NoSuchArticleException;
024    import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
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 CopyArticleAction 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                            copyArticle(actionRequest);
048    
049                            sendRedirect(actionRequest, actionResponse);
050                    }
051                    catch (Exception e) {
052                            if (e instanceof NoSuchArticleException ||
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 DuplicateArticleIdException ||
060                                             e instanceof ArticleIdException) {
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_article"));
077            }
078    
079            protected void copyArticle(ActionRequest actionRequest) throws Exception {
080                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
081                    String oldArticleId = ParamUtil.getString(
082                            actionRequest, "oldArticleId");
083                    String newArticleId = ParamUtil.getString(
084                            actionRequest, "newArticleId");
085                    boolean autoArticleId = ParamUtil.getBoolean(
086                            actionRequest, "autoArticleId");
087                    double version = ParamUtil.getDouble(actionRequest, "version");
088    
089                    JournalArticleServiceUtil.copyArticle(
090                            groupId, oldArticleId, newArticleId, autoArticleId, version);
091            }
092    
093    }