AnsweredAssumed Answered

Access companyhome/userhome objects, onUpdateNode?

Question asked by henrik on May 27, 2010
Latest reply on Jun 20, 2010 by henrik
Hello!

I am trying to configure an action where after some one adds an aspect to a specific node, one of the aspect properties is filled with a number.

I have a configured bean that looks like this, the script is executed on update and everything works fine there:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>
   <bean id="onUpdateNode_SetNewNummer" 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.mymodels.com/model/content/1.0}newNumber</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/module/company/context/scripts/onUpdateNode_SetNewNummer.js</value>
                                               </constructor-arg>
                                       </bean>
                               </property>
                       </bean>
               </property>
       </bean>
</beans>

Then, the JavaScript onUpdateNode_SetNewNumber.js contains this:

logger.log("LCM onUpdateNode_SetDocnumber has fired");

var scriptFailed = false;

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

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

// Check the arguments
if (behaviour.args == null)
{
   logger.log("LCM The args have not been set.")
   scriptFailed = true;
}
else
{
   if (behaviour.args.length == 1)
   {
       var nodeRef = behaviour.args[0];
       logger.log("LCM NodeRef: " + nodeRef.id);
       if (nodeRef.hasAspect("{http://www.mymodels.se/model/content/1.0}documentNumber")) {
          setDocumentId(nodeRef);
       }
   }
}

function setDocumentId(document) {
   var CounterValue = 1;
   var CurrentYear = new Date().getFullYear();
   // Get the Diarie Counter file
   var DiarieCounterFile = companyhome.childByNamePath(CurrentYear);
   
   // Check if the "YEAR" file exists allready
   if (DiarieCounterFile == null) {
      // If it does not exist, create it
      var fileNode = companyhome.createFile(CurrentYear);
      // Set the content to 1 (Default CounterValue value)
      fileNode.content = CounterValue;
   } else {
      // Add one to counter variable
      CounterValue= (parseInt(DiarieCounterFile.content)+1);
      // Set the content to the new counter value
      DiarieCounterFile.content = CounterValue;
   }
   
   // Set up aspect.property value
   var props = new Array(1);
   props["my:documentNumber"] = CurrentYear + '-' + utils.pad(CounterValue, 4);
   
   // Write the value to the document
   document.addAspect("my:documentNumber", props);
}


So, the problem I am having is that the object companyhome is not defined, neither is root nor userhome, as they are normally.

Why is that?
Is there a way around it?

Outcomes