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