Skip to main content



      Home
Home » Archived » BIRT » Creating and using xml data sources / sets programmatically
Creating and using xml data sources / sets programmatically [message #213660] Wed, 24 January 2007 08:36 Go to next message
Eclipse UserFriend
Originally posted by: firstname.lastname.napa.fi

Hi!

Is there any documentation and/or examples about programmatically =

creating and using xml data sources / sets?


From bits and pieces I found here and there managed to put together =

something like this (I have no idea whether this is correct):

-- clip --

// efactory is an ElementFactory
// design is a ReportDesignHandle

String dsrcName =3D "my_xml_dsource" ;
OdaDataSourceHandle dsource =3D efactory.newOdaDataSource( dsrcName, =

"org.eclipse.birt.report.data.oda.xml" ) ;
design.getDataSources().add( dsource ) ;

String dsName =3D "my_xml_dset" ;
OdaDataSetHandle dset =3D efactory.newOdaDataSet( dsName, =

"org.eclipse.birt.report.data.oda.xml.dataSet" ) ;
dset.setDataSource( dsrcName ) ;

dset.setProperty( "XML_FILE", "d:/temp/foo.xml" ) ;
design.getDataSets().add( dset ) ;

-- clip --

Is this correct?


But now I'm a bit lost. How do I create column mapping using xpath =

expressions?


Also (this is propably not specific to xml data source / set), how do=
I =

programmatically create a filter on a data set?
I could not find any examples, but I would guess classes
org.eclipse.birt.report.model.api.FilterConditionHandle
org.eclipse.birt.report.model.api.elements.structures.Filter Condition
are used in some fashion, possibly setting an instance of one of thos=
e =

classes as a value to data set property "filter". Am I on the right trac=
k?
I would guess the FilterCondition is to be created w/ =

StructureFactory.createFilterCond() ?



After that I need to create a table and bind the data set to that tab=
le, =

but http://wiki.eclipse.org/index.php/Dynamic_Table gives me an idea on =
=

how to go about that - I suppose there's nothing data source / set type =
=

specific here.



-Antti-
Re: Creating and using xml data sources / sets programmatically [message #214781 is a reply to message #213660] Sat, 27 January 2007 01:36 Go to previous messageGo to next message
Eclipse UserFriend
Here is a simple example using the DEAPI to create an xml ds.

Jason



import java.io.File;
import java.io.IOException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.DataItemHandle;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.DesignEngine;
import org.eclipse.birt.report.model.api.ElementFactory;
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.PropertyHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.RowHandle;
import org.eclipse.birt.report.model.api.OdaDataSetHandle;
import org.eclipse.birt.report.model.api.OdaDataSourceHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.TableHandle;
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.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.metadata.IMetaDataDictiona ry;
import org.eclipse.birt.report.model.elements.interfaces.IReportIte mModel;
import org.eclipse.birt.report.model.elements.interfaces.IStyleMode l;

import com.ibm.icu.util.ULocale;

public class XMLReport
{

ReportDesignHandle reportDesignHandle = null;

ElementFactory elementFactory = null;

IMetaDataDictionary dict = null;

ComputedColumn cs1, cs2, cs3 = null;

public static void main( String[] args ) throws SemanticException,
IOException
{
new XMLReport( ).createReport( );
}

void createReport( ) throws SemanticException, IOException
{
//Configure the Engine and start the Platform
DesignConfig config = new DesignConfig( );

config.setProperty("BIRT_HOME",
"C:/birt-runtime-2_1_1/birt-runtime-2_1_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 ) ;


// Create a new report
reportDesignHandle = session.createDesign( );

// Element factory is used to create instances of BIRT elements.
elementFactory = reportDesignHandle.getElementFactory( );

dict = new DesignEngine( null ).getMetaData( );

createMasterPages( );
createDataSources( );
createDataSets( );
createBody( );

String outputPath = "output";//$NON-NLS-1$
File outputFolder = new File( outputPath );
if ( !outputFolder.exists( ) && !outputFolder.mkdir( ) )
{
throw new IOException( "Can not create the output folder" );//$NON-NLS-1$
}
reportDesignHandle.saveAs( outputPath + "/" +
"XMLReport.rptdesign" );//$NON-NLS-1$//$NON-NLS-2$
System.out.println("finished");
}

//Scripted Data Set
/*
* private void createDataSources( ) throws SemanticException
{
ScriptDataSourceHandle dataSourceHandle =
elementFactory.newScriptDataSource( "Data Source" );//$NON-NLS-1$
reportDesignHandle.getDataSources( ).add( dataSourceHandle );
}

private void createDataSets( ) throws SemanticException
{
// Data Set
ScriptDataSetHandle dataSetHandle = elementFactory.newScriptDataSet( "Data
Set" );//$NON-NLS-1$
dataSetHandle.setDataSource( "Data Source" );//$NON-NLS-1$

// Set open( ) in code
dataSetHandle.setOpen( "i=0;"//$NON-NLS-1$
+ "sourcedata = new Array( new Array(3), new Array(3), new Array(3),
new Array(3));"//$NON-NLS-1$

+ "sourcedata[0][0] = 10; "//$NON-NLS-1$
+ "sourcedata[0][1] = \"Ice Bella\";"//$NON-NLS-1$
+ "sourcedata[0][2] = 304;"//$NON-NLS-1$

+ "sourcedata[1][0] = 10; "//$NON-NLS-1$
+ "sourcedata[1][1] = \"Nola Dicci\";"//$NON-NLS-1$
+ "sourcedata[1][2] = 258;"//$NON-NLS-1$

+ "sourcedata[2][0] = 11; "//$NON-NLS-1$
+ "sourcedata[2][1] = \"Ice Bella\";"//$NON-NLS-1$
+ "sourcedata[2][2] = 202;"//$NON-NLS-1$

+ "sourcedata[3][0] = 11; "//$NON-NLS-1$
+ "sourcedata[3][1] = \"Nola Dicci\";"//$NON-NLS-1$
+ "sourcedata[3][2] = 181;" );//$NON-NLS-1$

// Set fetch( ) in code
dataSetHandle.setFetch( "if ( i < 4 ){"//$NON-NLS-1$
+ "row[\"Month\"] = sourcedata[i][0];"//$NON-NLS-1$
+ "row[\"Product\"] = sourcedata[i][1];"//$NON-NLS-1$
+ "row[\"Amount\"] = sourcedata[i][2];"//$NON-NLS-1$
+ "i++;"//$NON-NLS-1$
+ "return true;}" + "else return false;" );//$NON-NLS-1$//$NON-NLS-2$

// Set computed columns
cs1 = StructureFactory.createComputedColumn( );
cs1.setName( "Month" );//$NON-NLS-1$
cs1.setExpression( "row[\"Month\"]" );//$NON-NLS-1$
cs1.setDataType( "integer" );//$NON-NLS-1$

cs2 = StructureFactory.createComputedColumn( );
cs2.setName( "Product" );//$NON-NLS-1$
cs2.setExpression( "row[\"Product\"]" );//$NON-NLS-1$
cs2.setDataType( "string" );//$NON-NLS-1$

cs3 = StructureFactory.createComputedColumn( );
cs3.setName( "Amount" );//$NON-NLS-1$
cs3.setExpression( "row[\"Amount\"]" );//$NON-NLS-1$
cs3.setDataType( "integer" );//$NON-NLS-1$

PropertyHandle computedSet = dataSetHandle.getPropertyHandle(
ScriptDataSetHandle.COMPUTED_COLUMNS_PROP );
computedSet.addItem( cs1 );
computedSet.addItem( cs2 );
computedSet.addItem( cs3 );

reportDesignHandle.getDataSets( ).add( dataSetHandle );
}
*/



private void createDataSources( ) throws SemanticException
{
OdaDataSourceHandle dataSourceHandle =
elementFactory.newOdaDataSource("Data Source",
"org.eclipse.birt.report.data.oda.xml");
dataSourceHandle.setProperty("FILELIST", "C:/xmltest/test.xml");
reportDesignHandle.getDataSources( ).add( dataSourceHandle );
}

private void createDataSets( ) throws SemanticException
{
// Data Set

OdaDataSetHandle dsHandle = elementFactory.newOdaDataSet( "Data Set",
"org.eclipse.birt.report.data.oda.xml.dataSet" );
dsHandle.setDataSource( "Data Source" );
dsHandle.setQueryText(
" table0#-TNAME-#table0#:#[/people/person/name]#:#{name;String ;} " );

reportDesignHandle.getDataSets( ).add( dsHandle );


}

private void createMasterPages( ) throws ContentException, NameException
{
DesignElementHandle simpleMasterPage =
elementFactory.newSimpleMasterPage( "Master Page" );//$NON-NLS-1$
reportDesignHandle.getMasterPages( ).add( simpleMasterPage );
}



private void createBody( ) throws SemanticException
{



TableHandle table = elementFactory.newTableItem( null, 3, 1, 1, 1 );
table.setProperty( IStyleModel.TEXT_ALIGN_PROP,
DesignChoiceConstants.TEXT_ALIGN_CENTER );
table.setWidth( "80%" );//$NON-NLS-1$
table.setProperty( IReportItemModel.DATA_SET_PROP, "Data
Set" );//$NON-NLS-1$

PropertyHandle computedSet = table.getColumnBindings( );
cs1 = StructureFactory.createComputedColumn( );

cs1.setName("Name");
cs1.setExpression( "dataSetRow[\"name\"]" );//$NON-NLS-1$
computedSet.addItem( cs1 );



// Header
RowHandle header = (RowHandle) table.getHeader( ).get( 0 );

CellHandle tcell = (CellHandle) header.getCells( ).get( 0 );
LabelHandle label = elementFactory.newLabel( null );
label.setText( "Name" );//$NON-NLS-1$
tcell.getContent( ).add( label );



DataItemHandle data = null;
// Detail
RowHandle detail = (RowHandle) table.getDetail( ).get( 0 );
tcell = (CellHandle) detail.getCells( ).get( 0 );
data = elementFactory.newDataItem( null );
data.setResultSetColumn( cs1.getName( ) );
tcell.getContent( ).add( data );


reportDesignHandle.getBody( ).add( table );


}


}
"Antti Karanta" <firstname.lastname@napa.fi> wrote in message
news:op.tmnurdp163e0vv@nw17.napa.fi...


Hi!

Is there any documentation and/or examples about programmatically
creating and using xml data sources / sets?


From bits and pieces I found here and there managed to put together
something like this (I have no idea whether this is correct):

-- clip --

// efactory is an ElementFactory
// design is a ReportDesignHandle

String dsrcName = "my_xml_dsource" ;
OdaDataSourceHandle dsource = efactory.newOdaDataSource( dsrcName,
"org.eclipse.birt.report.data.oda.xml" ) ;
design.getDataSources().add( dsource ) ;

String dsName = "my_xml_dset" ;
OdaDataSetHandle dset = efactory.newOdaDataSet( dsName,
"org.eclipse.birt.report.data.oda.xml.dataSet" ) ;
dset.setDataSource( dsrcName ) ;

dset.setProperty( "XML_FILE", "d:/temp/foo.xml" ) ;
design.getDataSets().add( dset ) ;

-- clip --

Is this correct?


But now I'm a bit lost. How do I create column mapping using xpath
expressions?


Also (this is propably not specific to xml data source / set), how do I
programmatically create a filter on a data set?
I could not find any examples, but I would guess classes
org.eclipse.birt.report.model.api.FilterConditionHandle
org.eclipse.birt.report.model.api.elements.structures.Filter Condition
are used in some fashion, possibly setting an instance of one of those
classes as a value to data set property "filter". Am I on the right track?
I would guess the FilterCondition is to be created w/
StructureFactory.createFilterCond() ?



After that I need to create a table and bind the data set to that table,
but http://wiki.eclipse.org/index.php/Dynamic_Table gives me an idea on
how to go about that - I suppose there's nothing data source / set type
specific here.



-Antti-
Re: Creating and using xml data sources / sets programmatically [message #215550 is a reply to message #214781] Wed, 31 January 2007 03:39 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: firstname.lastname.napa.fi

Thanks for the example! I'm afraid it is not quite working, at least =
not =

for me.

I changed the location of BIRT_HOME and tried to run, but

> OdaDataSourceHandle dataSourceHandle =3D
> elementFactory.newOdaDataSource("Data Source",
> "org.eclipse.birt.report.data.oda.xml");

returns null (resulting in an NPE on the next line where it's used). =
If =

this works for you, what could be causing this? I'm using birt runtime =

2.1.1


Also, what does the query text contain?

> dsHandle.setQueryText(
> " table0#-TNAME-#table0#:#[/people/person/name]#:#{name;String ;} " );

The xpath expression and col name + data type is pretty clear, but wh=
at =

is "table0#-TNAME-#table0#" in the beginning of the string? Is this =

constant? If not, what should I generate there?



-Antti-
Re: Creating and using xml data sources / sets programmatically [message #215580 is a reply to message #215550] Wed, 31 January 2007 04:06 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: firstname.lastname.napa.fi

On Wed, 31 Jan 2007 10:39:25 +0200, Antti Karanta =

<firstname.lastname@napa.fi> wrote:

>> OdaDataSourceHandle dataSourceHandle =3D
>> elementFactory.newOdaDataSource("Data Source",
>> "org.eclipse.birt.report.data.oda.xml");
>
> returns null (resulting in an NPE on the next line where it's used)=
.. =

> If this works for you, what could be causing this? I'm using birt =

> runtime 2.1.1

It gets stranger still... I got the NPE when running from eclipse and=
=

where I had added all the jars from %BIRT_HOME%\lib and =

%BIRT_HOME%\plugins to the classpath. When I run on command line

D:\work\napa\jnapa\birt_integration_test>java -cp =

bin;%BLIB%\coreapi.jar;%BLIB%\modelapi.jar;%BLIB%\com.ibm.ic u_3.4.4.1.
jar;%BLIB%\engineapi.jar;%BLIB%\commons-codec-1.3.jar;%BLIB% \js.jar =

XMLReport
finished

it works. ???


My guess is that birt is sensitive to what is on the jvm classpath? A=
nd =

jars from %BIRT_HOME%\plugins are NOT supposed to be on the jvm classpat=
h?


But anyhow now that I know it it's not a problem. I'd still like to k=
now =

a little more about the query string (see my previous post).



-Antti-
Re: Creating and using xml data sources / sets programmatically [message #215628 is a reply to message #214781] Wed, 31 January 2007 05:33 Go to previous messageGo to next message
Eclipse UserFriend
Originally posted by: firstname.lastname.napa.fi

> IDesignEngine engine =3D null;
> try{
>
>
> Platform.startup( config );
> IDesignEngineFactory factory =3D (IDesignEngineFactory) Platform
> .createFactoryObject(
> IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY );
> engine =3D factory.createDesignEngine( config );
>
> }catch( Exception ex){
> ex.printStackTrace();
> }
>
>
> SessionHandle session =3D engine.newSessionHandle( ULocale.ENGLISH )=
;
>
>
> // Create a new report
> reportDesignHandle =3D session.createDesign( );
>
> // Element factory is used to create instances of BIRT elements.
> elementFactory =3D reportDesignHandle.getElementFactory( );
>
> dict =3D new DesignEngine( null ).getMetaData( );

What is the reason for creating a new DesignEngine just to get the =

IMetaDataDictionary? Why not use the engine previously created (via the =
=

factory), i.e.

dict =3D engine.getMetaData( ) ;

?

And besides that, the "dict" instance variable is never referenced af=
ter =

this, i.e. the IMetaDataDictionary created here is never used. Is gettin=
g =

the IMetaDataDictionary instance still necessary for some reason?



> table.setProperty( IReportItemModel.DATA_SET_PROP, "Data Set" =

> );//$NON-NLS-1$

Why not

table.setDataSet( "Data Set" ) ;

? Or are they equivalent?



-Antti-
Re: Creating and using xml data sources / sets programmatically [message #215741 is a reply to message #215628] Wed, 31 January 2007 10:50 Go to previous message
Eclipse UserFriend
The second design engine is a bug. It does not need to be there.
The table.setDataset call requires a dataset handle. Which the code has but
the variable
was not global.

Jason

"Antti Karanta" <firstname.lastname@napa.fi> wrote in message
news:op.tm0kyd0r63e0vv@nw17.napa.fi...

> 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 ) ;
>
>
> // Create a new report
> reportDesignHandle = session.createDesign( );
>
> // Element factory is used to create instances of BIRT elements.
> elementFactory = reportDesignHandle.getElementFactory( );
>
> dict = new DesignEngine( null ).getMetaData( );

What is the reason for creating a new DesignEngine just to get the
IMetaDataDictionary? Why not use the engine previously created (via the
factory), i.e.

dict = engine.getMetaData( ) ;

?

And besides that, the "dict" instance variable is never referenced after
this, i.e. the IMetaDataDictionary created here is never used. Is getting
the IMetaDataDictionary instance still necessary for some reason?



> table.setProperty( IReportItemModel.DATA_SET_PROP, "Data
> et" );//$NON-NLS-1$

Why not

table.setDataSet( "Data Set" ) ;

? Or are they equivalent?



-Antti-
Previous Topic:BIRT viewer
Next Topic:BIRT upgrate - chart's dosn't work - PLEASE HELP
Goto Forum:
  


Current Time: Fri Jul 18 13:53:59 EDT 2025

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

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

Back to the top