001    /**
002     * Copyright (c) 2000-2013 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.dynamicdatalists.action;
016    
017    import com.liferay.portal.kernel.servlet.ServletResponseUtil;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.MimeTypesUtil;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.kernel.workflow.WorkflowConstants;
022    import com.liferay.portal.struts.PortletAction;
023    import com.liferay.portal.theme.ThemeDisplay;
024    import com.liferay.portal.util.PortalUtil;
025    import com.liferay.portal.util.WebKeys;
026    import com.liferay.portlet.dynamicdatalists.model.DDLRecordSet;
027    import com.liferay.portlet.dynamicdatalists.service.DDLRecordSetServiceUtil;
028    import com.liferay.portlet.dynamicdatalists.util.DDLExportFormat;
029    import com.liferay.portlet.dynamicdatalists.util.DDLExporter;
030    import com.liferay.portlet.dynamicdatalists.util.DDLExporterFactory;
031    
032    import javax.portlet.PortletConfig;
033    import javax.portlet.ResourceRequest;
034    import javax.portlet.ResourceResponse;
035    
036    import javax.servlet.http.HttpServletRequest;
037    import javax.servlet.http.HttpServletResponse;
038    
039    import org.apache.struts.action.ActionForm;
040    import org.apache.struts.action.ActionMapping;
041    
042    /**
043     * @author Marcellus Tavares
044     */
045    public class ExportRecordSetAction extends PortletAction {
046    
047            @Override
048            public void serveResource(
049                            ActionMapping actionMapping, ActionForm actionForm,
050                            PortletConfig portletConfig, ResourceRequest resourceRequest,
051                            ResourceResponse resourceResponse)
052                    throws Exception {
053    
054                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
055                            resourceRequest);
056                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
057                            resourceResponse);
058    
059                    ThemeDisplay themeDisplay = (ThemeDisplay)resourceRequest.getAttribute(
060                            WebKeys.THEME_DISPLAY);
061    
062                    long recordSetId = ParamUtil.getLong(resourceRequest, "recordSetId");
063    
064                    DDLRecordSet recordSet = DDLRecordSetServiceUtil.getRecordSet(
065                            recordSetId);
066    
067                    String fileExtension = ParamUtil.getString(
068                            resourceRequest, "fileExtension");
069    
070                    String fileName =
071                            recordSet.getName(themeDisplay.getLocale()) + CharPool.PERIOD +
072                                    fileExtension;
073    
074                    DDLExportFormat exportFormat = DDLExportFormat.parse(fileExtension);
075    
076                    DDLExporter exporter = DDLExporterFactory.getDDLExporter(exportFormat);
077    
078                    exporter.setLocale(themeDisplay.getLocale());
079    
080                    byte[] bytes = exporter.export(
081                            recordSetId, WorkflowConstants.STATUS_APPROVED);
082    
083                    String contentType = MimeTypesUtil.getContentType(fileName);
084    
085                    ServletResponseUtil.sendFile(
086                            request, response, fileName, bytes, contentType);
087            }
088    
089    }