Home » Archived » BIRT » Problem when generating dynamic tables(OutOfMemory error)
| |
Re: Problem when generating dynamic tables [message #929740 is a reply to message #929736] |
Mon, 01 October 2012 18:19 |
Gustavo Missing name Messages: 20 Registered: March 2011 |
Junior Member |
|
|
Sure this is my builder/factory java class called from php
package builder;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.report.model.api.DataItemHandle;
import org.eclipse.birt.report.model.api.DataSetHandle;
import org.eclipse.birt.report.model.api.DataSourceHandle;
import org.eclipse.birt.report.model.api.DesignConfig;
import org.eclipse.birt.report.model.api.DesignElementHandle;
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.OdaDataSetHandle;
import org.eclipse.birt.report.model.api.OdaDataSourceHandle;
import org.eclipse.birt.report.model.api.ReportDesignHandle;
import org.eclipse.birt.report.model.api.SessionHandle;
import org.eclipse.birt.report.model.api.SlotHandle;
import org.eclipse.birt.report.model.api.StructureFactory;
import org.eclipse.birt.report.model.api.StyleHandle;
import org.eclipse.birt.report.model.api.TableHandle;
import org.eclipse.birt.report.model.api.TextItemHandle;
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.command.StyleException;
import org.eclipse.birt.report.model.api.elements.DesignChoiceConstants;
import org.eclipse.birt.report.model.api.elements.structures.ComputedColumn;
import com.ibm.icu.util.ULocale;
public class ReportBuilder
{
public static final String DefaultTableStyle = "defaultTableStyle";
public static final String DefaultHeaderStyle = "defaultHeaderStyle";
public static final String DefaultDetailStyle = "defaultDetailStyle";
public static final String DefaultLabelStyle = "defaultLabelStyle";
public static final String DefaultReportItemStyle = "defaultReportItemStyle";
private static final String ON_PREPARE = "onPrepare";
private static final String ON_CREATE = "onCreate";
private static final String ON_RENDER = "onRender";
private static final String ON_PAGE_BREAK = "onPageBreak";
static Map<String, String> scriptMap = new HashMap<String, String>();
static ReportDesignHandle designHandle = null;
static ElementFactory designFactory = null;
static StructureFactory structFactory = null;
static SessionHandle session = null;
static IDesignEngine designEngine = null;
static DesignConfig designConfig = null;
static IDesignEngineFactory factory = null;
private String reportPath = null;
public void prepareOnPrepare(String script){
scriptMap.put(ON_PREPARE, script);
}
public void prepareOnCreate(String script){
scriptMap.put(ON_CREATE, script);
}
public void prepareOnRender(String script){
scriptMap.put(ON_RENDER, script);
}
public void prepareOnPageBreak(String script){
scriptMap.put(ON_PAGE_BREAK, script);
}
/**
* Called externally
* @param title
*/
public void setReportTitle(String title){
//Define titulo do relatório como o parametro
try {
designHandle.setTitle( title );
} catch (SemanticException e) {
System.out.println("ERROR: Report title cannot be null !");
e.printStackTrace();
}
}
/**
* Called externally
* @param author
*/
public void setReportAuthor(String author){
//Define titulo do relatório como o parametro
designHandle.setAuthor( author );
}
/**
* Called externally
* @param templatePath
* @param birtHome
*/
public void initReport(String templatePath, String birtHome){
try{
System.out.print(".....Build Report initiated!");
reportPath = templatePath;
designConfig = new DesignConfig();
designConfig.setBIRTHome(birtHome);
// Startup plugins.
Platform.startup( designConfig );
// Open de report design file.
openDesign();
// Removes design elements
dropReportObjects();
// Create default element style.
createStyles();
}catch (Exception e) {
System.out.println("..........ERROR!");
e.printStackTrace();
}
}
private void openDesign() throws Exception{
try{
System.out.print("\n.....Searching for template");
factory = (IDesignEngineFactory) Platform.createFactoryObject( IDesignEngineFactory.EXTENSION_DESIGN_ENGINE_FACTORY );
designEngine = factory.createDesignEngine( designConfig );
session = designEngine.newSessionHandle( ULocale.ENGLISH ) ;
File templateFile = new File(reportPath);
if(templateFile.exists()) {
System.out.println("..........done!");
designHandle = session.openDesign( reportPath );
} else {
System.out.println("..........template not found!");
System.out.print(".....Creating new design");
designHandle = session.createDesign();
System.out.println("..........done!");
}
designFactory = designHandle.getElementFactory( );
}catch( Exception ex){
throw ex;
}
}
/**
* Called externally
* @throws IOException
*/
public void closeReport()throws IOException{
try{
System.out.print(".....Saving report");
designHandle.saveAs(reportPath);
designHandle.close();
// session.closeAll(true);
}catch(IOException e){
System.out.println("..........ERROR!");
e.printStackTrace();
} finally {
System.out.println(".....Finished!");
Platform.shutdown();
}
}
/**
* Called externally
* @param name
* @param url
* @param user
* @param password
* @return OdaDataSourceHandle
*/
public OdaDataSourceHandle buildDataSource(String name, String url,String user, String password)
{
System.out.print(".....Creating dataSource: \""+name+"\"");
try {
OdaDataSourceHandle dsHandle = designFactory.newOdaDataSource("dataSource_"+name, "org.eclipse.birt.report.data.oda.jdbc" );
dsHandle.setProperty( "odaDriverClass", "oracle.jdbc.OracleDriver" );
dsHandle.setProperty( "odaURL", url );
dsHandle.setProperty( "odaUser", user );
dsHandle.setProperty( "odaPassword", password );
//dsHandle.setProperty( "odaJndiName", jndi );
designHandle.getDataSources( ).add( dsHandle );
System.out.println("..........done!");
return dsHandle;
} catch (Exception e) {
System.out.println("..........ERROR!");
e.printStackTrace();
return null;
}
}
/**
* Called externally
* @param name
* @param query
* @param dataSource
* @return
* @throws Exception
*/
public DataSetHandle buildDataSet(String name, String query ,DataSourceHandle dataSource)
{
System.out.print(".....Creating dataSet: \""+name+"\"");
try {
OdaDataSetHandle dsHandle = designFactory.newOdaDataSet( "dataSet_"+name, "org.eclipse.birt.report.data.oda.jdbc.JdbcSelectDataSet" );
dsHandle.setDataSource( dataSource.getName() );
dsHandle.setQueryText( query );
designHandle.getDataSets( ).add( dsHandle );
System.out.println("..........done!");
return dsHandle;
} catch (Exception e) {
System.out.println("..........ERROR!");
e.printStackTrace();
return null;
}
}
/**
* Called externally
* @param name
* @param text
*/
public void addToDesign(DesignElementHandle item)
{
System.out.print(".....Building item: \""+item.getName()+"\"");
try {
designHandle.getBody( ).add( item );
System.out.println("..........done!");
} catch (ContentException e) {
System.out.print("Error adding item to design.\n"+e.getMessage());
e.printStackTrace();
} catch (NameException e) {
System.out.print("Error adding item to design.\n"+e.getMessage());
e.printStackTrace();
}
}
/**
* Called externally
* @param name
* @param text
* @return
*/
public TextItemHandle createTextItem(String name, String text, String contentType, DataSetHandle dataSet,
ArrayList<String>columnBindings, HashMap<String, String> properties)
{
System.out.print(".....Creating text item: \""+name+"\"");
TextItemHandle textItem = designFactory.newTextItem("textItem_"+name);
try {
if (contentType.equalsIgnoreCase("auto")) {
textItem.setContentType( DesignChoiceConstants.TEXT_CONTENT_TYPE_AUTO );
} else if (contentType.equalsIgnoreCase("html")) {
textItem.setContentType( DesignChoiceConstants.TEXT_CONTENT_TYPE_HTML );
} else if (contentType.equalsIgnoreCase("plain")) {
textItem.setContentType( DesignChoiceConstants.TEXT_CONTENT_TYPE_PLAIN );
} else if (contentType.equalsIgnoreCase("rtf")) {
textItem.setContentType( DesignChoiceConstants.TEXT_CONTENT_TYPE_RTF );
}
if(dataSet != null) {
textItem.setDataSet( dataSet );
}
if(columnBindings != null) {
ComputedColumn dataBinding = null;
for(String binding : columnBindings) {
dataBinding = StructureFactory.createComputedColumn();
dataBinding.setName(binding);
dataBinding.setExpression("dataSetRow[\"" + binding + "\"]");
textItem.addColumnBinding(dataBinding, false);
}
}
textItem.setStyleName( DefaultReportItemStyle );
textItem.setContent(text);
textItem = (TextItemHandle)createElementProperties(textItem, properties);
System.out.println("..........done!");
return textItem;
} catch (SemanticException e) {
System.out.println("..........ERROR!");
e.printStackTrace();
return null;
}
}
/**
* Called externally
* @param name
* @param colSize
* @param ds
* @return
*/
public BirtReportTableHandler createTable(String name, int colSize, DataSetHandle ds, HashMap<String, String> properties)
{
try {
System.out.print(".....Creating table item: \""+name+"\"");
BirtReportTableHandler table = new BirtReportTableHandler(designFactory.newTableItem("table_"+name, colSize), ds);
table.setHandler((TableHandle)createElementProperties(table.getHandler(), properties));
System.out.println("..........done!");
return table;
} catch (Exception e) {
System.out.println("Error creating table: "+name+"\n"+e.getMessage());
e.printStackTrace();
return null;
}
}
/**
* Called externally
* @param name
* @param text
* @return
* @throws Exception
*/
public LabelHandle createLabel(String name, String text, HashMap<String, String> properties){
try {
System.out.print(".....Creating label item: \""+name+"\"");
LabelHandle label = designFactory.newLabel( "label_"+name );
label.setStyleName( DefaultLabelStyle );
label.setText( text );
label = (LabelHandle)createElementProperties(label, properties);
System.out.println("..........done!");
return label;
} catch (ContentException e) {
System.out.println("Error creating label: "+name+"\n"+e.getMessage());
e.printStackTrace();
} catch (NameException e) {
System.out.println("Error creating label: "+name+"\n"+e.getMessage());
e.printStackTrace();
} catch (StyleException e) {
System.out.println("Error creating label: "+name+"\n"+e.getMessage());
e.printStackTrace();
} catch (SemanticException e) {
System.out.println("Error creating label: "+name+"\n"+e.getMessage());
e.printStackTrace();
}
return null;
}
/**
* Called externally
* @param name
* @param expression
* @param dataSet
* @param resultSetCol
* @param columnBindings
* @return
* @throws Exception
*/
public DataItemHandle createDataItem(String name, String expression, DataSetHandle dataSet, String resultSetCol,
ArrayList<String>columnBindings, HashMap<String, String> properties) {
System.out.print(".....Creating data item: \""+name+"\"");
DataItemHandle data = designFactory.newDataItem( "data_"+name );
try {
data.setStyleName( DefaultDetailStyle );
if(dataSet != null) {
data.setDataSet( dataSet );
data.setResultSetColumn( resultSetCol );
}
if(columnBindings != null) {
ComputedColumn dataBinding = null;
for(String binding : columnBindings) {
dataBinding = StructureFactory.createComputedColumn();
dataBinding.setName(binding);
dataBinding.setExpression("dataSetRow[\"" + binding + "\"]");
data.addColumnBinding(dataBinding, false);
}
}
data = (DataItemHandle)createElementProperties(data, properties);
System.out.println("..........done!");
return data;
} catch (StyleException e) {
System.out.println("Error creating data item: "+name+"\n"+e.getMessage());
e.printStackTrace();
return null;
} catch (SemanticException e) {
System.out.println("Error creating data item: "+name+"\n"+e.getMessage());
e.printStackTrace();
return null;
}
}
@SuppressWarnings("unchecked")
private void dropReportObjects()throws Exception{
try{
Iterator<Object> reportItens = designHandle.getBody().getContents().iterator();
while(reportItens.hasNext()){
DesignElementHandle designHandler = (DesignElementHandle)reportItens.next();
designHandler.drop();
}
SlotHandle dataSets = designHandle.getDataSets( );
Iterator<DataSetHandle> itDataset = dataSets.iterator();
while(itDataset.hasNext()) {
DataSetHandle dataSet = itDataset.next();
dataSet.drop();
}
SlotHandle dataSources = designHandle.getDataSources( );
Iterator<DataSourceHandle> itDatasource = dataSources.iterator();
while(itDatasource.hasNext()) {
DataSourceHandle dataSource = itDatasource.next();
dataSource.drop();
}
} catch (Exception e){
throw e;
}
}
public static DesignElementHandle createElementProperties(DesignElementHandle element, HashMap<String, String> properties) throws SemanticException {
if(properties != null) {
try {
if(properties.get("borderLeft") != null) {
element.setProperty(StyleHandle.BORDER_LEFT_WIDTH_PROP, properties.get("borderLeft"));
element.setProperty(StyleHandle.BORDER_LEFT_STYLE_PROP, DesignChoiceConstants.LINE_STYLE_SOLID );
element.setProperty(StyleHandle.BORDER_LEFT_COLOR_PROP, "#000000");
}
if(properties.get("borderRight") != null) {
element.setProperty(StyleHandle.BORDER_RIGHT_WIDTH_PROP, properties.get("borderRight"));
element.setProperty(StyleHandle.BORDER_RIGHT_STYLE_PROP, DesignChoiceConstants.LINE_STYLE_SOLID );
element.setProperty(StyleHandle.BORDER_RIGHT_COLOR_PROP, "#000000");
}
if(properties.get("borderTop") != null) {
element.setProperty(StyleHandle.BORDER_TOP_WIDTH_PROP, properties.get("borderTop"));
element.setProperty(StyleHandle.BORDER_TOP_STYLE_PROP, DesignChoiceConstants.LINE_STYLE_SOLID );
element.setProperty(StyleHandle.BORDER_TOP_COLOR_PROP, "#000000");
}
if(properties.get("borderBottom") != null) {
element.setProperty(StyleHandle.BORDER_BOTTOM_WIDTH_PROP, properties.get("borderBottom"));
element.setProperty(StyleHandle.BORDER_BOTTOM_STYLE_PROP, DesignChoiceConstants.LINE_STYLE_SOLID );
element.setProperty(StyleHandle.BORDER_BOTTOM_COLOR_PROP, "#000000");
}
if(properties.get("paddingLeft") != null) element.setProperty(StyleHandle.PADDING_LEFT_PROP, properties.get("paddingLeft"));
if(properties.get("paddingRight") != null) element.setProperty(StyleHandle.PADDING_RIGHT_PROP, properties.get("paddingRight"));
if(properties.get("paddingTop") != null) element.setProperty(StyleHandle.PADDING_TOP_PROP, properties.get("paddingTop"));
if(properties.get("paddingBottom") != null) element.setProperty(StyleHandle.PADDING_BOTTOM_PROP, properties.get("paddingBottom"));
if(properties.get("marginLeft") != null) element.setProperty(StyleHandle.MARGIN_LEFT_PROP, properties.get("marginLeft"));
if(properties.get("marginRight") != null) element.setProperty(StyleHandle.MARGIN_RIGHT_PROP, properties.get("marginRight"));
if(properties.get("marginTop") != null) element.setProperty(StyleHandle.MARGIN_TOP_PROP, properties.get("marginTop"));
if(properties.get("marginBottom") != null) element.setProperty(StyleHandle.MARGIN_BOTTOM_PROP, properties.get("marginBottom"));
} catch (SemanticException e) {
throw e;
}
}
return element;
}
private void createStyles( ) throws Exception{
try{
//Style da Table
StyleHandle tableStyle = designFactory.newStyle( DefaultTableStyle );
tableStyle.setProperty(StyleHandle.CAN_SHRINK_PROP, true);
designHandle.getStyles( ).add( tableStyle );
//Cria o Style do label
StyleHandle labelStyle = designFactory.newStyle( DefaultLabelStyle );
labelStyle.setProperty( StyleHandle.FONT_WEIGHT_PROP,DesignChoiceConstants.FONT_WEIGHT_BOLD );
labelStyle.setProperty( StyleHandle.FONT_SIZE_PROP, DesignChoiceConstants.FONT_SIZE_X_SMALL);
labelStyle.setProperty( StyleHandle.FONT_FAMILY_PROP, "Arial" );
labelStyle.setProperty( StyleHandle.COLOR_PROP, "#000000" );
labelStyle.setProperty( StyleHandle.PADDING_LEFT_PROP, "2" );
labelStyle.setProperty( StyleHandle.PADDING_RIGHT_PROP, "2" );
labelStyle.setProperty( StyleHandle.PADDING_BOTTOM_PROP, "8" );
labelStyle.setProperty( StyleHandle.TEXT_ALIGN_PROP, "Left" );
designHandle.getStyles( ).add( labelStyle );
//Cria o Style do detalhe
StyleHandle dataStyle = designFactory.newStyle( DefaultDetailStyle );
dataStyle.setProperty( StyleHandle.FONT_SIZE_PROP, DesignChoiceConstants.FONT_SIZE_X_SMALL);
dataStyle.setProperty( StyleHandle.FONT_FAMILY_PROP, "Arial"
);
dataStyle.setProperty( StyleHandle.COLOR_PROP, "#000000" );
dataStyle.setProperty( StyleHandle.PADDING_LEFT_PROP, "2" );
dataStyle.setProperty( StyleHandle.PADDING_RIGHT_PROP, "2" );
dataStyle.setProperty( StyleHandle.TEXT_ALIGN_PROP, "Left" );
designHandle.getStyles( ).add( dataStyle );
//Cria o Style da TableHeader Row
StyleHandle headerStyle = designFactory.newStyle( DefaultHeaderStyle );
//headerStyle.setProperty( StyleHandle.BORDER_TOP_STYLE_PROP, DesignChoiceConstants.LINE_STYLE_SOLID );
//headerStyle.setProperty( StyleHandle.BORDER_TOP_COLOR_PROP, "#2275FF" );
//headerStyle.setProperty( StyleHandle.BORDER_TOP_WIDTH_PROP, "2px" );
headerStyle.setProperty( StyleHandle.BORDER_BOTTOM_STYLE_PROP, DesignChoiceConstants.LINE_STYLE_SOLID );
headerStyle.setProperty( StyleHandle.BORDER_BOTTOM_WIDTH_PROP, "2px" );
headerStyle.setProperty( StyleHandle.BORDER_BOTTOM_COLOR_PROP, "#2275FF" );
designHandle.getStyles( ).add( headerStyle );
//Cria o Style do report item
StyleHandle itemStyle = designFactory.newStyle( DefaultReportItemStyle );
itemStyle.setProperty( StyleHandle.FONT_SIZE_PROP, DesignChoiceConstants.FONT_SIZE_X_SMALL);
itemStyle.setProperty( StyleHandle.FONT_FAMILY_PROP, "Arial" );
itemStyle.setProperty( StyleHandle.COLOR_PROP, "#000000" );
designHandle.getStyles( ).add( itemStyle );
}catch(Exception e){
throw new Exception(e.getMessage());
}
}
}
so from php i call each element i wanna build according to my request, after my builder returns the build up java class to my php i add it using the "addToDesign" method, to the design body . It can be a text item and after it 2 tables each one with different data and different report itens and after more 2 text itens elements (my testing example).
|
|
| | | | | | |
Re: Problem when generating dynamic tables [message #931847 is a reply to message #931630] |
Wed, 03 October 2012 15:25 |
|
So in the report you posted, the first and last text item are bound to the same dataset. If you remove one of these the issue goes away?
I no data set caching was improved in 3.7, but these should be caching. One thing to try is to add a grid that contains the first text item, the table and then the last text item. Remove the binding setting for the text items and set it once on the grid. One another note, why in the following code are you setting the dataset on data items instead of setting on the container element?
public DataItemHandle createDataItem(String name, String expression, DataSetHandle dataSet, String resultSetCol,
ArrayList<String>columnBindings, HashMap<String, String> properties) {
System.out.print(".....Creating data item: \""+name+"\"");
DataItemHandle data = designFactory.newDataItem( "data_"+name );
try {
data.setStyleName( DefaultDetailStyle );
if(dataSet != null) {
data.setDataSet( dataSet );
data.setResultSetColumn( resultSetCol );
}
Jason
|
|
| |
Re: Problem when generating dynamic tables [message #931964 is a reply to message #931847] |
Wed, 03 October 2012 17:42 |
Gustavo Missing name Messages: 20 Registered: March 2011 |
Junior Member |
|
|
Because i can have a data item with different dataset binding, i already changed these lines to
if(dataSet != null) {
data.setDataSet( dataSet );
if(resultSetCol != null) {
data.setResultSetColumn( resultSetCol );
so when i create a table i dont inform dataItem's data set, just the resultsetcol and bind the dataset to the table. Only if the dataset parameter is set then it will be bound to the data item instead of the container.
I cant do the grid thing because as this is a dynamic proccess there are cases in what ill have n elements bound to n datasets, i mean in this example i had 2 text item to same dataset and 1 table to another, but what if i had 2 text in dataset 1, 2 table in dataset 2, more 2 texts in dataset 3 and 3 tables in dataset 4, if i follow ur container idea i will have to create a grid for almost each design element.
[Updated on: Wed, 03 October 2012 20:10] Report message to a moderator
|
|
| | | | | | |
Re: Problem when generating dynamic tables [message #933850 is a reply to message #932990] |
Fri, 05 October 2012 11:59 |
Gustavo Missing name Messages: 20 Registered: March 2011 |
Junior Member |
|
|
Jason, i limited the platform lifecycle to user's session in php, before was limited per build request. Now the platform is started during user session, the build became faster but unfortunatelly it still giving me the error
The test was made with platform started, i build up the report and test it without shutting down the platform(because if i do it ill need to restart it again in the next request so i would be back to the same place i was) and when the user ends his session the platform is then shutted down.
[Updated on: Fri, 05 October 2012 12:38] Report message to a moderator
|
|
| | | | | | | | |
Goto Forum:
Current Time: Thu Dec 26 21:33:31 GMT 2024
Powered by FUDForum. Page generated in 0.04845 seconds
|