001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
060     * @author Alexander Chow
061     * @author Raymond Augé
062     */
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                    try {
072                            ThemeDisplay themeDisplay =
073                                    (ThemeDisplay)actionRequest.getAttribute(WebKeys.THEME_DISPLAY);
074    
075                            long groupId = ParamUtil.getLong(actionRequest, "groupId");
076                            boolean privateLayout = ParamUtil.getBoolean(
077                                    actionRequest, "privateLayout");
078                            long[] layoutIds = ParamUtil.getLongValues(
079                                    actionRequest, "layoutIds");
080                            String fileName = ParamUtil.getString(
081                                    actionRequest, "exportFileName");
082                            String range = ParamUtil.getString(actionRequest, "range");
083    
084                            Date startDate = null;
085                            Date endDate = null;
086    
087                            if (range.equals("dateRange")) {
088                                    int startDateMonth = ParamUtil.getInteger(
089                                            actionRequest, "startDateMonth");
090                                    int startDateDay = ParamUtil.getInteger(
091                                            actionRequest, "startDateDay");
092                                    int startDateYear = ParamUtil.getInteger(
093                                            actionRequest, "startDateYear");
094                                    int startDateHour = ParamUtil.getInteger(
095                                            actionRequest, "startDateHour");
096                                    int startDateMinute = ParamUtil.getInteger(
097                                            actionRequest, "startDateMinute");
098                                    int startDateAmPm = ParamUtil.getInteger(
099                                            actionRequest, "startDateAmPm");
100    
101                                    if (startDateAmPm == Calendar.PM) {
102                                            startDateHour += 12;
103                                    }
104    
105                                    startDate = PortalUtil.getDate(
106                                            startDateMonth, startDateDay, startDateYear, startDateHour,
107                                            startDateMinute, themeDisplay.getTimeZone(),
108                                            new PortalException());
109    
110                                    int endDateMonth = ParamUtil.getInteger(
111                                            actionRequest, "endDateMonth");
112                                    int endDateDay = ParamUtil.getInteger(
113                                            actionRequest, "endDateDay");
114                                    int endDateYear = ParamUtil.getInteger(
115                                            actionRequest, "endDateYear");
116                                    int endDateHour = ParamUtil.getInteger(
117                                            actionRequest, "endDateHour");
118                                    int endDateMinute = ParamUtil.getInteger(
119                                            actionRequest, "endDateMinute");
120                                    int endDateAmPm = ParamUtil.getInteger(
121                                            actionRequest, "endDateAmPm");
122    
123                                    if (endDateAmPm == Calendar.PM) {
124                                            endDateHour += 12;
125                                    }
126    
127                                    endDate = PortalUtil.getDate(
128                                            endDateMonth, endDateDay, endDateYear, endDateHour,
129                                            endDateMinute, themeDisplay.getTimeZone(),
130                                            new PortalException());
131                            }
132                            else if (range.equals("fromLastPublishDate")) {
133                                    LayoutSet layoutSet = LayoutSetLocalServiceUtil.getLayoutSet(
134                                            groupId, privateLayout);
135    
136                                    UnicodeProperties settingsProperties =
137                                            layoutSet.getSettingsProperties();
138    
139                                    long lastPublishDate = GetterUtil.getLong(
140                                            settingsProperties.getProperty("last-publish-date"));
141    
142                                    if (lastPublishDate > 0) {
143                                            Calendar cal = Calendar.getInstance(
144                                                    themeDisplay.getTimeZone(), themeDisplay.getLocale());
145    
146                                            endDate = cal.getTime();
147    
148                                            cal.setTimeInMillis(lastPublishDate);
149    
150                                            startDate = cal.getTime();
151                                    }
152                            }
153                            else if (range.equals("last")) {
154                                    int rangeLast = ParamUtil.getInteger(actionRequest, "last");
155    
156                                    Date now = new Date();
157    
158                                    startDate = new Date(now.getTime() - (rangeLast * Time.HOUR));
159    
160                                    endDate = now;
161                            }
162    
163                            File file = LayoutServiceUtil.exportLayoutsAsFile(
164                                    groupId, privateLayout, layoutIds,
165                                    actionRequest.getParameterMap(), startDate, endDate);
166    
167                            HttpServletRequest request = PortalUtil.getHttpServletRequest(
168                                    actionRequest);
169                            HttpServletResponse response = PortalUtil.getHttpServletResponse(
170                                    actionResponse);
171    
172                            ServletResponseUtil.sendFile(
173                                    request, response, fileName, new FileInputStream(file),
174                                    ContentTypes.APPLICATION_ZIP);
175    
176                            FileUtil.delete(file);
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            }
191    
192            @Override
193            public ActionForward render(
194                            ActionMapping mapping, ActionForm form, PortletConfig portletConfig,
195                            RenderRequest renderRequest, RenderResponse renderResponse)
196                    throws Exception {
197    
198                    try {
199                            ActionUtil.getGroup(renderRequest);
200                    }
201                    catch (Exception e) {
202                            if (e instanceof NoSuchGroupException ||
203                                    e instanceof PrincipalException) {
204    
205                                    SessionErrors.add(renderRequest, e.getClass().getName());
206    
207                                    return mapping.findForward("portlet.layouts_admin.error");
208                            }
209                            else {
210                                    throw e;
211                            }
212                    }
213    
214                    return mapping.findForward(
215                            getForward(renderRequest, "portlet.layouts_admin.export_layouts"));
216            }
217    
218            private static Log _log = LogFactoryUtil.getLog(ExportLayoutsAction.class);
219    
220    }