001    /**
002     * Copyright (c) 2000-2010 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.communities.action;
016    
017    import com.liferay.portal.NoSuchGroupException;
018    import com.liferay.portal.kernel.exception.PortalException;
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.ContentTypes;
023    import com.liferay.portal.kernel.util.GetterUtil;
024    import com.liferay.portal.kernel.util.ParamUtil;
025    import com.liferay.portal.kernel.util.Time;
026    import com.liferay.portal.kernel.util.UnicodeProperties;
027    import com.liferay.portal.model.LayoutSet;
028    import com.liferay.portal.security.auth.PrincipalException;
029    import com.liferay.portal.service.LayoutServiceUtil;
030    import com.liferay.portal.service.LayoutSetLocalServiceUtil;
031    import com.liferay.portal.struts.ActionConstants;
032    import com.liferay.portal.struts.PortletAction;
033    import com.liferay.portal.theme.ThemeDisplay;
034    import com.liferay.portal.util.PortalUtil;
035    import com.liferay.portal.util.WebKeys;
036    import com.liferay.util.servlet.ServletResponseUtil;
037    
038    import java.io.File;
039    import java.io.FileInputStream;
040    
041    import java.util.Calendar;
042    import java.util.Date;
043    
044    import javax.portlet.ActionRequest;
045    import javax.portlet.ActionResponse;
046    import javax.portlet.PortletConfig;
047    import javax.portlet.RenderRequest;
048    import javax.portlet.RenderResponse;
049    
050    import javax.servlet.http.HttpServletRequest;
051    import javax.servlet.http.HttpServletResponse;
052    
053    import org.apache.struts.action.ActionForm;
054    import org.apache.struts.action.ActionForward;
055    import org.apache.struts.action.ActionMapping;
056    
057    /**
058     * @author Alexander Chow
059     * @author Raymond Augé
060     */
061    public class ExportPagesAction extends PortletAction {
062    
063            public void processAction(
064                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
065                            ActionRequest actionRequest, ActionResponse actionResponse)
066                    throws Exception {
067    
068                    try {
069                            ThemeDisplay themeDisplay =
070                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
071    
072                            long groupId = ParamUtil.getLong(actionRequest, "groupId");
073                            boolean privateLayout = ParamUtil.getBoolean(
074                                    actionRequest, "privateLayout");
075                            String fileName = ParamUtil.getString(
076                                    actionRequest, "exportFileName");
077                            String range = ParamUtil.getString(actionRequest, "range");
078    
079                            Date startDate = null;
080                            Date endDate = null;
081    
082                            if (range.equals("dateRange")) {
083                                    int startDateMonth = ParamUtil.getInteger(
084                                            actionRequest, "startDateMonth");
085                                    int startDateDay = ParamUtil.getInteger(
086                                            actionRequest, "startDateDay");
087                                    int startDateYear = ParamUtil.getInteger(
088                                            actionRequest, "startDateYear");
089                                    int startDateHour = ParamUtil.getInteger(
090                                            actionRequest, "startDateHour");
091                                    int startDateMinute = ParamUtil.getInteger(
092                                            actionRequest, "startDateMinute");
093                                    int startDateAmPm = ParamUtil.getInteger(
094                                            actionRequest, "startDateAmPm");
095    
096                                    if (startDateAmPm == Calendar.PM) {
097                                            startDateHour += 12;
098                                    }
099    
100                                    startDate = PortalUtil.getDate(
101                                            startDateMonth, startDateDay, startDateYear, startDateHour,
102                                            startDateMinute, themeDisplay.getTimeZone(),
103                                            new PortalException());
104    
105                                    int endDateMonth = ParamUtil.getInteger(
106                                            actionRequest, "endDateMonth");
107                                    int endDateDay = ParamUtil.getInteger(
108                                            actionRequest, "endDateDay");
109                                    int endDateYear = ParamUtil.getInteger(
110                                            actionRequest, "endDateYear");
111                                    int endDateHour = ParamUtil.getInteger(
112                                            actionRequest, "endDateHour");
113                                    int endDateMinute = ParamUtil.getInteger(
114                                            actionRequest, "endDateMinute");
115                                    int endDateAmPm = ParamUtil.getInteger(
116                                            actionRequest, "endDateAmPm");
117    
118                                    if (endDateAmPm == Calendar.PM) {
119                                            endDateHour += 12;
120                                    }
121    
122                                    endDate = PortalUtil.getDate(
123                                            endDateMonth, endDateDay, endDateYear, endDateHour,
124                                            endDateMinute, themeDisplay.getTimeZone(),
125                                            new PortalException());
126                            }
127                            else if (range.equals("fromLastPublishDate")) {
128                                    LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
129                                            groupId, privateLayout);
130    
131                                    UnicodeProperties settingsProperties =
132                                            layoutSet.getSettingsProperties();
133    
134                                    long lastPublishDate = GetterUtil.getLong(
135                                            settingsProperties.getProperty("last-publish-date"));
136    
137                                    if (lastPublishDate > 0) {
138                                            Calendar cal = Calendar.getInstance(
139                                                    themeDisplay.getTimeZone(), themeDisplay.getLocale());
140    
141                                            endDate = cal.getTime();
142    
143                                            cal.setTimeInMillis(lastPublishDate);
144    
145                                            startDate = cal.getTime();
146                                    }
147                            }
148                            else if (range.equals("last")) {
149                                    int rangeLast = ParamUtil.getInteger(actionRequest, "last");
150    
151                                    Date now = new Date();
152    
153                                    startDate = new Date(now.getTime() - (rangeLast * Time.HOUR));
154    
155                                    endDate = now;
156                            }
157    
158                            File file = LayoutServiceUtil.exportLayoutsAsFile(
159                                    groupId, privateLayout, null, actionRequest.getParameterMap(),
160                                    startDate, endDate);
161    
162                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
163                                    actionRequest);
164                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
165                                    actionResponse);
166    
167                            ServletResponseUtil.sendFile(
168                                    request, response, fileName, new FileInputStream(file),
169                                    ContentTypes.APPLICATION_ZIP);
170    
171                            setForward(actionRequest, ActionConstants.COMMON_NULL);
172                    }
173                    catch (Exception e) {
174                            _log.error(e, e);
175    
176                            SessionErrors.add(actionRequest, e.getClass().getName());
177    
178                            String pagesRedirect = ParamUtil.getString(
179                                    actionRequest, "pagesRedirect");
180    
181                            sendRedirect(actionRequest, actionResponse, pagesRedirect);
182                    }
183            }
184    
185            public ActionForward render(
186                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
187                            RenderRequest renderRequest, RenderResponse renderResponse)
188                    throws Exception {
189    
190                    try {
191                            ActionUtil.getGroup(renderRequest);
192                    }
193                    catch (Exception e) {
194                            if (e instanceof NoSuchGroupException ||
195                                    e instanceof PrincipalException) {
196    
197                                    SessionErrors.add(renderRequest, e.getClass().getName());
198    
199                                    return mapping.findForward("portlet.communities.error");
200                            }
201                            else {
202                                    throw e;
203                            }
204                    }
205    
206                    return mapping.findForward(
207                            getForward(renderRequest, "portlet.communities.export_pages"));
208            }
209    
210            private static Log _log = LogFactoryUtil.getLog(ExportPagesAction.class);
211    
212    }