|
Re: Get DataSet values into variable [message #500006 is a reply to message #499809] |
Tue, 24 November 2009 16:56 |
|
Zoli,
Try to put a computed column in with an aggregate and then just add a
data item to the header. Take a look at this example.
Jason
<?xml version="1.0" encoding="UTF-8"?>
<report xmlns="http://www.eclipse.org/birt/2005/design" version="3.2.20"
id="1">
<property name="createdBy">Eclipse BIRT Designer Version
2.5.1.v20090903 Build <2.5.1.v20090917-1447></property>
<property name="units">in</property>
<property name="comments">Copyright (c) 2006 <<Your Company
Name here>></property>
<method name="beforeFactory"><![CDATA[importPackage(
Packages.org.eclipse.birt.report.model.api );
reportDesignHandle = reportContext.getDesignHandle();
elementFactory = reportDesignHandle.getElementFactory()
dataSetHandle = elementFactory.newScriptDataSet( "Data Set" );
dataSetHandle.setDataSource( "Data Source" );
dataSetHandle.setOpen( "i=0;" );
dataSetHandle.setFetch( "if ( i < 4 ){"
+ "row[\"Month\"] = 1;"
+ "row[\"Product\"] = 'My Product';"
+ "row[\"Amount\"] = i;"
+ "i++;"
+ "return true;}" + "else return false;" );
// 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$
computedSet = dataSetHandle.getPropertyHandle( "computedColumns" );
computedSet.addItem( cs1 );
computedSet.addItem( cs2 );
computedSet.addItem( cs3 );
reportDesignHandle.getDataSets( ).add( dataSetHandle );
mytable = elementFactory.newTableItem( null, 3, 1, 1, 1 );
mytable.setWidth( "80%" );
mytable.setProperty( "dataSet", "Data Set" );//$NON-NLS-1$
computedSet = mytable.getColumnBindings( );
cs1.setName("Month");
cs1.setExpression( "dataSetRow[\"Month\"]" );
computedSet.addItem( cs1 );
cs2.setName("Product");
cs2.setExpression( "dataSetRow[\"Product\"]" );
computedSet.addItem( cs2 );
cs3.setName("Amount");
cs3.setExpression( "dataSetRow[\"Amount\"]" );
computedSet.addItem( cs3 );
cs4 = StructureFactory.createComputedColumn( );
cs4.setName( "SumAmnt" );//$NON-NLS-1$
cs4.setExpression( "row[\"Amount\"]" );//$NON-NLS-1$
cs4.setAggregateFunction("Sum");
cs4.setDataType( "integer" );//$NON-NLS-1$
computedSet.addItem( cs4 );
// Header
myheader = mytable.getHeader( ).get( 0 );
tcell = myheader.getCells( ).get( 0 );
mylabel = elementFactory.newLabel( null );
mylabel.setText( "Col1" );//$NON-NLS-1$
tcell.getContent( ).add( mylabel );
tcell = myheader.getCells( ).get( 1 );
mylabel = elementFactory.newLabel( null );
mylabel.setText( "Col2" );//$NON-NLS-1$
tcell.getContent( ).add( mylabel );
tcell = myheader.getCells( ).get( 2 );
//mylabel = elementFactory.newLabel( null );
//mylabel.setText( "Col3" );//$NON-NLS-1$
//tcell.getContent( ).add( mylabel );
mydata = elementFactory.newDataItem( null );
mydata.setResultSetColumn( "SumAmnt");
tcell.getContent( ).add( mydata );
mydetail = mytable.getDetail( ).get( 0 );
tcell = mydetail.getCells( ).get( 0 );
mydata = elementFactory.newDataItem( null );
mydata.setResultSetColumn( "Month");
tcell.getContent( ).add( mydata );
tcell = mydetail.getCells( ).get( 1 );
mydata = elementFactory.newDataItem( null );
mydata.setResultSetColumn( "Product");
tcell.getContent( ).add( mydata );
tcell = mydetail.getCells( ).get( 2 );
mydata = elementFactory.newDataItem( null );
mydata.setResultSetColumn( "Amount");
tcell.getContent( ).add( mydata );
reportDesignHandle.getBody( ).add( mytable );]]></method>
<property name="layoutPreference">auto layout</property>
<data-sources>
<script-data-source name="Data Source" id="4"/>
</data-sources>
<page-setup>
<simple-master-page name="Simple MasterPage" id="2">
<property name="topMargin">1in</property>
<property name="leftMargin">1.25in</property>
<property name="bottomMargin">1in</property>
<property name="rightMargin">1.25in</property>
<page-footer>
<text id="3">
<property name="contentType">html</property>
<text-property
name="content"><![CDATA[<value-of>new Date()</value-of>]]></text-property>
</text>
</page-footer>
</simple-master-page>
</page-setup>
</report>
Zoli Imre wrote:
> I want to retreive the values of a dataset column into variables using
> only code, not the designer.
> For now I have created a table, added dynamically some columns and made
> the column bindings. I managed to put the data into DataItem's and added
> to the table. Now I need to save the values of a dataset column into
> variables. How can i do this. I was trying to find a method, but I was
> lost in the world of Handle's.
> I need these data in varaibles to put them as header to the dynamically
> created columns.
> Any help or suggestion is welcome.
>
> Here is my code:
> importPackage (Packages.org.eclipse.birt.report.model.api);
> elementFactory =
> reportContext.getReportRunnable().designHandle.getElementFac tory();
> //Get data set handle
> dataHandle =
> reportContext.getReportRunnable().designHandle.findDataSet( "DETAIL" );//
> dynamicTable = elementFactory.newTableItem("myNewTable", 1);
> dynamicTable.setWidth("100%");
> //Bind data set to report item
> dynamicTable.setDataSet(dataHandle);
> //bind the data set columns to the table
> resultSetCols = dataHandle.getListProperty(dataHandle.RESULT_SET_PROP) ;
> boundCols = dynamicTable.getColumnBindings();
> for (iterator = resultSetCols.iterator(); iterator.hasNext();) {
> rsHandle = iterator.next();
> col = StructureFactory.createComputedColumn();
> col.setName(rsHandle.getColumnName());
> col.setExpression("dataSetRow[\"" + rsHandle.getColumnName() + "\"]");
> boundCols.addItem(col);
> }
> //End of column binding
>
> reportContext.getReportRunnable().designHandle.getBody().add
> (dynamicTable);
> dynamicTable =
> reportContext.getReportRunnable().designHandle.findElement("myNewTable ");
>
> //insert new detail row
> rowParam = new RowOperationParameters(2, -1, 1);
> dynamicTable.insertRow(rowParam);
>
> //insert some columns
> for (i=0;i<=5;i++)
> {
> dynamicTable.insertColumn(1+i, 1);
> }
>
> name = new String;
>
> for (i=0;i<=5;i++)
> {
> //get the first detail row and the 4th column. This is 0 based
> myNewHeader = dynamicTable.getHeader().get(0);
> myNewRow = dynamicTable.getDetail().get(1);
>
> detailCell = myNewRow.getCells().get(1+i);
> data1 = elementFactory.newDataItem(null);
> data1.setResultSetColumn("TOTALCOST");
> detailCell.getContent().add(data1);
>
> // I tried to put the values of the "HEADERS" column as header for my
> new columns, //but of course all headers were the same: the first entry
> of that column
> //myHeaderCell = myNewHeader.getCells().get(1+i);
> //data = elementFactory.newDataItem(null);
> //data.setResultSetColumn("HEADERS");
> //myHeaderCell.getContent().add(data);
>
> //I would like to store the values of the "HEADERS" column in the "name"
> string variable
> //HELP!!!!!!!!
>
> myHeaderCell = myNewHeader.getCells().get(1+i);
> label = elementFactory.newLabel(null);
> label.setText(name);
> myHeaderCell.getContent().add(label);
>
> }
|
|
|
|
Powered by
FUDForum. Page generated in 0.02482 seconds