001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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            public void processAction(
042                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
043                            ActionRequest actionRequest, ActionResponse actionResponse)
044                    throws Exception {
045    
046                    try {
047                            copyTemplate(actionRequest);
048    
049                            sendRedirect(actionRequest, actionResponse);
050                    }
051                    catch (Exception e) {
052                            if (e instanceof NoSuchTemplateException ||
053                                    e instanceof PrincipalException) {
054    
055                                    SessionErrors.add(actionRequest, e.getClass().getName());
056    
057                                    setForward(actionRequest, "portlet.journal.error");
058                            }
059                            else if (e instanceof DuplicateTemplateIdException ||
060                                             e instanceof TemplateIdException) {
061    
062                                    SessionErrors.add(actionRequest, e.getClass().getName());
063                            }
064                            else {
065                                    throw e;
066                            }
067                    }
068            }
069    
070            public ActionForward render(
071                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
072                            RenderRequest renderRequest, RenderResponse renderResponse)
073                    throws Exception {
074    
075                    return mapping.findForward(
076                            getForward(renderRequest, "portlet.journal.copy_template"));
077            }
078    
079            protected void copyTemplate(ActionRequest actionRequest) throws Exception {
080                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
081                    String oldTemplateId = ParamUtil.getString(
082                            actionRequest, "oldTemplateId");
083                    String newTemplateId = ParamUtil.getString(
084                            actionRequest, "newTemplateId");
085                    boolean autoTemplateId = ParamUtil.getBoolean(
086                            actionRequest, "autoTemplateId");
087    
088                    JournalTemplateServiceUtil.copyTemplate(
089                            groupId, oldTemplateId, newTemplateId, autoTemplateId);
090            }
091    
092    }