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.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  public class ExportPagesAction extends PortletAction {
63  
64      public void processAction(
65              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
66              ActionRequest actionRequest, ActionResponse actionResponse)
67          throws Exception {
68  
69          try {
70              ThemeDisplay themeDisplay =
71                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
72  
73              long groupId = ParamUtil.getLong(actionRequest, "groupId");
74              boolean privateLayout = ParamUtil.getBoolean(
75                  actionRequest, "privateLayout");
76              String fileName = ParamUtil.getString(
77                  actionRequest, "exportFileName");
78              boolean dateRange = ParamUtil.getBoolean(
79                  actionRequest, "dateRange");
80              Date startDate = null;
81              Date endDate = null;
82  
83              if (dateRange) {
84                  int startDateMonth = ParamUtil.getInteger(
85                      actionRequest, "startDateMonth");
86                  int startDateDay = ParamUtil.getInteger(
87                      actionRequest, "startDateDay");
88                  int startDateYear = ParamUtil.getInteger(
89                      actionRequest, "startDateYear");
90                  int startDateHour = ParamUtil.getInteger(
91                      actionRequest, "startDateHour");
92                  int startDateMinute = ParamUtil.getInteger(
93                      actionRequest, "startDateMinute");
94                  int startDateAmPm = ParamUtil.getInteger(
95                      actionRequest, "startDateAmPm");
96  
97                  if (startDateAmPm == Calendar.PM) {
98                      startDateHour += 12;
99                  }
100 
101                 startDate = PortalUtil.getDate(
102                     startDateMonth, startDateDay, startDateYear, startDateHour,
103                     startDateMinute, themeDisplay.getTimeZone(),
104                     new PortalException());
105 
106                 int endDateMonth = ParamUtil.getInteger(
107                     actionRequest, "endDateMonth");
108                 int endDateDay = ParamUtil.getInteger(
109                     actionRequest, "endDateDay");
110                 int endDateYear = ParamUtil.getInteger(
111                     actionRequest, "endDateYear");
112                 int endDateHour = ParamUtil.getInteger(
113                     actionRequest, "endDateHour");
114                 int endDateMinute = ParamUtil.getInteger(
115                     actionRequest, "endDateMinute");
116                 int endDateAmPm = ParamUtil.getInteger(
117                     actionRequest, "endDateAmPm");
118 
119                 if (endDateAmPm == Calendar.PM) {
120                     endDateHour += 12;
121                 }
122 
123                 endDate = PortalUtil.getDate(
124                     endDateMonth, endDateDay, endDateYear, endDateHour,
125                     endDateMinute, themeDisplay.getTimeZone(),
126                     new PortalException());
127             }
128 
129             FileCacheOutputStream fcos =
130                 LayoutServiceUtil.exportLayoutsAsStream(
131                     groupId, privateLayout, null,
132                     actionRequest.getParameterMap(), startDate, endDate);
133 
134             try {
135                 HttpServletResponse response =
136                     PortalUtil.getHttpServletResponse(actionResponse);
137 
138                 ServletResponseUtil.sendFile(
139                     response, fileName, fcos.getFileInputStream());
140             }
141             finally {
142                 fcos.cleanUp();
143             }
144 
145             setForward(actionRequest, ActionConstants.COMMON_NULL);
146         }
147         catch (Exception e) {
148             _log.error(e, e);
149         }
150     }
151 
152     public ActionForward render(
153             ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
154             RenderRequest renderRequest, RenderResponse renderResponse)
155         throws Exception {
156 
157         try {
158             ActionUtil.getGroup(renderRequest);
159         }
160         catch (Exception e) {
161             if (e instanceof NoSuchGroupException ||
162                 e instanceof PrincipalException) {
163 
164                 SessionErrors.add(renderRequest, e.getClass().getName());
165 
166                 return mapping.findForward("portlet.communities.error");
167             }
168             else {
169                 throw e;
170             }
171         }
172 
173         return mapping.findForward(
174             getForward(renderRequest, "portlet.communities.export_pages"));
175     }
176 
177     private static Log _log = LogFactoryUtil.getLog(ExportPagesAction.class);
178 
179 }