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 mapping, ActionForm form, PortletConfig portletConfig,
063                            ActionRequest actionRequest, 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                                    LiferayPortletConfig liferayPortletConfig =
079                                            (LiferayPortletConfig)portletConfig;
080    
081                                    SessionMessages.add(
082                                            actionRequest,
083                                            liferayPortletConfig.getPortletId() +
084                                                    SessionMessages.KEY_SUFFIX_CLOSE_REDIRECT,
085                                            closeRedirect);
086                            }
087    
088                            sendRedirect(actionRequest, actionResponse, redirect);
089                    }
090                    catch (Exception e) {
091                            if (e instanceof NoSuchStructureException ||
092                                    e instanceof PrincipalException) {
093    
094                                    SessionErrors.add(actionRequest, e.getClass());
095    
096                                    setForward(actionRequest, "portlet.dynamic_data_mapping.error");
097                            }
098                            else if (e instanceof StructureNameException) {
099                                    SessionErrors.add(actionRequest, e.getClass(), e);
100                            }
101                            else {
102                                    throw e;
103                            }
104                    }
105            }
106    
107            @Override
108            public ActionForward render(
109                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
110                            RenderRequest renderRequest, RenderResponse renderResponse)
111                    throws Exception {
112    
113                    try {
114                            ActionUtil.getStructure(renderRequest);
115                    }
116                    catch (NoSuchStructureException nsse) {
117    
118                            // Let this slide because the user can manually input a structure
119                            // key for a new structure that does not yet exist
120    
121                    }
122                    catch (Exception e) {
123                            if (e instanceof PrincipalException) {
124                                    SessionErrors.add(renderRequest, e.getClass());
125    
126                                    return mapping.findForward(
127                                            "portlet.dynamic_data_mapping.error");
128                            }
129                            else {
130                                    throw e;
131                            }
132                    }
133    
134                    return mapping.findForward(
135                            getForward(
136                                    renderRequest, "portlet.dynamic_data_mapping.copy_structure"));
137            }
138    
139            protected DDMStructure copyStructure(ActionRequest actionRequest)
140                    throws Exception {
141    
142                    long classPK = ParamUtil.getLong(actionRequest, "classPK");
143    
144                    Map<Locale, String> nameMap = LocalizationUtil.getLocalizationMap(
145                            actionRequest, "name");
146                    Map<Locale, String> descriptionMap =
147                            LocalizationUtil.getLocalizationMap(actionRequest, "description");
148    
149                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
150                            DDMStructure.class.getName(), actionRequest);
151    
152                    DDMStructure structure = DDMStructureServiceUtil.copyStructure(
153                            classPK, nameMap, descriptionMap, serviceContext);
154    
155                    copyTemplates(actionRequest, classPK, structure.getStructureId());
156    
157                    return structure;
158            }
159    
160            protected void copyTemplates(
161                            ActionRequest actionRequest, long structureId, long newStructureId)
162                    throws Exception {
163    
164                    long classNameId = PortalUtil.getClassNameId(DDMStructure.class);
165    
166                    ServiceContext serviceContext = ServiceContextFactory.getInstance(
167                            DDMTemplate.class.getName(), actionRequest);
168    
169                    boolean copyDisplayTemplates = ParamUtil.getBoolean(
170                            actionRequest, "copyDisplayTemplates");
171    
172                    if (copyDisplayTemplates) {
173                            DDMTemplateServiceUtil.copyTemplates(
174                                    classNameId, structureId, newStructureId,
175                                    DDMTemplateConstants.TEMPLATE_TYPE_DISPLAY, serviceContext);
176                    }
177    
178                    boolean copyFormTemplates = ParamUtil.getBoolean(
179                            actionRequest, "copyFormTemplates");
180    
181                    if (copyFormTemplates) {
182                            DDMTemplateServiceUtil.copyTemplates(
183                                    classNameId, structureId, newStructureId,
184                                    DDMTemplateConstants.TEMPLATE_TYPE_FORM, serviceContext);
185                    }
186            }
187    
188            protected String getSaveAndContinueRedirect(
189                            PortletConfig portletConfig, ActionRequest actionRequest,
190                            DDMStructure structure)
191                    throws Exception {
192    
193                    ThemeDisplay themeDisplay = (ThemeDisplay)actionRequest.getAttribute(
194                            WebKeys.THEME_DISPLAY);
195    
196                    PortletURLImpl portletURL = new PortletURLImpl(
197                            actionRequest, portletConfig.getPortletName(),
198                            themeDisplay.getPlid(), PortletRequest.RENDER_PHASE);
199    
200                    portletURL.setParameter(
201                            "struts_action", "/dynamic_data_mapping/copy_structure");
202    
203                    long classNameId = PortalUtil.getClassNameId(DDMStructure.class);
204    
205                    portletURL.setParameter(
206                            "classNameId", String.valueOf(classNameId), false);
207    
208                    portletURL.setParameter(
209                            "classPK", String.valueOf(structure.getStructureId()), false);
210                    portletURL.setParameter(
211                            "copyDetailTemplates",
212                            ParamUtil.getString(actionRequest, "copyDetailTemplates"), false);
213                    portletURL.setParameter(
214                            "copyListTemplates",
215                            ParamUtil.getString(actionRequest, "copyListTemplates"), false);
216                    portletURL.setWindowState(actionRequest.getWindowState());
217    
218                    return portletURL.toString();
219            }
220    
221    }