Creating an Interactive Report using the API

To create an interactive report using the API, there are three additional things you need to specify.
  1. Set the ReportBlox to interactive using the setInteractive method.
    rBlox.setInteractive(true);
  2. Set the URL prefix. This information is needed by the servlet handling interaction. Typically this is handled automatically for you when you use the Blox Report Tag Library to create your ReportBlox. When using the API, you have to explicitly specify the URL prefix as follows:
    BloxContext context = BloxContextFactory.getBloxContext(request, response);
    rBlox.setUrlPrefix(context.getContextPath() + "/" + 
       URLFactory.ALPHABLOX_SERVER_PREFIX);
  3. Place the ReportBlox into BloxContext. When you use the Blox Report Tag Library, this is also handled automatically for you. When using the API, you have to explicitly add the ReportBlox to the BloxContext and call the init() method.
     String scriptId = rBlox.getBloxName();
     if(scriptId == null)
     {
        scriptId = rBlox.getId();
     }
     rBlox.init(context,scriptId);
    This piece of code registers the ReportBlox with the BloxContext using the optional bloxName if it is specified. If not, it uses the Blox id.

The complete example is now as follows:

<%@ page import="com.alphablox.blox.*,
                 com.alphablox.net.URLFactory"
<html>
<head>
   <link rel="stylesheet" href="/AlphabloxServer/theme/report.css" /> 
</head>
<body>
<% 
  String query="SELECT Week_Ending, location, product_name, sales, units, cost 
     FROM qcc WHERE Week_Ending_Text = '04-08-2000'";

  try {
    ReportBlox rBlox = new ReportBlox();
    rBlox.setErrors(true);
    rBlox.setId("myRBlox");

    // 1. Set the ReportBlox to interactive
    rBlox.setInteractive(true);

    DataSourceConnectionBlox dConn = new DataSourceConnectionBlox();
    dConn.setDataSourceName("qcc2003-rdb");
    dConn.connect();

    SQLDataBlox dBlox = new SQLDataBlox();
    dBlox.setInput(dConn);
    dBlox.setQuery(query);
    dBlox.execute();

    // Create the grouping
    GroupBlox myGroup = new GroupBlox();
    myGroup.setMembers( new String [] {"location"});
    myGroup.setAggregationType("units", "none");

    // Set up the input for the group
    myGroup.setInput(dBlox);
    rBlox.setInput(myGroup);

    // 2. Set the URL prefix
    BloxContext context = BloxContextFactory.getBloxContext(request, response);
    rBlox.setUrlPrefix(context.getContextPath() + "/" + 
       URLFactory.ALPHABLOX_SERVER_PREFIX);


    // 3. Add the ReportBlox to the Context
    String scriptId = rBlox.getBloxName();
    if(scriptId == null)
    {
       scriptId = rBlox.getId();
    }
    rBlox.init(context,scriptId);
    // Finally call ReportBlox’s write() method to write it out
    rBlox.write(out);

  } 
  catch ( Exception e ) { 
    ErrorBlox eBlox = new ErrorBlox();
    Throwable msg = eBlox.getRootCause(e); 
    out.println("<br><b> This Exception was captured</b><br> "+
msg.getMessage()); 
  } 

</body>
</html>

You probably have found that it is a lot more convenient to use tags to create your relational report. You can always script to the objects using their id when you need to.