001/*
002 * (C) Copyright 2013-2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 * Contributors:
016 *     Vladimir Pasquier <vpasquier@nuxeo.com>
017 */
018package org.nuxeo.ecm.platform.groups.audit.service;
019
020import java.io.File;
021import java.io.IOException;
022import java.util.HashMap;
023import java.util.Map;
024
025import net.sf.jxls.exception.ParsePropertyException;
026import net.sf.jxls.transformer.XLSTransformer;
027
028import org.apache.commons.io.FileUtils;
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
032
033import org.nuxeo.common.Environment;
034import org.nuxeo.runtime.model.ComponentInstance;
035import org.nuxeo.runtime.model.DefaultComponent;
036
037/**
038 * Excel Export service generating Excel report file
039 *
040 * @since 5.7
041 */
042public class ExcelExportServiceImpl extends DefaultComponent implements ExcelExportService {
043
044    public static final Log log = LogFactory.getLog(ExcelExportServiceImpl.class);
045
046    public static final String EXCEL_EXPORT_EP = "excelExportFactory";
047
048    protected static final Map<String, ExcelExportServiceDescriptor> exportExcelRegistry = new HashMap<>();
049
050    @Override
051    public void registerContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
052        if (EXCEL_EXPORT_EP.equals(extensionPoint)) {
053            ExcelExportServiceDescriptor desc = (ExcelExportServiceDescriptor) contribution;
054            exportExcelRegistry.put(desc.getName(), desc);
055        } else {
056            log.error("Unknown extension point " + extensionPoint);
057        }
058    }
059
060    @Override
061    public void unregisterContribution(Object contribution, String extensionPoint, ComponentInstance contributor) {
062    }
063
064    /**
065     * Get excel export for a given name (contributed)
066     */
067    @Override
068    public File getExcelReport(String exportName) {
069        XLSTransformer transformer = new XLSTransformer();
070        File resultReport = null;
071        try {
072            resultReport = new File(getWorkingDir(), "audit-groups.xls");
073            resultReport.createNewFile();
074            ExcelExportServiceDescriptor descriptor = exportExcelRegistry.get(exportName);
075            transformer.transformXLS(descriptor.getTemplate().getAbsolutePath(), descriptor.getFactory()
076                                                                                           .getDataToInject(),
077                    resultReport.getAbsolutePath());
078        } catch (IOException | ParsePropertyException | InvalidFormatException e) {
079            log.error("Unable to create excel report result file:", e);
080        }
081        return resultReport;
082    }
083
084    /**
085     * Get excel export for a given name and given data
086     */
087    @Override
088    public File getExcelReport(String exportName, Map<String, Object> data) {
089        XLSTransformer transformer = new XLSTransformer();
090        File resultReport = null;
091        try {
092            resultReport = new File(getWorkingDir(), "audit-groups.xls");
093            resultReport.createNewFile();
094            ExcelExportServiceDescriptor descriptor = exportExcelRegistry.get(exportName);
095            transformer.transformXLS(descriptor.getTemplate().getAbsolutePath(), data, resultReport.getAbsolutePath());
096        } catch (IOException | ParsePropertyException | InvalidFormatException e) {
097            log.error("Unable to create excel report result file:", e);
098        }
099        return resultReport;
100    }
101
102    protected File getWorkingDir() {
103        File workingDir = new File(Environment.getDefault().getTemp(), "NXExcelExport" + System.currentTimeMillis());
104        if (workingDir.exists()) {
105            FileUtils.deleteQuietly(workingDir);
106        }
107        workingDir.mkdirs();
108        return workingDir;
109    }
110
111}