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.HttpServletResponse;
38  
39  import org.apache.struts.action.ActionForm;
40  import org.apache.struts.action.ActionMapping;
41  
42  /**
43   * <a href="ExportEventsAction.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Michael Young
46   * @author Bruno Farache
47   * @author Brian Wing Shun Chan
48   */
49  public class ExportEventsAction extends PortletAction {
50  
51      public void processAction(
52              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
53              ActionRequest actionRequest, ActionResponse actionResponse)
54          throws Exception {
55  
56          File file = null;
57  
58          try {
59              ThemeDisplay themeDisplay =
60                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
61  
62              long eventId = ParamUtil.getLong(actionRequest, "eventId");
63  
64              String exportFileName = ParamUtil.getString(
65                  actionRequest, "exportFileName");
66  
67              if (eventId > 0) {
68                  file = CalEventServiceUtil.exportEvent(eventId);
69              }
70              else {
71                  file = CalEventServiceUtil.exportGroupEvents(
72                      themeDisplay.getScopeGroupId(), exportFileName);
73              }
74  
75              HttpServletResponse response = PortalUtil.getHttpServletResponse(
76                  actionResponse);
77  
78              ServletResponseUtil.sendFile(
79                  response, file.getName(), new FileInputStream(file),
80                  ContentTypes.TEXT_CALENDAR);
81  
82              setForward(actionRequest, ActionConstants.COMMON_NULL);
83          }
84          catch (Exception e) {
85              _log.error(e, e);
86          }
87          finally {
88              FileUtil.delete(file);
89          }
90      }
91  
92      protected boolean isCheckMethodOnProcessAction() {
93          return _CHECK_METHOD_ON_PROCESS_ACTION;
94      }
95  
96      private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
97  
98      private static Log _log = LogFactoryUtil.getLog(ExportEventsAction.class);
99  
100 }