springboot + birt Design Engine API,Report Engine API

来源:互联网 发布:每天读书 知乎 编辑:程序博客网 时间:2024/06/05 00:10

springboot + birt

1.pom:

    <dependency>
        <groupId>org.eclipse.birt.runtime</groupId>
        <artifactId>org.eclipse.birt.runtime</artifactId>
        <version>4.2.0</version>
    </dependency>


2.service

package com.huawei.jad.service;


import java.util.logging.Level;

import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportEngineFactory;
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.SessionHandle;
import org.eclipse.core.internal.registry.RegistryProviderFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;

import com.huawei.jad.util.Path;
import com.ibm.icu.util.ULocale;

@Service
public class BaseService implements ApplicationContextAware
{
    public boolean isSingleton() {
        return true;
    }
    
    private ApplicationContext context;
    private IReportEngine reportEngine;
    private IDesignEngine designEngine;
            
    private ReportDesignHandle reportDesignHandle;

    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException
    {
        this.context = ctx;
        
    }
    /**
     *
     * TODO get EngineConfigObject(
     * @author zzhang
     * @date Jun 15, 2017
     * @return
     */
    public EngineConfig getEngineConfigObject(){
        EngineConfig config = new EngineConfig();  
        config.setBIRTHome(Path.getRootPath() + "ReportHome");
        config.setLogConfig(Path.getRootPath() + "ReportHome/logs", Level.FINE);
        config.getAppContext().put("spring", this.context);
        return config;
    }
    /**
     *
     * TODO get DesignConfigObject
     * @author zzhang
     * @date Jun 15, 2017
     * @return
     */
    public DesignConfig getDesignConfigObject(){
        DesignConfig config = new DesignConfig();
        config.setProperty("BIRT_HOME", Path.getRootPath() + "ReportHome");
        return config;
    }
    /**
     *
     * TODO get factory object
     * @author zzhang
     * @date Jun 15, 2017
     * @param config
     * @return
     */
    public IReportEngineFactory getReportEngineFactoryObject(EngineConfig config){

        try
        {
            Platform.startup(config);
        }
        catch (BirtException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        final IReportEngineFactory FACTORY = (IReportEngineFactory) Platform
            .createFactoryObject(IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY);
        return FACTORY;
    }
    
    public IDesignEngineFactory getDesignEngineFactoryObject(DesignConfig config){
        try
        {
            Platform.startup(config);
        }
        catch (BirtException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        final IDesignEngineFactory factory = (IDesignEngineFactory) Platform
        .createFactoryObject( IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY );
        return factory;
    }
    /**
     *     
     * TODO get ReportEngineObject
     * @author zzhang
     * @date Jun 15, 2017
     * @param config
     * @return
     */
    public IReportEngine getReportEngineObject(EngineConfig config){
        reportEngine = getReportEngineFactoryObject(config).createReportEngine(config);
        return reportEngine;
    }
    /**
     *     
     * TODO get DesignEngineObject
     * @author zzhang
     * @date Jun 15, 2017
     * @param config
     * @return
     */
    public IDesignEngine DesignEngineObject(DesignConfig config){
        designEngine = getDesignEngineFactoryObject(config).createDesignEngine(config);
        return designEngine;
        
    }
    /**
     *     
     * TODO get ReportDesignHandle
     * @author zzhang
     * @date Jun 15, 2017
     * @param config
     * @return
     */
    public ReportDesignHandle getReportDesignHandle(DesignConfig config){
        reportDesignHandle = DesignEngineObject(config).newSessionHandle( ULocale.ENGLISH ).createDesign( );
        return reportDesignHandle;
    }
    /**
     *
     * TODO close ReportEngineObject
     * @author zzhang
     * @date Jun 15, 2017
     */
    public void destroyReportEngine(){
        try{
            reportEngine.destroy();
            Platform.shutdown();
            RegistryProviderFactory.releaseDefault();
        }catch(Exception e){
            System.out.println("!Read reprot exception");
        }

    }
    public void destroyDesignEngine(String newName){
        try{
            reportDesignHandle.saveAs(newName);
            reportDesignHandle.close();
        }catch(Exception e){
            System.out.println("!Design reprot exception");
        }

    }
}



3.controller

package com.huawei.jad.controller;

import org.eclipse.birt.report.engine.api.EngineException;
import org.eclipse.birt.report.engine.api.HTMLRenderOption;
import org.eclipse.birt.report.engine.api.IReportEngine;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.GridHandle;
import org.eclipse.birt.report.model.api.ImageHandle;
import org.eclipse.birt.report.model.api.LabelHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.RowHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.command.ContentException;
import org.eclipse.birt.report.model.api.command.NameException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.huawei.jad.service.TestServices;
import com.huawei.jad.util.Path;
/**
 *
 *@Description:TODO
 *@author zzhang
 *@time:Apr 12, 2017 5:35:09 PM
 */
@RestController
public class reportEngineController {
    
    private Logger logger = LoggerFactory.getLogger(reportEngineController.class);

    @Autowired
    private TestServices testServices;
    
    @RequestMapping(value = "/show")
    public String show(){
        IReportEngine birtEngine = testServices.getReportEngineObject(testServices.getEngineConfigObject());
        IReportRunnable design =null;
        try
        {
            design = birtEngine.openReportDesign(Path.getRootPath() + "ReportHome/ReportFile/new_report.rptdesign");
            IRunAndRenderTask task = birtEngine.createRunAndRenderTask(design);
            final HTMLRenderOption HTML_OPTIONS = new HTMLRenderOption();       
            HTML_OPTIONS.setOutputFileName(Path.getRootPath() + "ReportHome/ReportFile/Parmdisp.html");
            HTML_OPTIONS.setOutputFormat("html");
            
            task.setRenderOption(HTML_OPTIONS);
            
            task.run();
        }
        catch (EngineException e)
        {
            logger.info("!System exception");
        } finally {
            testServices.destroyReportEngine();
       }
        return "去!";
    }
    
    @RequestMapping(value = "/tmi")
    public String tmi(){
        ReportDesignHandle reportDesignHandle = testServices.getReportDesignHandle(testServices.getDesignConfigObject());
        ElementFactory factory = reportDesignHandle.getElementFactory( );
        DesignElementHandle element = factory.newSimpleMasterPage( "Page Master" ); //$NON-NLS-1$
        try
        {
            reportDesignHandle.getMasterPages().add(element);
            GridHandle grid = factory.newGridItem( null, 2 /* cols */, 1 /* row */ );
            reportDesignHandle.getBody( ).add( grid );
            
            grid.setWidth( "100%" );
            
            RowHandle row = (RowHandle) grid.getRows( ).get( 0 );
            
            ImageHandle image = factory.newImage( null );
            CellHandle cell = (CellHandle) row.getCells( ).get( 0 );
            cell.getContent( ).add( image );
            image.setURL( Path.getRootPath() + "ReportHome/image/1494433950(1).jpg" );
            
            LabelHandle label = factory.newLabel( null );
            cell = (CellHandle) row.getCells( ).get( 1 );
            cell.getContent( ).add( label );
            label.setText( "Hello, world!" ); //$NON-NLS-1$
        }
        catch (ContentException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (NameException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (SemanticException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            testServices.destroyDesignEngine(Path.getRootPath() + "ReportHome/ReportFile/new_report.rptdesign");
        }
        return "secuess";
    }
    @RequestMapping(value = "/ci")
    public String ci(){
        return null;
    }
    
}


原创粉丝点击