Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
PUBLIC CLOUD
PRIVATE CLOUD
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
ListBox parameter with multiple default values (all) except "Null Value" and duplicate value
zsh
<p>Hi All,</p>
<p> </p>
<p>I have set up a simple report parameter group which contains a couple of parameters. Each parameter list contains all the items selected by default.</p>
<p> </p>
<p>
</span></em></p>
<p><em><span style="font-size:10px;">//Please note that all custom code received from Actuate Support is officially untested and unsupported.</span></em></p>
<p><em><span style="font-size:10px;">//This is meant for guidance purposes only and should not be implemented in a production environment without proper testing.</span></em></p>
<p><em><span style="font-size:10px;">//
</span></em></p>
<p><em><span style="font-size:10px;">//The following code will query a parameters dataset and return the result list from the parameters selected column.</span></em></p>
<p><em><span style="font-size:10px;">//This will set the parameters default list with all possible values listed. </span></em></p>
<p><em><span style="font-size:10px;">//The parameter should be setup with the following settings:</span></em></p>
<p><em><span style="font-size:10px;">// Display type: List Box</span></em></p>
<p><em><span style="font-size:10px;">// Selection list values: Dynamic</span></em></p>
<p><em><span style="font-size:10px;">// Allow Multiple Values: Checked</span></em></p>
<p><em><span style="font-size:10px;">// </span></em></p>
<p><em><span style="font-size:10px;">//This code should be placed in a parameters "getDefaultValueList" method.</span></em></p>
<p><em><span style="font-size:10px;">//Last updated: February 4th, 2015</span></em></p>
<p><em><span style="font-size:10px;">//
</span></em></p>
<p><em><span style="font-size:10px;">//The following value should be changed to match the parameter name this code is in. </span></em></p>
<p><em><span style="font-size:10px;">THIS_PARAMETERS_NAME = "GroupName";</span></em></p>
<p> </p>
<p><em><span style="font-size:10px;"> </span></em></p>
<p><em><span style="font-size:10px;">//
</span></em></p>
<p><em><span style="font-size:10px;">//Please note that all custom code received from Actuate Support is officially untested and unsupported.</span></em></p>
<p><em><span style="font-size:10px;">//This is meant for guidance purposes only and should not be implemented in a production environment without proper testing.</span></em></p>
<p><em><span style="font-size:10px;">//No editing below this line should be nessary</span></em></p>
<p><em><span style="font-size:10px;">//
</span></em></p>
<p><em><span style="font-size:10px;">//Gather Data Source Info</span></em></p>
<p><em><span style="font-size:10px;"><b>var</b> dataSetName = reportContext.getDesignHandle().findParameter(THIS_PARAMETERS_NAME).getProperty("dataSetName"); //Get parameters dataset name</span></em></p>
<p><em><span style="font-size:10px;"><b>var</b> columnName = reportContext.getDesignHandle().findParameter(THIS_PARAMETERS_NAME).getProperty("valueExpr").match( /"(.*?)"/ )[1]; //Get parameters dynamic column name</span></em></p>
<p><em><span style="font-size:10px;"><b>var</b> dataSourceName = reportContext.getDesignHandle().findDataSet(dataSetName).getDataSourceName(); //String - DataSetHandle (Get DataSource Name for dataSetName)</span></em></p>
<p> </p>
<p><em><span style="font-size:10px;">//Import Classes</span></em></p>
<p><em><span style="font-size:10px;">importClass(org.eclipse.birt.data.engine.api.querydefn.QueryDefinition); //Default implementation of the IQueryDefinition interface.</span></em></p>
<p><em><span style="font-size:10px;">importClass(org.eclipse.birt.report.data.adapter.api.DataRequestSession); //Main entry point to Data Engine functionalities.</span></em></p>
<p><em><span style="font-size:10px;">importClass(org.eclipse.birt.report.data.adapter.api.DataSessionContext); //Context information used to initialize a DataRequestSession.</span></em></p>
<p> </p>
<p><em><span style="font-size:10px;">//Prepare preQuery</span></em></p>
<p><em><span style="font-size:10px;"><b>var</b> engineConfig = reportContext.getReportRunnable().getReportEngine().getConfig(); //EngineConfig - IReportEngine (Get report engine configurations)</span></em></p>
<p><em><span style="font-size:10px;"><b>var</b> dataSourceHandle = reportContext.getDesignHandle().findDataSource(dataSourceName); //DataSourceHandle - ModuleHandleImpl (Get DataSource handle)</span></em></p>
<p><em><span style="font-size:10px;"><b>var</b> dataSetHandle = reportContext.getDesignHandle().findDataSet(dataSetName); //DataSetHandle - ModuleHandleImpl (Get DataSet handle)</span></em></p>
<p> </p>
<p><em><span style="font-size:10px;"><b>var</b> iScriptContext = _jsContext.getScriptContext(); //IScriptContext - ScriptContext (org.eclipse.birt.core.script.ScriptContext)</span></em></p>
<p><em><span style="font-size:10px;"><b>var</b> dataSessionContext = <b>new</b> DataSessionContext(DataSessionContext.MODE_DIRECT_PRESENTATION, reportContext.getDesignHandle(), iScriptContext, <b>null</b>); //Creates a DataEngine context in the provided mode, using the provided top level scope. Also sets the handle of the report design being executed. (int mode, ModuleHandle moduleHandle, ScriptContext scriptContext, ClassLoader classLoader)</span></em></p>
<p><em><span style="font-size:10px;"><b>var</b> dataRequestSession = DataRequestSession.newSession(engineConfig, dataSessionContext); //DataRequestSession - DataRequestSession (PlatformConfig, DataSessionContext)</span></em></p>
<p> </p>
<p><em><span style="font-size:10px;"><b>var</b> DataModelAdapter = dataRequestSession.getModelAdaptor(); //IModelAdapter - DataRequestSession (Get DataModelAdapter)</span></em></p>
<p><em><span style="font-size:10px;"><b>var</b> odaDataSourceAdapter = DataModelAdapter.adaptDataSource(dataSourceHandle); //BaseDataSourceDesign - IModelAdapter (Get OdaDataSourceAdapter)</span></em></p>
<p><em><span style="font-size:10px;"><b>var</b> odaDataSetAdapter = DataModelAdapter.adaptDataSet(dataSetHandle); //BaseDataSetDesign - IModelAdapter (Get OdaDataSetAdapter)</span></em></p>
<p> </p>
<p><em><span style="font-size:10px;">odaDataSetAdapter.setDataSource(dataSourceName); //void - BaseDataSetDesign</span></em></p>
<p><em><span style="font-size:10px;">dataRequestSession.defineDataSource( odaDataSourceAdapter ); //void - DataRequestSession</span></em></p>
<p><em><span style="font-size:10px;">dataRequestSession.defineDataSet( odaDataSetAdapter ); //void - DataRequestSession</span></em></p>
<p> </p>
<p><em><span style="font-size:10px;">//Is this an iHub3 engine</span></em></p>
<p><em><span style="font-size:10px;"><b>try</b> {iQueryDefinition = <b>new</b> QueryDefinition(<b>true</b>);} //iHub3 compatable (autoBinding)</span></em></p>
<p><em><span style="font-size:10px;"><b>catch</b>(err) {</span></em></p>
<p><em><span style="font-size:10px;">java.util.logging.Logger.getLogger("birt.report.logger").warning ("Not iHub3? (See error below)\n" + err + "\nTrying another way..."); //(OFF, SEVERE, WARNING, INFO, CONFIG, FINE, FINER, FINEST and ALL)</span></em></p>
<p><em><span style="font-size:10px;">iQueryDefinition = <b>new</b> QueryDefinition( ); //Pre iHub3 compatable</span></em></p>
<p><em><span style="font-size:10px;">iQueryDefinition.setAutoBinding(<b>true</b>); //void - QueryDefinition (Set autoBinding)</span></em></p>
<p><em><span style="font-size:10px;">}</span></em></p>
<p><em><span style="font-size:10px;">iQueryDefinition.setDataSetName( odaDataSetAdapter.getName() ); //Set the dataSetName Name of data set used by this query.</span></em></p>
<p> </p>
<p><em><span style="font-size:10px;">//Run Query </span></em></p>
<p><em><span style="font-size:10px;"><b>var</b> iPreparedQuery = dataRequestSession.prepare( iQueryDefinition ); //IPreparedQuery - DataRequestSession</span></em></p>
<p><em><span style="font-size:10px;"><b>var</b> iQueryResults = iPreparedQuery.execute( <b>null</b> ); //IQueryResults - IPreparedQuery</span></em></p>
<p><em><span style="font-size:10px;"><b>var</b> ri = iQueryResults.getResultIterator( ); //IResultIterator - IQueryResults</span></em></p>
<p> </p>
<p><em><span style="font-size:10px;">rsmd = ri.getResultMetaData(); //IResultMetaData - IBaseResultIterator (Get metadata handle)</span></em></p>
<p><em><span style="font-size:10px;">cc = rsmd.getColumnCount(); //int - IBaseResultMetaData (Get column count)</span></em></p>
<p> </p>
<p><em><span style="font-size:10px;"><b>for</b> (<b>var</b> index=1;index<=cc;index++) //Loop through each column in the row (row 0). *Note: index starts with 1 for getColumnName looping</span></em></p>
<p><em><span style="font-size:10px;"><b>if</b>(columnName == rsmd.getColumnName(index)) //Find column index of column name</span></em></p>
<p><em><span style="font-size:10px;"><b>break</b>; //Break when found</span></em></p>
<p> </p>
<p><em><span style="font-size:10px;"><b>var</b> resultArray = []; //Set return array</span></em></p>
<p><em><span style="font-size:10px;"><b>while</b> (ri.next()) //Loop through each row in the entire Data Set (row 1+) (iResultIterator)</span></em></p>
<p><em><span style="font-size:10px;">resultArray.push(ri.getValue(rsmd.getColumnName(index))); //Push row values into return arrayé}</span></em></p>
<p> </p>
<p><em><span style="font-size:10px;">//Close up</span></em></p>
<p><em><span style="font-size:10px;">ri.close( ); //void - IBaseResultIterator</span></em></p>
<p><em><span style="font-size:10px;">iQueryResults.close( ); //void - IBaseQueryResults</span></em></p>
<p><em><span style="font-size:10px;">dataRequestSession.shutdown( ); //void - DataRequestSession</span></em></p>
<p> </p>
<p><em><span style="font-size:10px;">//Return resultArray to populate default value list with select all values</span></em></p>
<p><em><span style="font-size:10px;">resultArray;</span></em></p>
<p><em><span style="font-size:10px;">//
</span></em></p>
<p> </p>
<p> </p>
<p>How to exclude</p>
<p>"<strong>Null Value</strong>" items and <strong>duplicate </strong>items</p>
<p>from the selection list?</p>
<p> </p>
<p>Thanks</p>
Find more posts tagged with
Comments
Matthew L.
<p>You can suppress duplicate values in the default selection list by checking for the value in the return array before adding a new value to the array.</p>
<p>Also to ignore all "Null Value" values, you can add a check in the duplicate value check if statement.</p>
<p>See code example:</p>
<p> </p>
<p style="color:rgb(40,40,40);font-family:'Source Sans Pro', sans-serif;"><span style="color:#ff0000;">//Replace these lines of code</span></p>
<p style="color:rgb(40,40,40);font-family:'Source Sans Pro', sans-serif;"><span style="color:#ff0000;"><em><span style="font-size:10px;"><b>while</b> (ri.next()) //Loop through each row in the entire Data Set (row 1+) (iResultIterator)</span></em></span></p>
<p style="color:rgb(40,40,40);font-family:'Source Sans Pro', sans-serif;"><span style="color:#ff0000;"><em><span style="font-size:10px;">resultArray.push(ri.getValue(rsmd.getColumnName(index))); //Push row values into return arrayé}</span></em></span></p>
<p><span style="color:#008000;">//With these lines of code:</span></p>
<pre class="_prettyXprint _lang-js">
var columnValue="";
while (ri.next()){ //Loop through each row in the entire Data Set (row 1+) (iResultIterator)
columnValue = ri.getValue(rsmd.getColumnName(index)); //Get column value
if(resultArray.indexOf(columnValue) == -1 && columnValue != "Null Value"){resultArray.push(columnValue);} //Prevent "Null Value" & duplicates in array
}
</pre>
zsh
<p>Hi Matthew,</p>
<p> </p>
<p>Thanks for the quick response!</p>
<p>The is almost perfect as the condition should filter out unneccessary items</p>
<p>
Matthew L.
<p>Sorry for the confusion.</p>
<p>However the FOR statement (while true) will execute the command(s) immediately following it.</p>
<p>If a {block} of code follows the FOR statement then everything in the block will execute.</p>
<p>However if it's a single line, then only the single line of code will execute within the FOR loop.</p>
<p> </p>
<p>Per the explanation below, see the source link for more details:</p>
<p><em>statement</em></p>
<div><em>A statement that is executed as long as the condition evaluates to true. To execute multiple statements within the loop, use a block statement ({ ... }) to group those statements. To execute no statement within the loop, use an empty statement ( ; )</em></div>
<div>
<p>source: <a data-ipb='nomediaparse' href='
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for'>https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for</a></p>
;
<div> </div>
</div>
<p>So per my example, see comments for each line explanation:</p>
<pre class="_prettyXprint _lang-js">
for (var index=1;index<=cc;index++) //While this line is true, execute only the following line
if(columnName == rsmd.getColumnName(index)) //If this line is true, execute only the following line
break; //This line is only seen by the IF statement, however when executed will cause the FOR loop to break
</pre>
<p>Another way to look at it is:</p>
<pre class="_prettyXprint _lang-js">
for (var index=1;index<=cc;index++){
if(columnName == rsmd.getColumnName(index)){
break;
}
}
</pre>
<p>This is also valid:</p>
<pre class="_prettyXprint _lang-js">
for(var index=1;index<=cc;index++){if(columnName == rsmd.getColumnName(index)){break;}}
</pre>
zsh
<p>Thanks for the clarification. It was useful as I am new to JS.</p>
<p> </p>
<p>Regarding the solution, while it seems perfect, the result is not as it is expected, though. Duplicated "Null Value" items are eliminated, only one still appears in the list. However, other duplicated values are still present. </p>
<p>Another issue is that not all the items became selected by default...</p>
<p> </p>
<p>I am going to try recreating the problem with a dummy database.</p>
<p> </p>
<p>Thanks</p>
Matthew L.
<p>Sorry I think I misinterpreted the solution for you:</p>
<p> </p>
<p>Try changing the following line:</p>
<p>if(resultArray.indexOf(columnValue) == -1 <span style="color:#ff0000;">&&</span> columnValue != "Null Value"){resultArray.push(columnValue);}// && = AND</p>
<p> </p>
<p>To:</p>
<p>if(resultArray.indexOf(columnValue) == -1 <span style="color:#008000;">||</span> columnValue != "Null Value"){resultArray.push(columnValue);}// || = OR</p>
<p> </p>
<p>I think this will correct the issue.</p>
<p> </p>
<p>Or if you are wanting "Null Value" to be selected as a default value as well, try:</p>
<p>if(resultArray.indexOf(columnValue) == -1){resultArray.push(columnValue);}// || = OR</p>
zsh
<p>I have recreated the issue on a sample Derby database of NetBeans:</p>
<p>
zsh
<p>Another way would be suitable to achieve my goal is, inserting an item "ALL" to the top of the selection list.</p>
<p>This extra item would be able to make the selection.</p>
<p> </p>
<p>Do you think that it would be feasible?</p>