001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.journal.action;
016    
017    import com.liferay.portal.kernel.servlet.SessionErrors;
018    import com.liferay.portal.kernel.util.ParamUtil;
019    import com.liferay.portal.security.auth.PrincipalException;
020    import com.liferay.portal.struts.PortletAction;
021    import com.liferay.portlet.journal.DuplicateTemplateIdException;
022    import com.liferay.portlet.journal.NoSuchTemplateException;
023    import com.liferay.portlet.journal.TemplateIdException;
024    import com.liferay.portlet.journal.service.JournalTemplateServiceUtil;
025    
026    import javax.portlet.ActionRequest;
027    import javax.portlet.ActionResponse;
028    import javax.portlet.PortletConfig;
029    import javax.portlet.RenderRequest;
030    import javax.portlet.RenderResponse;
031    
032    import org.apache.struts.action.ActionForm;
033    import org.apache.struts.action.ActionForward;
034    import org.apache.struts.action.ActionMapping;
035    
036    /**
037     * @author Brian Wing Shun Chan
038     */
039    public class CopyTemplateAction extends PortletAction {
040    
041            @Override
042            public void processAction(
043                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
044                            ActionRequest actionRequest, ActionResponse actionResponse)
045                    throws Exception {
046    
047                    try {
048                            copyTemplate(actionRequest);
049    
050                            sendRedirect(actionRequest, actionResponse);
051                    }
052                    catch (Exception e) {
053                            if (e instanceof NoSuchTemplateException ||
054                                    e instanceof PrincipalException) {
055    
056                                    SessionErrors.add(actionRequest, e.getClass().getName());
057    
058                                    setForward(actionRequest, "portlet.journal.error");
059                            }
060                            else if (e instanceof DuplicateTemplateIdException ||
061                                             e instanceof TemplateIdException) {
062    
063                                    SessionErrors.add(actionRequest, e.getClass().getName());
064                            }
065                            else {
066                                    throw e;
067                            }
068                    }
069            }
070    
071            @Override
072            public ActionForward render(
073                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
074                            RenderRequest renderRequest, RenderResponse renderResponse)
075                    throws Exception {
076    
077                    return mapping.findForward(
078                            getForward(renderRequest, "portlet.journal.copy_template"));
079            }
080    
081            protected void copyTemplate(ActionRequest actionRequest) throws Exception {
082                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
083                    String oldTemplateId = ParamUtil.getString(
084                            actionRequest, "oldTemplateId");
085                    String newTemplateId = ParamUtil.getString(
086                            actionRequest, "newTemplateId");
087                    boolean autoTemplateId = ParamUtil.getBoolean(
088                            actionRequest, "autoTemplateId");
089    
090                    JournalTemplateServiceUtil.copyTemplate(
091                            groupId, oldTemplateId, newTemplateId, autoTemplateId);
092            }
093    
094    }