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