1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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  }