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