001/* 002 * (C) Copyright 2013 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * Contributors: 014 * Vladimir Pasquier <vpasquier@nuxeo.com> 015 */ 016package org.nuxeo.ecm.platform.groups.audit.service; 017 018import java.io.File; 019import java.io.IOException; 020import java.util.HashMap; 021import java.util.Map; 022 023import net.sf.jxls.exception.ParsePropertyException; 024import net.sf.jxls.transformer.XLSTransformer; 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.apache.poi.openxml4j.exceptions.InvalidFormatException; 029import org.nuxeo.common.utils.FileUtils; 030import org.nuxeo.runtime.model.ComponentInstance; 031import org.nuxeo.runtime.model.DefaultComponent; 032 033/** 034 * Excel Export service generating Excel report file 035 * 036 * @since 5.7 037 */ 038public class ExcelExportServiceImpl extends DefaultComponent implements ExcelExportService { 039 040 public static final Log log = LogFactory.getLog(ExcelExportServiceImpl.class); 041 042 public static final String EXCEL_EXPORT_EP = "excelExportFactory"; 043 044 protected static final Map<String, ExcelExportServiceDescriptor> exportExcelRegistry = new HashMap<String, ExcelExportServiceDescriptor>(); 045 046 @Override 047 public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 048 if (EXCEL_EXPORT_EP.equals(extensionPoint)) { 049 ExcelExportServiceDescriptor desc = (ExcelExportServiceDescriptor) contribution; 050 exportExcelRegistry.put(desc.getName(), desc); 051 } else { 052 log.error("Unknown extension point " + extensionPoint); 053 } 054 } 055 056 @Override 057 public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) { 058 } 059 060 /** 061 * Get excel export for a given name (contributed) 062 */ 063 @Override 064 public File getExcelReport(String exportName) { 065 XLSTransformer transformer = new XLSTransformer(); 066 File resultReport = null; 067 try { 068 resultReport = new File(getWorkingDir(), "audit-groups.xls"); 069 resultReport.createNewFile(); 070 ExcelExportServiceDescriptor descriptor = exportExcelRegistry.get(exportName); 071 transformer.transformXLS(descriptor.getTemplate().getAbsolutePath(), 072 descriptor.getFactory().getDataToInject(), resultReport.getAbsolutePath()); 073 } catch (IOException | ParsePropertyException | InvalidFormatException e) { 074 log.error("Unable to create excel report result file:", e); 075 } 076 return resultReport; 077 } 078 079 /** 080 * Get excel export for a given name and given data 081 */ 082 @Override 083 public File getExcelReport(String exportName, Map<String, Object> data) { 084 XLSTransformer transformer = new XLSTransformer(); 085 File resultReport = null; 086 try { 087 resultReport = new File(getWorkingDir(), "audit-groups.xls"); 088 resultReport.createNewFile(); 089 ExcelExportServiceDescriptor descriptor = exportExcelRegistry.get(exportName); 090 transformer.transformXLS(descriptor.getTemplate().getAbsolutePath(), data, resultReport.getAbsolutePath()); 091 } catch (IOException | ParsePropertyException | InvalidFormatException e) { 092 log.error("Unable to create excel report result file:", e); 093 } 094 return resultReport; 095 } 096 097 protected File getWorkingDir() { 098 String dirPath = System.getProperty("java.io.tmpdir") + "/NXExcelExport" + System.currentTimeMillis(); 099 File workingDir = new File(dirPath); 100 if (workingDir.exists()) { 101 FileUtils.deleteTree(workingDir); 102 } 103 workingDir.mkdir(); 104 return workingDir; 105 } 106 107}