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