33
fme AG - #212261 Javascript Konsole für Entwicklung und Administration Meet the Experts in Hamburg Florian Maul 5.06.2012

Javascript Konsole für Entwicklung und Administration

Embed Size (px)

DESCRIPTION

Presentation by Florian Maul at the Alfresco community meetup in Hamburg on June 5th 2012 about my Javascript Console extension. Some of the slides are German but the main technical part is in English. http://alfresco.fme.de http://www.fme.de

Citation preview

Page 1: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

fme AG - #212261

Javascript Konsole für Entwicklung und Administration

Meet the Experts in Hamburg – Florian Maul – 5.06.2012

Page 2: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Über mich

• Florian Maul, Senior Consultant bei der fme AG

• Langjährige Erfahrung mit Alfresco

• Gewinner des Alfresco Dashlet Challenge 2011:

Gallery Plus Dashlet

• Alfresco Certified Engineer & Alfresco Certified Administrator

• Opensource

Contributor zu Share Extras und anderer Alfresco Extensions

Android CMIS Browser (2010)

• @fmaul

(c) fme AG - #205626 3

Page 3: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Agenda

• Kurzvorstellung fme AG

• fme Alfresco Extensions

• Was ist die Javascript Konsole?

• Viele Demos mit Beispielskripten

© fme AG - #212261 4

Page 4: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

#6120 - © fme AG

Fakten

• Gründung: 1995, Hauptsitz: Braunschweig, ca. 105 Mitarbeiter (fme-Gruppe)

• Weitere Geschäftsstellen in München, Düsseldorf, Frankfurt

• Töchter in Rumänien (100 %) und in den USA (60 %)

• Mehr als 10 Jahre EMC-Partner (höchster Partnerstatus), mehr als 80 ECM-Berater

• International tätig

• „We use what we sell“

• Mehr als 130 ECM-Kunden weltweit

Unser

Fokus

Das Unternehmen Q1/2012

Wir unterstützen unsere Kunden dabei, Informationen effektiver

und effizienter zu strukturieren und verwalten. Dies erreichen wir

durch den Einsatz von Enterprise Content Management, Business

Intelligence und Product Lifecycle Management Technologien. Der

Schwerpunkt liegt auf Lösungen für Life Sciences, industrielle

Fertigung und Energie.

Branchen

Techno-

logien Lösungen

5

Page 6: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

fme Migration Center

(c) fme AG - #205626 7

Analyse Organise Transform Validate Correct Import

Time

Page 7: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

fme Alfresco Extensions Gallery

• http://alfresco.fme.de/Overview.925.0.html

• 14 gelistete Alfresco Erweiterungen

• 8 unter Open Source (Sponsors wanted!)

• 6 unter Kommerzieller Lizenz

• Fokus:

Bedienkomfort von Alfresco maximieren

Oft benötigte/angefragte Erweiterungen

in Form von wiederverwendbaren

Modulen

Verwendung dieser Erweiterungen bei der

Umsetzung branchen-/kundenspezifischer

Alfresco Lösungen

© fme AG - #212261 8

Page 8: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Alfresco APIs

• What kinds of API are there?

Java Foundation API

Javascript API & Javascript Services

CMIS

• Where can I use Javascript in Alfresco:

Webscripts developed in Javascript

Javascript run by Actions (Run Script Action)

Javascript attached to Tasks & Events in Workflows

(c) fme AG - #205626 9

Page 9: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Webscripts developed in Javascript

• Most of the Alfresco Webscript are developed in Javascript

• Example: GET /alfresco/service/sample/blog/category/{category}

// check category exists?

var category = search.luceneSearch(

"PATH:\"/cm:generalclassifiable//cm:" + url.extension + "\"");

if (category == undefined) {

status.code = 404;

status.message = "Category " + url.extension + " not found.";

status.redirect = true;

} else {

// perform category search

var nodes = search.luceneSearch(

"PATH:\"/cm:generalclassifiable//cm:" + url.extension +

"//member\"");

model.resultset = nodes;

}

(c) fme AG - #205626 10

Page 10: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Javascript for Tasks & Events in Workflows

• Javascript can be executed from within Workflow definitions

• Example of Activti Workflow: parallelreview_group_processdefinition.xml

<decision name="isapproved">

<event type="node-enter">

<action class="org.alfresco.repo.workflow.jbpm.AlfrescoJavaScript">

<script>

<variable name="wf_actualPercent" access="write"/>

<expression>

wf_actualPercent = ((wf_approveCount * 100) /

people.getMembers(bpm_groupAssignee).length);

</expression>

</script>

</action>

</event>

</decision>

(c) fme AG - #205626 11

Page 11: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Javascript run by Actions (Run Script Action)

• Alfresco Explorer has a Run Script Action

• For Share there is an Execute Script extension in Share-Extras:

(c) fme AG - #205626 12

Page 12: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

But how to develop Javascript code?

• Webscripts

Develop in Eclipse

ANT build script to copy changes & reload the webscripts

Run the webscript manually in the browser

• Data Dictionary Scripts

upload script to Data Dictionary

run Script Action

watch errors in the Alfresco Log file (ScriptLogger=DEBUG)

fix bugs and iterate

• It takes to long to test out Javascript code and fix bugs.

(c) fme AG - #205626 13

Page 13: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Meet the Javascript Console

(c) fme AG - #205626 14

Page 14: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

JavaScript Console

• Komponente für die Share Admin Console

zum Schreiben & Ausführen von

JavaScript-Code im Repository

Vollzugriff auf die Alfresco JavaScript API

Auswahl des Ausführungskontext (Ordner)

Einfache Code-Completion

Laden & Speichern von Skripten

Formatierung von Ergebnislisten in Form

einer Tabelle (v0.4.x)

Import von JavaScript-Dateien aus dem

Alfresco Classpath

Verwendung von Freemarker-Templates

zur Formatierung der Ergebnismenge (v0.5)

© fme AG - #212261 15

Page 15: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Meet the Javascript Console

• Mike Farman (Director Product Management Alfresco):

„I really like the stuff you’ve been doing, my favourite is the JavaScript console, it’s the

first add-on I always install!“

• X

• x

(c) fme AG - #205626 16

Page 16: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Javascript Console milestones

• End of 2010

Need to develop and execute Javascript code for a project sparked development

of the first prototype

First internal versions were a single standalone page in Share

• April 2011:

Contact with Will Abson

refactoring and integration into the admin console of Alfresco 3.4

• May 2011:

First checkin into the Share-Extras project and first public release

• Nov 2011:

Version 0.4 with basic code completion

• June 2012:

Version 0.5 with freemarker editor

(c) fme AG - #205626 17

Page 17: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Download & Installation

• Visit: http://code.google.com/p/share-extras/

• Download at http://code.google.com/p/share-extras/downloads/list

Installation

• For version 0.4.x copy the jar file to tomcat/shared/lib/

• With 0.5 there will be two jar files –repo.jar and –share.jar. The repo.jar must be

copied to tomcat/webapps/alfresco/WEB-INF/lib and the share.jar to

tomcat/webapps/share/WEB-INF/lib.

(c) fme AG - #205626 18

Page 18: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Poor man‘s Nodebrowser

print(document);

print("--- PROPERTIES -----------------------------------------");

print(document.properties);

print("--- ASPECTS --------------------------------------------");

print(document.aspects);

print("--- ASSOCS ---------------------------------------------");

print(document.assocs);

print("--- PERMISSIONS ----------------------------------------");

print(document.fullPermissions);

(c) fme AG - #205626 19

Page 19: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

How does it work?

(c) fme AG - #205626 20

Alfresco Repository / Webscripts

Browser / Javascript / YUI / Codemirror

Execute Webscript

Input Output

• print(), recurse(), logger

• eval() / Java-Webscript

• <imports>

• Error handling

• Javascript

• Freemarker template

• space / document

• Print / log output

• Freemarker output

• Error messages

Page 20: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Selecting the file or folder to work with

• Select the space variable from

a directory chooser:

• Open any document in the

Javascript Console from the

document library:

(c) fme AG - #205626 21

Page 21: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Recurse() function

• The Javascript console has a special recurse function to iterate over a tree of nodes:

recurse(space, function(node) {

if (node.name.toLowerCase().indexOf("alfresco") >= 0) {

print("adding tag alfresco to " +

node.displayPath + "/" + node.name);

node.addTag("alfresco");

}

});

• If no function is given it returns the nodes as an array:

var allNodes = recurse(space);

(c) fme AG - #205626 22

Page 22: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Creating users and attaching avatars

function createUser(username,firstname, lastname, email, location, jobtitle)

{

var password = username; // use for testing only!

var p = people.createPerson(username, firstname, lastname,

email, password, true);

p.properties["cm:location"] = location;

p.properties["cm:jobtitle"] = jobtitle;

p.save();

}

createUser("homer", "Homer", "Simpson", "[email protected]",

"Springfield", "Nuclear Safety Inspector");

(c) fme AG - #205626 23

Page 23: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Creating users and attaching avatars

// pseudo-code not working

for each (imageNode in space.children) {

var user = people.getPerson(name);

user.createAssociation(

imageNode, "cm:avatar");

}

Full source code here:

http://www.techbits.de/2011/12/02/using-the-javascript-console-creating-users-with-avatar-images/

(c) fme AG - #205626 24

Page 24: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Assign users to sites

var nodes = search.luceneSearch(

"TYPE:cm\\:person +@cm\\:lastName:Simpson");

for each(var simpson in nodes) {

var site = siteService.getSite(

"javascript-console-talk");

site.setMembership(

simpson.properties["cm:userName"],

"SiteCollaborator");

}

(c) fme AG - #205626 25

Page 25: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Creating Webscripts

• The new version 0.5 makes

it easy to create webscripts:

Write a controller javascript

using logger.log output

Create a freemarker

template

Test different URL

arguments and different

users

(c) fme AG - #205626 26

Page 26: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Creating Webscripts

var result = [];

for each(node in companyhome.childByNamePath(args.path).children) {

result.push({

name : node.name,

creator : node.properties["cm:creator"],

creationDate : node.properties["cm:created"]

});

}

model.folder = result;

[

<#list folder as f>

{

"name" : "${f.name}",

"creator" : "${f.creator}",

"creationDate" : "${f.creationDate?date}"

}

</#list>

]

(c) fme AG - #205626 27

Page 27: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Access spring beans / unlock documents

• Example unlock a document:

var ctx = Packages.org.springframework.

web.context.ContextLoader.getCurrentWebApplicationContext();

var lockService = ctx.getBean("lockService");

lockService.unlock(document.nodeRef);

• This looks like a hack but is perfectly valid Alfresco Javascript

• It only works in Javascript stored in the Classpath (not in the Data Dictionary)

• Also very useful to test your own Beans written in Java

(c) fme AG - #205626 28

Page 28: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Access spring beans / reindex nodes

• We faced the issue with nodes getting lost from the index. ALF-12588

• You can force the reindexing of single nodes:

var ctx =

Packages.org.springframework.web.context.ContextLoader.getCurrentWebAppl

icationContext();

var nodeIndexer = ctx.getBean("nodeIndexer");

var nodeService = ctx.getBean("nodeService");

function indexNode(node) {

var childAssoc = nodeService.getPrimaryParent(node.nodeRef);

nodeIndexer.indexCreateNode(childAssoc);

}

indexNode(search.findNode("workspace://SpacesStore/d4923a10-96dc-4565-

a471-d6b6c8f7b170"));

• Warning: With great power comes great responsibility!

(c) fme AG - #205626 29

Page 29: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Special access to logger configuration

• The Javascript Console allows you to change to log4j log level at runtime.

• For your own Java classes you can use:

logger.setLevel("de.fme.alfresco", "DEBUG");

• The Alfresco Javascript runtime uses the ScriptLogger class:

logger.setLevel("org.alfresco.repo.jscript.ScriptLogger",

"DEBUG");

(c) fme AG - #205626 30

Page 30: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Javascript Puzzlers – Alfresco Edition

What do these two do?

• node.properties["cm:name"] = "textfile.txt";

• node.name = "textfile.txt";

Comparing ScriptNode nodeRefs:

• node1.nodeRef.equals(node2.nodeRef)

• node1.nodeRef == node2.nodeRef

• " " +node1.nodeRef == " " + node2.nodeRef

(c) fme AG - #205626 31

Page 31: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Batch processing

• Examples:

Set all documents to inherit permissions

Disable automatic versioning for all documents

• Small batches can run in the Javascript Console in one Transaction (10000 nodes)

I install the Javascript Console on every production system

• Large batches currently only possible using Java with multiple Transactions.

• Batch processing (updates for >100000 nodes)

How to find the nodes? Search or recursion?

Can not run in one transaction: Transaction Management in Javascript?

Processing in smaller batches

How to make use of multithreading?

(c) fme AG - #205626 32

Page 32: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170

Fragen, Feedback …?

[email protected]

• http://twitter.com/#!/fmaul

© fme AG - #212261 33

Page 33: Javascript Konsole für Entwicklung und Administration

230/0/0

178/178/178

205/205/205

206/144/144

144/206/153

216/221/234

170/182/204

69/101/140

118/139/170