001    /**
002     * Copyright (c) 2000-2012 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 mapping, ActionForm form, PortletConfig portletConfig,
050                            ResourceRequest resourceRequest, ResourceResponse resourceResponse)
051                    throws Exception {
052    
053                    HttpServletRequest request = PortalUtil.getHttpServletRequest(
054                            resourceRequest);
055                    HttpServletResponse response = PortalUtil.getHttpServletResponse(
056                            resourceResponse);
057    
058                    ThemeDisplay themeDisplay = (ThemeDisplay)resourceRequest.getAttribute(
059                            WebKeys.THEME_DISPLAY);
060    
061                    long recordSetId = ParamUtil.getLong(resourceRequest, "recordSetId");
062    
063                    DDLRecordSet recordSet = DDLRecordSetServiceUtil.getRecordSet(
064                            recordSetId);
065    
066                    String fileExtension = ParamUtil.getString(
067                            resourceRequest, "fileExtension");
068    
069                    String fileName =
070                            recordSet.getName(themeDisplay.getLocale()) + CharPool.PERIOD +
071                                    fileExtension;
072    
073                    DDLExportFormat exportFormat = DDLExportFormat.parse(fileExtension);
074    
075                    DDLExporter exporter = DDLExporterFactory.getDDLExporter(exportFormat);
076    
077                    byte[] bytes = exporter.export(
078                            recordSetId, WorkflowConstants.STATUS_APPROVED);
079    
080                    String contentType = MimeTypesUtil.getContentType(fileName);
081    
082                    ServletResponseUtil.sendFile(
083                            request, response, fileName, bytes, contentType);
084            }
085    
086    }