Presentation is loading. Please wait.

Presentation is loading. Please wait.

Actions and Behaviours

Similar presentations


Presentation on theme: "Actions and Behaviours"— Presentation transcript:

1 Actions and Behaviours
Alex Miller – Senior Engineer - Alfresco

2 Agenda Breaking it down Behaviours Actions Demo Questions
How to build simple repository tier extension Expose that extension through Share UI Uses a basic Purchase Order/Inventory content model Example code available

3 Breaking it down

4 Why Action and Behaviours?
Extend beyond content models. Make extensions smart. Encapsulate features. React to changes. Why they are useful Where are they used TODO: find some existing examples

5 Content Model + Behaviours Actions = Smart Repository Extension
Sum of the Parts … Content Model + Behaviours Actions = Smart Repository Extension Content – data structure and constraints Behaviours – interaction, consequences and restrictions Actions - execution

6 A Simple Extension

7 Behaviours Observer Pattern
Extension listens for events published by repository services

8 Architecture Services can be used, or provided by, extensions
the NodeService for accessing the nodes in the DB the ContentService for accessing your documents' content the SearchService for searching against properties, paths, content etc. Services define policies Node service defines NodeServicePolicies Before/On Add/RemoveAspect, Create/DeleteNode, Create/DeleteAssociation and many more. Behaviours can be Java or Javascript

9 Binding Behaviours Extension code can implement policy interfaces and register with policy component, to execute custom code on service events Method name can be different from policy interface definition, parameters must match.

10 Types of Bindings Service – fired every time the service generate the event Class – most common, fired for a particular type or aspect Association – fired for a particular association or association of a particular type Properties – fired for properties of a particular type or with a particular name

11 Code Example – Javascript Behaviour
Bind class behaviour <bean id="onAddAssetAspect" class="org.alfresco.repo.policy.registration.ClassPolicyRegistration" parent="policyRegistration" > <property name="policyName"> <value>{ </property> <property name="className"> <value>{ <property name="behaviour"> <bean class="org.alfresco.repo.jscript.ScriptBehaviour" parent="scriptBehaviour"> <property name="location"> <bean class="org.alfresco.repo.jscript.ClasspathScriptLocation"> <constructor-arg> <value>alfresco/extension/scripts/onAddAssetAspect.js</value> </constructor-arg> </bean>

12 Code Example – Javascript Behaviour (2)
Javascript implementation logger.log("onAddAspect policy code for devcon:asset"); var assetNode = behaviour.args[0]; var assetTitle = assetNode.properties["cm:name"]; // Get the issue creator's address. var assetCreatorId = assetNode.properties["cm:creator"]; var assetCreatorPerson = people.getPerson(assetCreatorId); var assetCreator Address = assetCreatorPerson.properties["cm: "]; logger.log("asset creator address = " + assetCreator Address); //create a 'mail' action var mail = actions.create("mail"); mail.parameters.to = assetCreator Address; mail.parameters.subject = "Asset '" + assetTitle + "' has been added"; mail.parameters.from = // But we'll keep it simple mail.parameters.text = "Asset was created"; // execute action against the issueNode mail.executeAsynchronously(assetNode);

13 Code Example – Java Behaviour
Bind class behaviour public void init() { policyComponent.bindClassBehaviour(OnAddAspectPolicy.QNAME, DevconModel.ASPECT_ASSET, new JavaBehaviour(this, "onAddAspect”, Behaviour.NotificationFrequency.EVERY_EVENT)); policyComponent.bindClassBehaviour(OnDeleteNodePolicy.QNAME, new JavaBehaviour(this, "onDeleteNode", policyComponent.bindClassBehaviour(OnRemoveAspectPolicy.QNAME, new JavaBehaviour(this, "onRemoveAspect", }

14 Notification Frequencies
Every time First Event Transaction Commit When should policies fire? Every time – default – every time the event is fired First Event – first time for a particular node, within a transaction Transaction commit – once, after the transaction has commited Won’t cause tx to rollback Node may have been deleted during tx. Use with care

15 Code Example – Java Behaviour (2)
Java Implementation public class AssetBehaviours implements NodeServicePolicies.OnAddAspectPolicy, NodeServicePolicies.OnRemoveAspectPolicy, NodeServicePolicies.OnDeleteNodePolicy { public void onDeleteNode(ChildAssociationRef childAssocRef, boolean isNodeArchived) assetRemoved(childAssocRef.getChildRef()); } public void onRemoveAspect(NodeRef nodeRef, QName aspectTypeQName) assetRemoved(nodeRef); public void onAddAspect(NodeRef nodeRef, QName aspectTypeQName) assetAdded(nodeRef);

16 Common uses for behaviours
Built in “rules” Service call back extension points Calculated properties Complex constraints Auto-casting Provides a “Smart Model” Custom copy behaviours Insure new id is generated Prevent aspect from being copied at all Model restrictions Behaviours can throw exceptions Runtime exceptions cause transactions to be rollback Enforce model constraints such as read-only properties Behaviours can be disabled from other services By type By node Per transaction All

17 Actions

18 What are actions? A unit of work executed in the repository
Have parameters and return a value Can executed synchronously or asynchronously Reusable Implmentation of the GoF “Command” pattern

19 Architecture

20 Anatomy of an Action

21 Code Example – Executing an Action
private void sendMail(NodeRef nodeRef, String siteGroup, String subject, String html) { Action mailAction = ActionService.createAction(MailActionExecuter.NAME); mailAction.setParameterValue(MailActionExecuter.PARAM_SUBJECT, subject); mailAction.setParameterValue(MailActionExecuter.PARAM_FROM, mailAction.setParameterValue(MailActionExecuter.PARAM_TO_MANY, siteGroup); mailAction.setParameterValue(MailActionExecuter.PARAM_HTML, html); actionService.executeAction(mailAction, nodeRef, true, true); } assetAdded and assetRemoved delegate to this method, which use the “Mail Action” to send notifications to all members of a site, when an asset is added or removed.

22 Custom Actions Extend built-in actions
Implemented in Java or JavaScript Available in Share UI Executable as a rule Conditions can be implemented in Java or Javascript Convient way to execute a unit of work, asynchronously.

23 Code Example – Custom Action Implementation
public class AddItemActionExecuter extends ActionExecuterAbstractBase { private static final String NAME = "create-purchase-order-items”; private static final String PARAM_ITEM = "item"; @Override protected void addParameterDefinitions(List<ParameterDefinition> paramList) { paramList.add(new ParameterDefinitionImpl(PARAM_ITEM, DataTypeDefinition.NODE_REF, true, getParamDisplayLabel(PARAM_ITEM), false, null)); } protected void executeImpl(Action action, NodeRef actionedUponNodeRef) NodeRef item = (NodeRef)action.getParameterValue(PARAM_ITEM); nodeService.createAssociation(actionedUponNodeRef, item, DevconModel.ASSOC_PURCHASE_ORDER_ITEMS);

24 Code Example – Custom Action Share Configuration (1)
<config evaluator="string-compare" condition="DocLibActions"> <actions> <action id="add-item" type="javascript" label="action.add_item"> <param name="function">onActionFormDialog</param> <param name="itemKind">action</param> <param name="itemId">add-item</param> <param name="mode">create</param> <param name="destination">{node.nodeRef}</param> <permissions> <permission allow="true">Write</permission> </permissions> <param name="successMessage">message.add_item.success</param> <param name="failureMessage">message.add_item.failure</param> <evaluator>evaluator.isPurchaseOrder</evaluator> </action> </actions> … </config>

25 Code Example – Custom Action Share Configuration (2)
<config evaluator="string-compare" condition="DocLibActions"> … <actionGroups> <actionGroup id="document-browse"> <action index="0" id="add-item"/> </actionGroup> <actionGroup id="document-details"> </actionGroups> </config>

26 Custom Action – Share Configuration (3)
<config evaluator="string-compare" condition="add-item"> <forms> <form> <field-visibility><show id="item"/></field-visibility> <appearance> <field id="item" label-id="add-item.field.item"> <control template="/org/alfresco/components/form/controls/association.ftl"> <control-param name="forceEditable"> true </control-param> <control-param name="startLocation"> {doclib} </control> </field> </appearance> </form> </forms> </config>

27 Common Uses of Actions Implement new features Rules Scheduled Tasks

28 Quick Demo

29 Questions?


Download ppt "Actions and Behaviours"

Similar presentations


Ads by Google