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