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)
DE API Remote Datasource
matute
Hello everybody again.
I?m using the DE API and I wanna know how can I use a XML Datasource, which is located in an application server, to construct my .rptdesign
Do I have to use "Script Datasource"? or "XML datasource"?
The situation is that I build a .xml file from a database and I locate it in a folder in the application server (jboss). How can I access to that .xml file to use it as a Datasource from the DE API?
Thank you to all
Find more posts tagged with
Comments
JasonW
Here is an example. Look a the query text. You can manually design one against the xml to get an idea what the syntax for the query is, but field are separated by #:# and the xpath row syntax is the second one. The third contains the columns eg:
row
[/BookStore/Book[
@category="
;{?p1?}"]]
columns (Name, type, xpath)
{Title;STRING;/Title},{Author;STRING;/Author},{category;STRING;/
@category}
Example de api.
package DEAPI;
import java.io.File;
import java.io.IOException;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.model.api.CellHandle;
import org.eclipse.birt.report.model.api.DataItemHandle;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.DesignElementHandle;
import org.eclipse.birt.report.model.api.DesignEngine;
import org.eclipse.birt.report.model.api.ElementFactory;
import org.eclipse.birt.report.model.api.IDesignEngine;
import org.eclipse.birt.report.model.api.IDesignEngineFactory;
import org.eclipse.birt.report.model.api.LabelHandle;
import org.eclipse.birt.report.model.api.PropertyHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.RowHandle;
import org.eclipse.birt.report.model.api.OdaDataSetHandle;
import org.eclipse.birt.report.model.api.OdaDataSourceHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.TableHandle;
import org.eclipse.birt.report.model.api.activity.SemanticException;
import org.eclipse.birt.report.model.api.command.ContentException;
import org.eclipse.birt.report.model.api.command.NameException;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants;
import org.eclipse.birt.report.model.api.elements.structures.ComputedColumn;
import org.eclipse.birt.report.model.api.metadata.IMetaDataDictionary;
import org.eclipse.birt.report.model.elements.interfaces.IReportItemModel;
import org.eclipse.birt.report.model.elements.interfaces.IStyleModel;
import com.ibm.icu.util.ULocale;
public class XMLReport
{
ReportDesignHandle reportDesignHandle = null;
ElementFactory elementFactory = null;
IMetaDataDictionary dict = null;
ComputedColumn cs1, cs2, cs3 = null;
public static void main( String[] args ) throws SemanticException,
IOException
{
new XMLReport( ).createReport( );
}
void createReport( ) throws SemanticException, IOException
{
//Configure the Engine and start the Platform
DesignConfig config = new DesignConfig( );
config.setBIRTHome("C:/birt/birt-runtime-2_5_1/birt-runtime-2_5_1/ReportEngine");
IDesignEngine engine = null;
try{
Platform.startup( config );
IDesignEngineFactory factory = (IDesignEngineFactory) Platform
.createFactoryObject(
IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY );
engine = factory.createDesignEngine( config );
}catch( Exception ex){
ex.printStackTrace();
}
SessionHandle session = engine.newSessionHandle( ULocale.ENGLISH ) ;
// Create a new report
reportDesignHandle = session.createDesign( );
// Element factory is used to create instances of BIRT elements.
elementFactory = reportDesignHandle.getElementFactory( );
dict = new DesignEngine( null ).getMetaData( );
createMasterPages( );
createDataSources( );
createDataSets( );
createBody( );
//OdaDataSourceHandle dataSourceHandle = (OdaDataSourceHandle)reportDesignHandle.findDataSource("myxmldatasource");
String outputPath = "output";//$NON-NLS-1$
File outputFolder = new File( outputPath );
if ( !outputFolder.exists( ) && !outputFolder.mkdir( ) )
{
throw new IOException( "Can not create the output folder" );//$NON-NLS-1$
}
reportDesignHandle.saveAs( "output/desample/XMLReport.rptdesign" );//$NON-NLS-1$//$NON-NLS-2$
System.out.println("finished");
}
private void createDataSources( ) throws SemanticException
{
OdaDataSourceHandle dataSourceHandle =
elementFactory.newOdaDataSource("Data Source",
"org.eclipse.birt.report.data.oda.xml");
dataSourceHandle.setProperty("FILELIST", "C:/temp/xmltest2.xml");
reportDesignHandle.getDataSources( ).add( dataSourceHandle );
}
private void createDataSets( ) throws SemanticException
{
// Data Set
OdaDataSetHandle dsHandle = elementFactory.newOdaDataSet( "Data Set",
"org.eclipse.birt.report.data.oda.xml.dataSet" );
dsHandle.setDataSource( "Data Source" );
dsHandle.setQueryText(
"table0#-TNAME-#table0#:#[/people/person/name]#:#{name;String;}" );
reportDesignHandle.getDataSets( ).add( dsHandle );
}
private void createMasterPages( ) throws ContentException, NameException
{
DesignElementHandle simpleMasterPage =
elementFactory.newSimpleMasterPage( "Master Page" );//$NON-NLS-1$
reportDesignHandle.getMasterPages( ).add( simpleMasterPage );
}
private void createBody( ) throws SemanticException
{
TableHandle table = elementFactory.newTableItem( null, 3, 1, 1, 1 );
table.setProperty( IStyleModel.TEXT_ALIGN_PROP,
DesignChoiceConstants.TEXT_ALIGN_CENTER );
table.setWidth( "80%" );//$NON-NLS-1$
table.setProperty( IReportItemModel.DATA_SET_PROP, "Data Set" );//$NON-NLS-1$
PropertyHandle computedSet = table.getColumnBindings( );
cs1 = StructureFactory.createComputedColumn( );
cs1.setName("Name");
cs1.setExpression( "dataSetRow[\"name\"]" );//$NON-NLS-1$
computedSet.addItem( cs1 );
// Header
RowHandle header = (RowHandle) table.getHeader( ).get( 0 );
CellHandle tcell = (CellHandle) header.getCells( ).get( 0 );
LabelHandle label = elementFactory.newLabel( null );
label.setText( "Name" );//$NON-NLS-1$
tcell.getContent( ).add( label );
DataItemHandle data = null;
// Detail
RowHandle detail = (RowHandle) table.getDetail( ).get( 0 );
tcell = (CellHandle) detail.getCells( ).get( 0 );
data = elementFactory.newDataItem( null );
data.setResultSetColumn( cs1.getName( ) );
tcell.getContent( ).add( data );
reportDesignHandle.getBody( ).add( table );
}
}
Jason
matute
Thanks for answering Jason.
I understand all the code except the syntax of the line where is the setQueryText method of the OdaDataSetHandle. Can you explain it again to me, please?.
And one more question. If I have the datasource that I need in a remote enviroment, the jboss server, how must I use the script datasource and dataset which supply the DE API?
I saw the flash example in "
http://eclipse.org/birt/phoenix/examples/scripting/scripteddatasource/"
, but I don`t understand it very well.
I'm trying to generate a pdf file for a remote client using the data of a table, which is in a database. All the API must be integrated in a server, so I have think in use a script to generate the datasource for the .rptdesign, leave this in the server, and after use the RE API to build the .pdf file.
Is this possible?
Thank you very much for all.
JasonW
The best way to understand the query syntax for the xml datasource is to build an example in the designer and look at the xml view to see the query text it generates. The main field will be the xpath syntax for the table row
[/BookStore/Book]
and for each column in the dataset
columns (Name, type, xpath syntax)
{Title;STRING;/Title},{Author;STRING;/Author},{category;STRING;/
@category}
I am not certain I understand your last question. If you want an example of DE API code that creates a scripted data source I can post that. Can you give a little more detail. Why would you want to use a scripted datasource if the data is in a database?
Jason
matute
Thank you Jason, I?ve solved the problem (I had an error in my querytext).
Finally I am using an XML datasource. But I think I'm doing too much work because my datasource can be an Arraylist which brings me the data from the database. And here is my next question:
Now I'm retrieving the data from the database into an XML file and after this I use the XML file as datasource for my .rptdesign.
Can I use directly the arraylist as datasource instead of create the XML file?
I know now how to create a dataset from an XML datasource and from an jdbc datasource, but I haven?t found enough info about how to create the datasource and the dataset from an ArrayList. Can you post somewhere to find info about this issue?
Thank you veery much for your help.
JasonW
How are you executing the report?
Jason
matute
Hi Jason,
I`m developing a server-client application.
When the client executes its .jar with java -jar "filename", this file calls an EJB which retrieves the data from de database into an arraylist. After that, the same bean of the EJB creates an xml file with that arraylist and after that, this bean also creates the .pdf file using the xml file as datasource.
I've attached a very simple example(paquete.jar) of how I do this
( arraylist ---> create .xml file ---> create .pdf file from xml datasource)
Can I avoid the create .xml file step and do: arraylist ---> create .pdf file from arraylist?
Thank you very much
kosta
Hi matute,<br />
<br />
you can use POJO's as Scripted Dataset in BIRT using the BIRT API.<br />
In your case the POJO is the arraylist.<br />
<br />
Look at this tutorial: <a class='bbc_url' href='
http://www.vogella.de/articles/EclipseBIRT/article.html'>http://www.vogella.de/articles/EclipseBIRT/article.html</a><br
/>
<br />
Or short: You should add the java objects as context to the task.<br />
<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
...
design = engine.openReportDesign("test.rptdesign");
IRunAndRenderTask task = engine.createRunAndRenderTask(design);
...
HashMap hm = (HashMap)task.getAppContext();
hm.put("MyArrayAlias", MyArray);
... many other lists
task.setAppContext(hm);
PDFRenderOption optionsPDF = new PDFRenderOption();
optionsPDF.setOutputFormat("pdf");
optionsPDF.setOutputFileName("output.pdf");
task.run();
task.close();
...
</pre>
<br />
create scripted dataset in the RD. For example 2 colums.<br />
<br />
after that in the scripted dataset script view:<br />
<br />
* open method<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
importPackage( Packages.com.acme );
var myArrayVar= MyArrayAlias.getList();
myIterator = myArrayVar.iterator();
</pre>
<br />
* fetch method<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
if(myIterator.hasNext()) {
var node = myIterator.next();
row[1] = node.getMethodOfThePojo1();
row[2] = node.getMethodOfThePojo2();
return true;
} else return false;
</pre>
<br />
* close method<br />
<pre class='_prettyXprint _lang-auto _linenums:0'>
myIterator = null;
node = null;
</pre>
JasonW
Kosta's respones is the one I would use.
Jason
matute
Thanks to both of you, I'll try with the POJO's method.
See you in next posts.
kosta
Just as hint:
If you are not using the BIRT API (and then not the getAppCont) the BIRT environment doesn't know about the POJO.
This can be solved with adding the class folder (usually the bin folder of the java project) on the classpath:
Preferences -> Report Design -> Classpath