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.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            @Override
042            public void processAction(
043                            ActionMapping actionMapping, ActionForm actionForm,
044                            PortletConfig portletConfig, ActionRequest actionRequest,
045                            ActionResponse actionResponse)
046                    throws Exception {
047    
048                    try {
049                            copyArticle(actionRequest);
050    
051                            sendRedirect(actionRequest, actionResponse);
052                    }
053                    catch (Exception e) {
054                            if (e instanceof NoSuchArticleException ||
055                                    e instanceof PrincipalException) {
056    
057                                    SessionErrors.add(actionRequest, e.getClass());
058    
059                                    setForward(actionRequest, "portlet.journal.error");
060                            }
061                            else if (e instanceof DuplicateArticleIdException ||
062                                             e instanceof ArticleIdException) {
063    
064                                    SessionErrors.add(actionRequest, e.getClass());
065                            }
066                            else {
067                                    throw e;
068                            }
069                    }
070            }
071    
072            @Override
073            public ActionForward render(
074                            ActionMapping actionMapping, ActionForm actionForm,
075                            PortletConfig portletConfig, RenderRequest renderRequest,
076                            RenderResponse renderResponse)
077                    throws Exception {
078    
079                    return actionMapping.findForward(
080                            getForward(renderRequest, "portlet.journal.copy_article"));
081            }
082    
083            protected void copyArticle(ActionRequest actionRequest) throws Exception {
084                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
085                    String oldArticleId = ParamUtil.getString(
086                            actionRequest, "oldArticleId");
087                    String newArticleId = ParamUtil.getString(
088                            actionRequest, "newArticleId");
089                    boolean autoArticleId = ParamUtil.getBoolean(
090                            actionRequest, "autoArticleId");
091                    double version = ParamUtil.getDouble(actionRequest, "version");
092    
093                    JournalArticleServiceUtil.copyArticle(
094                            groupId, oldArticleId, newArticleId, autoArticleId, version);
095            }
096    
097    }