1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.calendar.action;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.ContentTypes;
28  import com.liferay.portal.kernel.util.FileUtil;
29  import com.liferay.portal.kernel.util.ParamUtil;
30  import com.liferay.portal.struts.ActionConstants;
31  import com.liferay.portal.struts.PortletAction;
32  import com.liferay.portal.theme.ThemeDisplay;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.WebKeys;
35  import com.liferay.portlet.calendar.service.CalEventServiceUtil;
36  import com.liferay.util.servlet.ServletResponseUtil;
37  
38  import java.io.File;
39  import java.io.FileInputStream;
40  
41  import javax.portlet.ActionRequest;
42  import javax.portlet.ActionResponse;
43  import javax.portlet.PortletConfig;
44  
45  import javax.servlet.http.HttpServletResponse;
46  
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionMapping;
49  
50  /**
51   * <a href="ExportEventsAction.java.html"><b><i>View Source</i></b></a>
52   *
53   * @author Michael Young
54   * @author Bruno Farache
55   * @author Brian Wing Shun Chan
56   */
57  public class ExportEventsAction extends PortletAction {
58  
59      public void processAction(
60              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
61              ActionRequest actionRequest, ActionResponse actionResponse)
62          throws Exception {
63  
64          File file = null;
65  
66          try {
67              ThemeDisplay themeDisplay =
68                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
69  
70              long eventId = ParamUtil.getLong(actionRequest, "eventId");
71  
72              String exportFileName = ParamUtil.getString(
73                  actionRequest, "exportFileName");
74  
75              if (eventId > 0) {
76                  file = CalEventServiceUtil.exportEvent(eventId);
77              }
78              else {
79                  file = CalEventServiceUtil.exportGroupEvents(
80                      themeDisplay.getScopeGroupId(), exportFileName);
81              }
82  
83              HttpServletResponse response = PortalUtil.getHttpServletResponse(
84                  actionResponse);
85  
86              ServletResponseUtil.sendFile(
87                  response, file.getName(), new FileInputStream(file),
88                  ContentTypes.TEXT_CALENDAR);
89  
90              setForward(actionRequest, ActionConstants.COMMON_NULL);
91          }
92          catch (Exception e) {
93              _log.error(e, e);
94          }
95          finally {
96              FileUtil.delete(file);
97          }
98      }
99  
100     protected boolean isCheckMethodOnProcessAction() {
101         return _CHECK_METHOD_ON_PROCESS_ACTION;
102     }
103 
104     private static final boolean _CHECK_METHOD_ON_PROCESS_ACTION = false;
105 
106     private static Log _log = LogFactoryUtil.getLog(ExportEventsAction.class);
107 
108 }