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