Author Topic: Dynamic javascript expression inside report !  (Read 3734 times)

Jean-Baptiste Sarrodie

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 419
  • Archi Evangelist and Contributor. Visionary.
    • About me...
Dynamic javascript expression inside report !
« 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:
Code: [Select]
(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:
Code: [Select]
function thisis(){return "This is";};thisis() +" a "+"test !"
I think this opens new opportunities for reporting inside Archi. Let's explore them...

JB
If you value and use Archi please consider making a donation! https://www.archimatetool.com/donate

Phil Beauvoir

  • Archi Master
  • Hero Member
  • *****
  • Posts: 938
    • Archi
Re: Dynamic javascript expression inside report !
« Reply #1 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 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! https://www.archimatetool.com/donate

Jean-Baptiste Sarrodie

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 419
  • Archi Evangelist and Contributor. Visionary.
    • About me...
Re: Dynamic javascript expression inside report !
« Reply #2 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:
  • 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":
Code: [Select]
"<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) !

JB
« Last Edit: October 13, 2014, 14:50:21 PM by Jean-Baptiste Sarrodie »
If you value and use Archi please consider making a donation! https://www.archimatetool.com/donate