1   /**
2    * Copyright (c) 2000-2008 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.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.commons.logging.Log;
50  import org.apache.commons.logging.LogFactory;
51  import org.apache.struts.action.ActionForm;
52  import org.apache.struts.action.ActionForward;
53  import org.apache.struts.action.ActionMapping;
54  
55  /**
56   * <a href="ExportPagesAction.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Alexander Chow
59   * @author Raymond Augé
60   *
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             byte[] bytes = LayoutServiceUtil.exportLayouts(
130                 groupId, privateLayout, actionRequest.getParameterMap(),
131                 startDate, endDate);
132 
133             HttpServletResponse response = PortalUtil.getHttpServletResponse(
134                 actionResponse);
135 
136             ServletResponseUtil.sendFile(response, fileName, bytes);
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 = LogFactory.getLog(ExportPagesAction.class);
171 
172 }