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.calendar.action;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.ContentTypes;
020    import com.liferay.portal.kernel.util.FileUtil;
021    import com.liferay.portal.kernel.util.ParamUtil;
022    import com.liferay.portal.struts.ActionConstants;
023    import com.liferay.portal.struts.PortletAction;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.PortalUtil;
026    import com.liferay.portal.util.WebKeys;
027    import com.liferay.portlet.calendar.service.CalEventServiceUtil;
028    import com.liferay.util.servlet.ServletResponseUtil;
029    
030    import java.io.File;
031    import java.io.FileInputStream;
032    
033    import javax.portlet.ActionRequest;
034    import javax.portlet.ActionResponse;
035    import javax.portlet.PortletConfig;
036    
037    import javax.servlet.http.HttpServletRequest;
038    import javax.servlet.http.HttpServletResponse;
039    
040    import org.apache.struts.action.ActionForm;
041    import org.apache.struts.action.ActionMapping;
042    
043    /**
044     * @author Michael Young
045     * @author Bruno Farache
046     * @author Brian Wing Shun Chan
047     */
048    public class ExportEventsAction extends PortletAction {
049    
050            public void processAction(
051                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
052                            ActionRequest actionRequest, ActionResponse actionResponse)
053                    throws Exception {
054    
055                    File file = null;
056    
057                    try {
058                            ThemeDisplay themeDisplay =
059                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
060    
061                            long eventId = ParamUtil.getLong(actionRequest, "eventId");
062    
063                            String exportFileName = ParamUtil.getString(
064                                    actionRequest, "exportFileName");
065    
066                            if (exportFileName == null) {
067                                    exportFileName = "liferay.ics";
068                            }
069                            else {
070                                    exportFileName = FileUtil.getShortFileName(exportFileName);
071                            }
072    
073                            if (eventId > 0) {
074                                    file = CalEventServiceUtil.exportEvent(eventId);
075                            }
076                            else {
077                                    file = CalEventServiceUtil.exportGroupEvents(
078                                            themeDisplay.getScopeGroupId(), exportFileName);
079                            }
080    
081                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
082                                    actionRequest);
083                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
084                                    actionResponse);
085    
086                            ServletResponseUtil.sendFile(
087                                    request, response, exportFileName, new FileInputStream(file),
088                                    ContentTypes.TEXT_CALENDAR);
089    
090                            setForward(actionRequest, ActionConstants.COMMON_NULL);
091                    }
092                    catch (Exception e) {
093                            _log.error(e, e);
094                    }
095                    finally {
096                            FileUtil.delete(file);
097                    }
098            }
099    
100            protected boolean isCheckMethodOnProcessAction() {
101                    return _CHECK_METHOD_ON_PROCESS_ACTION;
102            }
103    
104            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
105    
106            private static Log _log = LogFactoryUtil.getLog(ExportEventsAction.class);
107    
108    }