Archi Forum

Archi => Share your Models, Templates and Reports => Topic started by: Jean-Baptiste Sarrodie on October 13, 2014, 12:51:59 PM

Title: Dynamic javascript expression inside report !
Post by: Jean-Baptiste Sarrodie on October 13, 2014, 12:51:59 PM
I just want to share one idea and something I've just discovered...

In my quest to add a scripting engine to Archi (mainly read only, for model analysis purpose), I add one funny idea: use jasperreport... This can seems inappropriated, but it we think about it, run a query or run a report is almost the same thing, so I thought it could be doable to dynamically create a JasperDesign object (potentially with the help of something like http://dynamicjasper.com) with user providing input for SELECT (list of properties to show), FROM (method to extract data from model) and WHERE ("HideWhen" formula).

I then did some rough tests to see if report language can be set to groovy and used from Archi: it dosen't (I don't know why, maybe just because it needs some additional jars).

Then, I discovered than Java6 comes with JavaScript engine by default, and here the magic happened: I just created an almost empty report (using Java language, no groovy) with only one text field using following expression:
(new javax.script.ScriptEngineManager()).getEngineByName("JavaScript").eval($P{REPORT_TITLE})

I then ran a report (any model, even empty will do the job) with the following title:
function thisis(){return "This is";};thisis() +" a "+"test !"

I think this opens new opportunities for reporting inside Archi. Let's explore them...

JB
Title: Re: Dynamic javascript expression inside report !
Post by: Phil Beauvoir on October 13, 2014, 13:20:26 PM
Interesting. I had a look at scripting for Archi recently. I remembered that some years ago, for a project, I wrote some code for a DOM "Monkey" scripting engine. It used JavaScript as the scripting language and the Eclipse Monkey (https://wiki.eclipse.org/Eclipse_Monkey/Overview) framework.

The code is here - http://ple-project.cvs.sourceforge.net/viewvc/ple-project/uk.ac.bolton.plex.monkey/ (http://ple-project.cvs.sourceforge.net/viewvc/ple-project/uk.ac.bolton.plex.monkey/)
Title: Re: Dynamic javascript expression inside report !
Post by: Jean-Baptiste Sarrodie on October 13, 2014, 14:32:40 PM
I will look at it...

For those who wants to play... You'll find attached a new report which bind some report variables to the script engine so we could imagine for example using some user defined JS code to change the way property name and value are displayed.

Basically, we could:

If you want to test, open a model (save it before, just in case) and run the attached report using the following "Report title":
"<b>Model name: </b><br><i>"+root_model.getName()+"</i>"

If it works, then you now have a scripting engine which uses "Report title" as input and generates a document using HTML markup language. The binding mecanism should allow direct access to almost all Archi classes and methods (maybe in read/write, so be carefull, this could become the first JasperReport based virus for Archi).

Mapped variables:

Idea (Phil?): create a plugin which open a "tab" (don't remember the exact name in Eclipse, but same a Visualizer, views...). Top of this "tab" should contains a selectable list of opened models, a big text area and a button "Go", the bottom should contains an HTLM view linked to a temporary file... created from the scripted report ran from the "Go" button ;-)

UPDATE: This is read/write: for example, using "root_model.setName("Something")" change your model's name without any undo possible (you've been warned) !

JB