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