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.NoSuchStructureException;
032    import com.liferay.portlet.dynamicdatamapping.StructureNameException;
033    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
034    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplate;
035    import com.liferay.portlet.dynamicdatamapping.model.DDMTemplateConstants;
036    import com.liferay.portlet.dynamicdatamapping.service.DDMStructureServiceUtil;
037    import com.liferay.portlet.dynamicdatamapping.service.DDMTemplateServiceUtil;
038    
039    import java.util.Locale;
040    import java.util.Map;
041    
042    import javax.portlet.ActionRequest;
043    import javax.portlet.ActionResponse;
044    import javax.portlet.PortletConfig;
045    import javax.portlet.PortletRequest;
046    import javax.portlet.RenderRequest;
047    import javax.portlet.RenderResponse;
048    
049    import org.apache.struts.action.ActionForm;
050    import org.apache.struts.action.ActionForward;
051    import org.apache.struts.action.ActionMapping;
052    
053    /**
054     * @author Marcellus Tavares
055     * @author Eduardo Lundgren
056     */
057    public class CopyStructureAction extends PortletAction {
058    
059            @Override
060            public void processAction(
061                            ActionMapping actionMapping, ActionForm actionForm,
062                            PortletConfig portletConfig, ActionRequest actionRequest,
063                            ActionResponse actionResponse)
064                    throws Exception {
065    
066                    try {
067                            DDMStructure structure = copyStructure(actionRequest);
068    
069                            String redirect = getSaveAndContinueRedirect(
070                                    portletConfig, actionRequest, structure);
071                            String closeRedirect = ParamUtil.getString(
072                                    actionRequest, "closeRedirect");
073    
074                            if (Validator.isNotNull(closeRedirect)) {
075                                    redirect = HttpUtil.setParameter(
076                                            redirect, "closeRedirect", closeRedirect);
077    
078                                    SessionMessages.add(
079                                            actionRequest,
080                                            PortalUtil.getPortletId(actionRequest) +
081                                                    SessionMessages.KEY_SUFFIX_CLOSE_REDIRECT,
082                                            closeRedirect);
083                            }
084    
085                            sendRedirect(actionRequest, actionResponse, redirect);
086                    }
087                    catch (Exception e) {
088                            if (e instanceof NoSuchStructureException ||
089                                    e instanceof PrincipalException) {
090    
091                                    SessionErrors.add(actionRequest, e.getClass());
092    
093                                    setForward(actionRequest, "portlet.dynamic_data_mapping.error");
094                            }
095                            else if (e instanceof StructureNameException) {
096                                    SessionErrors.add(actionRequest, e.getClass(), e);
097                            }
098                            else {
099                                    throw e;
100                            }
101                    }
102            }
103    
104            @Override
105            public ActionForward render(
106                            ActionMapping actionMapping, ActionForm actionForm,
107                            PortletConfig portletConfig, RenderRequest renderRequest,
108                            RenderResponse renderResponse)
109                    throws Exception {
110    
111                    try {
112                            ActionUtil.getStructure(renderRequest);
113                    }
114                    catch (NoSuchStructureException nsse) {
115    
116                            // Let this slide because the user can manually input a structure
117                            // key for a new structure that does not yet exist
118    
119                    }
120                    catch (Exception e) {
121                            if (e instanceof PrincipalException) {
122                                    SessionErrors.add(renderRequest, e.getClass());
123    
124                                    return actionMapping.findForward(
125                                            "portlet.dynamic_data_mapping.error");
126                            }
127                            else {
128                                    throw e;
129                            }
130                    }
131    
132                    return actionMapping.findForward(
133                            getForward(
134                                    renderRequest, "portlet.dynamic_data_mapping.copy_structure"));
135            }
136    
137            protected DDMStructure copyStructure(ActionRequest actionRequest)
138                    throws Exception {
139    
140                    long classPK = ParamUtil.getLong(actionRequest, "classPK");
141    
142                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
143                            actionRequest, "name");
144                    Map<Locale, String> descriptionMap =
145                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
146    
147                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
148                            DDMStructure.class.getName(), actionRequest);
149    
150                    DDMStructure structure = DDMStructureServiceUtil.copyStructure(
151                            classPK, nameMap, descriptionMap, serviceContext);
152    
153                    copyTemplates(actionRequest, classPK, structure.getStructureId());
154    
155                    return structure;
156            }
157    
158            protected void copyTemplates(
159                            ActionRequest actionRequest, long oldClassPK, long newClassPK)
160                    throws Exception {
161    
162                    long classNameId = PortalUtil.getClassNameId(DDMStructure.class);
163    
164                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
165                            DDMTemplate.class.getName(), actionRequest);
166    
167                    boolean copyDisplayTemplates = ParamUtil.getBoolean(
168                            actionRequest, "copyDisplayTemplates");
169    
170                    if (copyDisplayTemplates) {
171                            DDMTemplateServiceUtil.copyTemplates(
172                                    classNameId, oldClassPK, newClassPK,
173                                    DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY, serviceContext);
174                    }
175    
176                    boolean copyFormTemplates = ParamUtil.getBoolean(
177                            actionRequest, "copyFormTemplates");
178    
179                    if (copyFormTemplates) {
180                            DDMTemplateServiceUtil.copyTemplates(
181                                    classNameId, oldClassPK, newClassPK,
182                                    DDMTemplateConstants.TEMPLATE_TYPE_FORM, serviceContext);
183                    }
184            }
185    
186            protected String getSaveAndContinueRedirect(
187                            PortletConfig portletConfig, ActionRequest actionRequest,
188                            DDMStructure structure)
189                    throws Exception {
190    
191                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
192                            WebKeys.THEME_DISPLAY);
193    
194                    PortletURLImpl portletURL = new PortletURLImpl(
195                            actionRequest, portletConfig.getPortletName(),
196                            themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
197    
198                    portletURL.setParameter(
199                            "struts_action", "/dynamic_data_mapping/copy_structure");
200    
201                    long classNameId = PortalUtil.getClassNameId(DDMStructure.class);
202    
203                    portletURL.setParameter(
204                            "classNameId", String.valueOf(classNameId), false);
205    
206                    portletURL.setParameter(
207                            "classPK", String.valueOf(structure.getStructureId()), false);
208                    portletURL.setParameter(
209                            "copyFormTemplates",
210                            ParamUtil.getString(actionRequest, "copyFormTemplates"), false);
211                    portletURL.setParameter(
212                            "copyDisplayTemplates",
213                            ParamUtil.getString(actionRequest, "copyDisplayTemplates"), false);
214                    portletURL.setWindowState(actionRequest.getWindowState());
215    
216                    return portletURL.toString();
217            }
218    
219    }