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.dynamicdatamapping.action;
016    
017    import com.liferay.portal.kernel.portlet.LiferayPortletConfig;
018    import com.liferay.portal.kernel.servlet.SessionErrors;
019    import com.liferay.portal.kernel.servlet.SessionMessages;
020    import com.liferay.portal.kernel.util.HttpUtil;
021    import com.liferay.portal.kernel.util.LocalizationUtil;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.security.auth.PrincipalException;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.service.ServiceContextFactory;
027    import com.liferay.portal.struts.PortletAction;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.WebKeys;
030    import com.liferay.portlet.PortletURLImpl;
031    import com.liferay.portlet.dynamicdatamapping.NoSuchTemplateException;
032    import com.liferay.portlet.dynamicdatamapping.TemplateNameException;
033    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
034    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateServiceUtil;
035    
036    import java.util.Locale;
037    import java.util.Map;
038    
039    import javax.portlet.ActionRequest;
040    import javax.portlet.ActionResponse;
041    import javax.portlet.PortletConfig;
042    import javax.portlet.PortletRequest;
043    import javax.portlet.RenderRequest;
044    import javax.portlet.RenderResponse;
045    
046    import org.apache.struts.action.ActionForm;
047    import org.apache.struts.action.ActionForward;
048    import org.apache.struts.action.ActionMapping;
049    
050    /**
051     * @author Marcellus Tavares
052     */
053    public class CopyTemplateAction extends PortletAction {
054    
055            @Override
056            public void processAction(
057                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
058                            ActionRequest actionRequest, ActionResponse actionResponse)
059                    throws Exception {
060    
061                    try {
062                            DDMTemplate template = copyTemplate(actionRequest);
063    
064                            String redirect = getSaveAndContinueRedirect(
065                                    portletConfig, actionRequest, template);
066                            String closeRedirect = ParamUtil.getString(
067                                    actionRequest, "closeRedirect");
068    
069                            if (Validator.isNotNull(closeRedirect)) {
070                                    redirect = HttpUtil.setParameter(
071                                            redirect, "closeRedirect", closeRedirect);
072    
073                                    LiferayPortletConfig liferayPortletConfig =
074                                            (LiferayPortletConfig)portletConfig;
075    
076                                    SessionMessages.add(
077                                            actionRequest,
078                                            liferayPortletConfig.getPortletId() +
079                                                    SessionMessages.KEY_SUFFIX_CLOSE_REDIRECT,
080                                            closeRedirect);
081                            }
082    
083                            sendRedirect(actionRequest, actionResponse, redirect);
084                    }
085                    catch (Exception e) {
086                            if (e instanceof NoSuchTemplateException ||
087                                    e instanceof PrincipalException) {
088    
089                                    SessionErrors.add(actionRequest, e.getClass());
090    
091                                    setForward(actionRequest, "portlet.dynamic_data_mapping.error");
092                            }
093                            else if (e instanceof TemplateNameException) {
094                                    SessionErrors.add(actionRequest, e.getClass(), e);
095                            }
096                            else {
097                                    throw e;
098                            }
099                    }
100            }
101    
102            @Override
103            public ActionForward render(
104                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
105                            RenderRequest renderRequest, RenderResponse renderResponse)
106                    throws Exception {
107    
108                    try {
109                            ActionUtil.getTemplate(renderRequest);
110                    }
111                    catch (NoSuchTemplateException nste) {
112    
113                            // Let this slide because the user can manually input a template
114                            // key for a new template that does not yet exist
115    
116                    }
117                    catch (Exception e) {
118                            if (e instanceof PrincipalException) {
119                                    SessionErrors.add(renderRequest, e.getClass());
120    
121                                    return mapping.findForward(
122                                            "portlet.dynamic_data_mapping.error");
123                            }
124                            else {
125                                    throw e;
126                            }
127                    }
128    
129                    return mapping.findForward(
130                            getForward(
131                                    renderRequest, "portlet.dynamic_data_mapping.copy_template"));
132            }
133    
134            protected DDMTemplate copyTemplate(ActionRequest actionRequest)
135                    throws Exception {
136    
137                    long templateId = ParamUtil.getLong(actionRequest, "templateId");
138    
139                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
140                            actionRequest, "name");
141    
142                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
143                            DDMTemplate.class.getName(), actionRequest);
144    
145                    return DDMTemplateServiceUtil.copyTemplate(
146                            templateId, nameMap, null, serviceContext);
147            }
148    
149            protected String getSaveAndContinueRedirect(
150                            PortletConfig portletConfig, ActionRequest actionRequest,
151                            DDMTemplate template)
152                    throws Exception {
153    
154                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
155                            WebKeys.THEME_DISPLAY);
156    
157                    PortletURLImpl portletURL = new PortletURLImpl(
158                            actionRequest, portletConfig.getPortletName(),
159                            themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
160    
161                    portletURL.setParameter(
162                            "struts_action", "/dynamic_data_mapping/copy_template");
163                    portletURL.setParameter(
164                            "templateId", String.valueOf(template.getTemplateId()), false);
165                    portletURL.setWindowState(actionRequest.getWindowState());
166    
167                    return portletURL.toString();
168            }
169    
170    }