AnsweredAssumed Answered

Problem 'document.assocs' from Rhino Javascript

Question asked by sbroussi on Jun 8, 2006
Latest reply on Jun 8, 2006 by kevinr
I encounter a problem when I want to access one list of associations from the Javascript.
Using FreeMarker: NO PROBLEM  8-)


I get a "java.lang.ArrayIndexOutOfBoundsException: 1" when the Javascript "document" Node
want to retrieve the list of associations.

See below:
- the exception stack trace
- the model definition
- the Javascript code
- the Java source file of "Node"

=> Any idea ?

————————— Exception when invoking from javascript: "document.assocs[…]"

Caused by: org.alfresco.error.AlfrescoRuntimeException: Wrapped java.lang.ArrayIndexOutOfBoundsException: 1 (AlfrescoScript#216)
        at org.alfresco.repo.jscript.RhinoScriptService.executeScriptImpl(RhinoScriptService.java:220)
        at org.alfresco.repo.jscript.RhinoScriptService.executeScript(RhinoScriptService.java:141)
        … 81 more
Caused by: org.mozilla.javascript.WrappedException: Wrapped java.lang.ArrayIndexOutOfBoundsException: 1 (AlfrescoScript#216)
        at org.mozilla.javascript.Context.throwAsScriptRuntimeEx(Context.java:1693)
        at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:157)
        at org.mozilla.javascript.JavaMembers.get(JavaMembers.java:98)
        at org.mozilla.javascript.NativeJavaObject.get(NativeJavaObject.java:102)
        at org.mozilla.javascript.ScriptableObject.getProperty(ScriptableObject.java:1312)
        at org.mozilla.javascript.ScriptRuntime.getObjectProp(ScriptRuntime.java:1334)
        at org.mozilla.javascript.ScriptRuntime.getObjectProp(ScriptRuntime.java:1323)
        at org.mozilla.javascript.Interpreter.interpretLoop(Interpreter.java:2815)
        at org.mozilla.javascript.Interpreter.interpret(Interpreter.java:2250)
        at org.mozilla.javascript.InterpretedFunction.call(InterpretedFunction.java:149)
        at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:337)
        at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:2755)
        at org.mozilla.javascript.InterpretedFunction.exec(InterpretedFunction.java:160)
        at org.mozilla.javascript.Context.evaluateReader(Context.java:1163)
        at org.alfresco.repo.jscript.RhinoScriptService.executeScriptImpl(RhinoScriptService.java:214)
        … 82 more
Caused by: java.lang.ArrayIndexOutOfBoundsException: 1
        at org.alfresco.repo.jscript.Node.getAssocs(Node.java:336)
        at sun.reflect.GeneratedMethodAccessor759.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at org.mozilla.javascript.MemberBox.invoke(MemberBox.java:142)
        … 95 more

————————- Model definition

    <aspect name="myCompany:routable">
      <title>Routable</title>
      <properties>…</properties>
      <associations>
        <association name="myCompany:targetGroups">
          <title>Target Group(s)</title>
          <source>
            <role>myCompany:contentToRoute</role>
            <mandatory>false</mandatory>
            <many>true</many>
          </source>
          <target>
            <class>myCompany:inboxFolder</class>
            <role>myCompany:targetGroup</role>
            <mandatory>false</mandatory>
            <many>true</many>
          </target>
        </association>
      </associations>
    </aspect>
————————- Javascript which fire the Exception


nok = document.assocs["myCompany:targetGroups"];
nok = document.assocs["myCompany:targetGroups"][0];
nok = document.assocs["{myCompany.model.content.1.0}targetGroups"];
nok = document.assocs["{myCompany.model.content.1.0}targetGroups"][0];
nok = document.assocs;
————————- original Javascript loop


var targetGroups = document.assocs["myCompany:targetGroups"];      // EXCEPTION THROWN
if (targetGroups != null)
{
  if (debug)
    log("number of target groups: not null");
  if (debug)
    log("number of target groups: " + targetGroups.length);


  for (var i=0; i<targetGroups.length; i++)
  {

    var targetGroup = targetGroups[i];

    if (debug)
      log("target group #" + i + "; " + targetGroup.nodeRef);
  }
}


————————- the Java source file of "Node"

package org.alfresco.repo.jscript;

public Map<String, Node[]> getAssocs()
{
    if (this.assocs == null)
    {
        // this Map implements the Scriptable interface for native JS syntax property access
        this.assocs = new ScriptableQNameMap<String, Node[]>(this.services.getNamespaceService());
       
        List<AssociationRef> refs = this.nodeService.getTargetAssocs(this.nodeRef, RegexQNamePattern.MATCH_ALL);
        for (AssociationRef ref : refs)
        {
            String qname = ref.getTypeQName().toString();
            Node[] nodes = (Node[])assocs.get(qname);
            if (nodes == null)
            {
                // first access for the list for this qname
                nodes = new Node[1];
            }
            else
            {
                Node[] newNodes = new Node[nodes.length + 1];
                System.arraycopy(nodes, 0, newNodes, 0, nodes.length);
                nodes = newNodes;
            }
// ############################ EXCEPTION THROWN NEXT LINE:           
            nodes[nodes.length] = new Node(ref.getTargetRef(), this.services, this.imageResolver);

Outcomes