AnsweredAssumed Answered

Share simple Custom Behaviour

Question asked by augustodelucena on Oct 11, 2015
Hello!

I have an aspect in my customform model which has a property field of d:noderef type and this type is bind to a datalist which I use to have a "dynamic" field list.

The thing is that I have a custom advanced search form which uses this property as a search term and due to it's type(noderef) I can't search for the desired strings.

I found out that I could create one invisible property field for this aspect which would receive the string value from the noderef property mentioned and then it would be able to be looked up on advanced search.

To do that job, I thought of creating a behaviour and binding it to OnCreateNode and OnUpdateNode policies. I followed along JPotts guide (http://ecmarchitect.com/alfresco-developer-series-tutorials/behaviors/tutorial/tutorial.html), I wonder how do I declare 'property name="className"' to all of my custom types???

Later, when I was testing the behaviour I got the following error:

<blockcode>
ERROR [extensions.webscripts.AbstractRuntime] [http-bio-8080-exec-4] Exception from executeScript - redirecting to status template error: 09110001 Wrapped Exception (with status template): 09110011 Failed to execute script 'classpath*:alfresco/templates/webscripts/org/alfresco/slingshot/documentlibrary/type.post.json.js': 09110010 Failed to execute script 'alfresco/module/repo-behaviour/scripts/onUpdateNoderefToText.js': missing ) after condition (alfresco/module/repo-behaviour/scripts/onUpdateNoderefToText.js#3)
org.springframework.extensions.webscripts.WebScriptException: 09110001 Wrapped Exception (with status template): 09110011 Failed to execute script 'classpath*:alfresco/templates/webscripts/org/alfresco/slingshot/documentlibrary/type.post.json.js': 09110010 Failed to execute script 'alfresco/module/repo-behaviour/scripts/onUpdateNoderefToText.js': missing ) after condition (alfresco/module/repo-behaviour/scripts/onUpdateNoderefToText.js#3)
</blockcode>

My scrips are below:

onUpdateNoderefToText.js

<import resource="classpath:alfresco/extension/scripts/nodereftotext.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 == 2) {
          var nodedocument = behaviour.args[0];
          logger.log("Update text property fields");
                // do the magic
          nodereftotext(nodedocument);
    } else {
          logger.log("The number of arguments is incorrect.");
            scriptFailed = true;
    }
}

onCreateNoderefToText.js

<import resource="classpath:alfresco/extension/scripts/nodereftotext.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 != "onCreateNode") {
    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 nodedocument = behaviour.args[0];
          logger.log("Update text property fields");
                // do the magic
          nodereftotext(nodedocument);
    } else {
          logger.log("The number of arguments is incorrect.");
            scriptFailed = true;
    }
}
       

nodereftotext.js

function nodereftotext(document) {
if (document.hasAspect("ext1:aspect_dadosdocfunc")
       {
   var nodepessoa = document.properties["ext1:nome_func"];
   var nodeempresa = document.properties["ext1:nome_cliente"];
   var nodecontrato = document.properties["ext1:codfunc"];
   document.properties["ext1:nome_functext"] =  nodepessoa;
   document.properties["ext1:nome_clientetext"] = nodeempresa;
   document.properties["ext1:codfunctext"] = nodecontrato;
       document.save();
       }
       else
       {
       var nodeempresa = document.properties["ext1:nomecliente"];
       document.properties["ext1:nomeclientetext"] = nodempresa;
       document.save();
       }
}

My service-context.xml

<bean id="onCreateNoderefToText" class="org.alfresco.repo.policy.registration.ClassPolicyRegistration" parent="policyRegistration">
    <property name="policyName">
        <value>{http://www.alfresco.org}onCreateNode</value>
    </property>
     <property name="className">
        <value>{http://www.bcpg.fr/model/ext1/1.0}guias-impostos</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/${project.artifactId}/scripts/onCreateNoderefToText.js</value>
                    </constructor-arg>
                </bean>
            </property>
        </bean>
    </property>
</bean>



<bean id="onUpdateNoderefToText" 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.bcpg.fr/model/ext1/1.0}guias-impostos</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/${project.artifactId}/scripts/onUpdateNoderefToText.js</value>
                    </constructor-arg>
                </bean>
            </property>
        </bean>
    </property>
</bean>


Thanks!

Outcomes