Pregunta

I'm using Java Crystal Report SDK for generating reports using stored procedures or ResultSets.

Here is a post I made few months ago, trying to execute the stored procedure of my subreport within a report using a ResultSet : Java Crystal Report SDK - Report & SubReport. In this case, I knew the parameters to set for executing my stored procedure, so it was "easy".

Now, I'm trying to do the same thing (ie executing a stored procedure for populating my subreport within a main report using a ResultSet) but in a generic way : I don't know in advance the parameters, so I have to set each one using the ParameterFieldController but some come from the main ResultSet, others from static variables in the main report etc.

I realize that there is a lot to do and many cases to cover in order to be as generic as possible. While when I don't set a datasource for my main report, all that stuff is well done automatically.

So, is there an "easy" way to mix ResultSet and stored procedures in report and subreports ?

Or is there a way to bypass the useDatasource() method ? that certainly decides to use the main ResultSet for each subreport instead of given stored procedure.

(I have to keep the ResultSet for the main report and not reexecuting its stored procedure for performances reasons).

After a lot of tries, I'm back here. Unfortunately I'm still wondering how to simply pass my parameters to the subreports of a main report using a ResultSet...

Here is what I'm using for retrieving the links between main report and subreports and subreports parameters :

IStrings subreports = clientDoc.getSubreportController().getSubreportNames(); 
for (int i = 0; i < subreports.size(); i++) { 
    // Get subreport 
    String subreportName = subreports.getString(i); 
    ISubreportClientDocument subreport = clientDoc.getSubreportController().getSubreport(subreportName); 
    // Get datasource tables 
    databaseController = subreport.getDatabaseController(); 
    tables = databaseController.getDatabase().getTables(); 
    // Get links between subreport and main report 
    SubreportController subreportController = clientDoc.getSubreportController(); 
    SubreportLinks links = subreportController.getSubreportLinks(subreportName); 
    Fields params = subreport.getDataDefController().getDataDefinition().getParameterFields();  
    // Set datasource 
    setTablesLocation(tables, databaseController, args);  
 } 

The thing is that each SubreportLink from links is half filled or empty.

  • If the subreport parameter is linked to a field of the main report stored procedure or a formula field, I have the name of this parameter in the SubreportLink (using getMainReportFieldName()) but not the name of linked parameter from the subreport (using getSubreportFieldName()) and not the value of the link.
  • If the subreport parameter is linked to a parameter field of the main report, the SubreportLink objects are empty.

So, it's very tricky to set the subreports parameters in this condition, even using the half part of each Subreportlink + the ParameterField etc.

What I'm doing wrong ? Can I get all the information needed in my case ?

Or, is there a way to automatically set my subreports parameter as it's done when I use the setTablesLocation() method on my main report (instead of setting the datasource with a ResultSet) ?

¿Fue útil?

Solución

Ok ok so after a lot of fruitless tries, I found out something to generate my report containing subreports only if the main stored procedure returned data. I use a RowsetCursor.

Here is my code :

// Here I create a metadata using ONLY the database fields
IRowsetMetaData metadata = new RowsetMetaData();
Fields fields =  clientDoc.getDataDefController().getDataDefinition().getResultFields();
Fields dbFields = new Fields();
for (int i = 0; i < fields.size(); i++) {
    IField field = fields.getField(i);
    if (field instanceof DBField) {
        dbFields.add(field);
    }
}
metadata.setDataFields(dbFields);

// Create the rowset cursor with metadata
RowsetCursor cursor = clientDoc.getRowsetController().createCursor(null, metadata);
FetchedRecordCountInfo countInfo = new FetchedRecordCountInfo();
countInfo.setIsTotalRecordsKnown(false);

// Here, if I don't have any record from my stored procedure, I don't execute the report process
int nbRecords = cursor.getRecordCount(countInfo);
if (nbRecords <= 0) {
    throw new ReportingException("UnmanagedJob - Report generation : No data, generation aborted.");
}

I hope this will help other "lost user" of Crystal Report java SDK :)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top