001
014
015 package com.liferay.portlet.layoutsadmin.action;
016
017 import com.liferay.portal.NoSuchGroupException;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.servlet.ServletResponseUtil;
022 import com.liferay.portal.kernel.servlet.SessionErrors;
023 import com.liferay.portal.kernel.util.ArrayUtil;
024 import com.liferay.portal.kernel.util.ContentTypes;
025 import com.liferay.portal.kernel.util.FileUtil;
026 import com.liferay.portal.kernel.util.GetterUtil;
027 import com.liferay.portal.kernel.util.ParamUtil;
028 import com.liferay.portal.kernel.util.Time;
029 import com.liferay.portal.model.Layout;
030 import com.liferay.portal.model.LayoutSet;
031 import com.liferay.portal.security.auth.PrincipalException;
032 import com.liferay.portal.service.LayoutLocalServiceUtil;
033 import com.liferay.portal.service.LayoutServiceUtil;
034 import com.liferay.portal.service.LayoutSetLocalServiceUtil;
035 import com.liferay.portal.struts.ActionConstants;
036 import com.liferay.portal.struts.PortletAction;
037 import com.liferay.portal.theme.ThemeDisplay;
038 import com.liferay.portal.util.PortalUtil;
039 import com.liferay.portal.util.WebKeys;
040 import com.liferay.portlet.sites.action.ActionUtil;
041
042 import java.io.File;
043 import java.io.FileInputStream;
044
045 import java.util.Calendar;
046 import java.util.Date;
047 import java.util.List;
048
049 import javax.portlet.ActionRequest;
050 import javax.portlet.ActionResponse;
051 import javax.portlet.PortletConfig;
052 import javax.portlet.RenderRequest;
053 import javax.portlet.RenderResponse;
054
055 import javax.servlet.http.HttpServletRequest;
056 import javax.servlet.http.HttpServletResponse;
057
058 import org.apache.struts.action.ActionForm;
059 import org.apache.struts.action.ActionForward;
060 import org.apache.struts.action.ActionMapping;
061
062
066 public class ExportLayoutsAction extends PortletAction {
067
068 @Override
069 public void processAction(
070 ActionMapping actionMapping, ActionForm actionForm,
071 PortletConfig portletConfig, ActionRequest actionRequest,
072 ActionResponse actionResponse)
073 throws Exception {
074
075 File file = null;
076
077 try {
078 ThemeDisplay themeDisplay =
079 (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
080
081 long groupId = ParamUtil.getLong(actionRequest, "groupId");
082 boolean privateLayout = ParamUtil.getBoolean(
083 actionRequest, "privateLayout");
084 long[] layoutIds = ParamUtil.getLongValues(
085 actionRequest, "layoutIds");
086 String fileName = ParamUtil.getString(
087 actionRequest, "exportFileName");
088 String range = ParamUtil.getString(actionRequest, "range");
089
090 Date startDate = null;
091 Date endDate = null;
092
093 if (range.equals("dateRange")) {
094 int startDateMonth = ParamUtil.getInteger(
095 actionRequest, "startDateMonth");
096 int startDateDay = ParamUtil.getInteger(
097 actionRequest, "startDateDay");
098 int startDateYear = ParamUtil.getInteger(
099 actionRequest, "startDateYear");
100 int startDateHour = ParamUtil.getInteger(
101 actionRequest, "startDateHour");
102 int startDateMinute = ParamUtil.getInteger(
103 actionRequest, "startDateMinute");
104 int startDateAmPm = ParamUtil.getInteger(
105 actionRequest, "startDateAmPm");
106
107 if (startDateAmPm == Calendar.PM) {
108 startDateHour += 12;
109 }
110
111 startDate = PortalUtil.getDate(
112 startDateMonth, startDateDay, startDateYear, startDateHour,
113 startDateMinute, themeDisplay.getTimeZone(),
114 PortalException.class);
115
116 int endDateMonth = ParamUtil.getInteger(
117 actionRequest, "endDateMonth");
118 int endDateDay = ParamUtil.getInteger(
119 actionRequest, "endDateDay");
120 int endDateYear = ParamUtil.getInteger(
121 actionRequest, "endDateYear");
122 int endDateHour = ParamUtil.getInteger(
123 actionRequest, "endDateHour");
124 int endDateMinute = ParamUtil.getInteger(
125 actionRequest, "endDateMinute");
126 int endDateAmPm = ParamUtil.getInteger(
127 actionRequest, "endDateAmPm");
128
129 if (endDateAmPm == Calendar.PM) {
130 endDateHour += 12;
131 }
132
133 endDate = PortalUtil.getDate(
134 endDateMonth, endDateDay, endDateYear, endDateHour,
135 endDateMinute, themeDisplay.getTimeZone(),
136 PortalException.class);
137 }
138 else if (range.equals("fromLastPublishDate")) {
139 LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
140 groupId, privateLayout);
141
142 long lastPublishDate = GetterUtil.getLong(
143 layoutSet.getSettingsProperty("last-publish-date"));
144
145 if (lastPublishDate > 0) {
146 endDate = new Date();
147
148 startDate = new Date(lastPublishDate);
149 }
150 }
151 else if (range.equals("last")) {
152 int rangeLast = ParamUtil.getInteger(actionRequest, "last");
153
154 Date now = new Date();
155
156 startDate = new Date(now.getTime() - (rangeLast * Time.HOUR));
157
158 endDate = now;
159 }
160
161 if (layoutIds.length == 1) {
162 Layout layout = LayoutLocalServiceUtil.getLayout(
163 groupId, privateLayout, layoutIds[0]);
164
165 List<Layout> childLayouts = layout.getAllChildren();
166
167 for (Layout childLayout : childLayouts) {
168 layoutIds = ArrayUtil.append(
169 layoutIds, childLayout.getLayoutId());
170 }
171 }
172
173 file = LayoutServiceUtil.exportLayoutsAsFile(
174 groupId, privateLayout, layoutIds,
175 actionRequest.getParameterMap(), startDate, endDate);
176
177 HttpServletRequest request = PortalUtil.getHttpServletRequest(
178 actionRequest);
179 HttpServletResponse response = PortalUtil.getHttpServletResponse(
180 actionResponse);
181
182 ServletResponseUtil.sendFile(
183 request, response, fileName, new FileInputStream(file),
184 ContentTypes.APPLICATION_ZIP);
185
186 setForward(actionRequest, ActionConstants.COMMON_NULL);
187 }
188 catch (Exception e) {
189 _log.error(e, e);
190
191 SessionErrors.add(actionRequest, e.getClass());
192
193 String pagesRedirect = ParamUtil.getString(
194 actionRequest, "pagesRedirect");
195
196 sendRedirect(actionRequest, actionResponse, pagesRedirect);
197 }
198 finally {
199 FileUtil.delete(file);
200 }
201 }
202
203 @Override
204 public ActionForward render(
205 ActionMapping actionMapping, ActionForm actionForm,
206 PortletConfig portletConfig, RenderRequest renderRequest,
207 RenderResponse renderResponse)
208 throws Exception {
209
210 try {
211 ActionUtil.getGroup(renderRequest);
212 }
213 catch (Exception e) {
214 if (e instanceof NoSuchGroupException ||
215 e instanceof PrincipalException) {
216
217 SessionErrors.add(renderRequest, e.getClass());
218
219 return actionMapping.findForward("portlet.layouts_admin.error");
220 }
221 else {
222 throw e;
223 }
224 }
225
226 return actionMapping.findForward(
227 getForward(renderRequest, "portlet.layouts_admin.export_layouts"));
228 }
229
230 private static Log _log = LogFactoryUtil.getLog(ExportLayoutsAction.class);
231
232 }