Home
Analytics
How to pass a query params to birt report
drqbass
hello, i have a trouble with the birt report i have a data set data like parameters and this are a lot of params but i want that the user just give click to a link and this parameters put inside the birt report without user help. How can i do this?? i try to do this with
reportContext.getParamValues("name of param","paramValue");
but this only works for one param and i want all params. Please Help!!!
Find more posts tagged with
Comments
cypherdj
If I understand correctly, you have a database query within your report, and this query has a parameter, like:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
select col1, col2 from mytable where col3 = ?
</pre>
<br />
And you would like to pass a specific value to this query parameter at runtime using the report engine API, correct?<br />
<br />
If that's the case, let's assume you have created a report parameter, which is linked to your query parameter.<br />
<br />
To pass values at runtime, you can use the following API call:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
IRunAndRenderTask rrt = reportEngine.createRunAndRenderTask(reportDesign);
rrt.setParameterValue("reportparam", objValue);
</pre>
<br />
Or, if you want to pass multiple parameter values, you can simply use:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
rrt.setParameterValues(paramsMap);
</pre>
<br />
Regards,<br />
Cedric
drqbass
IRunAndRenderTask rrt = reportEngine.createRunAndRenderTask(reportDesign);<br />
rrt.setParameterValue("reportparam", objValue);<br />
[/code]<br />
<br />
Or, if you want to pass multiple parameter values, you can simply use:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
rrt.setParameterValues(paramsMap);
</pre>
<br />
<br />
Ok thanks cedric for reply!!! =D, and where i have to put this code?? i have to put into the jsp page or into initialize event???
drqbass
i have a query like this
select detdeb.cod_presup ,movdeb.monto_cuota, case when subcta.id_negocio is null then
(select ser.descripcion_servicio
from dbsctm.txn_servicio ser
where ser.id_txn_servicio = (select detrecinm.id_txn_servicio
from dbsctm.txn_det_recibo_inm detrecinm, dbsctm.txn_gen_recibo genrec
where genrec.id_sub_cta = (?,?,?) and
genrec.id_recibo = detrecinm.id_recibo))
else(select act.nombre_actividad
from dbsctm.cat_actividad act
where act.id_actividad = (select detrecneg.id_actividad
from dbsctm.txn_det_recibo_neg detrecneg, dbsctm.txn_gen_recibo genrec
where genrec.id_sub_cta = (?,?,?) and
genrec.id_recibo = detrecneg.id_recibo))
end
from dbsctm.txn_gen_recibo genrec, dbsctm.txn_sub_cta subcta, dbsctm.txn_mov_debito movdeb, dbsctm.txn_det_debito detdeb
where genrec.id_sub_cta = (?,?,?) and
genrec.id_sub_cta = subcta.id_sub_cta and
subcta.id_sub_cta= movdeb.id_sub_cta and
movdeb.id_sub_cta = detdeb.id_sub_cta
where the interrogations signs are the multiple params that i want to pass.
ok, for passing the parameters i put into an array all the id's that i have to use.
Having this i download a js archive but i dont know how to use this. Here is the code.
importPackage(Packages.java.util.logging);
var fileHandler = new FileHandler("/tmp/birt.log", true);
var rootLogger = Logger.getLogger("");
rootLogger.addHandler(fileHandler);
function log(str) {
Logger.getAnonymousLogger().info(str);
}
//reportContext.getReportRunnable().getReportEngine().changeLogLevel(Level.FINE);
importPackage(Packages.org.eclipse.birt.report.model.api);
//paramProcess("Accepted"); // you can specify a dataset to process this way, or process all
processAllQueries();
function processAllQueries() {
var DH = reportContext.getReportRunnable().getDesignHandle();
var dsAll = DH.getAllDataSets().toArray();
for(var ii = 0; ii < dsAll.length; ii++) {
var odaHandle = dsAll[ii];
if ( odaHandle instanceof OdaDataSetHandle ) {
paramProcess(odaHandle.getName());
}
}
}
function countParameters( arg1 ) {
var count = 0;
var index = 0;
while ( ( index = arg1.indexOf( "?", index ) ) != -1 ) {
++index;
++count;
}
return count;
}
/*
Replace an existing "placeholder" parameter binding with the first parameter from the parameter "in" list.
The others in the list will be inserted after this one.
*/
function replaceParameterBinding( curDataSet, parameterHandle, parameter, ii ) {
var params = curDataSet.parametersIterator();
var pos = 0;
while ( params.hasNext() ) {
pos++;
var obj = params.next();
if ( obj instanceof OdaDataSetParameterHandle && obj.getPosition() == ii) {
log("replacing parameter " + obj.getName() + " with " + parameter.getName());
parameterHandle.replaceItem( obj.getStructure(), parameter );
}
}
}
/*
Add or replace a parameter binding.
*/
function addParameterBinding( curDataSet, paramName, first, paramVal, overallParamPos ) {
var newParamName = paramName + "_" + overallParamPos
log("binding " + newParamName + " (" + paramVal + ") to position " + overallParamPos);
parameterHandle = curDataSet.getPropertyHandle( DataSetHandle.PARAMETERS_PROP );
parameter = StructureFactory.createOdaDataSetParameter();
parameter.setName( newParamName );
parameter.setPosition( overallParamPos );
parameter.setDataType( "integer" );
parameter.setDefaultValue( paramVal*1 );
parameter.setIsInput( true );
parameter.setIsOutput( false );
if(first==true) {
replaceParameterBinding(curDataSet, parameterHandle, parameter, overallParamPos);
} else {
log("inserting " + parameter.getName() + " at position " + overallParamPos);
parameterHandle.insertItem( parameter, overallParamPos-1 );
reOrderParameters(curDataSet);
}
}
/*
Each time we insert a new parameter, we have to shift the following parameters down
*/
function reOrderParameters( curDataSet ) {
var params = curDataSet.parametersIterator();
var pos = 0;
while ( params.hasNext() ) {
pos++;
var obj = params.next();
if ( obj instanceof OdaDataSetParameterHandle ) {
log("setPosition of " + obj.getName() + " " + obj.getPosition() + " to " + pos);
obj.setPosition( pos );
}
}
}
function listParameters( curDataSet ) {
var params = curDataSet.parametersIterator();
var pos = 0;
while ( params.hasNext() ) {
var obj = params.next();
if ( obj instanceof OdaDataSetParameterHandle ) {
log("Parameter " + obj.getName() + " at " + obj.getPosition() + " " + obj.getDataType() + " " + obj.getDefaultValue() + " " + obj.isInput() + " " + obj.getParamName());
}
}
}
function getParam(dataSetHandle, paramIndex) {
var params = dataSetHandle.parametersIterator();
var pos = 0;
var param = null;
while ( params.hasNext() ) {
param = params.next();
if ( param instanceof OdaDataSetParameterHandle && param.getPosition() == paramIndex) {
return param;
}
}
return null;
}
function updateDataSet(curDataSet, query ) {
var parts = query.split("?");
var newQuery = "";
var paramsIndex = 1;
for(var ii = 0; ii < parts.length; ii++) {
newQuery += parts[ii];
if((ii*1)+1 == parts.length) continue;
log("looking for parameter " + paramsIndex);
var param = getParam(curDataSet, paramsIndex);
var paramName = param.getParamName();
// the "paramName" here, is the Linked to Report Parameter name...
// it will be null if this parameter is not linked to a report parameter
// in which case, we'll leave it alone
log("paramName [" + ii + "] = " + paramName);
var paramValue = null;
var paramStr = "";
var pVals = null;
if(paramName != null)
paramValue = params[paramName]+"";
log("checking " + paramName + " " + paramValue + " " + paramsIndex);
if(paramValue == null)
paramStr = "?";
else {
pVals = paramValue.split(",");
var sep = "";
for ( var i = 0; i < pVals.length; i++ ) {
paramStr += sep + "?";
sep = ",";
}
}
newQuery += paramStr;
if(paramStr == "?") {
paramsIndex++;
continue;
}
log("updating parameter " + param.getName() + " " + paramValue + " pos " + param.getPosition() + " " + param.getDataType() + " " + param.getDefaultValue() + " " + param.isInput() + " " + param.getParamName());
for ( var i = 0; i < pVals.length; i++ ) {
addParameterBinding( curDataSet, paramName, (i==0)?true:false, pVals[ i ], paramsIndex++);
}
}
return newQuery;
}
function paramProcess(dataset) {
var check = dataset+"_processed";
if(reportContext.getPersistentGlobalVariable(check)=="yes")
return;
reportContext.setPersistentGlobalVariable(check, "yes");
var DH = reportContext.getReportRunnable().getDesignHandle();
var odaHandle = DH.findDataSet(dataset);
if(odaHandle == null) return;
var name = odaHandle.getName();
log("name = " + name);
var query = odaHandle.getQueryText();
log("paramProcess(" + query +")");
// for each parameter ? in the query, look up the corresponding parameter and see if it's specified as a list
// if it is, replace the ? with the correct # of ?'s and add/replace appropriate parameter entries
var paramCount = countParameters( query );
if(paramCount == 0) return;
var newQuery = updateDataSet(odaHandle, query );
odaHandle.setQueryText(newQuery);
log("query = " + newQuery);
}
please help me to understand this and if exist an easier way then i gonna be thankful to know that =D
cypherdj
Well, I'm not sure I understand your question. Presumably, you are trying to integrate the BIRT report engine into your own application, instead of say, using the BIRT web viewer. Is that right?
If that is the case, then you have probably already implemented a servlet which renders a report with no parameter, yeah?
Now, in this code, you've probably created a run task or a run and render task. To pass your parameters, you need to create a java.util.Map (and not an array) and use the method I've mentioned previously.
The code you have posted would be scripting code that you would include within your report design, so I'll assume it was posted by accident, it doesn't quite seem related to this thread...
arvindraj
Hi cypherdj,
Based on part of your above reply, I'd like to get the following clarified :
Could you tell me the difference between using the Report Engine and using the Web Viewer to integrate with your application?
Does the Web Viewer depend on the Report Engine API to show the reports?
Regards,
arvindraj
cypherdj
Hi Arvindraj,
you are absolutely correct, the webviewer depends on the report engine. The webviewer simply provides a web front end to running reports, setting the parameters at runtime, handling multi-paging, exporting to excel/pdf/...
The report engine itself provides the backend functionality.
If your requirements are simply to provide a set of reports for online viewing, then the web viewer might be an inexpensive way to achieve just that.
Typically you would either integrate the report engine in your own application, which would provide the report parameters, maybe store the report in a specific location, stream the report to the front end, generate in a specific format.
For instance, your requirements might be to generate a weekly report in PDF format, to be emailed to a distribution list. The webviewer cannot achieve this, but with the report engine, you can generate the report and build the rest of the functionality to send the email and attach the generated PDF.
In addition, there are also the chart engine and the design engine. The former allows you to generate charts at runtime, independently of a report design. The latter allows you to build new report design or to modify an existing report design, at runtime. For instance, you might want to allow your users to generate ad hoc reports. The report design depends on user's selection, so can only be created a runtime.
Does that answer your question or do you need further explanations?
Regards,
Cedric
arvindraj
Hi Cedric,
Thank you so much for the info.
It has given me a clearer understanding of the BIRT Viewer & Engine.
Will revert for further clarification.
Thanks & Regards,
arvindraj