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.json.JSONArray;
019    import com.liferay.portal.kernel.json.JSONFactoryUtil;
020    import com.liferay.portal.kernel.json.JSONObject;
021    import com.liferay.portal.kernel.lar.ExportImportHelperUtil;
022    import com.liferay.portal.kernel.log.Log;
023    import com.liferay.portal.kernel.log.LogFactoryUtil;
024    import com.liferay.portal.kernel.servlet.SessionErrors;
025    import com.liferay.portal.kernel.util.ArrayUtil;
026    import com.liferay.portal.kernel.util.Constants;
027    import com.liferay.portal.kernel.util.DateRange;
028    import com.liferay.portal.kernel.util.ParamUtil;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.model.Layout;
031    import com.liferay.portal.security.auth.PrincipalException;
032    import com.liferay.portal.service.LayoutLocalServiceUtil;
033    import com.liferay.portal.service.LayoutServiceUtil;
034    import com.liferay.portal.struts.PortletAction;
035    import com.liferay.portlet.sites.action.ActionUtil;
036    
037    import java.util.ArrayList;
038    import java.util.Date;
039    import java.util.List;
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.PortletRequestDispatcher;
046    import javax.portlet.RenderRequest;
047    import javax.portlet.RenderResponse;
048    import javax.portlet.ResourceRequest;
049    import javax.portlet.ResourceResponse;
050    
051    import org.apache.struts.action.ActionForm;
052    import org.apache.struts.action.ActionForward;
053    import org.apache.struts.action.ActionMapping;
054    
055    /**
056     * @author Alexander Chow
057     * @author Raymond Aug??
058     */
059    public class ExportLayoutsAction extends PortletAction {
060    
061            @Override
062            public void processAction(
063                            ActionMapping actionMapping, ActionForm actionForm,
064                            PortletConfig portletConfig, ActionRequest actionRequest,
065                            ActionResponse actionResponse)
066                    throws Exception {
067    
068                    String cmd = ParamUtil.getString(actionRequest, Constants.CMD);
069    
070                    try {
071                            long groupId = ParamUtil.getLong(actionRequest, "groupId");
072                            boolean privateLayout = ParamUtil.getBoolean(
073                                    actionRequest, "privateLayout");
074                            long[] layoutIds = getLayoutIds(
075                                    groupId, privateLayout,
076                                    ParamUtil.getString(actionRequest, "layoutIds"));
077                            String fileName = ParamUtil.getString(
078                                    actionRequest, "exportFileName");
079    
080                            DateRange dateRange = ExportImportHelperUtil.getDateRange(
081                                    actionRequest, groupId, privateLayout, 0, null);
082    
083                            Date startDate = dateRange.getStartDate();
084                            Date endDate = dateRange.getEndDate();
085    
086                            if (Validator.isNotNull(cmd)) {
087                                    LayoutServiceUtil.exportLayoutsAsFileInBackground(
088                                            fileName, groupId, privateLayout, layoutIds,
089                                            actionRequest.getParameterMap(), startDate, endDate,
090                                            fileName);
091    
092                                    String redirect = ParamUtil.getString(
093                                            actionRequest, "redirect");
094    
095                                    sendRedirect(actionRequest, actionResponse, redirect);
096                            }
097                            else {
098                                    if (startDate != null) {
099                                            actionResponse.setRenderParameter(
100                                                    "startDate", String.valueOf(startDate.getTime()));
101                                    }
102    
103                                    if (endDate != null) {
104                                            actionResponse.setRenderParameter(
105                                                    "endDate", String.valueOf(endDate.getTime()));
106                                    }
107                            }
108                    }
109                    catch (Exception e) {
110                            _log.error(e, e);
111    
112                            SessionErrors.add(actionRequest, e.getClass());
113    
114                            String pagesRedirect = ParamUtil.getString(
115                                    actionRequest, "pagesRedirect");
116    
117                            sendRedirect(actionRequest, actionResponse, pagesRedirect);
118                    }
119            }
120    
121            @Override
122            public ActionForward render(
123                            ActionMapping actionMapping, ActionForm actionForm,
124                            PortletConfig portletConfig, RenderRequest renderRequest,
125                            RenderResponse renderResponse)
126                    throws Exception {
127    
128                    try {
129                            ActionUtil.getGroup(renderRequest);
130                    }
131                    catch (Exception e) {
132                            if (e instanceof NoSuchGroupException ||
133                                    e instanceof PrincipalException) {
134    
135                                    SessionErrors.add(renderRequest, e.getClass());
136    
137                                    return actionMapping.findForward("portlet.layouts_admin.error");
138                            }
139                            else {
140                                    throw e;
141                            }
142                    }
143    
144                    return actionMapping.findForward(
145                            getForward(renderRequest, "portlet.layouts_admin.export_layouts"));
146            }
147    
148            @Override
149            public void serveResource(
150                            ActionMapping actionMapping, ActionForm actionForm,
151                            PortletConfig portletConfig, ResourceRequest resourceRequest,
152                            ResourceResponse resourceResponse)
153                    throws Exception {
154    
155                    PortletContext portletContext = portletConfig.getPortletContext();
156    
157                    PortletRequestDispatcher portletRequestDispatcher =
158                            portletContext.getRequestDispatcher(
159                                    "/html/portlet/layouts_admin/export_layouts_processes.jsp");
160    
161                    portletRequestDispatcher.include(resourceRequest, resourceResponse);
162            }
163    
164            protected void addLayoutIds(
165                            List<Long> layoutIds, long groupId, boolean privateLayout,
166                            long layoutId)
167                    throws Exception {
168    
169                    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
170                            groupId, privateLayout, layoutId);
171    
172                    for (Layout layout : layouts) {
173                            layoutIds.add(layout.getLayoutId());
174    
175                            addLayoutIds(
176                                    layoutIds, layout.getGroupId(), layout.isPrivateLayout(),
177                                    layout.getLayoutId());
178                    }
179            }
180    
181            protected long[] getLayoutIds(
182                            long groupId, boolean privateLayout, String layoutIdsJSON)
183                    throws Exception {
184    
185                    if (Validator.isNull(layoutIdsJSON)) {
186                            return new long[0];
187                    }
188    
189                    List<Long> layoutIds = new ArrayList<Long>();
190    
191                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray(layoutIdsJSON);
192    
193                    for (int i = 0; i < jsonArray.length(); ++i) {
194                            JSONObject jsonObject = jsonArray.getJSONObject(i);
195    
196                            long layoutId = jsonObject.getLong("layoutId");
197    
198                            if (layoutId > 0) {
199                                    layoutIds.add(layoutId);
200                            }
201    
202                            if (jsonObject.getBoolean("includeChildren")) {
203                                    addLayoutIds(layoutIds, groupId, privateLayout, layoutId);
204                            }
205                    }
206    
207                    return ArrayUtil.toArray(layoutIds.toArray(new Long[layoutIds.size()]));
208            }
209    
210            private static Log _log = LogFactoryUtil.getLog(ExportLayoutsAction.class);
211    
212    }