| /** |
| * |
| * 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.xtext.reportdsl.common; |
| |
| import java.io.ByteArrayInputStream; |
| import java.io.ByteArrayOutputStream; |
| import java.io.IOException; |
| import java.io.InputStream; |
| import java.net.URL; |
| import java.util.Properties; |
| import java.util.logging.Level; |
| |
| 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.DesignConfig; |
| import org.eclipse.birt.report.model.api.IDesignEngine; |
| import org.eclipse.birt.report.model.api.IDesignEngineFactory; |
| import org.eclipse.birt.report.model.api.ReportDesignHandle; |
| import org.eclipse.birt.report.model.api.command.CustomMsgException; |
| import org.eclipse.osbp.ui.api.metadata.IDSLMetadataService; |
| import org.eclipse.osbp.ui.api.report.IReportProvider; |
| import org.eclipse.osbp.ui.api.report.IReportProvider.Rendering; |
| import org.eclipse.osbp.utils.session.VaadinSessionAttributes; |
| import org.eclipse.osbp.xtext.reportdsl.jvmmodel.ReportModelGenerator; |
| 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; |
| |
| public class BirtEngine { |
| |
| |
| private static final Logger LOGGER = LoggerFactory.getLogger(BirtEngine.class); |
| private final static String CONFIG_FILE = "BirtConfig.properties"; |
| |
| private static Properties configProps = new Properties(); |
| private static EngineConfig config = new EngineConfig(); |
| private static boolean isStarted = false; |
| |
| public static void start() { |
| if (!isStarted) { |
| LOGGER.debug("CCBirtEngine - start"); |
| loadEngineProps(); |
| isStarted = true; |
| } |
| } |
| |
| public static void stop() { |
| if (isStarted) { |
| LOGGER.debug("CCBirtEngine - stop"); |
| Platform.shutdown(); |
| isStarted = false; |
| } |
| } |
| |
| /** |
| * <b>only callable by</b><ul> |
| * <li>{@link #getReportEngine()}</li> |
| * <li>{@link ReportModelGenerator#generateReportFile(org.eclipse.osbp.xtext.reportdsl.CCReportPackage, org.eclipse.osbp.xtext.reportdsl.CCReport, org.eclipse.xtext.generator.IFileSystemAccess)}</li> |
| * </ul> |
| */ |
| public static IReportEngine createReportEngine(BaseReport report) { |
| start(); |
| config.setResourcePath("i18n"); |
| config.setResourceLocator(new ResourceLocator(report)); |
| config.setLogConfig("log/birt.log",Level.FINE); |
| IPlatformContext context = new PlatformFileContext(); |
| config.setPlatformContext( context ); |
| try { |
| Platform.startup( config ); |
| } |
| catch ( BirtException e ) { |
| LOGGER.error(e.getLocalizedMessage()); |
| if (e.getCause()!=null) LOGGER.error(" "+e.getCause()); |
| } |
| IReportEngineFactory factory = (IReportEngineFactory) Platform.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY ); |
| return factory.createReportEngine( config ); |
| } |
| |
| public static synchronized IReportEngine getReportEngine(BaseReport report) { |
| IReportEngine result = (IReportEngine) VaadinSessionAttributes.get(IReportEngine.class); |
| if (result == null) { |
| result = createReportEngine(report); |
| VaadinSessionAttributes.set(IReportEngine.class, result); |
| LOGGER.debug("CCBirtEngine - session-static report engine created"); |
| } |
| return result; |
| } |
| |
| public static synchronized void destroyReportEngine() { |
| IReportEngine result = (IReportEngine) VaadinSessionAttributes.get(IReportEngine.class); |
| if (result != null) { |
| result.destroy(); |
| VaadinSessionAttributes.remove(IReportEngine.class); |
| LOGGER.debug("CCBirtEngine - session-static report engine destroyed"); |
| } |
| } |
| |
| /** |
| * <b>only callable by</b><ul> |
| * <li>{@link #getDesignEngine()}</li> |
| * <li>{@link ReportModelGenerator#generateReportFile(org.eclipse.osbp.xtext.reportdsl.CCReportPackage, org.eclipse.osbp.xtext.reportdsl.CCReport, org.eclipse.xtext.generator.IFileSystemAccess)}</li> |
| * </ul> |
| */ |
| public static IDesignEngine createDesignEngine() { |
| IDesignEngine result = null; |
| try { |
| start(); |
| DesignConfig config = new DesignConfig(); |
| IDesignEngineFactory engineFactory = (IDesignEngineFactory) Platform.createFactoryObject(IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY); |
| Platform.startup(config); |
| result = engineFactory.createDesignEngine(config); |
| } catch (BirtException e) { |
| LOGGER.error(e.getLocalizedMessage()); |
| if (e.getCause()!=null) LOGGER.error(" "+e.getCause()); |
| } |
| return result; |
| } |
| |
| public static synchronized IDesignEngine getDesignEngine() { |
| IDesignEngine result = (IDesignEngine) VaadinSessionAttributes.get(IDesignEngine.class); |
| if (result == null) { |
| result = createDesignEngine(); |
| if (result != null) { |
| VaadinSessionAttributes.set(IDesignEngine.class, result); |
| LOGGER.debug("CCBirtEngine - session-static design engine created"); |
| } |
| } |
| return result; |
| } |
| |
| public Object clone() throws CloneNotSupportedException { |
| throw new CloneNotSupportedException(); |
| } |
| |
| private static void loadEngineProps() { |
| try { |
| //Config File must be in classpath |
| ClassLoader cl = Thread.currentThread ().getContextClassLoader(); |
| InputStream in = null; |
| in = cl.getResourceAsStream (CONFIG_FILE); |
| if (in != null) { |
| configProps.load(in); |
| in.close(); |
| } |
| } catch (Exception e) { |
| LOGGER.error(e.getLocalizedMessage()); |
| if (e.getCause()!=null) LOGGER.error(" "+e.getCause()); |
| } |
| |
| } |
| |
| public static InputStream get(BaseReport report, Rendering renderer) { |
| return get(report, renderer, null); |
| } |
| |
| @SuppressWarnings("unchecked") |
| public static InputStream get(BaseReport report, Rendering renderer, String path) { |
| IReportEngine reportEngine = getReportEngine(report); |
| IReportRunnable design = null; |
| ByteArrayInputStream bis = null; |
| |
| try { |
| // Open report design |
| Object object = report.getRunnableOrFilename(); |
| if (object instanceof String) { |
| URL url = null; |
| InputStream inputStream = null; |
| try { |
| url = report.getClass().getClassLoader().getResource((String)object); |
| inputStream = url.openStream(); |
| } |
| catch (Exception e2) { |
| url = null; |
| inputStream = null; |
| } |
| if (inputStream != null) { |
| design = reportEngine.openReportDesign(url.toExternalForm(), inputStream); |
| if (design.getDesignHandle() instanceof ReportDesignHandle) { |
| report.addCss((ReportDesignHandle) design.getDesignHandle()); |
| addTranslations((ReportDesignHandle) design.getDesignHandle(), report.getDslMetadataService()); |
| } |
| } |
| } |
| else if (object instanceof IReportRunnable) { |
| design = (IReportRunnable)object; |
| } |
| else if (object instanceof ReportDesignHandle) { |
| design = reportEngine.openReportDesign((ReportDesignHandle)object); |
| } |
| // create task to run and render report |
| if (design != null) { |
| IRunAndRenderTask task = reportEngine.createRunAndRenderTask(design); |
| task.getAppContext().put(IReportProvider.APPCONTEXT_REPORT, report); |
| task.getAppContext().put(IReportProvider.APPCONTEXT_THEMERESOURCESERVICE, report.getThemeResourceService()); |
| task.getAppContext().put(IReportProvider.APPCONTEXT_DSLMETADATASERVICE, report.getDslMetadataService()); |
| task.getAppContext().put(IReportProvider.APPCONTEXT_USER, report.getUser()); |
| task.getAppContext().put(IReportProvider.APPCONTEXT_DATAMARTINSTANCE, report.getDatamartInstance()); |
| task.getAppContext().put(IReportProvider.APPCONTEXT_FILTERMAP, report.getFilterGenerator().getFilter(false)); |
| task.getAppContext().put(IReportProvider.APPCONTEXT_PROPERTYLOOKUP, report.getPropertyLookupMap()); |
| task.setLocale(report.getUser().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(path); |
| } |
| else if (renderer.asStream()) { |
| bos = new ByteArrayOutputStream(); |
| // try { |
| // bos.toString("ISO-8859-1"); |
| // } catch (UnsupportedEncodingException e) { |
| // e.printStackTrace(); |
| // } |
| options.setOutputStream(bos); |
| } |
| } |
| task.setRenderOption(options); |
| // run report |
| task.run(); |
| |
| if (renderer.asStream()) { |
| if (renderer.forPrinting() && (bos != null)) { |
| // --- get the generated BIRT PDF document --- |
| PdfReader js1 = new PdfReader(bos.toByteArray()); |
| // --- create a new output stream --- |
| bos = new ByteArrayOutputStream(); |
| // --- generate a new PDF document --- |
| PdfCopyFields cpy = new PdfCopyFields(bos); // new FileOutputStream("output/resample/printdialog.pdf" )); |
| // --- add the previously generated BIRT PDF document --- |
| cpy.addDocument(js1); |
| // --- add a print-statement into the new PDF document --- |
| cpy.addJavaScript( "this.print({bUI: true,bSilent: false,bShrinkToFit:true});" ); |
| cpy.close(); |
| } |
| if (bos != null) { |
| bis = new ByteArrayInputStream(bos.toByteArray()); |
| } |
| } |
| task.close(); |
| } |
| } |
| catch (EngineException | NullPointerException | IOException | DocumentException e) { |
| bis = null; |
| LOGGER.error(e.getLocalizedMessage()); |
| if (e.getCause()!=null) LOGGER.error(" "+e.getCause()); |
| } |
| return bis; |
| } |
| |
| private static 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); |
| } |
| } |
| } |
| } |
| } |
| } |