AnsweredAssumed Answered

onAddAspect policy does not work

Question asked by stijndereede on Nov 14, 2007
Latest reply on Nov 26, 2007 by rwetherall
I want to create an aspect that adds the (MD5) hash of the content as a property to a content object. Creating the aspect in my custom model is no problem, I just add an aspect with the property contenthash of type d:text.
I use Alfresco 2.2.0 (dev 616).

But I want this hash to be automatically calculated when the content is changed, and of course when the aspect is first added to a content object. If I'm not mistaken, I need to create a policy behaviour for this.
So I got a javascript implementation of MD5 hashing, and created these files:
contenthash.js:

<import resource="classpath:alfresco/extension/scripts/md5.js">
function computeContentHash(node) {
   
   if (!node.hasAspect("{http://www.cayman.com/model/content/1.0}autoversionable")) {
      logger.log("Node does not have the autoversionable aspect");
      return;
   }
   var c = new String(node.content);
   var hash = hex_md5©;
   node.properties.contenthash = hash;
   node.save();
   return;
}

onContentHash.js

<import resource="classpath:alfresco/extension/scripts/contenthash.js">
var scriptFailed = false;
logger.log("-=-=-=-=-=-=-=- onContentHash.js called -=-=-=-=-=-=-=-");
// 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) {
    logger.log("The behaviour name has not been set correctly.");
    scriptFailed = true;
}

// check the behaviour arguments
if (behaviour.args == null) {
   logger.log("The args have not been set.");
   scriptFailed = true;
}

// check which behaviour was called
if (behaviour.name == "onContentUpdate") {
   if (behaviour.args.length == 2) {
      var node = behaviour.args[0];
      var update = behaviour.args[1];   
   } else {
      logger.log("The number of arguments is incorrect.");
      scriptFailed = true;
   }
} else if (behaviour.name == "onAddAspect") {
   if (behaviour.args.length == 2) {
      var node = behaviour.args[0];
      var aspectname = behaviour.args[1];
   } else {
      logger.log("The number of arguments is incorrect.");
      scriptFailed = true;
   }            
   if (aspectname != "autoversionable") {
      logger.log("The aspect added was not autoversionable.");
      scriptFailed = true;
   }
} else {
   logger.log("The behaviour name has not been set correctly.");
   scriptFailed = true;   
}

// call the content hash functions
if (!scriptFailed) {
   logger.log("Calling compute contenthash");
   computeContentHash(node);
}

Now I'm ready to add to policies in my model-content.xml. One for the event onContentUpdate, and one for onAddAspect:

   <bean id="onAddAspectContentHash" class="org.alfresco.repo.policy.registration.ClassPolicyRegistration" parent="policyRegistration">
        <property name="policyName">
            <value>{http://www.alfresco.org}onAddAspect</value>
        </property>
        <property name="className">
            <value>{http://www.cayman.com/model/content/1.0}doc</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/onContentHash.js</value>
                        </constructor-arg>
                               </bean>
                        </property>
           </bean>
        </property>
    </bean> 
   
   <bean id="onContentUpdateContentHash" class="org.alfresco.repo.policy.registration.ClassPolicyRegistration" parent="policyRegistration">
        <property name="policyName">
            <value>{http://www.alfresco.org}onContentUpdate</value>
        </property>
        <property name="className">
            <value>{http://www.cayman.com/model/content/1.0}doc</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/onContentHash.js</value>
                        </constructor-arg>
                               </bean>
                        </property>
           </bean>
        </property>
    </bean>

And my problem is: the onContentUpdate policy works perfectly, but the onAddAspect policy is never executed: the onContentHash.js file is never loaded. Is this policy broken or something?

Outcomes