001    /**
002     * Copyright (c) 2000-2013 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            @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    }