1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.communities.action;
24  
25  import com.liferay.portal.NoSuchGroupException;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.kernel.io.FileCacheOutputStream;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.servlet.SessionErrors;
31  import com.liferay.portal.kernel.util.ParamUtil;
32  import com.liferay.portal.security.auth.PrincipalException;
33  import com.liferay.portal.service.LayoutServiceUtil;
34  import com.liferay.portal.struts.ActionConstants;
35  import com.liferay.portal.struts.PortletAction;
36  import com.liferay.portal.theme.ThemeDisplay;
37  import com.liferay.portal.util.PortalUtil;
38  import com.liferay.portal.util.WebKeys;
39  import com.liferay.util.servlet.ServletResponseUtil;
40  
41  import java.util.Calendar;
42  import java.util.Date;
43  
44  import javax.portlet.ActionRequest;
45  import javax.portlet.ActionResponse;
46  import javax.portlet.PortletConfig;
47  import javax.portlet.RenderRequest;
48  import javax.portlet.RenderResponse;
49  
50  import javax.servlet.http.HttpServletResponse;
51  
52  import org.apache.struts.action.ActionForm;
53  import org.apache.struts.action.ActionForward;
54  import org.apache.struts.action.ActionMapping;
55  
56  /**
57   * <a href="ExportPagesAction.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Alexander Chow
60   * @author Raymond Augé
61   *
62   */
63  public class ExportPagesAction extends PortletAction {
64  
65      public void processAction(
66              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
67              ActionRequest actionRequest, ActionResponse actionResponse)
68          throws Exception {
69  
70          try {
71              ThemeDisplay themeDisplay =
72                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
73  
74              long groupId = ParamUtil.getLong(actionRequest, "groupId");
75              boolean privateLayout = ParamUtil.getBoolean(
76                  actionRequest, "privateLayout");
77              String fileName = ParamUtil.getString(
78                  actionRequest, "exportFileName");
79              boolean dateRange = ParamUtil.getBoolean(
80                  actionRequest, "dateRange");
81              Date startDate = null;
82              Date endDate = null;
83  
84              if (dateRange) {
85                  int startDateMonth = ParamUtil.getInteger(
86                      actionRequest, "startDateMonth");
87                  int startDateDay = ParamUtil.getInteger(
88                      actionRequest, "startDateDay");
89                  int startDateYear = ParamUtil.getInteger(
90                      actionRequest, "startDateYear");
91                  int startDateHour = ParamUtil.getInteger(
92                      actionRequest, "startDateHour");
93                  int startDateMinute = ParamUtil.getInteger(
94                      actionRequest, "startDateMinute");
95                  int startDateAmPm = ParamUtil.getInteger(
96                      actionRequest, "startDateAmPm");
97  
98                  if (startDateAmPm == Calendar.PM) {
99                      startDateHour += 12;
100                 }
101 
102                 startDate = PortalUtil.getDate(
103                     startDateMonth, startDateDay, startDateYear, startDateHour,
104                     startDateMinute, themeDisplay.getTimeZone(),
105                     new PortalException());
106 
107                 int endDateMonth = ParamUtil.getInteger(
108                     actionRequest, "endDateMonth");
109                 int endDateDay = ParamUtil.getInteger(
110                     actionRequest, "endDateDay");
111                 int endDateYear = ParamUtil.getInteger(
112                     actionRequest, "endDateYear");
113                 int endDateHour = ParamUtil.getInteger(
114                     actionRequest, "endDateHour");
115                 int endDateMinute = ParamUtil.getInteger(
116                     actionRequest, "endDateMinute");
117                 int endDateAmPm = ParamUtil.getInteger(
118                     actionRequest, "endDateAmPm");
119 
120                 if (endDateAmPm == Calendar.PM) {
121                     endDateHour += 12;
122                 }
123 
124                 endDate = PortalUtil.getDate(
125                     endDateMonth, endDateDay, endDateYear, endDateHour,
126                     endDateMinute, themeDisplay.getTimeZone(),
127                     new PortalException());
128             }
129 
130             FileCacheOutputStream fcos =
131                 LayoutServiceUtil.exportLayoutsAsStream(
132                     groupId, privateLayout, null,
133                     actionRequest.getParameterMap(), startDate, endDate);
134 
135             HttpServletResponse response = PortalUtil.getHttpServletResponse(
136                 actionResponse);
137 
138             ServletResponseUtil.sendFile(
139                 response, fileName, fcos.getFileInputStream());
140 
141             setForward(actionRequest, ActionConstants.COMMON_NULL);
142         }
143         catch (Exception e) {
144             _log.error(e, e);
145         }
146     }
147 
148     public ActionForward render(
149             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
150             RenderRequest renderRequest, RenderResponse renderResponse)
151         throws Exception {
152 
153         try {
154             ActionUtil.getGroup(renderRequest);
155         }
156         catch (Exception e) {
157             if (e instanceof NoSuchGroupException ||
158                 e instanceof PrincipalException) {
159 
160                 SessionErrors.add(renderRequest, e.getClass().getName());
161 
162                 return mapping.findForward("portlet.communities.error");
163             }
164             else {
165                 throw e;
166             }
167         }
168 
169         return mapping.findForward(
170             getForward(renderRequest, "portlet.communities.export_pages"));
171     }
172 
173     private static Log _log = LogFactoryUtil.getLog(ExportPagesAction.class);
174 
175 }