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