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