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.communities.action;
16  
17  import com.liferay.portal.NoSuchGroupException;
18  import com.liferay.portal.kernel.exception.PortalException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.servlet.SessionErrors;
22  import com.liferay.portal.kernel.util.ContentTypes;
23  import com.liferay.portal.kernel.util.ParamUtil;
24  import com.liferay.portal.security.auth.PrincipalException;
25  import com.liferay.portal.service.LayoutServiceUtil;
26  import com.liferay.portal.struts.ActionConstants;
27  import com.liferay.portal.struts.PortletAction;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.util.servlet.ServletResponseUtil;
32  
33  import java.io.File;
34  import java.io.FileInputStream;
35  
36  import java.util.Calendar;
37  import java.util.Date;
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.ActionResponse;
41  import javax.portlet.PortletConfig;
42  import javax.portlet.RenderRequest;
43  import javax.portlet.RenderResponse;
44  
45  import javax.servlet.http.HttpServletResponse;
46  
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionForward;
49  import org.apache.struts.action.ActionMapping;
50  
51  /**
52   * <a href="ExportPagesAction.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Alexander Chow
55   * @author Raymond Augé
56   */
57  public class ExportPagesAction extends PortletAction {
58  
59      public void processAction(
60              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
61              ActionRequest actionRequest, ActionResponse actionResponse)
62          throws Exception {
63  
64          try {
65              ThemeDisplay themeDisplay =
66                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
67  
68              long groupId = ParamUtil.getLong(actionRequest, "groupId");
69              boolean privateLayout = ParamUtil.getBoolean(
70                  actionRequest, "privateLayout");
71              String fileName = ParamUtil.getString(
72                  actionRequest, "exportFileName");
73              boolean dateRange = ParamUtil.getBoolean(
74                  actionRequest, "dateRange");
75              Date startDate = null;
76              Date endDate = null;
77  
78              if (dateRange) {
79                  int startDateMonth = ParamUtil.getInteger(
80                      actionRequest, "startDateMonth");
81                  int startDateDay = ParamUtil.getInteger(
82                      actionRequest, "startDateDay");
83                  int startDateYear = ParamUtil.getInteger(
84                      actionRequest, "startDateYear");
85                  int startDateHour = ParamUtil.getInteger(
86                      actionRequest, "startDateHour");
87                  int startDateMinute = ParamUtil.getInteger(
88                      actionRequest, "startDateMinute");
89                  int startDateAmPm = ParamUtil.getInteger(
90                      actionRequest, "startDateAmPm");
91  
92                  if (startDateAmPm == Calendar.PM) {
93                      startDateHour += 12;
94                  }
95  
96                  startDate = PortalUtil.getDate(
97                      startDateMonth, startDateDay, startDateYear, startDateHour,
98                      startDateMinute, themeDisplay.getTimeZone(),
99                      new PortalException());
100 
101                 int endDateMonth = ParamUtil.getInteger(
102                     actionRequest, "endDateMonth");
103                 int endDateDay = ParamUtil.getInteger(
104                     actionRequest, "endDateDay");
105                 int endDateYear = ParamUtil.getInteger(
106                     actionRequest, "endDateYear");
107                 int endDateHour = ParamUtil.getInteger(
108                     actionRequest, "endDateHour");
109                 int endDateMinute = ParamUtil.getInteger(
110                     actionRequest, "endDateMinute");
111                 int endDateAmPm = ParamUtil.getInteger(
112                     actionRequest, "endDateAmPm");
113 
114                 if (endDateAmPm == Calendar.PM) {
115                     endDateHour += 12;
116                 }
117 
118                 endDate = PortalUtil.getDate(
119                     endDateMonth, endDateDay, endDateYear, endDateHour,
120                     endDateMinute, themeDisplay.getTimeZone(),
121                     new PortalException());
122             }
123 
124             File file = LayoutServiceUtil.exportLayoutsAsFile(
125                 groupId, privateLayout, null, actionRequest.getParameterMap(),
126                 startDate, endDate);
127 
128             HttpServletResponse response = PortalUtil.getHttpServletResponse(
129                 actionResponse);
130 
131             ServletResponseUtil.sendFile(
132                 response, fileName, new FileInputStream(file),
133                 ContentTypes.APPLICATION_ZIP);
134 
135             setForward(actionRequest, ActionConstants.COMMON_NULL);
136         }
137         catch (Exception e) {
138             _log.error(e, e);
139 
140             SessionErrors.add(actionRequest, e.getClass().getName());
141 
142             String pagesRedirect = ParamUtil.getString(
143                 actionRequest, "pagesRedirect");
144 
145             sendRedirect(actionRequest, actionResponse, pagesRedirect);
146         }
147     }
148 
149     public ActionForward render(
150             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
151             RenderRequest renderRequest, RenderResponse renderResponse)
152         throws Exception {
153 
154         try {
155             ActionUtil.getGroup(renderRequest);
156         }
157         catch (Exception e) {
158             if (e instanceof NoSuchGroupException ||
159                 e instanceof PrincipalException) {
160 
161                 SessionErrors.add(renderRequest, e.getClass().getName());
162 
163                 return mapping.findForward("portlet.communities.error");
164             }
165             else {
166                 throw e;
167             }
168         }
169 
170         return mapping.findForward(
171             getForward(renderRequest, "portlet.communities.export_pages"));
172     }
173 
174     private static Log _log = LogFactoryUtil.getLog(ExportPagesAction.class);
175 
176 }