icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

24
1 The Road to Damascas A Conversion Experience: LS and @Formula to SSJS 2014/03/17 Matthew Fyleman

Upload: ics-user-group

Post on 01-Jul-2015

109 views

Category:

Presentations & Public Speaking


0 download

DESCRIPTION

ICS User Group Conference 2014 - Tranforming Lotus Script and @Formula to Server Side Java Script SSJS by Matthew Fylement, We4IT

TRANSCRIPT

Page 1: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

1

The Road to Damascas –

A Conversion Experience:

LS and @Formula to SSJS2014/03/17 – Matthew Fyleman

Page 2: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

2

Matthew Fyleman

21 Years as a Notes/Domino Developer

Mostly Working on:

Xpages conversions

Product development

Who Am I?

Page 3: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

3

Based on My Experiences

Converting LotusScript and @Formula to

SSJS

Tools that can help – particularly regular

expressions

What is this Talk About?

Page 4: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

4

When should you convert existing code?

Conversion Options

NotesAgent.run() with parameter doc

Search and Replace

Dedicated Tools

Search and Replace

Preparation

Introduction to Regular Expressions

Examples and Demonstration

Tips and Traps

Dedicated Tools

Questions

What am I talking about?

Page 5: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

5

Never!

When should you convert existing code?

Page 6: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

6

What is the problem?

It is always going to be slow

GIGO

You will introduce new bugs

Re-developing will be quicker, cheaper and

you will end up with a better result

But if you really must ...

When should you convert existing code?

Page 7: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

7

What are your options?

NotesAgent.run()

Quick and Dirty

Agent must run independently

Only use when agents are available and time is critical

Search and Replace

LotusScript -> JavaScript (and Java)

Less useful for @Formula

Dedicated Tools

@Formula

Conversion Options

Page 8: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

8

Search and Replace is most useful for LS conversion

Syntactically similar

Easiest if you do a little refactoring first

Option Declare

doc.field(0) -> doc.getItemValue(“ Field“ )(0)

Camel Case Notes Objects

Make Sure Method calls are consistently named

Best to Avoid All-In-One-Go

Function or Sub at a Time

Variable with one purpose in one location may have a different use elsewhere

Converting LotusScript to SSJS - Preparation

Page 9: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

9

Regular Expressions are your new BFF

Sophisticated Pattern Matching

Elements from search can be carried through to

replace

The Search and Replace built in to DDE can use

Regular Expressions

Useful outside LS conversion (e.g. Validation)

See Planet Lotus - http://planetlotus.org/profiles/ross-swick_97733

Regular Expressions

Page 10: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

10

Tidy up first – Option Declare, remove clustering e.g.:

Dim x as Integer,y as Integer,z as Integer

We want to match any variable name in the pattern:

Dim <var name> As <Any valid type>

Fairly simple:

Dim[ ]+[A-Za-z0-9_]+[ ]+As[ ]+(Integer|String|Boolean|Double|Variant)

But how do we replace?

Modify the search:

Dim[ ]+([\w]* )[ ]+As[ ]+String

Use this for replace

var \1 = “ ” ;

Starting Simple – Dim Statements

Page 11: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

11

For Notes Objects, Things are simpler

Dim <var name> As Notes<rest of object name>

- Ignore ... As New NotesSession for now

Also, initialising in SSJS, = null is ok:

var <var name>:Notes<rest of object name> = null;

So our terms are:

Search:

Dim[ ]+([\w]* )[ ]+As[ ]+(Notes[\w]* )

Replace:

var \1\:\2 = null;

Starting Simple – Dim Statements (2)

Page 12: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

12

For the most part, simple S & R (but order is important):

End If to }

[ ]*Then to \) {

Else[ ]+If[ ]* to } else if \(

If[ ]* to if \(

But what about:

If (x = 10) Then

Use Search: If[ ]+([\w\(\)\[\]\.<>" ]* )=([\w\(\)\[\]\.<> " ]* )[ ]+Then

Use Replace: if \(\1==\2\) \{

NB: Works but not optimal!

Other comparison operators not a problem

A bit more complex – If Statements

Page 13: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

13

The problem:

Session object is global in ssjs: ‘session’

In LS it is created:

Dim sessAs New NotesSession

Need to find all LS session objects, and replace with session

How do you get a list of session object names?

! – session objects

You need a coffee!

Page 14: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

14

Java String Object has regex search and replace

String source = “Dim x As String”;

String result = source.replaceAll(“Dim[ ]+([\w]*)[ ]+As[ ]+String”, “var $1 = \“\”;”);

Pattern and Matcher objects make this even more powerful

Pattern p = Pattern.compile(pattern);

Matcher m = p.matcher(this.source);

int startPos = 0;

while (m.find(startPos)) {

if (!itemList.contains(m.group(1))) {

itemList.add(m.group(1));

}

startPos = m.end() + 1;

}

Adding Java

Page 15: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

15

Similar Issue to Session

Need to find all document object names, and replace field handling

methods

Will probably need to handle dot notation

Arrgghh!

How do you search for dot notation?

<doc name>\.([^GetItemValue])\([0-9]+\)

Still hard work!

Field Handling

Page 16: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

16

There are other types than string!

Always review and test converted code thoroughly

Date handling is a pain

Script libraries can help here – Java?

Watch out for User interaction and particularly dialogues

Work out your strategies in advance!

Search and Replace – Tips and Traps

Page 17: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

17

In some respects @Formula -> SSJS is easier than LS -> SSJS

@Formula JavaScript Wrappers help a lot

Mostly just ‘;’ to ‘,’, and converting lists to arrays

Some constructions are obvious:

@SetField(“ Field” , Value);

Goes to:

doc.replaceItemValue(“ Field” , Value);

Or

S: @SetField\([ ]* ([\w” ]* )[ ]* \,[ ]* ([\w” ]* )[ ]* \);

R: doc\.replaceItemValue\(\1, \2\);

But there are some issues ...

Converting Formula

@

Page 18: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

18

No direct equivalent in SSJS for *+, *= *>= etc.

when applied to Lists

Need to plan for this

Java Class/Library to provide direct substitute

Unfortunately, Java does not permit operator

overloading, so has to be a set of methods

Converting Formula – List Processing

@!

Page 19: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

19

@If(@Contains(_WFData_neu;_Key);" " ;

@Do(

@Set(" _Sachgebiet_Zuordnung" ;@DbLookup("NOTES" :"NOCACHE" ;" " :_ADM$StructureDB;" Workflows" ;" WFArbeitsanweisung" ;" Sachgebietzuordn

ung" ));

@If(_Sachgebiet_Zuordnung = " " | !@Contains(_Sachgebiet_Zuordnung;_Key2);@Do(

@Prompt([Ok];" Hinweis" ;" In der System Struktur VBM wurde für das aktuelle Sachgebiet kein Workflow definiert. Das Dokument wird zum ehemaligen

Kompetenzträger zurückgegeben, damit dieser ein neues Sachgebiet auswählen kann." );

@Set(" _Kompetenzträger" ;Bearbeiter1);

@Set(" _tmpintern" ;5)

);

@Do(

@Prompt([Ok];" Hinweis" ;" In der System Struktur VBM wurde für das aktuelle Sachgebiet ein neues Sachgebiet konfiguriert. Das Dokument wird zum

Kompetenzträger zurückgegeben, damit dieser das neue Sachgebiet auswählen kann." );

@Set(" _neues_Sachgebiet" ;@Left(@Right(_Sachgebiet_Zuordnung;_key2);" ||" ));

@Set(" _Elements" ;@Elements(@Explode(@Left(@Left(@Right(_WFData_neu;" ||Sachgebiet#" );" ||" ); _neues_Sachgebiet) + _neues_Sachgebiet; " $"

)));

@Set(" _KompetenzträgerData" ;@Explode(@Left(@Right(_WFData_neu;" ||Kompetenzträger#" );" ||" ); " $" ));

@Set(" _Kompetenzträger" ;@Subset(@Subset(_KompetenzträgerData;_Elements);-1));

@Set(" _tmpintern" ;6)

)

)

)

);

Converting Formula - @If, @Do and @While

@!!!

Page 20: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

20

Search and Replace can be used for @Formula ->

SSJS ...

... but it can only take you so far

A dedicated parser can go further

Only real alternative to manual translation for complex

statements

Time consuming to create

Still not a silver bullet

Parsers

Page 21: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

21

Espresso - http://www.ultrapico.com/Expresso.htm

Good for learning regex, and serious regex dev

Free!

PowerGREP

Sophisticated regex file search

Where regex started (UNIX grep)!

Expensive

Dedicated Tools

Page 22: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

22

We4IT – www.we4it.com

OpenNTF– www.openntf.org

Regex Quick Reference

http://www.night-ray.com/regex.pdf

Loads of websites for all aspects of regex development

Mastering Regular Expressions – Jeffrey E.F. Friedl –

O’Reilly Publishing

Resources and Information

Page 23: Icsug conf 14_dev03_xpages-roadtodamascas-lotus-script-and-formula-to-ssjs

23

Questions?