08-26-2010 02:47 PM
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??
08-26-2010 06:02 PM
//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;
}
<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;
}
}
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.