Dynamic javascript expression inside report !

Started by Jean-Baptiste Sarrodie, October 13, 2014, 12:51:59 PM

Previous topic - Next topic

Jean-Baptiste Sarrodie

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...

If you value and use Archi, please consider making a donation!
Ask your ArchiMate related questions to the ArchiMate Community's Discussion Board.

Phil Beauvoir

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 framework.

The code is here - http://ple-project.cvs.sourceforge.net/viewvc/ple-project/uk.ac.bolton.plex.monkey/
If you value and use Archi please consider making a donation. :-)

Jean-Baptiste Sarrodie

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:

  • bind more variables (key, value, documentation, purpose...)
  • add some pre-defined JS functions before loading JS user script

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:

  • report_path: report full path
  • date_now: date at which the report has been run
  • report_title: title of the report, in this case, this is your script code.
  • report_data_source: data source (com.archimatetool.jasperreports.data.ArchimateModelDataSource)
  • root_model: ArchiMate model (com.archimatetool.model.IArchimateModel)

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) !

If you value and use Archi, please consider making a donation!
Ask your ArchiMate related questions to the ArchiMate Community's Discussion Board.