1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.calendar.action;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.ContentTypes;
20  import com.liferay.portal.kernel.util.FileUtil;
21  import com.liferay.portal.kernel.util.ParamUtil;
22  import com.liferay.portal.struts.ActionConstants;
23  import com.liferay.portal.struts.PortletAction;
24  import com.liferay.portal.theme.ThemeDisplay;
25  import com.liferay.portal.util.PortalUtil;
26  import com.liferay.portal.util.WebKeys;
27  import com.liferay.portlet.calendar.service.CalEventServiceUtil;
28  import com.liferay.util.servlet.ServletResponseUtil;
29  
30  import java.io.File;
31  import java.io.FileInputStream;
32  
33  import javax.portlet.ActionRequest;
34  import javax.portlet.ActionResponse;
35  import javax.portlet.PortletConfig;
36  
37  import javax.servlet.http.HttpServletRequest;
38  import javax.servlet.http.HttpServletResponse;
39  
40  import org.apache.struts.action.ActionForm;
41  import org.apache.struts.action.ActionMapping;
42  
43  /**
44   * <a href="ExportEventsAction.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Michael Young
47   * @author Bruno Farache
48   * @author Brian Wing Shun Chan
49   */
50  public class ExportEventsAction extends PortletAction {
51  
52      public void processAction(
53              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
54              ActionRequest actionRequest, ActionResponse actionResponse)
55          throws Exception {
56  
57          File file = null;
58  
59          try {
60              ThemeDisplay themeDisplay =
61                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
62  
63              long eventId = ParamUtil.getLong(actionRequest, "eventId");
64  
65              String exportFileName = ParamUtil.getString(
66                  actionRequest, "exportFileName");
67  
68              if (eventId > 0) {
69                  file = CalEventServiceUtil.exportEvent(eventId);
70              }
71              else {
72                  file = CalEventServiceUtil.exportGroupEvents(
73                      themeDisplay.getScopeGroupId(), exportFileName);
74              }
75  
76              HttpServletRequest request = PortalUtil.getHttpServletRequest(
77                  actionRequest);
78              HttpServletResponse response = PortalUtil.getHttpServletResponse(
79                  actionResponse);
80  
81              ServletResponseUtil.sendFile(
82                  request, response, file.getName(), new FileInputStream(file),
83                  ContentTypes.TEXT_CALENDAR);
84  
85              setForward(actionRequest, ActionConstants.COMMON_NULL);
86          }
87          catch (Exception e) {
88              _log.error(e, e);
89          }
90          finally {
91              FileUtil.delete(file);
92          }
93      }
94  
95      protected boolean isCheckMethodOnProcessAction() {
96          return _CHECK_METHOD_ON_PROCESS_ACTION;
97      }
98  
99      private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
100 
101     private static Log _log = LogFactoryUtil.getLog(ExportEventsAction.class);
102 
103 }