cancel
Showing results for 
Search instead for 
Did you mean: 

Person object in a custom behaviour

rjohnson
Star Contributor
Star Contributor
I am currently working in 4.0a

I have written a custom behaviour that executes when an aspect of a given type is added to a node. I wanted to record in the aspect who did the adding (just the users full name, not the full user object).

Having written several repo tier web scripts I used person.properties.userName only to be told that "person" is not defined.

On the basis that a behaviour runs in a security aware context, Alfresco must "know" who is running it, but I am at a loss as to how to get that information in the absence of the "person" object.

For further information I have attached the data model context file in which the behaviour is defined and the javascript that gets executed.

If anyone could assist, I would be most grateful.

Bob Johnson
10 REPLIES 10

mitpatoliya
Star Collaborator
Star Collaborator
Hi Bob,
Your attached files are not visible.
Please add it one more time.

rjohnson
Star Contributor
Star Contributor
Hi Mits

When I edited my post the files were listed. I removed them and re-added them with marginally different file names. Hopefully they are visible now unless I am doing something particularly silly.

Bob

mitpatoliya
Star Collaborator
Star Collaborator
Still not visible Smiley Sad

lista
Star Contributor
Star Contributor
Yeah, forum obviously doesn't work, you can't add attachments.
Just upload that somewhere and paste a link.

rjohnson
Star Contributor
Star Contributor
Well, as we have found a forum bug, lets do it the old fashioned way

The context file looks like this


<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>

<beans>

    <!– Registration of new models –>   
    <bean id="extension.dictionaryBootstrap" parent="dictionaryModelBootstrap" depends-on="dictionaryBootstrap">
        <property name="models">
            <list>
                <value>alfresco/extension/fgempModel.xml</value>
            </list>
        </property>
      <property name="labels">
         <list>
                <value>alfresco/extension/fgempModelRepo</value>
         </list>
      </property>
    </bean>

   <bean id="onDischargeManagementRef"
           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.farthestgate.co.uk/emp/model/1.0}discharged</value>
           </property>
           <property name="behaviour">
                  <bean class="org.alfresco.repo.jscript.ScriptBehaviour"
                        parent="scriptBehaviour">
                       <property name="notificationFrequency" value="FIRST_EVENT"/>
                       <property name="location">
                              <bean class="org.alfresco.repo.jscript.ClasspathScriptLocation">
                                   <constructor-arg>
                                            <value>alfresco/extension/scripts/onAddDischargedAspect.js</value>
                                   </constructor-arg>
                              </bean>
                       </property>
                  </bean>
          </property>
    </bean>
             
</beans>



and the javascript looks like this


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;
}
if (behaviour.name == null && behaviour.name != "onAddAspect") {
   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 {
   // We should have nodeType in [0] and a list of aspects in [1]
   if (behaviour.args.length == 2) {
      var docNode = behaviour.args[0];
      try {
         docNode.properties["{http://www.farthestgate.co.uk/emp/model/1.0}whoDischarged"] = person.properties.userName;      
      } catch (e) {
         logger.log("Set whoDischarged failed " + e.message);
      }
      try {
         docNode.properties["{http://www.farthestgate.co.uk/emp/model/1.0}whenDischarged"] = new Date();
      } catch (e2) {
         logger.log("Set whenDischarged failed " + e2.message);
      }
      docNode.save();
   } else {
      logger.log("The number of arguments is incorrect.it is " + behaviour.args.length + " They are " + behaviour.args[0] + " and " + behaviour.args[1]);
      scriptFailed = true;
   }
}


It fails on the line


docNode.properties["{http://www.farthestgate.co.uk/emp/model/1.0}whoDischarged"] = person.properties.userName;


and logs the error "Set whoDischarged failed "person" is not defined"

Thank you for your perseverance.

Bob

mitpatoliya
Star Collaborator
Star Collaborator
Have you tried with this?
docNode.properties["{http://www.farthestgate.co.uk/emp/model/1.0}whoDischarged"] = person.properties.name;

Also check is this person object null or not.

rjohnson
Star Contributor
Star Contributor
I modified the code to look as below


      try {
         if(person != null) {
            logger.log("person != null");
            docNode.properties["{http://www.farthestgate.co.uk/emp/model/1.0}whoDischarged"] = person.properties.name;               
         } else {
            logger.log("person == null");
         }
      } catch (e) {
         logger.log("Set whoDischarged failed person check null, use person.properties.name" + e.message);
      }


and got the log message


10:31:25,372 DEBUG [org.alfresco.repo.jscript.ScriptLogger] Behaviour name: onAddAspect
10:31:25,379 DEBUG [org.alfresco.repo.jscript.ScriptLogger] Set whoDischarged failed person check null, use person.properties.name"person" is not defined.


Which suggest to me that the execution failed on the if person != null check as there are no other errors in the log.

So I then tried

      try {
         if(person.properties.name != null) {
            logger.log("person.properties.name != null");
            docNode.properties["{http://www.farthestgate.co.uk/emp/model/1.0}whoDischarged"] = person.properties.name;
         } else {
            logger.log("person.properties.name == null");
         }               
      } catch (e) {
         logger.log("Set whoDischarged, use person.properties.name" + e.message);
      }


and got the log message


10:50:17,759 DEBUG [org.alfresco.repo.jscript.ScriptLogger] Behaviour name: onAddAspect
10:50:17,766 DEBUG [org.alfresco.repo.jscript.ScriptLogger] Set whoDischarged, use person.properties.name"person" is not defined.


Which seems to confirm to me that the person object is not defined in this context.

Any further thoughts will be welcome.

Thanks

Bob

mitpatoliya
Star Collaborator
Star Collaborator
Serviceregistry object is injected in the org.alfresco.repo.jscript.ScriptBehaviour class
So what I assume is it should be available in the script.
If it is then you can get AuthenticationService from it
and that service has api getCurrentUserName() which will return you currently logged in user's name.
Give it a shot.

rjohnson
Star Contributor
Star Contributor
Hi Mits

Thank you so much for you input on this, but I have tried


    var sras = ServiceRegistry.AuthenticationService.getCurrentUserName();
    var sras = serviceRegistry.AuthenticationService.getCurrentUserName();
    var sras = servicesegistry.AuthenticationService.getCurrentUserName();
    var sras = Serviceregistry.AuthenticationService.getCurrentUserName();


individually and all of them say ServiceRegistry (however capitalised) "is not defined".

Any other thoughts?

Thanks again

Bob