Adding elements from library throwing error (Grid is child element or a virtual element( inside the

Hi BIRT Guru's,


I have blank rptdesign and all my elements are in library.  I need to add the elements from library at runtime(based on parameters).


 


 


In my library i have one grid(1 row, 1 column) and a label with some text.


 


I am adding the libraries at runtime to the report design and copying the elements from library to report design on onPrepare() method. When i try to add a label within a grid i am getting the below error.


 


 


onPrepare Code:


importPackage(Packages.org.eclipse.birt.report.model.api);


reportContext.getDesignHandle().includeLibrary("TestLibrary.rptlibrary","TestLibrary");


grid = reportContext.getDesignHandle().getLibrary("TestLibrary").findElement("grid1")

newGrid =  reportContext.getDesignHandle().getElementFactory().newElementFrom(grid,"G1");

label = reportContext.getDesignHandle().getLibrary("TestLibrary").findElement("label1");

newLabel = reportContext.getDesignHandle().getElementFactory().newElementFrom(label,"label2");


//reportContext.getDesignHandle().dropLibraryAndBreakExtends(reportContext.getDesignHandle().getLibrary("TestLibrary"));


//add label in the grid

newGrid.getCell(1,1).getContent().add(newLabel);


//add grid to the report body

reportContext.getDesignHandle().getBody().add(newGrid);


 


Output Error:


Wrapped org.eclipse.birt.report.model.api.command.ContentException: Element "Grid("G1").Rows[0].Cells[0]" is child element or a virtual element( inside the child ), can not change its structure. (/report/method[@name="onPrepare"]#11)

    at org.eclipse.birt.report.engine.executor.ExecutionContext.addException(ExecutionContext.java:1245)

    at org.eclipse.birt.report.engine.api.impl.ScriptedDesignHandler.processOnPrepareScript(ScriptedDesignHandler.java:229)

    at org.eclipse.birt.report.engine.api.impl.ScriptedDesignHandler.handleOnPrepare(ScriptedDesignHandler.java:110)


 


 


I know that dropLibraryAndBreakExtends() will do the trick but i am not familiar in using it.


 


Please help me on it.


 


Comments

  • Hi,


     


    I have a few alternative solutions for you.


    Looking at your posted code, I think you can save some complicated steps by using the copy() method instead of building all new elements based on the library elements within the code.


    An example of this is:



    var libraryHandle = reportContext.getDesignHandle().findLibrary("TestLibrary.rptlibrary"); //Find linked rptlibrary
    var gridItem = libraryHandle.findElement("grid1").copy(); //Copy grid element with its sub elements from library
    reportContext.getReportRunnable().designHandle.getBody().add(gridItem); //Add grid element to report body

    Another solution would be to build the rptdesign using the layout view.


    This would allow you to import all library elements and layout the report design via the GUI.


    You would then select each element in the layout and use the visibility property to define if the element should be in the report based on your parameter input.


    *Note that table elements that are not visible (controlled by the visibility property) will not query the data source if they do not appear in the report.


    This would also be my preferred suggestion per the requirements described in the original post.


     


    For the API method I've also attached a zip file that includes both .rptdesign and .rptlibrary files as an example.


    Matthew L.
    Actuate is now OpenText
    http://www.actuate.com

  • Dear Matthew,


    Thanks for your reply.


     


    1. In your attached example, the library contains grid(level 1) which have label(level 2) within it. In my requirement i will be having both grid and label as separate elements(Level 1). Only in reportdesign onPrepare() method, i will add the label inside the grid.


    2. Copy() method is returning design element and not element handle. I would need element handle so that i can set the properties of the element.

Sign In or Register to comment.