How to retrieve BDM data from groovy classes of your Rest API extension

ismail.lagouilly_1386495's picture
ismail.lagouilly_1386495
Blog Categories: 

If you ever wondered how to retrieve data from a Rest API extension this article is for you.

When developing a Rest API extension, if you need to access data from your Database, these two options can be available to you :

  1. Using DAO and one of the queries available in your BDM;
  2. Using a SQL Query.
1. Using DAO :

As a first step, to be able to import your BDM objects you will need to add the following dependencies in your "pom.xml" file :

    <dependency>
    <groupId>com.company.model</groupId>
    <artifactId>bdm-client</artifactId>
    <version>1.0.0</version>
    <scope>provided</scope>
    </dependency>

    <dependency>
    <groupId>com.company.model</groupId>
    <artifactId>bdm-dao</artifactId>
    <version>1.0.0</version>
    <scope>provided</scope>
    </dependency>`

For the first option, here's an example of code that you can use (in index.groovy for example) to call one of the queries available in your BDM :

    def veloDAO = context.apiClient.getDAO(VeloDAO.class);

    List<Velo> listeVelos = veloDAO.myQuery(0, 100)

Here we have already created a Business object called "Velo" and a custom query in the BDM (with no parameters). The list "listeVelos" will contain the results of your custom query.

2. Using a SQL Query :

For the second option, here's an another example of code to use (in index.groovy for example) in order to call a SQL query directly from your groovy class :

    private DataSource getDataSourceConnection(String sqlDataSourceName) {

    try {
        final Context ctx = new InitialContext();
        final DataSource dataSource = (DataSource) ctx.lookup(sqlDataSourceName);
        return dataSource;

    } catch (NamingException e) {
        // Handle your exception
    }

    return null;
}

Here you need to get your datasource (using your datasource name). Once you have your object Datasource, you can open a connection to the database and run your sql query as this example :

    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet results = null;

    final DataSource dataSource = getDataSourceConnection(sqlDataSourceName);

    if (dataSource != null) {
        con = dataSource.getConnection();

        String sqlRequest = "SELECT * FROM VELO";

        pstmt = con.prepareStatement(sqlRequest);
        results = pstmt.executeQuery();
    }

The result of your SQL query will be stored in a varialble of ResultSet type. After, you can access to all the fields contained inside.

Here's an example of a Groovy class having these code examples :
https://drive.google.com/file/d/1mpMGbZ8liTyuF2wtqs2jSZ63YiRFQ6Xs

Also, you can find here an example of a project with the same examples (Bonita 7.8.4 Subscription only) :
https://drive.google.com/file/d/1l5t4wq3VYXD2t8RYyRaFQewnuyv7sVoM

Here's also the link to test your page in local : http://localhost:8080/bonita/API/extension/resourceName

I hope this little article helped you.

Notifications