cancel
Showing results for 
Search instead for 
Did you mean: 

Simple script called by behavior-context

cristian
Champ in-the-making
Champ in-the-making
I've got this script (onUpdateOrder.js) that works right in a rule's "Execute Script" action.
The goal is to update a custom cm:folder property with the sum of its children property, when a custom type ("my:item") is updated in the context folder.
var childrenList = space.children;
if (childrenList)
{
    for (var i = 0; i < childrenList.length; i++)
    {
        item = childrenList[i];
        itemPrice = item.properties["my:itemPrice"];
        total += itemPrice;
    }
    space.properties["my:itemsSum"] = total;
    space.save();
}
.. I tried to trigger this script in a behavior-context.xml:
    <bean id="onUpdateOrderNode" class="org.alfresco.repo.policy.registration.ClassPolicyRegistration" parent="policyRegistration">
        <property name="policyName">
            <value>{http://www.alfresco.org}onUpdateNode</value>
        </property>
        <property name="className">
            <value>{http://www.alfresco.org/orders/content/1.0}item</value>
        </property>
        <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/onUpdateOrder.js</value>
                        </constructor-arg>
                               </bean>
                        </property>
           </bean>
        </property>
    </bean>
..but "space" is undefined. How to get the correct context in this environment??
1 REPLY 1

cristian
Champ in-the-making
Champ in-the-making
Good time spent on Jeff Potts book (Alfresco Developer Guide). I worked on the User Rating Calculator and found the right solution:

This is the script used to recalculate Orders value (orders.js):
//calculate orders
function calculateOrders(childAssocRef) {
      var parentRef = childAssocRef.parent;
      
      // check the parent to make sure it has the right aspect
      if (parentRef.hasAspect("{http://www.alfresco.org/orders/content/1.0}orderAspect")) {
         // continue, this is what we want
      } else {
         logger.log("Item's parent ref did not have order aspect.");
         return;
      }
      
      // get the parent node's children
      var children = parentRef.children;

      // iterate through the children to compute the total
      var orderValue = 0;

      if (children != null && children.length > 0) {
         for (i in children) {
            var child = children[i];
            var itemPrice = child.properties["{http://www.alfresco.org/orders/content/1.0}itemPrice"];
            orderValue += itemPrice;
         }
      }         
   
      logger.log("Order result:" + orderValue);
      
      // store the value on the parent node
      parentRef.properties["{http://www.alfresco.org/orders/content/1.0}orderValue"] = orderValue;
      parentRef.save();
      
      logger.log("Property set");
      
      return;

}

..and the controller (onUpdateItem.js) that calls the script to run against the current node:
<import resource="classpath:alfresco/extension/scripts/orders.js">
var scriptFailed = false;

//Have a look at the behaviour object that should have been passed
if (behaviour == null) {
logger.log("The behaviour object has not been set.");
scriptFailed = true;
}

//Check the name of the behaviour
if (behaviour.name == null && behaviour.name != "onUpdateNode") {
logger.log("The behaviour name has not been set correctly.");
scriptFailed = true;
} else {
logger.log("Behaviour name: " + behaviour.name);
}

//Check the arguments
if (behaviour.args == null) {
logger.log("The args have not been set.");
scriptFailed = true;
} else {
if (behaviour.args.length == 1) {
     var childAssoc = behaviour.args[0];
     calculateOrders(childAssoc);
} else {
     logger.log("The number of arguments is incorrect.");
     scriptFailed = true;
}
}    

Deployed and tested, it works as well.