Jeremy Stein - Brain

« »

Crystal JNDI

Crystal Reports has lousy JNDI support for web applications. If you’re using JNDI, it’s probably so that you can change the database connection without changing the WAR. Even when configured to use JNDI, Crystal stores each table and stored procedure reference with an explicit reference to the database name. Thus, when you deploy on a server whose JNDI is pointing to a different database than the designer used, it no longer works.

I opened case 302844579 regarding this issue. The Business Objects response was:

While we make the JRC to point a JNDI connection at runtime, you are expecting JRC to get the database details automatically and set it by itself internally.

However JRC SDK is not designed to automatically get those database properties from the JNDI.

So we have to specify database properties manually in the code.

So, if you don’t want to embed the database name in your deployment, you have to specify a context parameter with that value. Here is a method I wrote that prepares the Crystal report for display. You’ll need to change DATABASE_NAME to whatever method you use to get the database name from the context.

public ReportClientDocument prepareReport(String filename, Object... parameter) {
    try {
        ReportClientDocument reportClientDoc = new ReportClientDocument();
        reportClientDoc.open(filename, 0);

        DatabaseController databaseController = reportClientDoc.getDatabaseController();
        Tables tables = databaseController.getDatabase().getTables();

        for (int i=0; i < tables.size(); i++) {
            ITable originalTable = tables.getTable(i);
            ITable changedTable = tables.getTable(i);
            String qualifiedTableName = originalTable.getQualifiedName();
            int dotIndex = qualifiedTableName.indexOf('.');
            String tableName = qualifiedTableName.substring(dotIndex);
            changedTable.setQualifiedName(DATABASE_NAME + tableName);
            databaseController.setTableLocation(originalTable,changedTable);
        }

        for (int index = 0; index < parameter.length; index+=2) {
            String name = (String) (parameter[index]);
            Object value = parameter[index+1];
            JRCHelper.addDiscreteParameterValue(reportClientDoc, name, "", value);
        }

        return reportClientDoc;
    } catch (ReportSDKException e) {
        throw new RuntimeException(e);
    }
}

August 24, 2007 No Comments.

No Comments

Be the first to comment!

Leave a Reply

Your email address will not be published. Required fields are marked *

Why ask?

« »