Discussions
Categories
Groups
Community Home
Categories
INTERNAL ENABLEMENT
POPULAR
THRUST SERVICES & TOOLS
CLOUD EDITIONS
Quick Links
MY LINKS
HELPFUL TIPS
Back to website
Home
Intelligence (Analytics)
write table rows to disk
Calder
I have several queries that return rows. The rows may appear in other queries.
I need to list the results for all queries uniquely and sorted in order.
Is there a way to grab the results from several queries and report on them.
Perhaps write the queries to disk (CSV) then read them into another query?
How do you write the query result rows to disk?
TIA
Find more posts tagged with
Comments
johnw
In the initialize statement of your report, create a HashMap that stores an array or an ArrayList, using the key of your row as the Map key. So in the initialize event:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
var myMap = new Packages.java.util.HashMap();
reportContext.setGlobalVariable("myMap", myMap);
</pre>
<br />
Then, in the onFetch event for all of your datasets, you'd do something like:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
var myMap = reportContext.getGlobalVariable("myMap");
var myArray = new Packages.java.util.ArrayList();
myArray.add(row["col1"]);
myArray.add(row["col2"]);
myMap.put(row["id"], myArray);
reportContext.setGlobalVariable("myMap", myMap); //this usually isn't necessary, but to be safe.
</pre>
<br />
In your report, you would have all the datasets in Tables with the visibility expression set to false. This will build your unique entries. You could also use a set as well.<br />
<br />
Then, you have a Scripted Data Source that will do something like:<br />
<br />
Open on Data Set:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
count = 0;
localMap = reportContext.getGlobalVariable("myMap");
localKeys = localMap.keySet();
</pre>
<br />
Fetch:<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
if (count < localKeys.size())
{
var currentKey = localKeys.get(count);
var currentRow = localMap.get(currentKey);
row["id"] = currentKey;
row["col1"] = currentRow.get(0);
row["col2"] = currentRow.get(1);
...
count++;
return (true);
}
return (false);
</pre>
<br />
Then, you drop the scripted data set into your report. You can sort it at the table level.
Calder
Thanks for your quick response.
I am working with a UML Data Source, not a SQL data source, so there are no events on the data sets.
Any suggestions?
johnw
That would be weird. the onFetch and onOpen are defined in the parent class for data sets. But if there really aren't any events in the data set, you can do it on the Tables in the onCreate events.
Calder
<blockquote class='ipsBlockquote' data-author="'johnw'" data-cid="97021" data-time="1330529838" data-date="29 February 2012 - 08:37 AM"><p>
That would be weird. the onFetch and onOpen are defined in the parent class for data sets. But if there really aren't any events in the data set, you can do it on the Tables in the onCreate events.<br /></p></blockquote>
<br />
you were right - I was looking at the Data Sets group, rather than the individual data sets.<br />
<br />
I am trying to apply your javascript to a classicModels example, to get it working. I get an error message:<br />
<p class='bbc_indent' style='margin-left: 40px;'><br />
The following items have errors: <br />
<br />
<br />
ReportDesign (id = 1): <br />
+ There are errors evaluating script "if (count < localKeys.size())<br />
{<br />
var currentKey = localKeys.get(count);<br />
<br />
var currentRow = localMap.get(currentKey);<br />
<br />
row["id"] = currentKey;<br />
row["col1"] = currentRow.get(0);<br />
row["col2"] = currentRow.get(1);<br />
<br />
<br />
count++;<br />
return (true);<br />
}<br />
<br />
return (false);":<br />
{1}. <br />
Error.ScriptEvaluationError ( 1 time(s) )<br />
detail : org.eclipse.birt.report.engine.api.EngineException: There are errors evaluating script "if (count < localKeys.size())<br />
{<br />
var currentKey = localKeys.get(count);<br />
<br />
var currentRow = localMap.get(currentKey);<br />
<br />
row["id"] = currentKey;<br />
row["col1"] = currentRow.get(0);<br />
row["col2"] = currentRow.get(1);<br />
<br />
<br />
count++;<br />
return (true);<br />
}<br />
<br />
return (false);":<br />
{1}.<br />
at org.eclipse.birt.report.engine.script.internal.DtEScriptExecutor.handleJS(Unknown Source)<br />
at org.eclipse.birt.report.engine.script.internal.DataSetScriptExecutor.handleJS(Unknown Source)<br />
at org.eclipse.birt.report.engine.script.internal.ScriptDataSetScriptExecutor.handleFetch(Unknown Source)<br />
at org.eclipse.birt.data.engine.impl.ScriptDataSetRuntime.fetch(Unknown Source)<br />
at org.eclipse.birt.data.engine.impl.PreparedScriptDSQuery$ScriptDSQueryExecutor$CustomDataSet.fetch(Unknown Source)<br />
at org.eclipse.birt.data.engine.executor.cache.OdiAdapter.fetch(Unknown Source)<br />
at org.eclipse.birt.data.engine.executor.cache.RowResultSet.next(Unknown Source)<br />
at org.eclipse.birt.data.engine.executor.cache.ExpandableRowResultSet.next(Unknown Source)<br />
at org.eclipse.birt.data.engine.executor.cache.SmartCacheHelper.populateData(Unknown Source)<br />
at org.eclipse.birt.data.engine.executor.cache.SmartCacheHelper.initInstance(Unknown Source)<br />
at org.eclipse.birt.data.engine.executor.cache.SmartCacheHelper.getResultSetCache(Unknown Source)<br />
at org.eclipse.birt.data.engine.executor.cache.SmartCache.<init>(Unknown Source)<br />
at org.eclipse.birt.data.engine.executor.transform.pass.PassUtil.populateOdiResultSet(Unknown Source)<br />
at org.eclipse.birt.data.engine.executor.transform.pass.PassUtil.pass(Unknown Source)<br />
at org.eclipse.birt.data.engine.executor.transform.pass.PassManager.doSinglePass(Unknown Source)<br />
at org.eclipse.birt.data.engine.executor.transform.pass.PassManager.prepareDataSetResultSet(Unknown Source)<br />
at org.eclipse.birt.data.engine.executor.transform.pass.PassManager.pass(Unknown Source)<br />
at org.eclipse.birt.data.engine.executor.transform.pass.PassManager.populateResultSet(Unknown Source)<br />
at org.eclipse.birt.data.engine.executor.transform.ResultSetPopulator.populateResultSet(Unknown Source)<br />
at org.eclipse.birt.data.engine.executor.transform.CachedResultSet.<init>(Unknown Source)<br />
at org.eclipse.birt.data.engine.executor.CandidateQuery.execute(Unknown Source)<br />
at org.eclipse.birt.data.engine.impl.PreparedScriptDSQuery$ScriptDSQueryExecutor.executeOdiQuery(Unknown Source)<br />
at org.eclipse.birt.data.engine.impl.QueryExecutor.execute(Unknown Source)<br />
at org.eclipse.birt.data.engine.impl.ServiceForQueryResults.executeQuery(Unknown Source)<br />
at org.eclipse.birt.data.engine.impl.QueryResults.getResultIterator(Unknown Source)<br />
at org.eclipse.birt.report.engine.data.dte.QueryResultSet.<init>(Unknown Source)<br />
at org.eclipse.birt.report.engine.data.dte.DteDataEngine.doExecuteQuery(Unknown Source)<br />
at org.eclipse.birt.report.engine.data.dte.AbstractDataEngine.execute(Unknown Source)<br />
at org.eclipse.birt.report.engine.executor.ExecutionContext.executeQuery(Unknown Source)<br />
at org.eclipse.birt.report.engine.executor.QueryItemExecutor.executeQuery(Unknown Source)<br />
at org.eclipse.birt.report.engine.executor.TableItemExecutor.execute(Unknown Source)<br />
at org.eclipse.birt.report.engine.internal.executor.dup.SuppressDuplicateItemExecutor.execute(Unknown Source)<br />
at org.eclipse.birt.report.engine.internal.executor.wrap.WrappedReportItemExecutor.execute(Unknown Source)<br />
at org.eclipse.birt.report.engine.internal.executor.l18n.LocalizedReportItemExecutor.execute(Unknown Source)<br />
at org.eclipse.birt.report.engine.layout.html.HTMLBlockStackingLM.layoutNodes(Unknown Source)<br />
at org.eclipse.birt.report.engine.layout.html.HTMLPageLM.layout(Unknown Source)<br />
at org.eclipse.birt.report.engine.layout.html.HTMLReportLayoutEngine.layout(Unknown Source)<br />
at org.eclipse.birt.report.engine.api.impl.RunAndRenderTask.doRun(Unknown Source)<br />
at org.eclipse.birt.report.engine.api.impl.RunAndRenderTask.run(Unknown Source)<br />
at org.eclipse.birt.report.service.ReportEngineService.runAndRenderReport(Unknown Source)<br />
at org.eclipse.birt.report.service.BirtViewerReportService.runAndRenderReport(Unknown Source)<br />
at org.eclipse.birt.report.service.actionhandler.BirtGetPageAllActionHandler.__execute(Unknown Source)<br />
at org.eclipse.birt.report.service.actionhandler.AbstractBaseActionHandler.execute(Unknown Source)<br />
at org.eclipse.birt.report.soapengine.processor.AbstractBaseDocumentProcessor.__executeAction(Unknown Source)<br />
at org.eclipse.birt.report.soapengine.processor.AbstractBaseComponentProcessor.executeAction(Unknown Source)<br />
at org.eclipse.birt.report.soapengine.processor.BirtDocumentProcessor.handleGetPageAll(Unknown Source)<br />
at sun.reflect.GeneratedMethodAccessor211.invoke(Unknown Source)<br />
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)<br />
at java.lang.reflect.Method.invoke(Unknown Source)<br />
at org.eclipse.birt.report.soapengine.processor.AbstractBaseComponentProcessor.process(Unknown Source)<br />
at org.eclipse.birt.report.soapengine.endpoint.BirtSoapBindingImpl.getUpdatedObjects(Unknown Source)<br />
at sun.reflect.GeneratedMethodAccessor194.invoke(Unknown Source)<br />
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)<br />
at java.lang.reflect.Method.invoke(Unknown Source)<br />
at org.apache.axis.providers.java.RPCProvider.invokeMethod(Unknown Source)<br />
at org.apache.axis.providers.java.RPCProvider.processMessage(Unknown Source)<br />
at org.apache.axis.providers.java.JavaProvider.invoke(Unknown Source)<br />
at org.apache.axis.strategies.InvocationStrategy.visit(Unknown Source)<br />
at org.apache.axis.SimpleChain.doVisiting(Unknown Source)<br />
at org.apache.axis.SimpleChain.invoke(Unknown Source)<br />
at org.apache.axis.handlers.soap.SOAPService.invoke(Unknown Source)<br />
at org.apache.axis.server.AxisServer.invoke(Unknown Source)<br />
at org.apache.axis.transport.http.AxisServlet.doPost(Unknown Source)<br />
at org.eclipse.birt.report.servlet.BirtSoapMessageDispatcherServlet.doPost(Unknown Source)<br />
at javax.servlet.http.HttpServlet.service(Unknown Source)<br />
at org.apache.axis.transport.http.AxisServletBase.service(Unknown Source)<br />
at javax.servlet.http.HttpServlet.service(Unknown Source)<br />
at org.eclipse.birt.report.servlet.BirtSoapMessageDispatcherServlet.service(Unknown Source)<br />
at org.eclipse.equinox.http.registry.internal.ServletManager$ServletWrapper.service(Unknown Source)<br />
at org.eclipse.equinox.http.servlet.internal.ServletRegistration.service(Unknown Source)<br />
at org.eclipse.equinox.http.servlet.internal.ProxyServlet.processAlias(Unknown Source)<br />
at org.eclipse.equinox.http.servlet.internal.ProxyServlet.service(Unknown Source)<br />
at javax.servlet.http.HttpServlet.service(Unknown Source)<br />
at org.eclipse.equinox.http.jetty.internal.HttpServerManager$InternalHttpServiceServlet.service(Unknown Source)<br />
at org.mortbay.jetty.servlet.ServletHolder.handle(Unknown Source)<br />
at org.mortbay.jetty.servlet.ServletHandler.handle(Unknown Source)<br />
at org.mortbay.jetty.servlet.SessionHandler.handle(Unknown Source)<br />
at org.mortbay.jetty.handler.ContextHandler.handle(Unknown Source)<br />
at org.mortbay.jetty.handler.HandlerWrapper.handle(Unknown Source)<br />
at org.mortbay.jetty.Server.handle(Unknown Source)<br />
at org.mortbay.jetty.HttpConnection.handleRequest(Unknown Source)<br />
at org.mortbay.jetty.HttpConnection$RequestHandler.content(Unknown Source)<br />
at org.mortbay.jetty.HttpParser.parseNext(Unknown Source)<br />
at org.mortbay.jetty.HttpParser.parseAvailable(Unknown Source)<br />
at org.mortbay.jetty.HttpConnection.handle(Unknown Source)<br />
at org.mortbay.io.nio.SelectChannelEndPoint.run(Unknown Source)<br />
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(Unknown Source)<br />
Caused by: org.eclipse.birt.data.engine.core.DataException: Fail to execute script in function __bm_FETCH(). Source:<br />
<br />
" + if (count < localKeys.size())<br />
{<br />
var currentKey = localKeys.get(count);<br />
<br />
var currentRow = localMap.get(currentKey);<br />
<br />
row["id"] = currentKey;<br />
row["col1"] = currentRow.get(0);<br />
row["col2"] = currentRow.get(1);<br />
<br />
<br />
count++;<br />
return (true);<br />
}<br />
<br />
return (false); + "<br />
<br />
A BIRT exception occurred. See next exception for more information.<br />
Error evaluating Javascript expression. Script engine error: TypeError: Cannot find function get in object [0.0].<br />
Script source: /report/data-sets/script-data-set[
@id="
;10"]/method[
@name="
;fetch"], line: 0, text:<br />
__bm_FETCH()<br />
at org.eclipse.birt.data.engine.script.JSMethodRunner.runScript(Unknown Source)<br />
... 87 more<br />
Caused by: org.eclipse.birt.data.engine.core.DataException: A BIRT exception occurred. See next exception for more information.<br />
Error evaluating Javascript expression. Script engine error: TypeError: Cannot find function get in object [0.0].<br />
Script source: /report/data-sets/script-data-set[
@id="
;10"]/method[
@name="
;fetch"], line: 0, text:<br />
__bm_FETCH()<br />
at org.eclipse.birt.data.engine.core.DataException.wrap(Unknown Source)<br />
at org.eclipse.birt.data.engine.script.ScriptEvalUtil.evaluateJSAsExpr(Unknown Source)<br />
... 88 more<br />
Caused by: org.eclipse.birt.core.exception.CoreException: Error evaluating Javascript expression. Script engine error: TypeError: Cannot find function get in object [0.0].<br />
Script source: /report/data-sets/script-data-set[
@id="
;10"]/method[
@name="
;fetch"], line: 0, text:<br />
__bm_FETCH()<br />
at org.eclipse.birt.core.script.JavascriptEvalUtil.wrapRhinoException(Unknown Source)<br />
at org.eclipse.birt.core.script.JavascriptEvalUtil.evaluateRawScript(Unknown Source)<br />
at org.eclipse.birt.core.script.JavascriptEvalUtil.evaluateScript(Unknown Source)<br />
... 89 more<br />
Caused by: org.mozilla.javascript.EcmaError: TypeError: Cannot find function get in object [0.0].<br />
at org.mozilla.javascript.ScriptRuntime.constructError(Unknown Source)<br />
at org.mozilla.javascript.ScriptRuntime.constructError(Unknown Source)<br />
at org.mozilla.javascript.ScriptRuntime.typeError(Unknown Source)<br />
at org.mozilla.javascript.ScriptRuntime.typeError2(Unknown Source)<br />
at org.mozilla.javascript.ScriptRuntime.notFunctionError(Unknown Source)<br />
at org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThisHelper(Unknown Source)<br />
at org.mozilla.javascript.ScriptRuntime.getPropFunctionAndThis(Unknown Source)<br />
at org.mozilla.javascript.gen.c715._c1(Unknown Source)<br />
at org.mozilla.javascript.gen.c715.call(Unknown Source)<br />
at org.mozilla.javascript.optimizer.OptRuntime.callName0(Unknown Source)<br />
at org.mozilla.javascript.gen.c549._c0(Unknown Source)<br />
at org.mozilla.javascript.gen.c549.call(Unknown Source)<br />
at org.mozilla.javascript.ContextFactory.doTopCall(Unknown Source)<br />
at org.mozilla.javascript.ScriptRuntime.doTopCall(Unknown Source)<br />
at org.mozilla.javascript.gen.c549.call(Unknown Source)<br />
at org.mozilla.javascript.gen.c549.exec(Unknown Source)<br />
... 91 more<br />
<br />
+ Data Set script method "Fetch" returned null; expected a Boolean value. <br /></p>
I have attached my report design if you could have a look at it and tell me what I am doing wrong.<br />
<br />
tia
johnw
There is a logic error in the non-scripted Data Sets Fetch method. You are doing the following:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>var myMap = reportContext.getGlobalVariable("myMap");
var myArray = new Packages.java.util.ArrayList();
count=0;
myArray.add(row["CUSTOMERNAME"]);
myArray.add(row["CONTACTLASTNAME"]);
myMap.put(count, myArray);
reportContext.setGlobalVariable("myMap", myMap);
count++;</pre>
<br />
This will always be setting Map[0, myArray] since count is continually being reset to 0. Set that in a global variable called count. So change it to look something like:<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>var myMap = reportContext.getGlobalVariable("myMap");
var myArray = new Packages.java.util.ArrayList();
var myCount = reportContext.getGlobalVariable("count");
if (myCount == null)
myCount=0;
myArray.add(row["CUSTOMERNAME"]);
myArray.add(row["CONTACTLASTNAME"]);
myMap.put(count, myArray);
reportContext.setGlobalVariable("myMap", myMap);
myCount++;
reportContext.setGlobalVariable("count", myCount);
</pre>
Calder
Just to clarify, Data Sets do have onFetch, but join data sets have no methods.
I tried to put the javascript on the create method of the table, but it only grabs the first record and ignores the rest.
johnw
Use onPrepare then. I might have the order of execution backwards in the Event Model