[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [birt-dev] scripted datasource cannot access public methods



On Tue, May 23, 2017 at 5:52 AM, Uli Schulze-Eyssing <uli@xxxxxxxx> wrote:
Hi,
IÂm trying to set up a scripted Datasource using pojos from my app (java 1.8).
I embedded a reportengine (4.4.0) in my app via maven and created a report with a scripted datasourse. The pojos can be seen by the report.

bookSrv = Packages.de.me.service.ReportDataService;

This code just has access to the class ReportDataService, so you would be able to invoke just static methods on it.

Â


works.

I can access static methods of my object, but whatever i try, accessing public methods or fields results in an exception.

So

books = bookSrv.books;

gives
org.mozilla._javascript_.EvaluatorException: Java class "de.me.service.ReportDataService" has no public instance field or method named "books". (/report/data-sets/script-data-set[@id="5"]/method[@name="open"]#11)
or

books = bookSrv.showMyBooks('Peter');


gives
org.mozilla._javascript_.EvaluatorException: Java class "de.my.service.ReportDataService" has no public instance field or method named "showMyBooks". (/report/data-sets/script-data-set[@id="5"]/method[@name="open"]#11)

But:

books = Packages.me.tdesk.service.ReportDataService.myBooksStatic('Peter');

works like expected. (Simply a static wrapper around my method)



here is my java class:

package de.me.service;

import de.me.model.dto.BookDto;

import java.util.ArrayList;

import java.util.List;

public class ReportDataService {

  public ReportDataService() {

    books = showMyBooks("Peter");

    System.out.println("ReportDataService.init()");

  }

  private List<BookDto> books;

  public List<BookDto> getBooks() {

    return books;

  }

  public void setBooks(List<BookDto> books) {

    this.books = books;

  }

  public List<BookDto> showMyBooks(String commodity){

    BookDto dto = new BookDto(12l, "otto", "My", 12l);

    List<BookDto> books = new ArrayList<>();

    books.add(dto);

    dto = new BookDto(122l, "paul", "My", 122l);

    books.add(dto);

    return books;

  }

  public static List<BookDto> myBooksStatic(String commodity){

    ReportDataService rep = new ReportDataService();

    return rep.showMyBooks(commodity);

  }

}


Many thanks
Uli


It is required to create the object to invoke the methods on it. Do it as,

importPackage(Packages.de.me.service); // this imports all classes in the package
var rds = new ReportDataService();
var books = rds.showMyBooks('Peter');

You could also create the object as such,
var rds = new Packages.de.me.service.ReportDataService();

Birt uses Rhino _javascript_ engine for its scripting.Â
https://developer.mozilla.org/en-US/docs/Mozilla/Projects/Rhino/Scripting_Java

Also, refer to the BIRT integration documentation under the *Miscellaneous* section another way of setting up Java objects for scripting.
http://www.eclipse.org/birt/documentation/integrating/reapi.php


Regards,
ZorawarÂ