1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.journal.action;
21  
22  import com.liferay.portal.kernel.servlet.SessionErrors;
23  import com.liferay.portal.kernel.util.ParamUtil;
24  import com.liferay.portal.security.auth.PrincipalException;
25  import com.liferay.portal.struts.PortletAction;
26  import com.liferay.portlet.journal.DuplicateTemplateIdException;
27  import com.liferay.portlet.journal.NoSuchTemplateException;
28  import com.liferay.portlet.journal.TemplateIdException;
29  import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
30  
31  import javax.portlet.ActionRequest;
32  import javax.portlet.ActionResponse;
33  import javax.portlet.PortletConfig;
34  import javax.portlet.RenderRequest;
35  import javax.portlet.RenderResponse;
36  
37  import org.apache.struts.action.ActionForm;
38  import org.apache.struts.action.ActionForward;
39  import org.apache.struts.action.ActionMapping;
40  
41  /**
42   * <a href="CopyTemplateAction.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class CopyTemplateAction extends PortletAction {
48  
49      public void processAction(
50              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
51              ActionRequest actionRequest, ActionResponse actionResponse)
52          throws Exception {
53  
54          try {
55              copyTemplate(actionRequest);
56  
57              sendRedirect(actionRequest, actionResponse);
58          }
59          catch (Exception e) {
60              if (e instanceof NoSuchTemplateException ||
61                  e instanceof PrincipalException) {
62  
63                  SessionErrors.add(actionRequest, e.getClass().getName());
64  
65                  setForward(actionRequest, "portlet.journal.error");
66              }
67              else if (e instanceof DuplicateTemplateIdException ||
68                       e instanceof TemplateIdException) {
69  
70                  SessionErrors.add(actionRequest, e.getClass().getName());
71              }
72              else {
73                  throw e;
74              }
75          }
76      }
77  
78      public ActionForward render(
79              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
80              RenderRequest renderRequest, RenderResponse renderResponse)
81          throws Exception {
82  
83          return mapping.findForward(
84              getForward(renderRequest, "portlet.journal.copy_template"));
85      }
86  
87      protected void copyTemplate(ActionRequest actionRequest) throws Exception {
88          long groupId = ParamUtil.getLong(actionRequest, "groupId");
89          String oldTemplateId = ParamUtil.getString(
90              actionRequest, "oldTemplateId");
91          String newTemplateId = ParamUtil.getString(
92              actionRequest, "newTemplateId");
93          boolean autoTemplateId = ParamUtil.getBoolean(
94              actionRequest, "autoTemplateId");
95  
96          JournalTemplateServiceUtil.copyTemplate(
97              groupId, oldTemplateId, newTemplateId, autoTemplateId);
98      }
99  
100 }