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