| /** |
| * |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| */ |
| package org.eclipse.osbp.report; |
| |
| import java.io.ByteArrayInputStream; |
| import java.io.ByteArrayOutputStream; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.net.URL; |
| import java.util.Map; |
| |
| import org.eclipse.birt.core.exception.BirtException; |
| import org.eclipse.birt.core.framework.IPlatformContext; |
| import org.eclipse.birt.core.framework.Platform; |
| import org.eclipse.birt.core.framework.PlatformFileContext; |
| import org.eclipse.birt.report.engine.api.EngineConfig; |
| import org.eclipse.birt.report.engine.api.EngineException; |
| import org.eclipse.birt.report.engine.api.HTMLRenderOption; |
| import org.eclipse.birt.report.engine.api.IRenderOption; |
| import org.eclipse.birt.report.engine.api.IReportEngine; |
| import org.eclipse.birt.report.engine.api.IReportEngineFactory; |
| import org.eclipse.birt.report.engine.api.IReportRunnable; |
| import org.eclipse.birt.report.engine.api.IRunAndRenderTask; |
| import org.eclipse.birt.report.engine.api.PDFRenderOption; |
| import org.eclipse.birt.report.model.api.ReportDesignHandle; |
| import org.eclipse.birt.report.model.api.command.CustomMsgException; |
| import org.eclipse.osbp.report.pdfservice.PDFService; |
| import org.eclipse.osbp.ui.api.datamart.IDataMart; |
| import org.eclipse.osbp.ui.api.datamart.IDatamartFilterGenerator; |
| import org.eclipse.osbp.ui.api.metadata.IDSLMetadataService; |
| import org.eclipse.osbp.ui.api.report.IReportProvider; |
| import org.eclipse.osbp.ui.api.themes.IThemeResourceService; |
| import org.eclipse.osbp.ui.api.user.IUser; |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartDSLPackage; |
| import org.eclipse.osbp.xtext.datamartdsl.DatamartDefinition; |
| import org.eclipse.osbp.xtext.reportdsl.Report; |
| import org.eclipse.osbp.xtext.reportdsl.ReportDSLPackage; |
| import org.osgi.service.component.annotations.Component; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| import com.lowagie.text.DocumentException; |
| import com.lowagie.text.pdf.PdfCopyFields; |
| import com.lowagie.text.pdf.PdfReader; |
| |
| @Component(immediate = true) |
| public class ReportProvider implements IReportProvider { |
| public enum Rendering { |
| PDF_FILE(false, true, true, false, false), PDF_STREAM(false, true, |
| false, true, false), |
| /** @see {@link http://www.eclipse.org/forums/index.php/t/204085/} */ |
| PDF_PRINT_STREAM(false, true, false, true, true), HTML_FILE(true, |
| false, true, false, false), HTML_STREAM(true, false, false, |
| true, false); |
| |
| public boolean asHtml() { |
| return asHtml; |
| } |
| |
| public boolean asPdf() { |
| return asPdf; |
| } |
| |
| public boolean asFile() { |
| return asFile; |
| } |
| |
| public boolean asStream() { |
| return asStream; |
| } |
| |
| public boolean forPrinting() { |
| return forPrinting; |
| } |
| |
| private final boolean asHtml; |
| private final boolean asPdf; |
| private final boolean asFile; |
| private final boolean asStream; |
| private final boolean forPrinting; |
| |
| private Rendering(boolean asHtml, boolean asPdf, boolean asFile, |
| boolean asStream, boolean forPrinting) { |
| this.asHtml = asHtml; |
| this.asPdf = asPdf; |
| this.asFile = asFile; |
| this.asStream = asStream; |
| this.forPrinting = forPrinting; |
| } |
| } |
| |
| private final Logger LOGGER = LoggerFactory |
| .getLogger(ReportProvider.class); |
| |
| private IReportEngineFactory reportFactory = (IReportEngineFactory) Platform |
| .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY); |
| // private IDesignEngineFactory designFactory = (IDesignEngineFactory) |
| // Platform.createFactoryObject(IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY); |
| private IReportEngine reportEngine; |
| // private IDesignEngine designEngine; |
| |
| private EngineConfig engineConfig = new EngineConfig(); |
| |
| // private DesignConfig designConfig = new DesignConfig(); |
| |
| public ReportProvider() { |
| IPlatformContext context = new PlatformFileContext(); |
| engineConfig.setResourcePath("i18n"); |
| engineConfig.setPlatformContext(context); |
| // designConfig.setPlatformContext(context); |
| try { |
| Platform.startup(engineConfig); |
| reportEngine = reportFactory.createReportEngine(engineConfig); |
| // designEngine = designFactory.createDesignEngine( designConfig ); |
| } catch (BirtException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| |
| public void printReportAsPdf(String reportId, IUser user, IDSLMetadataService dslMetadataService, Map<String, String> filter) { |
| PDFService.printReportAsPdf(get(reportId, user, dslMetadataService, filter), user.getPrintService()); |
| } |
| |
| public InputStream get(String reportId, IUser user, IDSLMetadataService dslMetadataService, Map<String, String> filter) { |
| return get(reportId, user, dslMetadataService, filter, IReportProvider.Rendering.PDF_PRINT_STREAM, null, null); |
| } |
| |
| @SuppressWarnings("unchecked") |
| public InputStream get(String reportId, IUser user, |
| IDSLMetadataService dslMetadataService, Map<String, String> filter, |
| IReportProvider.Rendering renderer, |
| IThemeResourceService themeResourceService, |
| String outputPath) { |
| if (reportEngine == null) { |
| LOGGER.error("report engine not initialized"); |
| return null; |
| } |
| // prepare datamart instance |
| Report report = (Report) dslMetadataService.getMetadata(reportId, ReportDSLPackage.Literals.REPORT); |
| if(report == null) { |
| LOGGER.error("report with id "+reportId+" not found"); |
| return null; |
| } |
| String datamartId = report.getDatamartRef().getName();//dslMetadataService.getFullyQualifiedName(report.getDatamartRef()); |
| DatamartDefinition datamart = (DatamartDefinition) dslMetadataService.getMetadata(datamartId, DatamartDSLPackage.Literals.DATAMART_DEFINITION); |
| if(datamart == null) { |
| LOGGER.error("datamart with id "+datamartId+" not found"); |
| return null; |
| } |
| IDataMart datamartInstance = (IDataMart) dslMetadataService.getClassInstance(datamart, "datamart"); |
| if(datamartInstance == null) { |
| LOGGER.error("report with id "+reportId+" has invalid datamart "+datamartId); |
| return null; |
| } |
| // create report |
| ByteArrayInputStream bis = null; |
| IRunAndRenderTask task = null; |
| InputStream inputStream = null; |
| try { |
| URL url = dslMetadataService.getResourceURL(report, "report", "rptdesign"); |
| inputStream = url.openStream(); |
| if (inputStream != null) { |
| IReportRunnable design = reportEngine.openReportDesign(url.toExternalForm(), inputStream); |
| if (design.getDesignHandle() instanceof ReportDesignHandle) { |
| addTranslations( |
| (ReportDesignHandle) design.getDesignHandle(), |
| dslMetadataService); |
| } |
| task = reportEngine.createRunAndRenderTask(design); |
| task.getAppContext().put( |
| IReportProvider.APPCONTEXT_THEMERESOURCESERVICE, |
| themeResourceService); |
| task.getAppContext().put( |
| IReportProvider.APPCONTEXT_DSLMETADATASERVICE, |
| dslMetadataService); |
| task.getAppContext().put(IReportProvider.APPCONTEXT_USER, user); |
| task.getAppContext().put( |
| IReportProvider.APPCONTEXT_DATAMARTINSTANCE, |
| datamartInstance); |
| task.getAppContext().put( |
| IReportProvider.APPCONTEXT_FILTERMAP, |
| filter); |
| task.setLocale(user.getLocale()); |
| |
| // set output options |
| IRenderOption options = null; |
| ByteArrayOutputStream bos = null; |
| if (renderer.asHtml()) { |
| options = new HTMLRenderOption(); |
| options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML); |
| ((HTMLRenderOption) options).setEmbeddable(true); |
| } else if (renderer.asPdf()) { |
| options = new PDFRenderOption(); |
| options.setOutputFormat(PDFRenderOption.OUTPUT_FORMAT_PDF); |
| } |
| if (options != null) { |
| if (renderer.asFile()) { |
| options.setOutputFileName(outputPath); |
| } else if (renderer.asStream()) { |
| bos = new ByteArrayOutputStream(); |
| options.setOutputStream(bos); |
| } |
| } |
| task.setRenderOption(options); |
| task.run(); |
| |
| if (renderer.asStream()) { |
| if (renderer.forPrinting() && (bos != null)) { |
| PdfReader js1 = new PdfReader(bos.toByteArray()); |
| bos = new ByteArrayOutputStream(); |
| PdfCopyFields cpy = new PdfCopyFields(bos); |
| cpy.addDocument(js1); |
| cpy.addJavaScript("this.print({bUI: true,bSilent: false,bShrinkToFit:true});"); |
| cpy.close(); |
| } |
| if (bos != null) { |
| bis = new ByteArrayInputStream(bos.toByteArray()); |
| } |
| } |
| } |
| } catch (EngineException | NullPointerException | IOException |
| | DocumentException e) { |
| LOGGER.error("{}", e); |
| } finally { |
| if(inputStream != null) { |
| try { |
| inputStream.close(); |
| } catch (IOException e) { |
| } |
| } |
| if (task != null) { |
| task.close(); |
| } |
| } |
| return bis; |
| } |
| |
| private void addTranslations(ReportDesignHandle reportDesignHandle, |
| IDSLMetadataService dslMetadataService) { |
| for (String locale : dslMetadataService.getTranslations().keySet()) { |
| // TODO: birt4.4 cannot handle fallbacks of language variants with |
| // missing keys when locale was added - so don't supply for this |
| // version |
| if (!locale.contains("_")) { |
| for (Object key : dslMetadataService.getTranslations() |
| .get(locale).keySet()) { |
| try { |
| reportDesignHandle.addTranslation((String) key, locale, |
| (String) dslMetadataService.getTranslations() |
| .get(locale).get(key)); |
| } catch (CustomMsgException e) { |
| LOGGER.error("{}", e); |
| } |
| } |
| } |
| } |
| } |
| } |