AnsweredAssumed Answered

How to write webscript to get multi value properties

Question asked by anuradha@1994 on Jan 20, 2018
Latest reply on Jan 22, 2018 by afaust

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 Axel Faust: Use syntax highlighter for source and output (did not "pretty print" format - that exercise is left to the original poster)

Outcomes