Home
Analytics
Change report design filter dynamically via Java coding
donwood
Does anyone have an example of how to modify the filter of an existing report design using Java code in my application (NOT in an eventhandler of the report)?
I have two versions of a report, one of which needs to remove the existing filter and add several new filter conditions. I know I could accomplish this by making another copy of the report and simply creating the correct filter condition for each report but I'd prefer not to do that if I don't have to.
I'm using BIRT 2.3.2.
Thanks in advance.
-Don
Find more posts tagged with
Comments
JasonW
Here is a sample of several de api operations you can do:
Look at the addFilterCondition method. If you want to clear the filter just add
ph.clearValue() before the addItem method.
package DEAPI;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.model.api.ActionHandle;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.ColumnHandle;
import org.eclipse.birt.report.model.api.DataItemHandle;
import org.eclipse.birt.report.model.api.DesignConfig;
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.JointDataSetHandle;
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.PropertyHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.ReportElementHandle;
import org.eclipse.birt.report.model.api.RowHandle;
import org.eclipse.birt.report.model.api.ScalarParameterHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.SortKeyHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.StyleHandle;
import org.eclipse.birt.report.model.api.TableHandle;
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.Action;
import org.eclipse.birt.report.model.api.elements.structures.ComputedColumn;
import org.eclipse.birt.report.model.api.elements.structures.EmbeddedImage;
import org.eclipse.birt.report.model.api.elements.structures.FilterCondition;
import org.eclipse.birt.report.model.api.elements.structures.HideRule;
import org.eclipse.birt.report.model.api.elements.structures.HighlightRule;
import org.eclipse.birt.report.model.api.elements.structures.IncludeScript;
import org.eclipse.birt.report.model.api.elements.structures.JoinCondition;
import org.eclipse.birt.report.model.api.elements.structures.MapRule;
import org.eclipse.birt.report.model.api.elements.structures.ParamBinding;
import org.eclipse.birt.report.model.api.elements.structures.PropertyBinding;
import org.eclipse.birt.report.model.api.elements.structures.SortKey;
import org.eclipse.birt.report.model.api.elements.structures.TOC;
import org.eclipse.birt.report.model.elements.JointDataSet;
import org.eclipse.birt.report.model.elements.ReportItem;
import com.ibm.icu.util.ULocale;
/**
* Simple BIRT Design Engine API (DEAPI) demo.
*/
public class StructFactoryTest
{
ReportDesignHandle designHandle = null;
ElementFactory designFactory = null;
StructureFactory structFactory = null;
public static void main( String[] args )
{
try
{
StructFactoryTest de = new StructFactoryTest();
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", "" );
PropertyBinding pb = new PropertyBinding();
designHandle.getDataSources( ).add( dsHandle );
long currid = dsHandle.getID();
//pb.setName("odaUser");
//pb.setID(currid);
//pb.setValue("params[\"NewParameter\"].value");
//PropertyHandle ph = designHandle.getPropertyHandle("propertyBindings");
//ph.addItem(pb);
}
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 );
addFilterCondition( dsHandle );
designHandle.getDataSets( ).add( dsHandle );
}
void buildDataSet2( ) throws SemanticException
{
OdaDataSetHandle dsHandle = designFactory.newOdaDataSet( "ds2",
"org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet" );
dsHandle.setDataSource( "Data Source" );
String qry = "Select * from orderdetails where ordernumber = ?";
dsHandle.setQueryText( qry );
addFilterCondition( dsHandle );
designHandle.getDataSets( ).add( dsHandle );
}
void buildJointDataSet( ) throws SemanticException
{
OdaDataSetHandle dsHandle1 = designFactory.newOdaDataSet( "ds1",
"org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet" );
dsHandle1.setDataSource( "Data Source" );
String qry1 = "Select * from customers";
dsHandle1.setQueryText( qry1 );
OdaDataSetHandle dsHandle2 = designFactory.newOdaDataSet( "ds2",
"org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet" );
dsHandle2.setDataSource( "Data Source" );
String qry2 = "Select * from orders";
dsHandle2.setQueryText( qry2 );
JointDataSetHandle jds = designFactory.newJointDataSet("test");
designHandle.getDataSets( ).add( dsHandle1 );
designHandle.getDataSets( ).add( dsHandle2 );
jds.addDataSet("ds1");
jds.addDataSet("ds2");
String leftExpression = "dataSetRow[\"CUSTOMERNUMBER\"]";
String rightExpression = "dataSetRow[\"CUSTOMERNUMBER\"]";
JoinCondition condition = StructureFactory.createJoinCondition( );
condition.setJoinType( DesignChoiceConstants.JOIN_TYPE_LEFT_OUT );
condition.setOperator( DesignChoiceConstants.JOIN_OPERATOR_EQALS );
condition.setLeftDataSet( "ds1" ); //$NON-NLS-1$
condition.setRightDataSet( "ds2" ); //$NON-NLS-1$
condition.setLeftExpression( leftExpression ); //$NON-NLS-1$
condition.setRightExpression( rightExpression ); //$NON-NLS-1$
PropertyHandle conditionHandle = jds
.getPropertyHandle( JointDataSet.JOIN_CONDITONS_PROP );
conditionHandle.addItem( condition );
designHandle.getDataSets( ).add( jds );
}
void addMapRule(TableHandle th){
try{
MapRule mr = structFactory.createMapRule();
mr.setTestExpression("row[\"CustomerCreditLimit\"]");
mr.setOperator(DesignChoiceConstants.MAP_OPERATOR_EQ);
mr.setValue1("0");
mr.setDisplay("N/A");
PropertyHandle ph = th.getPropertyHandle(StyleHandle.MAP_RULES_PROP);
ph.addItem(mr);
}catch (Exception e){
e.printStackTrace();
}
}
void addVisRule(ReportElementHandle rh){
try{
HideRule hr = structFactory.createHideRule();
hr.setFormat("pdf");
hr.setExpression("true");
PropertyHandle ph = rh.getPropertyHandle(ReportItem.VISIBILITY_PROP);
ph.addItem(hr);
}catch (Exception e){
e.printStackTrace();
}
}
void addBottomBorder(ReportElementHandle rh){
try{
rh.setProperty(StyleHandle.BORDER_BOTTOM_COLOR_PROP, "#000000");
rh.setProperty(StyleHandle.BORDER_BOTTOM_STYLE_PROP, "solid");
rh.setProperty(StyleHandle.BORDER_BOTTOM_WIDTH_PROP, "2px");
}catch (Exception e){
e.printStackTrace();
}
}
void addHighLightRule(RowHandle th){
try{
HighlightRule hr = structFactory.createHighlightRule();
hr.setOperator(DesignChoiceConstants.MAP_OPERATOR_GT);
hr.setTestExpression("row[\"CustomerCreditLimit\"]");
hr.setValue1("100000");
hr.setProperty(HighlightRule.BACKGROUND_COLOR_MEMBER, "blue");
PropertyHandle ph = th.getPropertyHandle(StyleHandle.HIGHLIGHT_RULES_PROP);
ph.addItem(hr);
}catch (Exception e){
e.printStackTrace();
}
}
void addSortKey(TableHandle th){
try{
SortKey sk = structFactory.createSortKey();
//sk.setKey("row[\"CustomerName\"]");
sk.setDirection(DesignChoiceConstants.SORT_DIRECTION_ASC);
sk.setKey("if( params[\"srt\"].value){ if( params[\"srt\"].value == 'a' ){ row[\"CustomerName\"]; }else{ row[\"CustomerCity\"];}}");
PropertyHandle ph = th.getPropertyHandle(TableHandle.SORT_PROP);
ph.addItem(sk);
}catch (Exception e){
e.printStackTrace();
}
}
void modSortKey(TableHandle th){
try{
SortKeyHandle sk;
PropertyHandle ph = th.getPropertyHandle(TableHandle.SORT_PROP);
//get number or iterate
sk = (SortKeyHandle)ph.get(0);
sk.setDirection(DesignChoiceConstants.SORT_DIRECTION_DESC);
}catch (Exception e){
e.printStackTrace();
}
}
void addFilterCondition(OdaDataSetHandle dh){
try{
FilterCondition fc = structFactory.createFilterCond();
fc.setExpr("row[\"COUNTRY\"]");
fc.setOperator(DesignChoiceConstants.MAP_OPERATOR_EQ);
fc.setValue1("'USA'");
dh.addFilter(fc);
}catch (Exception e){
e.printStackTrace();
}
}
void addFilterCondition(TableHandle th){
try{
FilterCondition fc = structFactory.createFilterCond();
fc.setExpr("row[\"CustomerCountry\"]");
fc.setOperator(DesignChoiceConstants.MAP_OPERATOR_EQ);
fc.setValue1("'USA'");
PropertyHandle ph = th.getPropertyHandle(TableHandle.FILTER_PROP);
ph.addItem(fc);
}catch (Exception e){
e.printStackTrace();
}
}
void addHyperlink(LabelHandle lh){
try{
Action ac = structFactory.createAction();
ActionHandle actionHandle = lh.setAction( ac );
//actionHandle.setURI("'http://www.google.com'");
actionHandle.setLinkType(DesignChoiceConstants.ACTION_LINK_TYPE_DRILL_THROUGH);
actionHandle.setReportName("c:/test/xyz.rptdesign");
actionHandle.setTargetFileType("report-design");
actionHandle.setTargetWindow("_blank");
actionHandle.getMember("paramBindings");
ParamBinding pb = structFactory.createParamBinding();
pb.setParamName("order");
pb.setExpression("row[\"ORDERNUMBER\"]");
actionHandle.addParamBinding(pb);
/*
<structure name="action">
<property name="linkType">drill-through</property>
<property name="reportName">detail.rptdesign</property>
<property name="targetWindow">_blank</property>
<property name="targetFileType">report-design</property>
<list-property name="paramBindings">
<structure>
<property name="paramName">order</property>
<expression name="expression">row["ORDERNUMBER"]</expression>
</structure>
</list-property>
</structure>
*/
}catch (Exception e){
e.printStackTrace();
}
}
void addToc(DataItemHandle dh){
try{
TOC myToc = structFactory.createTOC("row[\"CustomerName\"]");
dh.addTOC(myToc);
}catch (Exception e){
e.printStackTrace();
}
}
void addImage(){
try{
EmbeddedImage image = structFactory.createEmbeddedImage( );
image.setType( DesignChoiceConstants.IMAGE_TYPE_IMAGE_JPEG );
image.setData( load( "logo3.jpg" ) );
image.setName( "mylogo" );
designHandle.addImage( image );
}catch (Exception e){
e.printStackTrace();
}
}
public byte[] load( String fileName ) throws IOException
{
InputStream is = null;
is = new BufferedInputStream( this.getClass( ).getResourceAsStream(
fileName ) );
byte data[] = null;
if ( is != null )
{
try
{
data = new byte[is.available( )];
is.read( data );
}
catch ( IOException e1 )
{
throw e1;
}
}
return data;
}
void addScript(ReportDesignHandle rh){
try{
IncludeScript is = structFactory.createIncludeScript();
is.setFileName("test.js");
//PropertyHandle ph = rh.getPropertyHandle(ReportDesign.INCLUDE_SCRIPTS_PROP);
//ph.addItem(is);
}catch (Exception e){
e.printStackTrace();
}
}
void buildReport() throws IOException, SemanticException
{
DesignConfig config = new DesignConfig( );
config.setBIRTHome("C:/birt/birt-runtime-2_3_1/birt-runtime-2_3_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 ) ;
ReportDesignHandle design = null;
try{
//open a design or a template
designHandle = session.createDesign();
addScript( designHandle);
designFactory = designHandle.getElementFactory( );
buildDataSource();
buildDataSet();
buildJointDataSet();
TableHandle table = designFactory.newTableItem( "table", 3 );
table.setWidth( "100%" );
table.setDataSet( designHandle.findDataSet( "ds" ) );
//table.getColumnBindings().clearValue();
//StructureFactory.createIncludedCssStyleSheet()
PropertyHandle computedSet = table.getColumnBindings( );
ComputedColumn cs1, cs2, cs3, cs4, cs5;
cs1 = StructureFactory.createComputedColumn();
cs1.setName("CustomerName");
cs1.setExpression("dataSetRow[\"CUSTOMERNAME\"]");
computedSet.addItem(cs1);
cs2 = StructureFactory.createComputedColumn();
cs2.setName("CustomerCity");
cs2.setExpression("dataSetRow[\"CITY\"]");
//cs2.setDataType(dataType)
computedSet.addItem(cs2);
cs3 = StructureFactory.createComputedColumn();
cs3.setName("CustomerCountry");
cs3.setExpression("dataSetRow[\"COUNTRY\"]");
computedSet.addItem(cs3);
cs4 = StructureFactory.createComputedColumn();
cs4.setName("CustomerCreditLimit");
cs4.setExpression("dataSetRow[\"CREDITLIMIT\"]");
computedSet.addItem(cs4);
cs5 = StructureFactory.createComputedColumn();
cs5.setName("CustomerCreditLimitSum");
cs5.setExpression("dataSetRow[\"CREDITLIMIT\"]");
cs5.setAggregateFunction("sum");
computedSet.addItem(cs5);
// table header
RowHandle tableheader = (RowHandle) table.getHeader( ).get( 0 );
ColumnHandle ch = (ColumnHandle)table.getColumns().get(0);
ch.setProperty("width", "50%");
LabelHandle label1 = designFactory.newLabel("Label1" );
label1.setOnRender("var x = 3;");
addBottomBorder(label1);
label1.setText("Customer");
CellHandle cell = (CellHandle) tableheader.getCells( ).get( 0 );
cell.getContent( ).add( label1 );
LabelHandle label2 = designFactory.newLabel("Label2" );
label2.setText("City");
cell = (CellHandle) tableheader.getCells( ).get( 1 );
cell.getContent( ).add( label2 );
LabelHandle label3 = designFactory.newLabel("Label3" );
label3.setText("Credit Limit");
cell = (CellHandle) tableheader.getCells( ).get( 2 );
cell.getContent( ).add( label3 );
// table detail
RowHandle tabledetail = (RowHandle) table.getDetail( ).get( 0 );
cell = (CellHandle) tabledetail.getCells( ).get( 0 );
DataItemHandle data = designFactory.newDataItem( "data1" );
data.setResultSetColumn("CustomerName");
addToc( data );
cell.getContent( ).add( data );
cell = (CellHandle) tabledetail.getCells( ).get( 1 );
data = designFactory.newDataItem( "data2" );
data.setResultSetColumn("CustomerCity");
cell.getContent( ).add( data );
cell = (CellHandle) tabledetail.getCells( ).get( 2 );
data = designFactory.newDataItem( "data3" );
data.setResultSetColumn("CustomerCreditLimit");
cell.getContent( ).add( data );
addHyperlink(label1);
addMapRule(table);
addHighLightRule(tabledetail);
addSortKey(table);
modSortKey(table);
addFilterCondition(table);
addImage();
RowHandle tablefooter = (RowHandle) table.getFooter().get( 0 );
cell = (CellHandle) tablefooter.getCells( ).get( 0 );
//ImageHandle image1 = designFactory.newImage( "mylogo" );
//image1.setImageName( "mylogo" );
//addVisRule( image1 );
//cell.getContent( ).add( image1 );
cell = (CellHandle) tablefooter.getCells( ).get( 2 );
data = designFactory.newDataItem( "datasum" );
data.setResultSetColumn("CustomerCreditLimitSum");
cell.getContent( ).add( data );
ScalarParameterHandle sph = designFactory.newScalarParameter("srt");
sph.setIsRequired(false);
//sph.setAllowNull(true);
//sph.setAllowBlank(true);
sph.setValueType(DesignChoiceConstants.PARAM_VALUE_TYPE_STATIC);
sph.setDataType(DesignChoiceConstants.PARAM_TYPE_STRING);
designHandle.getParameters().add(sph);
designHandle.getBody( ).add( table );
// Save the design and close it.
designHandle.saveAs("output/desample/structfactorytest.rptdesign" );
designHandle.close( );
Platform.shutdown();
System.out.println("Finished");
}catch (Exception e){
e.printStackTrace();
}
}
}
Jason
donwood
Jason,<br />
<br />
Thanks for the information. I have a question.<br />
<br />
Is there a way to change the filter of a BIRT report and then, without saving the changes to the file system, run the report with my changes? <br />
<br />
The book <strong class='bbc'>Integrating and Extending BIRT</strong> states that "You can also open a report design from an IReportRunnable object by using the getDesignHandle() method." I assumed that the IReportRunnable object would have a "buffered copy" of the report but it appears that it simply has a pointer to what's sitting on the file system. I really don't want to change the file system copy of the report design since, conceivably, someone could run the report and get a version of the report that contains filters they hadn't intended to be there.<br />
<br />
Sorry if I'm way off base on this. If there's something I'm missing please let me know.<br />
<br />
Thanks,<br />
Don
JasonW
You can absolutely do this. Look at this sample:
package DEAPI;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.engine.api.EngineConfig;
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.IReportEngineFactory;
import org.eclipse.birt.report.engine.api.IReportRunnable;
import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
public class ModifyRunningReport {
public void runReport() throws EngineException
{
IReportEngine engine=null;
EngineConfig config = null;
try{
config = new EngineConfig( );
config.setBIRTHome("C:/birt/birt-runtime-2_6_2/birt-runtime-2_6_2/ReportEngine");
//config.setLogConfig(null, Level.FINE);
Platform.startup( config );
IReportEngineFactory factory = (IReportEngineFactory) Platform
.createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
engine = factory.createReportEngine( config );
IReportRunnable design = null;
//Open the report design
design = engine.openReportDesign("Reports/TopNPercent.rptdesign");
ReportDesignHandle report = (ReportDesignHandle) design.getDesignHandle( );
report.findElement("NewChart").drop();
//Create task to run and render the report,
IRunAndRenderTask task = engine.createRunAndRenderTask(design);
task.setParameterValue("Top Percentage", new Integer(3));
task.setParameterValue("Top Count", new Integer(5));
task.validateParameters();
HTMLRenderOption options = new HTMLRenderOption();
options.setOutputFileName("output/desample/ModifiedTopNPercent.html");
options.setOutputFormat("html");
options.setImageDirectory("images");
task.setRenderOption(options);
task.run();
task.close();
engine.destroy();
Platform.shutdown();
System.out.println("Finished");
}catch( Exception ex){
ex.printStackTrace();
}
}
/**
*
@param
args
*/
public static void main(String[] args) {
try
{
ModifyRunningReport ex = new ModifyRunningReport( );
ex.runReport();
}
catch ( Exception e )
{
e.printStackTrace();
}
}
}
This example just drops a chart from the running report. The report on disk is never changed. BTW keep in mind with this example that it is not effecient as it starts the platform runs one report and shuts down the platform. In a running system you would most likely on want to start the platform up once and use the engine to create a new task for each report and then shut down the platform when the application shuts down.
Jason
donwood
Jason,
I made the changes and it worked as I wanted.
Thanks so much for your timely input.
Don