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.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 actionMapping, ActionForm actionForm,
054                            PortletConfig portletConfig, ActionRequest actionRequest,
055                            ActionResponse actionResponse)
056                    throws Exception {
057    
058                    File file = null;
059    
060                    try {
061                            ThemeDisplay themeDisplay =
062                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
063    
064                            long eventId = ParamUtil.getLong(actionRequest, "eventId");
065    
066                            String exportFileName = ParamUtil.getString(
067                                    actionRequest, "exportFileName");
068    
069                            if (Validator.isNull(exportFileName)) {
070                                    exportFileName = "liferay.ics";
071                            }
072                            else {
073                                    exportFileName = FileUtil.getShortFileName(exportFileName);
074                            }
075    
076                            if (eventId > 0) {
077                                    file = CalEventServiceUtil.exportEvent(eventId);
078                            }
079                            else {
080                                    file = CalEventServiceUtil.exportGroupEvents(
081                                            themeDisplay.getScopeGroupId(), exportFileName);
082                            }
083    
084                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
085                                    actionRequest);
086                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
087                                    actionResponse);
088    
089                            ServletResponseUtil.sendFile(
090                                    request, response, exportFileName, new FileInputStream(file),
091                                    ContentTypes.TEXT_CALENDAR);
092    
093                            setForward(actionRequest, ActionConstants.COMMON_NULL);
094                    }
095                    catch (Exception e) {
096                            _log.error(e, e);
097                    }
098                    finally {
099                            FileUtil.delete(file);
100                    }
101            }
102    
103            @Override
104            protected boolean isCheckMethodOnProcessAction() {
105                    return _CHECK_METHOD_ON_PROCESS_ACTION;
106            }
107    
108            private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
109    
110            private static Log _log = LogFactoryUtil.getLog(ExportEventsAction.class);
111    
112    }