Using a Java object to access a data source

Hey there,


 


I'm using Birt in a spring boot application and I'm trying to call my service bean method in the open() script method, but I couldn't get the application context using the typical code

 



importPackage(Packages.org.springframework.context);
importPackage(Packages.org.springframework.web.context.support );
//ServletContext
var sc = reportContext.getHttpServletRequest().getSession().getServletContext();
//ApplicationContext
var spring = WebApplicationContextUtils.getWebApplicationContext(sc);
var mypojo = spring.getBean("carService");
this.text = mypojo.getAllCars().get(0).getMake();

I saw that there is a way to use java object to call open method, because it will get me out of the problem of getting the context but how can where should I save the list of objects after callen the service in code bellow?



public class DocScriptedDataSet extends ScriptedDataSetEventAdapter {

public int recordCount = 0;

@Override
public boolean fetch(IDataSetInstance dataSet, IUpdatableDataSetRow row) {
try {
if (recordCount < 20) {
recordCount++;
row.setColumnValue("col1", recordCount);
row.setColumnValue("col2", "Count = " + recordCount);
row.setColumnValue("col3", recordCount * .05);
return true;
} else {
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}

@Override
public void open(IDataSetInstance dataSet) {
recordCount = 0; //here I should call my service and get the list of objects, but where I have to save them?
}
}

Comments

  • kclarkkclark BIRT Guru EM

    Do you have a REST endpoint for your application?  One option would be to create a scripted data source for your report using something like $.AJAX().


     


    Another option could be to create a POJO for this reports data set.


    Kristopher Clark

    Actuate Corporation – The BIRT Company™

    http://www.actuate.com | http://birtondemand.com

     

    My Blog

    Twitter

    LinkedIn
    Facebook
     

  • You should save your objects into instance fields. It is the same way the recordCount variable is shown in the sample code for DocScriptedDataSet.


    After initializing your required objects in the open() method, they are then available for use in the fetch() method.



    public class DocScriptedDataSet extends ScriptedDataSetEventAdapter {
         private Iterator<Car> cars;
         
         @override
         public void open(IDataSetInstance dataSet) {
              this.cars = ... // obtained from CarService via Spring
         }
         
    @override
    public boolean fetch(IDataSetInstance dataSet, IUpdatableDataSetRow row) {
    if(!cars.hasNext()) {
    return false; // no more work to do
    }
    Car car = cars.next();
    ... // use the Car object
    return true;
    }
    }

    Hope I understood what you wanted to do.

Sign In or Register to comment.