1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.communities.action;
16  
17  import com.liferay.portal.NoSuchGroupException;
18  import com.liferay.portal.kernel.exception.PortalException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.servlet.SessionErrors;
22  import com.liferay.portal.kernel.util.ContentTypes;
23  import com.liferay.portal.kernel.util.ParamUtil;
24  import com.liferay.portal.security.auth.PrincipalException;
25  import com.liferay.portal.service.LayoutServiceUtil;
26  import com.liferay.portal.struts.ActionConstants;
27  import com.liferay.portal.struts.PortletAction;
28  import com.liferay.portal.theme.ThemeDisplay;
29  import com.liferay.portal.util.PortalUtil;
30  import com.liferay.portal.util.WebKeys;
31  import com.liferay.util.servlet.ServletResponseUtil;
32  
33  import java.io.File;
34  import java.io.FileInputStream;
35  
36  import java.util.Calendar;
37  import java.util.Date;
38  
39  import javax.portlet.ActionRequest;
40  import javax.portlet.ActionResponse;
41  import javax.portlet.PortletConfig;
42  import javax.portlet.RenderRequest;
43  import javax.portlet.RenderResponse;
44  
45  import javax.servlet.http.HttpServletRequest;
46  import javax.servlet.http.HttpServletResponse;
47  
48  import org.apache.struts.action.ActionForm;
49  import org.apache.struts.action.ActionForward;
50  import org.apache.struts.action.ActionMapping;
51  
52  /**
53   * <a href="ExportPagesAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Alexander Chow
56   * @author Raymond Augé
57   */
58  public class ExportPagesAction extends PortletAction {
59  
60      public void processAction(
61              ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
62              ActionRequest actionRequest, ActionResponse actionResponse)
63          throws Exception {
64  
65          try {
66              ThemeDisplay themeDisplay =
67                  (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
68  
69              long groupId = ParamUtil.getLong(actionRequest, "groupId");
70              boolean privateLayout = ParamUtil.getBoolean(
71                  actionRequest, "privateLayout");
72              String fileName = ParamUtil.getString(
73                  actionRequest, "exportFileName");
74              boolean dateRange = ParamUtil.getBoolean(
75                  actionRequest, "dateRange");
76              Date startDate = null;
77              Date endDate = null;
78  
79              if (dateRange) {
80                  int startDateMonth = ParamUtil.getInteger(
81                      actionRequest, "startDateMonth");
82                  int startDateDay = ParamUtil.getInteger(
83                      actionRequest, "startDateDay");
84                  int startDateYear = ParamUtil.getInteger(
85                      actionRequest, "startDateYear");
86                  int startDateHour = ParamUtil.getInteger(
87                      actionRequest, "startDateHour");
88                  int startDateMinute = ParamUtil.getInteger(
89                      actionRequest, "startDateMinute");
90                  int startDateAmPm = ParamUtil.getInteger(
91                      actionRequest, "startDateAmPm");
92  
93                  if (startDateAmPm == Calendar.PM) {
94                      startDateHour += 12;
95                  }
96  
97                  startDate = PortalUtil.getDate(
98                      startDateMonth, startDateDay, startDateYear, startDateHour,
99                      startDateMinute, themeDisplay.getTimeZone(),
100                     new PortalException());
101 
102                 int endDateMonth = ParamUtil.getInteger(
103                     actionRequest, "endDateMonth");
104                 int endDateDay = ParamUtil.getInteger(
105                     actionRequest, "endDateDay");
106                 int endDateYear = ParamUtil.getInteger(
107                     actionRequest, "endDateYear");
108                 int endDateHour = ParamUtil.getInteger(
109                     actionRequest, "endDateHour");
110                 int endDateMinute = ParamUtil.getInteger(
111                     actionRequest, "endDateMinute");
112                 int endDateAmPm = ParamUtil.getInteger(
113                     actionRequest, "endDateAmPm");
114 
115                 if (endDateAmPm == Calendar.PM) {
116                     endDateHour += 12;
117                 }
118 
119                 endDate = PortalUtil.getDate(
120                     endDateMonth, endDateDay, endDateYear, endDateHour,
121                     endDateMinute, themeDisplay.getTimeZone(),
122                     new PortalException());
123             }
124 
125             File file = LayoutServiceUtil.exportLayoutsAsFile(
126                 groupId, privateLayout, null, actionRequest.getParameterMap(),
127                 startDate, endDate);
128 
129             HttpServletRequest request = PortalUtil.getHttpServletRequest(
130                 actionRequest);
131             HttpServletResponse response = PortalUtil.getHttpServletResponse(
132                 actionResponse);
133 
134             ServletResponseUtil.sendFile(
135                 request, response, fileName, new FileInputStream(file),
136                 ContentTypes.APPLICATION_ZIP);
137 
138             setForward(actionRequest, ActionConstants.COMMON_NULL);
139         }
140         catch (Exception e) {
141             _log.error(e, e);
142 
143             SessionErrors.add(actionRequest, e.getClass().getName());
144 
145             String pagesRedirect = ParamUtil.getString(
146                 actionRequest, "pagesRedirect");
147 
148             sendRedirect(actionRequest, actionResponse, pagesRedirect);
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 }