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.DuplicateTemplateIdException;
22  import com.liferay.portlet.journal.NoSuchTemplateException;
23  import com.liferay.portlet.journal.TemplateIdException;
24  import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
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="CopyTemplateAction.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   */
41  public class CopyTemplateAction 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              copyTemplate(actionRequest);
50  
51              sendRedirect(actionRequest, actionResponse);
52          }
53          catch (Exception e) {
54              if (e instanceof NoSuchTemplateException ||
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 DuplicateTemplateIdException ||
62                       e instanceof TemplateIdException) {
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_template"));
79      }
80  
81      protected void copyTemplate(ActionRequest actionRequest) throws Exception {
82          long groupId = ParamUtil.getLong(actionRequest, "groupId");
83          String oldTemplateId = ParamUtil.getString(
84              actionRequest, "oldTemplateId");
85          String newTemplateId = ParamUtil.getString(
86              actionRequest, "newTemplateId");
87          boolean autoTemplateId = ParamUtil.getBoolean(
88              actionRequest, "autoTemplateId");
89  
90          JournalTemplateServiceUtil.copyTemplate(
91              groupId, oldTemplateId, newTemplateId, autoTemplateId);
92      }
93  
94  }