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