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.model.Layout;
20  import com.liferay.portal.security.auth.PrincipalException;
21  import com.liferay.portal.struts.PortletAction;
22  import com.liferay.portal.util.WebKeys;
23  import com.liferay.portlet.journal.ArticleIdException;
24  import com.liferay.portlet.journal.DuplicateArticleIdException;
25  import com.liferay.portlet.journal.NoSuchArticleException;
26  import com.liferay.portlet.journal.service.JournalArticleServiceUtil;
27  
28  import javax.portlet.ActionRequest;
29  import javax.portlet.ActionResponse;
30  import javax.portlet.PortletConfig;
31  import javax.portlet.RenderRequest;
32  import javax.portlet.RenderResponse;
33  
34  import org.apache.struts.action.ActionForm;
35  import org.apache.struts.action.ActionForward;
36  import org.apache.struts.action.ActionMapping;
37  
38  /**
39   * <a href="CopyArticleAction.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   */
43  public class CopyArticleAction extends PortletAction {
44  
45      public void processAction(
46              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
47              ActionRequest actionRequest, ActionResponse actionResponse)
48          throws Exception {
49  
50          try {
51              copyArticle(actionRequest);
52  
53              sendRedirect(actionRequest, actionResponse);
54          }
55          catch (Exception e) {
56              if (e instanceof NoSuchArticleException ||
57                  e instanceof PrincipalException) {
58  
59                  SessionErrors.add(actionRequest, e.getClass().getName());
60  
61                  setForward(actionRequest, "portlet.journal.error");
62              }
63              else if (e instanceof DuplicateArticleIdException ||
64                       e instanceof ArticleIdException) {
65  
66                  SessionErrors.add(actionRequest, e.getClass().getName());
67              }
68              else {
69                  throw e;
70              }
71          }
72      }
73  
74      public ActionForward render(
75              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
76              RenderRequest renderRequest, RenderResponse renderResponse)
77          throws Exception {
78  
79          return mapping.findForward(
80              getForward(renderRequest, "portlet.journal.copy_article"));
81      }
82  
83      protected void copyArticle(ActionRequest actionRequest) throws Exception {
84          Layout layout = (Layout)actionRequest.getAttribute(WebKeys.LAYOUT);
85  
86          long groupId = ParamUtil.getLong(actionRequest, "groupId");
87          String oldArticleId = ParamUtil.getString(
88              actionRequest, "oldArticleId");
89          String newArticleId = ParamUtil.getString(
90              actionRequest, "newArticleId");
91          boolean autoArticleId = ParamUtil.getBoolean(
92              actionRequest, "autoArticleId");
93          double version = ParamUtil.getDouble(actionRequest, "version");
94  
95          JournalArticleServiceUtil.copyArticle(
96              groupId, oldArticleId, newArticleId, autoArticleId, version,
97              layout.getPlid());
98      }
99  
100 }