001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 mapping, ActionForm form, PortletConfig portletConfig,
044                            ActionRequest actionRequest, ActionResponse actionResponse)
045                    throws Exception {
046    
047                    try {
048                            copyArticle(actionRequest);
049    
050                            sendRedirect(actionRequest, actionResponse);
051                    }
052                    catch (Exception e) {
053                            if (e instanceof NoSuchArticleException ||
054                                    e instanceof PrincipalException) {
055    
056                                    SessionErrors.add(actionRequest, e.getClass().getName());
057    
058                                    setForward(actionRequest, "portlet.journal.error");
059                            }
060                            else if (e instanceof DuplicateArticleIdException ||
061                                             e instanceof ArticleIdException) {
062    
063                                    SessionErrors.add(actionRequest, e.getClass().getName());
064                            }
065                            else {
066                                    throw e;
067                            }
068                    }
069            }
070    
071            @Override
072            public ActionForward render(
073                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
074                            RenderRequest renderRequest, RenderResponse renderResponse)
075                    throws Exception {
076    
077                    return mapping.findForward(
078                            getForward(renderRequest, "portlet.journal.copy_article"));
079            }
080    
081            protected void copyArticle(ActionRequest actionRequest) throws Exception {
082                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
083                    String oldArticleId = ParamUtil.getString(
084                            actionRequest, "oldArticleId");
085                    String newArticleId = ParamUtil.getString(
086                            actionRequest, "newArticleId");
087                    boolean autoArticleId = ParamUtil.getBoolean(
088                            actionRequest, "autoArticleId");
089                    double version = ParamUtil.getDouble(actionRequest, "version");
090    
091                    JournalArticleServiceUtil.copyArticle(
092                            groupId, oldArticleId, newArticleId, autoArticleId, version);
093            }
094    
095    }