1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.journal.action;
16  
17  import com.liferay.portal.kernel.servlet.SessionErrors;
18  import com.liferay.portal.kernel.util.ParamUtil;
19  import com.liferay.portal.security.auth.PrincipalException;
20  import com.liferay.portal.struts.PortletAction;
21  import com.liferay.portlet.journal.ArticleIdException;
22  import com.liferay.portlet.journal.DuplicateArticleIdException;
23  import com.liferay.portlet.journal.NoSuchArticleException;
24  import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
25  
26  import javax.portlet.ActionRequest;
27  import javax.portlet.ActionResponse;
28  import javax.portlet.PortletConfig;
29  import javax.portlet.RenderRequest;
30  import javax.portlet.RenderResponse;
31  
32  import org.apache.struts.action.ActionForm;
33  import org.apache.struts.action.ActionForward;
34  import org.apache.struts.action.ActionMapping;
35  
36  /**
37   * <a href="CopyArticleAction.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class CopyArticleAction extends PortletAction {
42  
43      public void processAction(
44              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
45              ActionRequest actionRequest, ActionResponse actionResponse)
46          throws Exception {
47  
48          try {
49              copyArticle(actionRequest);
50  
51              sendRedirect(actionRequest, actionResponse);
52          }
53          catch (Exception e) {
54              if (e instanceof NoSuchArticleException ||
55                  e instanceof PrincipalException) {
56  
57                  SessionErrors.add(actionRequest, e.getClass().getName());
58  
59                  setForward(actionRequest, "portlet.journal.error");
60              }
61              else if (e instanceof DuplicateArticleIdException ||
62                       e instanceof ArticleIdException) {
63  
64                  SessionErrors.add(actionRequest, e.getClass().getName());
65              }
66              else {
67                  throw e;
68              }
69          }
70      }
71  
72      public ActionForward render(
73              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
74              RenderRequest renderRequest, RenderResponse renderResponse)
75          throws Exception {
76  
77          return mapping.findForward(
78              getForward(renderRequest, "portlet.journal.copy_article"));
79      }
80  
81      protected void copyArticle(ActionRequest actionRequest) throws Exception {
82          long groupId = ParamUtil.getLong(actionRequest, "groupId");
83          String oldArticleId = ParamUtil.getString(
84              actionRequest, "oldArticleId");
85          String newArticleId = ParamUtil.getString(
86              actionRequest, "newArticleId");
87          boolean autoArticleId = ParamUtil.getBoolean(
88              actionRequest, "autoArticleId");
89          double version = ParamUtil.getDouble(actionRequest, "version");
90  
91          JournalArticleServiceUtil.copyArticle(
92              groupId, oldArticleId, newArticleId, autoArticleId, version);
93      }
94  
95  }