1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.communities.action;
21  
22  import com.liferay.portal.NoSuchGroupException;
23  import com.liferay.portal.PortalException;
24  import com.liferay.portal.kernel.io.FileCacheOutputStream;
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.servlet.SessionErrors;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.security.auth.PrincipalException;
30  import com.liferay.portal.service.LayoutServiceUtil;
31  import com.liferay.portal.struts.ActionConstants;
32  import com.liferay.portal.struts.PortletAction;
33  import com.liferay.portal.theme.ThemeDisplay;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portal.util.WebKeys;
36  import com.liferay.util.servlet.ServletResponseUtil;
37  
38  import java.util.Calendar;
39  import java.util.Date;
40  
41  import javax.portlet.ActionRequest;
42  import javax.portlet.ActionResponse;
43  import javax.portlet.PortletConfig;
44  import javax.portlet.RenderRequest;
45  import javax.portlet.RenderResponse;
46  
47  import javax.servlet.http.HttpServletResponse;
48  
49  import org.apache.struts.action.ActionForm;
50  import org.apache.struts.action.ActionForward;
51  import org.apache.struts.action.ActionMapping;
52  
53  /**
54   * <a href="ExportPagesAction.java.html"><b><i>View Source</i></b></a>
55   *
56   * @author Alexander Chow
57   * @author Raymond Augé
58   *
59   */
60  public class ExportPagesAction extends PortletAction {
61  
62      public void processAction(
63              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
64              ActionRequest actionRequest, ActionResponse actionResponse)
65          throws Exception {
66  
67          try {
68              ThemeDisplay themeDisplay =
69                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
70  
71              long groupId = ParamUtil.getLong(actionRequest, "groupId");
72              boolean privateLayout = ParamUtil.getBoolean(
73                  actionRequest, "privateLayout");
74              String fileName = ParamUtil.getString(
75                  actionRequest, "exportFileName");
76              boolean dateRange = ParamUtil.getBoolean(
77                  actionRequest, "dateRange");
78              Date startDate = null;
79              Date endDate = null;
80  
81              if (dateRange) {
82                  int startDateMonth = ParamUtil.getInteger(
83                      actionRequest, "startDateMonth");
84                  int startDateDay = ParamUtil.getInteger(
85                      actionRequest, "startDateDay");
86                  int startDateYear = ParamUtil.getInteger(
87                      actionRequest, "startDateYear");
88                  int startDateHour = ParamUtil.getInteger(
89                      actionRequest, "startDateHour");
90                  int startDateMinute = ParamUtil.getInteger(
91                      actionRequest, "startDateMinute");
92                  int startDateAmPm = ParamUtil.getInteger(
93                      actionRequest, "startDateAmPm");
94  
95                  if (startDateAmPm == Calendar.PM) {
96                      startDateHour += 12;
97                  }
98  
99                  startDate = PortalUtil.getDate(
100                     startDateMonth, startDateDay, startDateYear, startDateHour,
101                     startDateMinute, themeDisplay.getTimeZone(),
102                     new PortalException());
103 
104                 int endDateMonth = ParamUtil.getInteger(
105                     actionRequest, "endDateMonth");
106                 int endDateDay = ParamUtil.getInteger(
107                     actionRequest, "endDateDay");
108                 int endDateYear = ParamUtil.getInteger(
109                     actionRequest, "endDateYear");
110                 int endDateHour = ParamUtil.getInteger(
111                     actionRequest, "endDateHour");
112                 int endDateMinute = ParamUtil.getInteger(
113                     actionRequest, "endDateMinute");
114                 int endDateAmPm = ParamUtil.getInteger(
115                     actionRequest, "endDateAmPm");
116 
117                 if (endDateAmPm == Calendar.PM) {
118                     endDateHour += 12;
119                 }
120 
121                 endDate = PortalUtil.getDate(
122                     endDateMonth, endDateDay, endDateYear, endDateHour,
123                     endDateMinute, themeDisplay.getTimeZone(),
124                     new PortalException());
125             }
126 
127             FileCacheOutputStream fcos =
128                 LayoutServiceUtil.exportLayoutsAsStream(
129                     groupId, privateLayout, null,
130                     actionRequest.getParameterMap(), startDate, endDate);
131 
132             HttpServletResponse response = PortalUtil.getHttpServletResponse(
133                 actionResponse);
134 
135             ServletResponseUtil.sendFile(
136                 response, fileName, fcos.getFileInputStream());
137 
138             setForward(actionRequest, ActionConstants.COMMON_NULL);
139         }
140         catch (Exception e) {
141             _log.error(e, e);
142         }
143     }
144 
145     public ActionForward render(
146             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
147             RenderRequest renderRequest, RenderResponse renderResponse)
148         throws Exception {
149 
150         try {
151             ActionUtil.getGroup(renderRequest);
152         }
153         catch (Exception e) {
154             if (e instanceof NoSuchGroupException ||
155                 e instanceof PrincipalException) {
156 
157                 SessionErrors.add(renderRequest, e.getClass().getName());
158 
159                 return mapping.findForward("portlet.communities.error");
160             }
161             else {
162                 throw e;
163             }
164         }
165 
166         return mapping.findForward(
167             getForward(renderRequest, "portlet.communities.export_pages"));
168     }
169 
170     private static Log _log = LogFactoryUtil.getLog(ExportPagesAction.class);
171 
172 }