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.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 mapping, ActionForm form, PortletConfig portletConfig,
062                            ActionRequest actionRequest, ActionResponse actionResponse)
063                    throws Exception {
064    
065                    try {
066                            DDMStructure structure = copyStructure(actionRequest);
067    
068                            String redirect = getSaveAndContinueRedirect(
069                                    portletConfig, actionRequest, structure);
070    
071                            String closeRedirect = ParamUtil.getString(
072                                    actionRequest, "closeRedirect");
073    
074                            if (Validator.isNotNull(closeRedirect)) {
075                                    LiferayPortletConfig liferayPortletConfig =
076                                            (LiferayPortletConfig)portletConfig;
077    
078                                    SessionMessages.add(
079                                            actionRequest,
080                                            liferayPortletConfig.getPortletId() +
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 mapping, ActionForm form, PortletConfig portletConfig,
107                            RenderRequest renderRequest, RenderResponse renderResponse)
108                    throws Exception {
109    
110                    try {
111                            ActionUtil.getStructure(renderRequest);
112                    }
113                    catch (NoSuchStructureException nsse) {
114    
115                            // Let this slide because the user can manually input a structure
116                            // key for a new structure 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 mapping.findForward(
124                                            "portlet.dynamic_data_mapping.error");
125                            }
126                            else {
127                                    throw e;
128                            }
129                    }
130    
131                    return mapping.findForward(
132                            getForward(
133                                    renderRequest, "portlet.dynamic_data_mapping.copy_structure"));
134            }
135    
136            protected DDMStructure copyStructure(ActionRequest actionRequest)
137                    throws Exception {
138    
139                    long classPK = ParamUtil.getLong(actionRequest, "classPK");
140    
141                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
142                            actionRequest, "name");
143    
144                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
145                            DDMStructure.class.getName(), actionRequest);
146    
147                    DDMStructure structure = DDMStructureServiceUtil.copyStructure(
148                            classPK, nameMap, null, serviceContext);
149    
150                    copyTemplates(actionRequest, classPK, structure.getStructureId());
151    
152                    return structure;
153            }
154    
155            protected void copyTemplates(
156                            ActionRequest actionRequest, long structureId, long newStructureId)
157                    throws Exception {
158    
159                    long classNameId = PortalUtil.getClassNameId(DDMStructure.class);
160    
161                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
162                            DDMTemplate.class.getName(), actionRequest);
163    
164                    boolean copyDisplayTemplates = ParamUtil.getBoolean(
165                            actionRequest, "copyDisplayTemplates");
166    
167                    if (copyDisplayTemplates) {
168                            DDMTemplateServiceUtil.copyTemplates(
169                                    classNameId, structureId, newStructureId,
170                                    DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY, serviceContext);
171                    }
172    
173                    boolean copyFormTemplates = ParamUtil.getBoolean(
174                            actionRequest, "copyFormTemplates");
175    
176                    if (copyFormTemplates) {
177                            DDMTemplateServiceUtil.copyTemplates(
178                                    classNameId, structureId, newStructureId,
179                                    DDMTemplateConstants.TEMPLATE_TYPE_FORM, serviceContext);
180                    }
181            }
182    
183            protected String getSaveAndContinueRedirect(
184                            PortletConfig portletConfig, ActionRequest actionRequest,
185                            DDMStructure structure)
186                    throws Exception {
187    
188                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
189                            WebKeys.THEME_DISPLAY);
190    
191                    PortletURLImpl portletURL = new PortletURLImpl(
192                            actionRequest, portletConfig.getPortletName(),
193                            themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
194    
195                    portletURL.setWindowState(actionRequest.getWindowState());
196    
197                    portletURL.setParameter(
198                            "struts_action", "/dynamic_data_mapping/copy_structure");
199    
200                    long classNameId = PortalUtil.getClassNameId(DDMStructure.class);
201    
202                    portletURL.setParameter(
203                            "classNamId", String.valueOf(classNameId), false);
204    
205                    portletURL.setParameter(
206                            "classPK", String.valueOf(structure.getStructureId()), false);
207                    portletURL.setParameter(
208                            "copyDetailTemplates",
209                            ParamUtil.getString(actionRequest, "copyDetailTemplates"), false);
210                    portletURL.setParameter(
211                            "copyListTemplates",
212                            ParamUtil.getString(actionRequest, "copyListTemplates"), false);
213    
214                    return portletURL.toString();
215            }
216    
217    }