Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Archived » BIRT » Programmatic manipulation of report designs
Programmatic manipulation of report designs [message #636117] Fri, 29 October 2010 11:50 Go to next message
Marco  is currently offline Marco Friend
Messages: 216
Registered: August 2009
Senior Member
Hi,

I need to patch an existing design to duplicate measures in a crosstab.

The specific question is, how can I duplicate measureViews?

There is a methode copy() to "deep copy an element". Sounds good, but it returns an IReportItem and I just know how to something with Handles.

How can I create a handle from an IReportItem, or how to I deep copy all properties from an IReportItem to a Handle?

Thx,
Marco
Re: Programmatic manipulation of report designs [message #636134 is a reply to message #636117] Fri, 29 October 2010 12:57 Go to previous messageGo to next message
Marco  is currently offline Marco Friend
Messages: 216
Registered: August 2009
Senior Member
I just recognized that copy() just return null for a valid MeasureViewHandle of an existing element.

So what is the right way to duplicate e.g. MeasureViews?

/m
Re: Programmatic manipulation of report designs [message #636212 is a reply to message #636134] Fri, 29 October 2010 16:04 Go to previous messageGo to next message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Are you wanting a measureview with the same measure?

Attached is a de api example of creating a cube and crosstab.

Jason


package DEAPI;


import java.io.IOException;

import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.item.crosstab.core.ICrosstabConstant s;
import org.eclipse.birt.report.item.crosstab.core.de.CrosstabCellHa ndle;
import
org.eclipse.birt.report.item.crosstab.core.de.CrosstabReport ItemHandle;
import org.eclipse.birt.report.item.crosstab.core.de.DimensionViewH andle;
import org.eclipse.birt.report.item.crosstab.core.de.LevelViewHandl e;
import org.eclipse.birt.report.item.crosstab.core.de.MeasureViewHan dle;
import
org.eclipse.birt.report.item.crosstab.core.util.CrosstabExte ndedItemFactory;
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.DesignChoiceConst ants;
import org.eclipse.birt.report.model.api.elements.structures.Comput edColumn;
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.TabularDimensionHandl e;
import org.eclipse.birt.report.model.api.olap.TabularHierarchyHandl e;
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
{
TabularCubeHandle cubeHandle = designHandle.getElementFactory( )
.newTabularCube( "MyCube" );
cubeHandle.setDataSet((DataSetHandle)designHandle.getDataSet s().get(0));
designHandle.getCubes( ).add( cubeHandle );


// measure group
MeasureGroupHandle measureGroupHandle = designHandle
.getElementFactory( ).newTabularMeasureGroup(
"testMeasureGroup" );
cubeHandle.setProperty( ICubeModel.MEASURE_GROUPS_PROP,
measureGroupHandle );

// measure
measureGroupHandle.add( MeasureGroupHandle.MEASURES_PROP,
designFactory.newTabularMeasure( null ) );
MeasureHandle measure = (MeasureHandle)
measureGroupHandle.getContent(MeasureGroupHandle.MEASURES_PR OP, 0 );
measure.setName( "CREDITLIMIT" );
measure.setMeasureExpression( "dataSetRow['CREDITLIMIT']" );
measure.setFunction( DesignChoiceConstants.MEASURE_FUNCTION_SUM );
measure.setCalculated( false );
measure.setDataType( DesignChoiceConstants.COLUMN_DATA_TYPE_FLOAT );


// dimension
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) );


// level
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 );

/*
level.setDateTimeLevelType(
DesignChoiceConstants.DATE_TIME_LEVEL_TYPE_QUARTER );
level.setDateTimeFormat( "mm" ); //$NON-NLS-1$
// level.setInterval( DesignChoiceConstants.INTERVAL_MONTH );
level.setIntervalRange( 5 );
level.setIntervalBase( valuePrix + level.getIntervalBase( ) );
*/

ExtendedItemHandle xtab =
CrosstabExtendedItemFactory.createCrosstabReportItem(designH andle,
cubeHandle, "MyCrosstab" );
designHandle.getBody().add(xtab);


IReportItem reportItem = xtab.getReportItem( );
CrosstabReportItemHandle xtabHandle = (CrosstabReportItemHandle)
reportItem;
//ICrosstabConstants.COLUMN_AXIS_TYPE
//ICrosstabConstants.ROW_AXIS_TYPE

//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);

DataItemHandle dataHandle = designFactory.newDataItem( level.getName( ) );
dataHandle.setResultSetColumn( bindingHandle.getName( ) );
cellHandle.addContent( dataHandle );

MeasureViewHandle mvh = xtabHandle.insertMeasure(measure, 0);

//((DataItemHandle)mvh.getCell().getContents().get(0)).setPr operty( "style",
"mydata");

//mvh.addHeader( );

LabelHandle labelHandle = designFactory.newLabel( null );
labelHandle.setText( measure.getName() );
mvh.getHeader( ).addContent( labelHandle );



}


void buildReport() throws IOException, SemanticException
{

DesignConfig config = new DesignConfig( );

config.setBIRTHome("C:/birt/birt-runtime-2_6_1/birt-runtime-2_6_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.

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

}
}



On 10/29/2010 8:57 AM, Marco wrote:
> I just recognized that copy() just return null for a valid
> MeasureViewHandle of an existing element.
>
> So what is the right way to duplicate e.g. MeasureViews?
>
> /m
Re: Programmatic manipulation of report designs [message #636220 is a reply to message #636212] Fri, 29 October 2010 17:37 Go to previous messageGo to next message
Marco  is currently offline Marco Friend
Messages: 216
Registered: August 2009
Senior Member
Thx for the answer.

I need "deep copies", i.e. MeasureView, Measure, and even binding. I have no problem to copy each of this elements. But I need to be sure that each attribute is copied from the source elements.

From the sample code I cannot see how to copy.

/m
Re: Programmatic manipulation of report designs [message #636224 is a reply to message #636220] Fri, 29 October 2010 18:12 Go to previous message
Jason Weathersby is currently offline Jason WeathersbyFriend
Messages: 9167
Registered: July 2009
Senior Member

Marco,

To copy a Measure you can do this:

measureGroupHandle.add( MeasureGroupHandle.MEASURES_PROP,
designFactory.newTabularMeasure( null ) );
TabularMeasureHandle measure = (TabularMeasureHandle)
measureGroupHandle.getContent(MeasureGroupHandle.MEASURES_PR OP, 0 );
measure.setName( "CREDITLIMIT" );
measure.setMeasureExpression( "dataSetRow['CREDITLIMIT']" );
measure.setFunction( DesignChoiceConstants.MEASURE_FUNCTION_SUM );
measure.setCalculated( false );
measure.setDataType( DesignChoiceConstants.COLUMN_DATA_TYPE_FLOAT );

TabularMeasure mhcopy = (TabularMeasure) measure.copy();
If you add mhcopy to the cube you will need to rename it to something
else. You can get its handle by calling getHandle(null).

It appears the copy on MeasureView does not work. Can you log a bug for
this?

Jason


On 10/29/2010 1:37 PM, Marco wrote:
> Thx for the answer.
>
> I need "deep copies", i.e. MeasureView, Measure, and even binding. I
> have no problem to copy each of this elements. But I need to be sure
> that each attribute is copied from the source elements.
>
> From the sample code I cannot see how to copy.
>
> /m
Previous Topic:Problem with Oracle sys_refcursor
Next Topic:SQL query bulder error
Goto Forum:
  


Current Time: Thu Apr 25 23:09:02 GMT 2024

Powered by FUDForum. Page generated in 0.04180 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top