How to write webscript to get multi value properties

cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Active Member II

How to write webscript to get multi value properties

Hi,

I am developing an webscript to extract some meta data in the alfresco repository. my files are below.

cps-commissionpaperdetails.get.desc.xml

<webscript>
<shortname>Search Cps Documents</shortname>
<description>Returns metadata as JSON for all Cps documents in the repository that matches search keyword</description>
<url>/tutorial/cpsdocs?q={keyword}</url>
<authentication>user</authentication>
<format default="json"></format>
<family>Alfresco Tutorials</family>
</webscript>

cps-commissionpaperdetails.get.js

function CpsDocumentInfo(doc) {
this.name = doc.name;
this.creator = doc.properties.creator;
this.createdDate = doc.properties.created;
this.modifier = doc.properties.modifier;
this.modifiedDate = doc.properties.modified;
this.compaperno = doc.properties["cps:compaperno"];
this.compapersub = doc.properties["cps:compapersub"];
this.comsubdivision = doc.properties["cps:comsubdivision"];
this.comsubdate = doc.properties["cps:comsubdate"];
model.otherreldivisions = doc.properties["cps:otherreldivisions"];
this.commeetingdate = doc.properties["cps:commeetingdate"];
this.comdecision = doc.properties["cps:comdecision"];
this.remarks = doc.properties["cps:remarks"];
}

function main() {
var searchKeyword = args["q"];
if (searchKeyword == null || searchKeyword.length == 0) {
searchKeyword = "";
} else {
searchKeyword = " AND TEXT:\"" + searchKeyword + "\"";
}

var cpsDocNodes = search.luceneSearch("TYPE:\"cps:commissionpaperdetails\"" + searchKeyword);
if (cpsDocNodes == null || cpsDocNodes.length == 0) {
status.code = 404;
status.message = "No Cps documents found";
status.redirect = true;
} else {
var cpsDocInfos = new Array();
for (i = 0; i < cpsDocNodes.length; i++) {
cpsDocInfos[i] = new CpsDocumentInfo(cpsDocNodes[i]);
}
model.cpsDocs = cpsDocInfos;
return model;
}
}

main();

cps-commissionpaperdetails.get.json.ftl

<#assign datetimeformat="EEE, dd MMM yyyy HH:mm:ss zzz">
{
"cpsDocs" : [
<#list cpsDocs as cpsDoc>
{
"name" : "${cpsDoc.name}",
"creator" : "${cpsDoc.creator}",
"createdDate" : "${cpsDoc.createdDate?string(datetimeformat)}",
"modifier" : "${cpsDoc.modifier}",
"modifiedDate" : "${cpsDoc.modifiedDate?string(datetimeformat)}",
"compaperno" : "${cpsDoc.compaperno!"Unknown"}",
"compapersub" : "${cpsDoc.compapersub!"Unknown"}",
"comsubdivision" : "${cpsDoc.comsubdivision!"Unknown"}",
"comsubdate" : "${cpsDoc.comsubdate?string(datetimeformat)}",
<#list otherreldivisions as otherreldivision>
"otherreldivisions" : "${otherreldivision}"
<#if otherreldivision_has_next>,</#if>
</#list>
"commeetingdate" : "${cpsDoc.commeetingdate?string(datetimeformat)}",
"comdecision" : "${cpsDoc.comdecision!"Unknown"}",
"remarks" : "${cpsDoc.remarks!"Unknown"}",
}
<#if cpsDoc_has_next>,</#if>
</#list>
]
}

The above web script gives me below result.

{     "cpsDocs" : [             {                 "name"                      : "989-3695-1-SM.pdf",                 "creator"                   : "admin",                 "createdDate"               : "Fri, 19 Jan 2018 01:44:24 EST",                 "modifier"                  : "admin",                 "modifiedDate"              : "Fri, 19 Jan 2018 01:55:17 EST",                 "compaperno"                : "002",                 "compapersub"               : "consumerdivisionpaper",                 "comsubdivision"            : "Consumer",                 "comsubdate"                : "Fri, 19 Jan 2018 00:00:00 EST",                      "otherreldivisions"         : "Consumer",                      ,                      "otherreldivisions"         : "Environment",                                      "commeetingdate"            : "Thu, 25 Jan 2018 00:00:00 EST",                 "comdecision"               : "Approved",                 "remarks"                   : "xaaaaaassssssssssssedeeeeeeeeeeeeeeeeeerrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrgggggggggggggggggggggggggggggggggggggggggggggg",             }             ,             {                 "name"                      : "COde.txt",                 "creator"                   : "admin",                 "createdDate"               : "Fri, 19 Jan 2018 01:44:46 EST",                 "modifier"                  : "admin",                 "modifiedDate"              : "Fri, 19 Jan 2018 01:57:52 EST",                 "compaperno"                : "003",                 "compapersub"               : "environmetdivisionpaper",                 "comsubdivision"            : "Environment",                 "comsubdate"                : "Fri, 19 Jan 2018 00:00:00 EST",                      "otherreldivisions"         : "Consumer",                      ,                      "otherreldivisions"         : "Environment",                                      "commeetingdate"            : "Thu, 25 Jan 2018 00:00:00 EST",                 "comdecision"               : "Commission requests further information",                 "remarks"                   : "sdfdasfjkshdfkjdhfckjsxcjdsahfehsandcsandsaddfsdcsdsdef",             }             ,             {                 "name"                      : "Capture.PNG",                 "creator"                   : "admin",                 "createdDate"               : "Fri, 19 Jan 2018 01:43:57 EST",                 "modifier"                  : "admin",                 "modifiedDate"              : "Fri, 19 Jan 2018 01:58:18 EST",                 "compaperno"                : "001",                 "compapersub"               : "communicationdivisionpaper",                 "comsubdivision"            : "Communication",                 "comsubdate"                : "Fri, 19 Jan 2018 00:00:00 EST",                      "otherreldivisions"         : "Consumer",                      ,                      "otherreldivisions"         : "Environment",                                      "commeetingdate"            : "Thu, 25 Jan 2018 00:00:00 EST",                 "comdecision"               : "Approved subject to conditions",                 "remarks"                   : "rftugysidhadysgadygsaofysgdfdsgfsdygfcsdihcsdiucjdwufhdyfgydsgfcsdychsdcidoshfdyfhdsiufhdiofhdfhdsiufhdsiofjdscjdshcdsyrfgyfghdsociscigsdychudhgvuiscjsdhcjdoicsdchjodscjdsockdspocjdsucjsdygvfivdspcosachdjslkcjxc;oudsdsvhcdshvsicjsxcjsdoicjds",             }                  ] }

The only problem i am having is, all three nodes return the same value in the otherreldivisions property, I couldn't understand the problem. (The otherreldivisions property is a multi valued property).
I need an immediate help guys.
Please help me

Edit by ‌: Use syntax highlighter for source and output (did not "pretty print" format - that exercise is left to the original poster)

6 Replies
Highlighted
Master

Re: How to write webscript to get multi value properties

The reason is that you are setting and accessing the otherreldivisions differently than all the other properties.

Inside of your constructor for the CpsDocumentInfo you are setting model.otherreldivisions instead of this.otherreldivisions, and in the FreeMarker you are accessing otherreldivisions again from the global variable context, instead of the cpsDoc. Because of that you will only ever show the otherreldivisions of the last node processed in the JavaScript code.

Highlighted
Active Member II

Re: How to write webscript to get multi value properties

Thank for replying me Axel.

I have tried in various ways, but it gave me some errors. Please can you help me correct my code. Because i am completely new to free marker. 

Highlighted
Active Member II

Re: How to write webscript to get multi value properties

otherreldivisions  is a multi valued property, so that's why i am accessing it using a different way than others. 

Highlighted
Master

Re: How to write webscript to get multi value properties

I already provided all relevant hints. Switch from model.otherreldivisions to this.otherreldivisions in your JavaScript, and use cpsDoc.otherreldivisions instead of just otherreldivisions in your FreeMarker file. You are already dealing with the multi-valued properties correctly otherwise...

Highlighted
Active Member II

Re: How to write webscript to get multi value properties

Thank you Axel.

I modified the code as you said & it works perfectly. I need to learn more about free marker. Can you plz suggest good resource to me?

Highlighted
Master

Re: How to write webscript to get multi value properties

All I ever used was the manual. Bear in mind though that Alfresco uses FreeMarker 2.3.20 (in Alfresco 5.1/5.2), not the most current version documented there.