cancel
Showing results for 
Search instead for 
Did you mean: 

Problem passing JavaScript function

dolo
Champ in-the-making
Champ in-the-making
Hello,

I've got a problem here. I can't pass the method site.listMembers as a JavaScript function so FreeMarker will use it.
I have to add a property that comes with an aspect to the site object. Because the site objects returned by siteService.listUserSites() is a Java object I can't augment it with the new property, so I decided to copy all properties and functions that are needed by the FreeMarker file. Unfortunately this will not work for the site.listMembers method.

Excerpt from the JavaScript code:

    // Get the list of sites
    var sites = siteService.listUserSites(userName);

    // Problem: sites[index] is a java class that has no attribute "duration"
    var jsSites = new Array(sites.length);
    for (var index = 0; index < sites.length; index++)
    {
       var site = {};
       // Copy needed site properties from java site object to JavaScript site object
       site.shortName = sites[index].shortName;
       site.sitePreset = sites[index].sitePreset;
       site.title = sites[index].title;
       site.description = sites[index].description;
       site.node = sites[index].node;
       site.isPublic = sites[index].isPublic;
       site.visibility = sites[index].visibility;
       site.customProperties = sites[index].customProperties;
       site.listMembers = sites[index].listMembers;
       
        // Put the property duration (from aspect biddingDuration) into the site.
        // Convert it from a "org.mozilla.javascript.NativeDate" to Date so ftl will accept it.
       var siteNode = sites[index].getNode();
       if (siteNode.hasAspect("hbg:biddingDuration"))
       {
          site.duration = new Date(siteNode.properties["hbg:duration"]);
       }
       
       jsSites[index] = site;
    }
    model.sites = jsSites;

The offending line in the site.lib.ftl file:

    <#assign managers = site.listMembers(null, "SiteManager", 0)?keys />

And the error message from tomcat (BTW: why can't I find this error message in the tomcat log files?)

Expected method. site.listMembers evaluated instead to freemarker.template.SimpleHash on line 39, column 45 in org/alfresco/repository/site/site.lib.ftl.
The problematic instruction:
———-
==> assignment: managers=site.listMembers(null, "SiteManager", 0)?keys [on line 39, column 25 in org/alfresco/repository/site/site.lib.ftl]
in user-directive siteLib.siteJSON [on line 5, column 17 in org/alfresco/repository/person/person.sites.get.json.ftl]
———-

Java backtrace for programmers:
———-
freemarker.template.TemplateException: Expected method. site.listMembers evaluat
ed instead to freemarker.template.SimpleHash on line 39, column 45 in org/alfres
co/repository/site/site.lib.ftl.
        at freemarker.core.TemplateObject.invalidTypeException(TemplateObject.java:135)
        at freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:114)
        at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
        at freemarker.core.BuiltIn$keysBI._getAsTemplateModel(BuiltIn.java:589)
        at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
        at freemarker.core.Assignment.accept(Assignment.java:90)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.MixedContent.accept(MixedContent.java:92)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.ConditionalBlock.accept(ConditionalBlock.java:79)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.MixedContent.accept(MixedContent.java:92)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.EscapeBlock.accept(EscapeBlock.java:84)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.Macro$Context.runMacro(Macro.java:164)
        at freemarker.core.Environment.visit(Environment.java:601)
        at freemarker.core.UnifiedCall.accept(UnifiedCall.java:106)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.MixedContent.accept(MixedContent.java:92)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:179)
        at freemarker.core.Environment.visit(Environment.java:415)
        at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.MixedContent.accept(MixedContent.java:92)
        at freemarker.core.Environment.visit(Environment.java:208)
        at freemarker.core.Environment.process(Environment.java:188)
        at freemarker.template.Template.process(Template.java:237)
      …

Thanks a lot for your help!
Doris
1 REPLY 1

rwetherall
Confirmed Champ
Confirmed Champ
Hi,

You are not assigning the method to the slot on the model with the line …

site.listMembers = sites[index].listMembers;

.. you are assigning the result of this method which is a Map.

This is why you get the error you are seeing when executing the Freemarker template.

Cheers,
Roy