Jump to: navigation, search

Difference between revisions of "Parameter Details (BIRT) 2.1"

m
m
 
Line 65: Line 65:
 
   //Create Parameter Definition Task and retrieve parameter definitions
 
   //Create Parameter Definition Task and retrieve parameter definitions
 
   IGetParameterDefinitionTask task = engine.createGetParameterDefinitionTask( design );
 
   IGetParameterDefinitionTask task = engine.createGetParameterDefinitionTask( design );
   Collection params = task.getParameterDefns( true );
+
   Collection<IParameterDefnBase> params = (Collection<IParameterDefnBase>)task.getParameterDefns( true );
 
   
 
   
 
   //Iterate over each parameter
 
   //Iterate over each parameter
   Iterator iter = params.iterator( );
+
   for ( IParameterDefnBase param : params )
  while ( iter.hasNext( ) )
+
 
   {
 
   {
  IParameterDefnBase param = (IParameterDefnBase) iter.next( );
 
 
 
   if ( param instanceof IParameterGroupDefn )
 
   if ( param instanceof IParameterGroupDefn )
 
   {
 
   {
Line 80: Line 77:
 
     // Do something with the parameter group.
 
     // Do something with the parameter group.
 
     // Iterate over group contents.
 
     // Iterate over group contents.
     Iterator i2 = group.getContents( ).iterator( );
+
     for ( Iterator i2 = group.getContents( ).iterator( ); i2.hasNext( ) )
    while ( i2.hasNext( ) )
+
 
     {
 
     {
 
     IScalarParameterDefn scalar = (IScalarParameterDefn) i2.next( );
 
     IScalarParameterDefn scalar = (IScalarParameterDefn) i2.next( );
Line 115: Line 111:
 
   HashMap<String, Serializable> parameter = new HashMap<String, Serializable>();
 
   HashMap<String, Serializable> parameter = new HashMap<String, Serializable>();
 
   
 
   
   if( group == null){
+
   parameter.put("Parameter Group", group == null ? "Default" : group.getName());   
  parameter.put("Parameter Group", "Default");
+
  }else{
+
  parameter.put("Parameter Group", group.getName());   
+
  }
+
 
   parameter.put("Name", scalar.getName());
 
   parameter.put("Name", scalar.getName());
 
   parameter.put("Help Text", scalar.getHelpText());
 
   parameter.put("Help Text", scalar.getHelpText());
Line 126: Line 118:
 
   parameter.put("Display Format", scalar.getDisplayFormat());
 
   parameter.put("Display Format", scalar.getDisplayFormat());
 
   
 
   
   if( scalar.isHidden() ){
+
   parameter.put("Hidden", scalar.isHidden() ? "Yes" : "No");
  parameter.put("Hidden", "Yes");
+
   parameter.put("Allow Blank", scalar.allowBlank() ? "Yes" : "No");
  }else{
+
   parameter.put("Allow Null", scalar.allowNull() ? "Yes" : "No");
  parameter.put("Hidden", "No");
+
   parameter.put("Conceal Entry", scalar.isValueConcealed() ? "Yes" : "No");  //ie passwords etc
   }
+
  if( scalar.allowBlank() ){
+
  parameter.put("Allow Blank", "Yes");
+
   }else{
+
  parameter.put("Allow Blank", "No");
+
  }
+
  if( scalar.allowNull() ){
+
  parameter.put("Allow Null", "Yes");  
+
   }else{
+
  parameter.put("Allow Null", "No");
+
  }
+
  if( scalar.isValueConcealed() ){
+
  parameter.put("Conceal Entry", "Yes");  //ie passwords etc
+
  }else{
+
  parameter.put("Conceal Entry", "No");
+
  }
+
 
   
 
   
 
   
 
   
Line 178: Line 154:
 
   //retrieve selection list for cascaded parameter
 
   //retrieve selection list for cascaded parameter
 
   if ( parameterHandle.getContainer( ) instanceof CascadingParameterGroupHandle ){
 
   if ( parameterHandle.getContainer( ) instanceof CascadingParameterGroupHandle ){
    Collection sList = Collections.EMPTY_LIST;
 
 
     if ( parameterHandle.getContainer( ) instanceof CascadingParameterGroupHandle )
 
     if ( parameterHandle.getContainer( ) instanceof CascadingParameterGroupHandle )
 
     {
 
     {
     int index = parameterHandle.getContainerSlotHandle( )
+
     int index = parameterHandle.getContainerSlotHandle( ).findPosn( parameterHandle );
    .findPosn( parameterHandle );
+
 
     Object[] keyValue = new Object[index];
 
     Object[] keyValue = new Object[index];
 
     for ( int i = 0; i < index; i++ )
 
     for ( int i = 0; i < index; i++ )
 
     {
 
     {
       ScalarParameterHandle handle = (ScalarParameterHandle) ( (CascadingParameterGroupHandle) parameterHandle.getContainer( ) ).getParameters( )
+
       ScalarParameterHandle handle = (ScalarParameterHandle) ( (CascadingParameterGroupHandle) parameterHandle.getContainer( ) ).getParameters( ).get( i );
      .get( i );
+
 
       //Use parameter default values
 
       //Use parameter default values
 
       keyValue[i] = handle.getDefaultValue();
 
       keyValue[i] = handle.getDefaultValue();
Line 194: Line 167:
 
     task.evaluateQuery( groupName );
 
     task.evaluateQuery( groupName );
 
   
 
   
     sList = task.getSelectionListForCascadingGroup( groupName, keyValue );
+
     Collection<IParameterSelectionChoice> sList = (Collection<IParameterSelectionChoice>)task.getSelectionListForCascadingGroup( groupName, keyValue );
 
     HashMap<Object, String> dynamicList = new HashMap<Object, String>();       
 
     HashMap<Object, String> dynamicList = new HashMap<Object, String>();       
 
   
 
   
 
   
 
   
     for ( Iterator sl = sList.iterator( ); sl.hasNext( ); )
+
     for ( IParameterSelectionChoice sI : sList )
 
     {
 
     {
      IParameterSelectionChoice sI = (IParameterSelectionChoice) sl.next( );
 
 
 
 
       Object value = sI.getValue( );
 
       Object value = sI.getValue( );
 
       Object label = sI.getLabel( );
 
       Object label = sI.getLabel( );
Line 215: Line 185:
 
   }else{
 
   }else{
 
     //retrieve selection list
 
     //retrieve selection list
     Collection selectionList = task.getSelectionList( scalar.getName() );
+
     Collection<IParameterSelectionChoice> selectionList = (Collection<IParameterSelectionChoice>)task.getSelectionList( scalar.getName() );
 
   
 
   
 
     if ( selectionList != null )
 
     if ( selectionList != null )
Line 221: Line 191:
 
     HashMap<Object, String> dynamicList = new HashMap<Object, String>();       
 
     HashMap<Object, String> dynamicList = new HashMap<Object, String>();       
 
   
 
   
     for ( Iterator sliter = selectionList.iterator( ); sliter.hasNext( ); )
+
     for ( IParameterSelectionChoice selectionItem : selectionList )
 
     {
 
     {
      IParameterSelectionChoice selectionItem = (IParameterSelectionChoice) sliter.next( );
 
 
 
       Object value = selectionItem.getValue( );
 
       Object value = selectionItem.getValue( );
 
       String label = selectionItem.getLabel( );
 
       String label = selectionItem.getLabel( );
Line 240: Line 208:
 
   //Print out results
 
   //Print out results
 
   System.out.println("======================Parameter =" + scalar.getName());
 
   System.out.println("======================Parameter =" + scalar.getName());
   for (Iterator iter = parameter.entrySet().iterator(); iter.hasNext(); ) {
+
   for (Map.Entry<String, Serializable> entry : parameter.entrySet( ) {
  Map.Entry entry = (Map.Entry) iter.next();
+
   String name = entry.getKey();  
   String name = (String) entry.getKey();  
+
 
   if( name.equals("Selection List")){
 
   if( name.equals("Selection List")){
     HashMap selList = (HashMap)entry.getValue();
+
     HashMap<?,?> selList = (HashMap<?,?>)entry.getValue();
     Iterator selIter = selList.entrySet().iterator();
+
     for (Map.Entry<?,?> entry : selList.entrySet()) {
    for (Iterator selIter = selList.entrySet().iterator(); selIter.hasNext(); ) {
+
    Map.Entry entry = (Map.Entry)selIter.next();
+
 
     System.out.println( "Selection List Entry ===== Key = " + entry.getKey() + " Value = " + entry.getValue());
 
     System.out.println( "Selection List Entry ===== Key = " + entry.getKey() + " Value = " + entry.getValue());
 
     }
 
     }

Latest revision as of 14:06, 27 March 2010

< To: Integration Examples (BIRT)

Parameter Details

This example demonstrates using the RE API to extract parameter details from the report design. It also uses the DE API, to extract report parameter default values.

Add comments at the bottom of the example.

BIRT Report Engine API Return to the BIRT Report Engine API examples

Source

ParametersTask.java

import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.logging.Level;

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.IGetParameterDefinitionTask;
import org.eclipse.birt.report.engine.api.IParameterDefnBase;
import org.eclipse.birt.report.engine.api.IParameterGroupDefn;
import org.eclipse.birt.report.engine.api.IParameterSelectionChoice;
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.IScalarParameterDefn;
import org.eclipse.birt.report.model.api.CascadingParameterGroupHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.ScalarParameterHandle;
public class ParametersTask {

static void executeReport() throws EngineException
{
 HashMap<String, HashMap<String, Serializable>> parmDetails = new HashMap<String, HashMap<String, Serializable>>();

 IReportEngine engine=null;
 EngineConfig config = null;
 try{
  config = new EngineConfig( );
  config.setEngineHome( "C:/birt-runtime-2_1_1/birt-runtime-2_1_1/ReportEngine" );
  config.setLogConfig(null, Level.FINE);

  Platform.startup( config );
  IReportEngineFactory factory = (IReportEngineFactory) Platform
  .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
  engine = factory.createReportEngine( config );
  engine.changeLogLevel( Level.WARNING );

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

 IReportRunnable design = null;

 //Open a report design 
 design = engine.openReportDesign("C:/test/2.1/parameters/Parameters.rptdesign"); 

 //Create Parameter Definition Task and retrieve parameter definitions
 IGetParameterDefinitionTask task = engine.createGetParameterDefinitionTask( design );
 Collection<IParameterDefnBase> params = (Collection<IParameterDefnBase>)task.getParameterDefns( true );

 //Iterate over each parameter
 for ( IParameterDefnBase param : params )
 {
  if ( param instanceof IParameterGroupDefn )
  {
   IParameterGroupDefn group = (IParameterGroupDefn) param;
   //System.out.println( "Parameter Group: " + group.getName( ) );

   // Do something with the parameter group.
   // Iterate over group contents.
   for ( Iterator i2 = group.getContents( ).iterator( ); i2.hasNext( ) )
   {
    IScalarParameterDefn scalar = (IScalarParameterDefn) i2.next( );
    //System.out.println("\t" + scalar.getName());
    //Get details on the parameter
    parmDetails.put( scalar.getName(), loadParameterDetails( task, scalar, design, group));
   }

  }
  else
  {

   IScalarParameterDefn scalar = (IScalarParameterDefn) param;
   //System.out.println(param.getName());
   //get details on the parameter
   parmDetails.put( scalar.getName(),loadParameterDetails( task, scalar, design, null));                   
  }
 }

 //Destroy the engine and shutdown the Platform
 //Note - If the program stays resident do not shutdown the Platform or the Engine  
 engine.shutdown();
 Platform.shutdown();
 System.out.println("Finished");
}
 
//Function to load parameter details in a map.
private static HashMap<String, Serializable> loadParameterDetails(IGetParameterDefinitionTask task, IScalarParameterDefn scalar, IReportRunnable report, IParameterGroupDefn group)
{


 
 HashMap<String, Serializable> parameter = new HashMap<String, Serializable>();

 parameter.put("Parameter Group", group == null ? "Default" : group.getName());   
 parameter.put("Name", scalar.getName());
 parameter.put("Help Text", scalar.getHelpText());
 parameter.put("Display Name", scalar.getDisplayName());
 //this is a format code such as  > for UPPERCASE
 parameter.put("Display Format", scalar.getDisplayFormat());

 parameter.put("Hidden", scalar.isHidden() ? "Yes" : "No");
 parameter.put("Allow Blank", scalar.allowBlank() ? "Yes" : "No");
 parameter.put("Allow Null", scalar.allowNull() ? "Yes" : "No");
 parameter.put("Conceal Entry", scalar.isValueConcealed() ? "Yes" : "No");  //ie passwords etc


 switch (scalar.getControlType()) {
 case IScalarParameterDefn.TEXT_BOX:  parameter.put("Type", "Text Box"); break;
 case IScalarParameterDefn.LIST_BOX:  parameter.put("Type", "List Box"); break;
 case IScalarParameterDefn.RADIO_BUTTON:  parameter.put("Type", "List Box"); break;
 case IScalarParameterDefn.CHECK_BOX:  parameter.put("Type", "List Box"); break;
 default: parameter.put("Type", "Text Box");break;
 }


 switch (scalar.getDataType()) {
 case IScalarParameterDefn.TYPE_STRING:  parameter.put("Data Type", "String"); break;
 case IScalarParameterDefn.TYPE_FLOAT:  parameter.put("Data Type", "Float"); break;
 case IScalarParameterDefn.TYPE_DECIMAL:  parameter.put("Data Type", "Decimal"); break;
 case IScalarParameterDefn.TYPE_DATE_TIME:  parameter.put("Data Type", "Date Time"); break;
 case IScalarParameterDefn.TYPE_BOOLEAN:  parameter.put("Data Type", "Boolean"); break;
 default:  parameter.put("Data Type", "Any"); break;
 }


 //Get report design and find default value, prompt text and data set expression using the DE API
 ReportDesignHandle reportHandle = ( ReportDesignHandle ) report.getDesignHandle( );
 ScalarParameterHandle parameterHandle = ( ScalarParameterHandle ) reportHandle.findParameter( scalar.getName() );
 parameter.put("Default Value", parameterHandle.getDefaultValue());
 parameter.put("Prompt Text", parameterHandle.getPromptText());
 parameter.put("Data Set Expression", parameterHandle.getValueExpr());

 if(scalar.getControlType() !=  IScalarParameterDefn.TEXT_BOX)
 {
  //retrieve selection list for cascaded parameter
  if ( parameterHandle.getContainer( ) instanceof CascadingParameterGroupHandle ){
   if ( parameterHandle.getContainer( ) instanceof CascadingParameterGroupHandle )
   {
    int index = parameterHandle.getContainerSlotHandle( ).findPosn( parameterHandle );
    Object[] keyValue = new Object[index];
    for ( int i = 0; i < index; i++ )
    {
     ScalarParameterHandle handle = (ScalarParameterHandle) ( (CascadingParameterGroupHandle) parameterHandle.getContainer( ) ).getParameters( ).get( i );
     //Use parameter default values
     keyValue[i] = handle.getDefaultValue();
    }
    String groupName = parameterHandle.getContainer( ).getName( );
    task.evaluateQuery( groupName );

    Collection<IParameterSelectionChoice> sList = (Collection<IParameterSelectionChoice>)task.getSelectionListForCascadingGroup( groupName, keyValue );
    HashMap<Object, String> dynamicList = new HashMap<Object, String>();       


    for ( IParameterSelectionChoice sI : sList )
    {
     Object value = sI.getValue( );
     Object label = sI.getLabel( );
     System.out.println( label + "--" + value);
     dynamicList.put(value,(String) label);

    }         
    parameter.put("Selection List", dynamicList);


   }         
  }else{
   //retrieve selection list
   Collection<IParameterSelectionChoice> selectionList = (Collection<IParameterSelectionChoice>)task.getSelectionList( scalar.getName() );

   if ( selectionList != null )
   {
    HashMap<Object, String> dynamicList = new HashMap<Object, String>();       

    for ( IParameterSelectionChoice selectionItem : selectionList )
    {
     Object value = selectionItem.getValue( );
     String label = selectionItem.getLabel( );
     //System.out.println( label + "--" + value);
     dynamicList.put(value,label);

    }
    parameter.put("Selection List", dynamicList);
   }
  }

 }


 //Print out results
 System.out.println("======================Parameter =" + scalar.getName());
 for (Map.Entry<String, Serializable> entry : parameter.entrySet( ) {
  String name = entry.getKey(); 
  if( name.equals("Selection List")){
   HashMap<?,?> selList = (HashMap<?,?>)entry.getValue();
   for (Map.Entry<?,?> entry : selList.entrySet()) {
    System.out.println( "Selection List Entry ===== Key = " + entry.getKey() + " Value = " + entry.getValue());
   }

  }else{
   System.out.println( name + " = " + entry.getValue());     
  }
 }
 return parameter;
}

/**
 * @param args
 */
public static void main(String[] args) {
 try
 {
  executeReport( );
 }
 catch ( Exception e )
 {
  e.printStackTrace();
 }
}

}


Comments

Please enter comments below by selecting the edit icon to the right. You will need a Bugzilla account to add comments.