001    /**
002     * Copyright (c) 2000-2013 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 actionMapping, ActionForm actionForm,
044                            PortletConfig portletConfig, ActionRequest actionRequest,
045                            ActionResponse actionResponse)
046                    throws Exception {
047    
048                    try {
049                            copyTemplate(actionRequest);
050    
051                            sendRedirect(actionRequest, actionResponse);
052                    }
053                    catch (Exception e) {
054                            if (e instanceof NoSuchTemplateException ||
055                                    e instanceof PrincipalException) {
056    
057                                    SessionErrors.add(actionRequest, e.getClass());
058    
059                                    setForward(actionRequest, "portlet.journal.error");
060                            }
061                            else if (e instanceof DuplicateTemplateIdException ||
062                                             e instanceof TemplateIdException) {
063    
064                                    SessionErrors.add(actionRequest, e.getClass());
065                            }
066                            else {
067                                    throw e;
068                            }
069                    }
070            }
071    
072            @Override
073            public ActionForward render(
074                            ActionMapping actionMapping, ActionForm actionForm,
075                            PortletConfig portletConfig, RenderRequest renderRequest,
076                            RenderResponse renderResponse)
077                    throws Exception {
078    
079                    return actionMapping.findForward(
080                            getForward(renderRequest, "portlet.journal.copy_template"));
081            }
082    
083            protected void copyTemplate(ActionRequest actionRequest) throws Exception {
084                    long groupId = ParamUtil.getLong(actionRequest, "groupId");
085                    String oldTemplateId = ParamUtil.getString(
086                            actionRequest, "oldTemplateId");
087                    String newTemplateId = ParamUtil.getString(
088                            actionRequest, "newTemplateId");
089                    boolean autoTemplateId = ParamUtil.getBoolean(
090                            actionRequest, "autoTemplateId");
091    
092                    JournalTemplateServiceUtil.copyTemplate(
093                            groupId, oldTemplateId, newTemplateId, autoTemplateId);
094            }
095    
096    }