1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
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 (exportFileName == null) {
69                  exportFileName = "liferay.ics";
70              }
71              else {
72                  exportFileName = FileUtil.getShortFileName(exportFileName);
73              }
74  
75              if (eventId > 0) {
76                  file = CalEventServiceUtil.exportEvent(eventId);
77              }
78              else {
79                  file = CalEventServiceUtil.exportGroupEvents(
80                      themeDisplay.getScopeGroupId(), exportFileName);
81              }
82  
83              HttpServletRequest request = PortalUtil.getHttpServletRequest(
84                  actionRequest);
85              HttpServletResponse response = PortalUtil.getHttpServletResponse(
86                  actionResponse);
87  
88              ServletResponseUtil.sendFile(
89                  request, response, exportFileName, new FileInputStream(file),
90                  ContentTypes.TEXT_CALENDAR);
91  
92              setForward(actionRequest, ActionConstants.COMMON_NULL);
93          }
94          catch (Exception e) {
95              _log.error(e, e);
96          }
97          finally {
98              FileUtil.delete(file);
99          }
100     }
101 
102     protected boolean isCheckMethodOnProcessAction() {
103         return _CHECK_METHOD_ON_PROCESS_ACTION;
104     }
105 
106     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
107 
108     private static Log _log = LogFactoryUtil.getLog(ExportEventsAction.class);
109 
110 }