0
votes

I am having big problems to get my webscript working. Best thing is that ín the Alfresco JavaScript Console everything is working. Not if I deploy the webscript.

javascript:

try {

var nodeRefs = args[0].split('&');
var paths = [];
for (var n in nodeRefs) {
    var doc = search.findNode(nodeRefs[n]);
    var path = doc.displayPath + "/" + doc.name;
    var host = headers['Host'];
    path = path.replace('/Firmen-Home/Sites', '');
    path = path.replace('/Companyhome/Sites', '');
    path = path.replace(/\//g, '\\');
    paths.push("\\\\" + host + "@7070\\alfresco" + path);
}

model.paths = paths;
model.nodeRefs = nodeRefs;

} catch (e) {

}

freemarker template:

<#escape x as jsonUtils.encodeJSONString(x)>
<#assign nodeRefs = nodeRefs>
{
<#assign n = 0>
<#list paths as path>
"${nodeRefs[n]}" : "${path}"<#if path_has_next>,</#if>
<#assign n = n + 1>
</#list>
}
</#escape>

JavaScript Console Output (I used the same noderef 3 times for testing):

{
   "workspace:\/\/SpacesStore\/5fa74ad3-9b5b-461b-9df5-de407f1f4fe7" : "\\\\localhost:8080@7070\\alfresco\\swsdp\\documentLibrary\\Budget Files\\budget.xls",
   "workspace:\/\/SpacesStore\/5fa74ad3-9b5b-461b-9df5-de407f1f4fe7" : "\\\\localhost:8080@7070\\alfresco\\swsdp\\documentLibrary\\Budget Files\\budget.xls",
   "workspace:\/\/SpacesStore\/5fa74ad3-9b5b-461b-9df5-de407f1f4fe7" : "\\\\localhost:8080@7070\\alfresco\\swsdp\\documentLibrary\\Budget Files\\budget.xls"
   }

I get 3 different Errors from the deployed webscripts at all possible solutions I tried:

freemarker.core.ParseException - Encountered "{" at line 1, column 19 in convert/uuidsToPaths.get.json.ftl. Was expecting one of: "in" ... ">"

freemarker.core.InvalidReferenceException: Error on line 3, column 9 in convert\uuidsToPaths.get.json.ftl nodeRefs is undefined. It cannot be assigned to nodeRefs

Expected collection or sequence. paths evaluated instead to freemarker.template.SimpleHash

What am I mussing here? I tried ${nodeRefs}, $nodeRefs, {nodeRefs} .... but nothing seems to work.

Regards, Michael

EDIT:

JavaScript Controler:

function main() {

    var nodeRefs = args[0].split("_");
    var paths = [ nodeRefs.length ];
    var i = 0;

    for ( var n in nodeRefs) {
        var doc = search.findNode(nodeRefs[n]);
        var path = doc.displayPath + "/" + doc.name;
        var host = headers["Host"];
        paths[i] = "\\" + host + "@7070\alfresco\\" + path;
        i++;
    }

    model.code = "200";
    model.paths = paths;
    model.nodeRefs = nodeRefs;

}
main();

Freemarker JSON

<#escape x as jsonUtils.encodeJSONString(x)>
    {
        "status" : {
            "code" : ${code} // easier to differ success from error
            },
        <#assign n = 0>
        <#list paths as path>
        "${nodeRefs[n]}" : "${path}"<#if path_has_next>,</#if>
        <#assign n = n + 1>
        </#list>
    }
</#escape>
1
For what "possible solutions" do you get which error? The syntax is ${someExpression}, the other two is just static text and will be printed as is. - ddekany
Why are you using #assign for nodeRefs since you already got the array stored in your model.nodeRefs? Also: the error seems to be pointing out your paths sequence is not a proper array but a simplehash. It could be that your array is stored with keys and values instead of solo values like: paths[0].value = \\\\localhost and path[0].key = 8080@7070\\alfresco\\swsdp\\documentLibrary\\Budget Files\\budget.xls. Try to check that. Btw, the correct syntax for index seq is: ${seq[n]} - Teqnology
Thank you for your help guys! nodeRefs is just an array which stores an unknown amount of noderefs). @Alch3mi5t how else can I dynamically creat and extend an array where the size of it can differ for each call? - Pali
Using <#assign nodeRefs = ${nodeRefs}> throws freemarker.core.ParseException: Encountered "{" at line 2, column 19 - Pali

1 Answers

3
votes

This isn't really the answer, but the discussion in your comment isn't looking promising.

First of all, remove the try/catch element in the JavaScript so that you will actually see the error in your alfresco.log/share.log. In your case I wouldn't even add a try/catch, the variables will be empty which will throw an error in Freemarker.

<#assign nodeRefs = nodeRefs>

This is assigning an already available variable to itself which makes no sense at all :). For your information all the model.<variable> are mapped and thus assigned to Freemarker variables.

So in your case model.nodeRefs in the controller JavaScript becomes nodeRefs in Freemarker.

Second remove the <#escape x as jsonUtils.encodeJSONString(x)> in the Freemarker so your matching error doesn't mess up the linings (you should add it later on, once it's working).

The main error in your log file is:

freemarker.core.InvalidReferenceException: Error on line 3, column 9 in convert\uuidsToPaths.get.json.ftl nodeRefs is undefined. It cannot be assigned to nodeRefs

This means the model.nodeRefs doesn't exists en thus your JavaScript has thrown an error which it doesn't show in the log.