001    /**
002     * Copyright (c) 2000-present 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.exportimport.action;
016    
017    import com.liferay.portal.NoSuchGroupException;
018    import com.liferay.portal.kernel.language.LanguageUtil;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.servlet.SessionErrors;
022    import com.liferay.portal.kernel.util.Constants;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.StringPool;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.kernel.workflow.WorkflowConstants;
028    import com.liferay.portal.model.Layout;
029    import com.liferay.portal.security.auth.PrincipalException;
030    import com.liferay.portal.service.LayoutLocalServiceUtil;
031    import com.liferay.portal.service.ServiceContext;
032    import com.liferay.portal.struts.PortletAction;
033    import com.liferay.portal.theme.ThemeDisplay;
034    import com.liferay.portal.util.WebKeys;
035    import com.liferay.portlet.exportimport.LARFileNameException;
036    import com.liferay.portlet.exportimport.configuration.ExportImportConfigurationConstants;
037    import com.liferay.portlet.exportimport.configuration.ExportImportConfigurationSettingsMapFactory;
038    import com.liferay.portlet.exportimport.lar.ExportImportHelperUtil;
039    import com.liferay.portlet.exportimport.model.ExportImportConfiguration;
040    import com.liferay.portlet.exportimport.service.ExportImportConfigurationLocalServiceUtil;
041    import com.liferay.portlet.exportimport.service.ExportImportServiceUtil;
042    import com.liferay.portlet.sites.action.ActionUtil;
043    
044    import java.io.Serializable;
045    
046    import java.util.ArrayList;
047    import java.util.LinkedHashSet;
048    import java.util.Map;
049    import java.util.Set;
050    
051    import javax.portlet.ActionRequest;
052    import javax.portlet.ActionResponse;
053    import javax.portlet.PortletConfig;
054    import javax.portlet.PortletContext;
055    import javax.portlet.PortletRequest;
056    import javax.portlet.PortletRequestDispatcher;
057    import javax.portlet.RenderRequest;
058    import javax.portlet.RenderResponse;
059    import javax.portlet.ResourceRequest;
060    import javax.portlet.ResourceResponse;
061    
062    import org.apache.struts.action.ActionForm;
063    import org.apache.struts.action.ActionForward;
064    import org.apache.struts.action.ActionMapping;
065    
066    /**
067     * @author Alexander Chow
068     * @author Raymond Aug??
069     */
070    public class ExportLayoutsAction extends PortletAction {
071    
072            @Override
073            public void processAction(
074                            ActionMapping actionMapping, ActionForm actionForm,
075                            PortletConfig portletConfig, ActionRequest actionRequest,
076                            ActionResponse actionResponse)
077                    throws Exception {
078    
079                    hideDefaultSuccessMessage(actionRequest);
080    
081                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
082    
083                    if (Validator.isNull(cmd)) {
084                            return;
085                    }
086    
087                    try {
088                            ThemeDisplay themeDisplay =
089                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
090    
091                            long groupId = ParamUtil.getLong(actionRequest, "liveGroupId");
092                            boolean privateLayout = ParamUtil.getBoolean(
093                                    actionRequest, "privateLayout");
094                            long[] layoutIds = getLayoutIds(actionRequest);
095    
096                            String taskName = StringPool.BLANK;
097    
098                            if (privateLayout) {
099                                    taskName = LanguageUtil.get(
100                                            actionRequest.getLocale(), "private-pages");
101                            }
102                            else {
103                                    taskName = LanguageUtil.get(
104                                            actionRequest.getLocale(), "public-pages");
105                            }
106    
107                            Map<String, Serializable> settingsMap =
108                                    ExportImportConfigurationSettingsMapFactory.buildSettingsMap(
109                                            themeDisplay.getUserId(), groupId, privateLayout, layoutIds,
110                                            actionRequest.getParameterMap(), themeDisplay.getLocale(),
111                                            themeDisplay.getTimeZone());
112    
113                            ServiceContext serviceContext = new ServiceContext();
114    
115                            ExportImportConfiguration exportImportConfiguration =
116                                    ExportImportConfigurationLocalServiceUtil.
117                                            addExportImportConfiguration(
118                                                    themeDisplay.getUserId(), groupId, taskName,
119                                                    StringPool.BLANK,
120                                                    ExportImportConfigurationConstants.TYPE_EXPORT_LAYOUT,
121                                                    settingsMap, WorkflowConstants.STATUS_DRAFT,
122                                                    serviceContext);
123    
124                            ExportImportServiceUtil.exportLayoutsAsFileInBackground(
125                                    exportImportConfiguration);
126    
127                            String redirect = ParamUtil.getString(actionRequest, "redirect");
128    
129                            sendRedirect(actionRequest, actionResponse, redirect);
130                    }
131                    catch (Exception e) {
132                            SessionErrors.add(actionRequest, e.getClass());
133    
134                            if (!(e instanceof LARFileNameException)) {
135                                    _log.error(e, e);
136    
137                                    sendRedirect(actionRequest, actionResponse);
138                            }
139                    }
140            }
141    
142            @Override
143            public ActionForward render(
144                            ActionMapping actionMapping, ActionForm actionForm,
145                            PortletConfig portletConfig, RenderRequest renderRequest,
146                            RenderResponse renderResponse)
147                    throws Exception {
148    
149                    try {
150                            ActionUtil.getGroup(renderRequest);
151                    }
152                    catch (Exception e) {
153                            if (e instanceof NoSuchGroupException ||
154                                    e instanceof PrincipalException) {
155    
156                                    SessionErrors.add(renderRequest, e.getClass());
157    
158                                    return actionMapping.findForward("portlet.export_import.error");
159                            }
160                            else {
161                                    throw e;
162                            }
163                    }
164    
165                    return actionMapping.findForward(
166                            getForward(renderRequest, "portlet.export_import.export_layouts"));
167            }
168    
169            @Override
170            public void serveResource(
171                            ActionMapping actionMapping, ActionForm actionForm,
172                            PortletConfig portletConfig, ResourceRequest resourceRequest,
173                            ResourceResponse resourceResponse)
174                    throws Exception {
175    
176                    PortletContext portletContext = portletConfig.getPortletContext();
177    
178                    PortletRequestDispatcher portletRequestDispatcher =
179                            portletContext.getRequestDispatcher(
180                                    "/html/portlet/export_import/export_layouts_processes.jsp");
181    
182                    portletRequestDispatcher.include(resourceRequest, resourceResponse);
183            }
184    
185            protected long[] getLayoutIds(PortletRequest portletRequest)
186                    throws Exception {
187    
188                    Set<Layout> layouts = new LinkedHashSet<>();
189    
190                    Map<Long, Boolean> layoutIdMap = ExportImportHelperUtil.getLayoutIdMap(
191                            portletRequest);
192    
193                    for (Map.Entry<Long, Boolean> entry : layoutIdMap.entrySet()) {
194                            long plid = GetterUtil.getLong(String.valueOf(entry.getKey()));
195                            boolean includeChildren = entry.getValue();
196    
197                            Layout layout = LayoutLocalServiceUtil.getLayout(plid);
198    
199                            if (!layouts.contains(layout)) {
200                                    layouts.add(layout);
201                            }
202    
203                            if (includeChildren) {
204                                    layouts.addAll(layout.getAllChildren());
205                            }
206                    }
207    
208                    return ExportImportHelperUtil.getLayoutIds(
209                            new ArrayList<Layout>(layouts));
210            }
211    
212            private static final Log _log = LogFactoryUtil.getLog(
213                    ExportLayoutsAction.class);
214    
215    }