Using a Java object to access a data source

Axel00
edited February 11, 2022 in Analytics #1
<p>Hey there,</p>
<p> </p>
<p>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<br>
 </p>
<pre class="_prettyXprint _lang-js">
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();
</pre>
<p>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?</p>
<pre class="_prettyXprint _lang-js">
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?
}
}
</pre>

Comments

  • <p>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().</p>
    <p> </p>
    <p>Another option could be to create a POJO for this reports data set.</p>
    Warning No formatter is installed for the format ipb
  • <p>You should save your objects into instance fields. It is the same way the recordCount variable is shown in the sample code for DocScriptedDataSet.</p>
    <p>After initializing your required objects in the open() method, they are then available for use in the fetch() method.</p>
    <pre class="_prettyXprint">
    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;
    }
    }
    </pre>
    <p>Hope I understood what you wanted to do.</p>