使用BIRT API创建交叉报表2

https://code.google.com/p/my-open-source/source/browse/code/birt/CreateDataCube.java?spec=svn4&r=4

package tes.birt;

import java.io.IOException;

import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.item.crosstab.core.ICrosstabConstants;
import org.eclipse.birt.report.item.crosstab.core.de.CrosstabCellHandle;
import org.eclipse.birt.report.item.crosstab.core.de.CrosstabReportItemHandle;
import org.eclipse.birt.report.item.crosstab.core.de.DimensionViewHandle;
import org.eclipse.birt.report.item.crosstab.core.de.LevelViewHandle;
import org.eclipse.birt.report.item.crosstab.core.de.MeasureViewHandle;
import org.eclipse.birt.report.item.crosstab.core.util.CrosstabExtendedItemFactory;
import org.eclipse.birt.report.model.api.ComputedColumnHandle;
import org.eclipse.birt.report.model.api.DataItemHandle;
import org.eclipse.birt.report.model.api.DataSetHandle;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.ExtendedItemHandle;
import org.eclipse.birt.report.model.api.IDesignEngine;
import org.eclipse.birt.report.model.api.IDesignEngineFactory;
import org.eclipse.birt.report.model.api.LabelHandle;
import org.eclipse.birt.report.model.api.OdaDataSetHandle;
import org.eclipse.birt.report.model.api.OdaDataSourceHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.ReportItemHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants;
import org.eclipse.birt.report.model.api.elements.structures.ComputedColumn;
import org.eclipse.birt.report.model.api.extension.IReportItem;
import org.eclipse.birt.report.model.api.olap.MeasureGroupHandle;
import org.eclipse.birt.report.model.api.olap.MeasureHandle;
import org.eclipse.birt.report.model.api.olap.TabularCubeHandle;
import org.eclipse.birt.report.model.api.olap.TabularDimensionHandle;
import org.eclipse.birt.report.model.api.olap.TabularHierarchyHandle;
import org.eclipse.birt.report.model.api.olap.TabularLevelHandle;
import org.eclipse.birt.report.model.elements.interfaces.ICubeModel;

import com.ibm.icu.util.ULocale;

/**
 * Simple BIRT Design Engine API (DEAPI) demo.
 */

public class CreateDataCube {
        ReportDesignHandle designHandle = null;
        ElementFactory designFactory = null;
        StructureFactory structFactory = null;

        public static void main(String[] args) {
                try {
                        CreateDataCube de = new CreateDataCube();
                        de.buildReport();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (SemanticException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }

        void buildDataSource() throws SemanticException {

                OdaDataSourceHandle dsHandle = designFactory.newOdaDataSource(
                                "Data Source", "org.eclipse.birt.report.data.oda.jdbc");
                dsHandle.setProperty("odaDriverClass",
                                "org.eclipse.birt.report.data.oda.sampledb.Driver");
                dsHandle.setProperty("odaURL", "jdbc:classicmodels:sampledb");
                dsHandle.setProperty("odaUser", "ClassicModels");
                dsHandle.setProperty("odaPassword", "");
                designHandle.getDataSources().add(dsHandle);

        }

        void buildDataSet() throws SemanticException {

                OdaDataSetHandle dsHandle = designFactory.newOdaDataSet("ds",
                                "org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet");
                dsHandle.setDataSource("Data Source");
                String qry = "Select * from customers";

                dsHandle.setQueryText(qry);

                designHandle.getDataSets().add(dsHandle);

        }

        void buidCube() throws SemanticException {

                // 使用引擎工厂创建TabularCube 指定名称 创建多维数据集 名称为 "MyCube"
                TabularCubeHandle cubeHandle = designHandle.getElementFactory()
                                .newTabularCube("MyCube");

                // 获取第一个dataset 对象 填充到多维数据集 "MyCube"
                cubeHandle
                                .setDataSet((DataSetHandle) designHandle.getDataSets().get(0));
                // 把多维数据集加入引擎
                designHandle.getCubes().add(cubeHandle);

                // measure group 创建多为数据集的度量 名称为"testMeasureGroup"
                MeasureGroupHandle measureGroupHandle = designHandle
                                .getElementFactory().newTabularMeasureGroup("testMeasureGroup");

                // designHandle.getElementFactory().newTabularMeasureGroup("testMeasureGroup"
                // );

                cubeHandle.setProperty(ICubeModel.MEASURE_GROUPS_PROP,
                                measureGroupHandle);// 设置为度量的多为数据集
                // cubeHandle.add(ICubeModel.MEASURE_GROUPS_PROP, measureGroupHandle);
                // measure
                measureGroupHandle.add(MeasureGroupHandle.MEASURES_PROP,
                                designFactory.newTabularMeasure(null));
                MeasureHandle measure = (MeasureHandle) measureGroupHandle.getContent(
                                MeasureGroupHandle.MEASURES_PROP, 0);
                // 设置 Measure
                measure.setName("CREDITLIMIT");
                // 设置表达式
                measure.setMeasureExpression("dataSetRow['CREDITLIMIT']");
                // 设置计算函数
                measure.setFunction(DesignChoiceConstants.MEASURE_FUNCTION_SUM);
                // 是否根据其他度量计算

                measure.setCalculated(false);
                // 设置数据类型DesignChoiceConstants.COLUMN_DATA_TYPE_FLOAT
                measure.setDataType(DesignChoiceConstants.COLUMN_DATA_TYPE_FLOAT);

                // dimension 设置 未指定name
                TabularDimensionHandle dimension = designFactory
                                .newTabularDimension(null);
                cubeHandle.add(TabularCubeHandle.DIMENSIONS_PROP, dimension);
                dimension.setTimeType(false);
                // dimension.setDefaultHierarchy( factory
                // .newTabularHierarchy( "testDefaultHierarchy" ) );

                // hierarchy
                dimension.add(TabularDimensionHandle.HIERARCHIES_PROP,
                                designFactory.newTabularHierarchy(null));
                TabularHierarchyHandle hierarchy = (TabularHierarchyHandle) dimension
                                .getContent(TabularDimensionHandle.HIERARCHIES_PROP, 0);
                // hierarchy.setName( namePrix + hierarchy.getName( ) );
                hierarchy.setDataSet((DataSetHandle) designHandle.getDataSets().get(0));

                // 添加组信息
                hierarchy.add(TabularHierarchyHandle.LEVELS_PROP,
                                designFactory.newTabularLevel(dimension, null));
                TabularLevelHandle level = (TabularLevelHandle) hierarchy.getContent(
                                TabularHierarchyHandle.LEVELS_PROP, 0);
                level.setName("testlevel");
                level.setColumnName("CUSTOMERNAME");
                level.setDataType(DesignChoiceConstants.COLUMN_DATA_TYPE_STRING);

                // 创建 交叉报表
                ExtendedItemHandle xtab = CrosstabExtendedItemFactory
                                .createCrosstabReportItem(designHandle, cubeHandle,
                                                "MyCrosstab");
                // /xtab.setWidth(500);
                // xtab.setHeight(500);
                // 添加到body内

                designHandle.getBody().add(xtab);

                IReportItem reportItem = xtab.getReportItem();
                CrosstabReportItemHandle xtabHandle = (CrosstabReportItemHandle) reportItem;
                // ICrosstabConstants.COLUMN_AXIS_TYPE
                // ICrosstabConstants.ROW_AXIS_TYPE
                // xtabHandle.getMeasure(index)
                // DimensionViewHandle dvh = xtabHandle.insertDimension(dimension,
                // ICrosstabConstants.COLUMN_AXIS_TYPE, 0);
                DimensionViewHandle dvh = xtabHandle.insertDimension(dimension,
                                ICrosstabConstants.ROW_AXIS_TYPE, 0);
                LevelViewHandle levelViewHandle = dvh.insertLevel(level, 0);
                CrosstabCellHandle cellHandle = levelViewHandle.getCell();
                DesignElementHandle eii = xtabHandle.getModelHandle();

                ComputedColumn bindingColumn = StructureFactory.newComputedColumn(eii,
                                level.getName());
                ComputedColumnHandle bindingHandle = ((ReportItemHandle) eii)
                                .addColumnBinding(bindingColumn, false);
                bindingColumn.setDataType(level.getDataType());

                String exp = "dimension['" + dimension.getName() + "']['"
                                + level.getName() + "']";
                bindingColumn.setExpression(exp);

                // xtabHandle.addGrandTotal(axisType, measureList, functionList)
                DataItemHandle dataHandle = designFactory.newDataItem(level.getName());
                dataHandle.setResultSetColumn(bindingHandle.getName());
                cellHandle.addContent(dataHandle);
        }

        void buildReport() throws IOException, SemanticException {

                DesignConfig config = new DesignConfig();
                config.setBIRTHome("E:/birt-runtime-3_7_1/ReportEngine");
                IDesignEngine engine = null;
                try {

                        Platform.startup(config);
                        IDesignEngineFactory factory = (IDesignEngineFactory) Platform
                                        .createFactoryObject(IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY);
                        engine = factory.createDesignEngine(config);

                } catch (Exception ex) {
                        ex.printStackTrace();
                }

                SessionHandle session = engine.newSessionHandle(ULocale.ENGLISH);

                try {
                        // open a design or a template
                        designHandle = session.createDesign();

                        designFactory = designHandle.getElementFactory();

                        buildDataSource();
                        buildDataSet();
                        buidCube();

                        // Save the design and close it. output/desample/

                        designHandle.saveAs("output/birtsample/cubetest.rptdesign");
                        designHandle.close();
                        Platform.shutdown();
                        System.out.println("Finished");
                } catch (Exception e) {
                        e.printStackTrace();
                }

        }
}

你可能感兴趣的:(birt)