Javascript Konsole für Entwicklung und AdministrationMeet the Experts in Hamburg – Florian Maul – 5.06.2012fme AG - #212261
Über mich    •   Florian Maul, Senior Consultant bei der fme AG    •   Langjährige Erfahrung mit Alfresco    •   Gewinner ...
Agenda•    Kurzvorstellung fme AG•    fme Alfresco Extensions•    Was ist die Javascript Konsole?•    Viele Demos mit Beis...
Das Unternehmen    Q1/2012                                                                                    Techno-     ...
Kunden fme AG    Auswahl              Industrielle Fertigung        Life Science              Energie                  Wei...
fme Migration Center    Analyse   Organise   Transform          Validate   Correct   Import                               ...
fme Alfresco Extensions Gallery•     http://alfresco.fme.de/Overview.925.0.html•     14 gelistete Alfresco Erweiterungen• ...
Alfresco APIs•    What kinds of API are there?         Java Foundation API         Javascript API & Javascript Services ...
Webscripts developed in Javascript•    Most of the Alfresco Webscript are developed in Javascript•    Example: GET /alfres...
Javascript for Tasks & Events in Workflows•    Javascript can be executed from within Workflow definitions•    Example of ...
Javascript run by Actions (Run Script Action)•    Alfresco Explorer has a Run Script Action•    For Share there is an Exec...
But how to develop Javascript code?•    Webscripts         Develop in Eclipse         ANT build script to copy changes &...
Meet the Javascript Console14                             (c) fme AG - #205626
JavaScript Console•    Komponente für die Share Admin Console      zum Schreiben & Ausführen von     JavaScript-Code im Re...
Meet the Javascript Console••    Mike Farman (Director Product Management Alfresco):     „I really like the stuff you’ve b...
Javascript Console milestones•    End of 2010         Need to develop and execute Javascript code for a project sparked d...
Download & Installation•    Visit: http://code.google.com/p/share-extras/•    Download at http://code.google.com/p/share-e...
Poor man‘s Nodebrowserprint(document);print("--- PROPERTIES -----------------------------------------");print(document.pro...
How does it work?     Alfresco Repository / Webscripts                           •   print(), recurse(), logger           ...
Selecting the file or folder to work with• Select the space variable from   • Open any document in the     a directory cho...
Recurse() function•    The Javascript console has a special recurse function to iterate over a tree of nodes:     recurse(...
Creating users and attaching avatarsfunction createUser(username,firstname, lastname, email, location, jobtitle){     var ...
Creating users and attaching avatars// pseudo-code not workingfor each (imageNode in space.children) {     var user = peop...
Assign users to sites                   var nodes = search.luceneSearch(                     "TYPE:cm:person +@cm:lastName...
Creating Webscripts•    The new version 0.5 makes     it easy to create webscripts:         Write a controller javascript...
Creating Webscriptsvar result = [];for each(node in companyhome.childByNamePath(args.path).children) {     result.push({  ...
Access spring beans / unlock documents•    Example unlock a document:     var ctx = Packages.org.springframework.     web....
Access spring beans / reindex nodes•    We faced the issue with nodes getting lost from the index. ALF-12588•    You can f...
Special access to logger configuration•    The Javascript Console allows you to change to log4j log level at runtime.•    ...
Javascript Puzzlers – Alfresco EditionWhat do these two do?•    node.properties["cm:name"] = "textfile.txt";•    node.name...
Batch processing•    Examples:         Set all documents to inherit permissions         Disable automatic versioning for...
Fragen, Feedback …?•    f.maul@fme.de•    http://twitter.com/#!/fmaul33                                 © fme AG - #212261
Javascript Konsole für Entwicklung und Administration
Upcoming SlideShare
Loading in …5
×

Javascript Konsole für Entwicklung und Administration

1,687 views
1,593 views

Published on

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

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,687
On SlideShare
0
From Embeds
0
Number of Embeds
12
Actions
Shares
0
Downloads
13
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Javascript Konsole für Entwicklung und Administration

  1. 1. Javascript Konsole für Entwicklung und AdministrationMeet the Experts in Hamburg – Florian Maul – 5.06.2012fme AG - #212261
  2. 2. Ü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) • @fmaul3 (c) fme AG - #205626
  3. 3. Agenda• Kurzvorstellung fme AG• fme Alfresco Extensions• Was ist die Javascript Konsole?• Viele Demos mit Beispielskripten4 © fme AG - #212261
  4. 4. Das Unternehmen Q1/2012 Techno- Lösungen logien Wir unterstützen unsere Kunden dabei, Informationen effektiver und effizienter zu strukturieren und verwalten. Dies erreichen wir Unser durch den Einsatz von Enterprise Content Management, Business Branchen Fokus Intelligence und Product Lifecycle Management Technologien. Der Schwerpunkt liegt auf Lösungen für Life Sciences, industrielle Fertigung und Energie. • 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 %) Fakten • 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 weltweit5 #6120 - © fme AG
  5. 5. Kunden fme AG Auswahl Industrielle Fertigung Life Science Energie Weitere6 © fme AG - #212261
  6. 6. fme Migration Center Analyse Organise Transform Validate Correct Import Time7 (c) fme AG - #205626
  7. 7. 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ösungen8 © fme AG - #212261
  8. 8. 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 Workflows9 (c) fme AG - #205626
  9. 9. 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; }10 (c) fme AG - #205626
  10. 10. 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>11 (c) fme AG - #205626
  11. 11. 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:12 (c) fme AG - #205626
  12. 12. 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.13 (c) fme AG - #205626
  13. 13. Meet the Javascript Console14 (c) fme AG - #205626
  14. 14. 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)15 © fme AG - #212261
  15. 15. 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!“• X16 (c) fme AG - #205626
  16. 16. 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 editor17 (c) fme AG - #205626
  17. 17. Download & Installation• Visit: http://code.google.com/p/share-extras/• Download at http://code.google.com/p/share-extras/downloads/listInstallation• 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.18 (c) fme AG - #205626
  18. 18. Poor man‘s Nodebrowserprint(document);print("--- PROPERTIES -----------------------------------------");print(document.properties);print("--- ASPECTS --------------------------------------------");print(document.aspects);print("--- ASSOCS ---------------------------------------------");print(document.assocs);print("--- PERMISSIONS ----------------------------------------");print(document.fullPermissions);19 (c) fme AG - #205626
  19. 19. How does it work? Alfresco Repository / Webscripts • print(), recurse(), logger • eval() / Java-Webscript • <imports> Execute Webscript • Error handling Browser / Javascript / YUI / Codemirror Input Output • Javascript • Print / log output • Freemarker template • Freemarker output • space / document • Error messages20 (c) fme AG - #205626
  20. 20. Selecting the file or folder to work with• Select the space variable from • Open any document in the a directory chooser: Javascript Console from the document library:21 (c) fme AG - #205626
  21. 21. 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);22 (c) fme AG - #205626
  22. 22. Creating users and attaching avatarsfunction 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", "h.simpson@springfield.fox", "Springfield", "Nuclear Safety Inspector");23 (c) fme AG - #205626
  23. 23. Creating users and attaching avatars// pseudo-code not workingfor 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/24 (c) fme AG - #205626
  24. 24. 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"); }25 (c) fme AG - #205626
  25. 25. 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 users26 (c) fme AG - #205626
  26. 26. Creating Webscriptsvar 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>]27 (c) fme AG - #205626
  27. 27. 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 Java28 (c) fme AG - #205626
  28. 28. 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!29 (c) fme AG - #205626
  29. 29. 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");30 (c) fme AG - #205626
  30. 30. Javascript Puzzlers – Alfresco EditionWhat 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.nodeRef31 (c) fme AG - #205626
  31. 31. 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?32 (c) fme AG - #205626
  32. 32. Fragen, Feedback …?• f.maul@fme.de• http://twitter.com/#!/fmaul33 © fme AG - #212261

×