10-23-2019 12:38 PM
Hello.
I`m trying to make a webscript which will be able to:
1. receive json request;
2. get and transform argument from json request to java format;
3. find node in a specific folder using json argument as a search request;
4. get properties from found node;
5. transform found properties back to json string;
6. send answer in json;
Example of json request that I want to process
$postData = array( 'didNumber' => '0443334000', 'srcNumber' => '0670219424', 'companyID' => '3041', 'requestType' => 'gettingCallSettings' );
I`ve created next files after reading about json processing in Alfresco:
.get.desc.xml
<webscript> <shortname>negotiate</shortname> <description>negotiate JSON</description> <url>/negotiate</url> <authentication>guest</authentication> <negotiate accept="application/json">json</negotiate> </webscript>
.get.json.ftl
{"response": "myObjAsString"}
.get.js
var tel = json.get("srcNumber"); var node = search.luceneSearch("@cm\\:name:\"" + nodeName+ "\" AND PARENT:\"workspace://SpacesStore/61d28f13-24e0-434a-8b90-5ea54d2fafa2\""); var name = node.properties["zkz:Name"]; var myObj = {'name':'name'}; var myObjAsString = jsonUtils.toJSONString(myObj); model.myObjAsString = myObjAsString;
But every time I try to execute my srcipt I receive something like this:
{ "status" : { "code" : 500, "name" : "Internal Error", "description" : "An error inside the HTTP server which prevented it from fulfilling the request." }, "message" : "09220628 Cannot locate template processor for template org\/alfresco\/negotiate.get.json", "exception" : "org.springframework.extensions.webscripts.WebScriptException - 09220628 Cannot locate template processor for template org\/alfresco\/negotiate.get.json",
or if I clean all data from .get.js I receive text from .ftl file.
In the end I need to send back something like this:
die(json_encode(array( 'customerData' => array( 'name' => '1111111', 'description' => '11111111111', ) )));
Please, can someone help me with js file? Can it be on Java or it must be in Json format?
10-23-2019 04:25 PM
jsonUtils.toJSONString should not be used - it does not fully support all native JS types - instead return the JS object as part of the model, and use FTL rendering to construct a proper JSON object structure.
search.luceneSearch should not be used - Lucene is legacy, only supported for unit / integration tests, does not receive any performance improvements etc.
You are missing a format element in the desc.xml to specify that the default response type should be JSON and thus the JSON FTL be used.
10-27-2019 09:11 AM
Thank you for you advice!
I made some changes using this tutorials https://docs.alfresco.com/5.2/references/dev-extension-points-webscripts.html
It works but now I need to use another answer format and can't change *.ftl file.
In tutorial it was:
<#assign datetimeformat="EEE, dd MMM yyyy HH:mm:ss zzz"> { "acmeDocs" : [ <#list acmeDocs as acmeDoc> { "name" : "${acmeDoc.name}", "creator" : "${acmeDoc.creator}", "createdDate" : "${acmeDoc.createdDate?string(datetimeformat)}", "modifier" : "${acmeDoc.modifier}", "modifiedDate" : "${acmeDoc.modifiedDate?string(datetimeformat)}", "docId" : "${acmeDoc.docId!"Unknown"}", "securityClass" : "${acmeDoc.securityClassification!"Unknown"}" } <#if acmeDoc_has_next>,</#if> </#list> ] }
Ad it gives ne answer:
{ "acmeDocs" : [ { "name" : "weqweqweqwrerwerwerweqwe", "creator" : "i.gerts", "createdDate" : "Sun, 27 Oct 2019 14:50:46 EET", "modifier" : "i.gerts", "modifiedDate" : "Sun, 27 Oct 2019 14:50:49 EET", "docId" : "Unknown", "securityClass" : "Unknown" } ] }
I tried to change it to my format:
die(json_encode(array( 'customerData' => array( 'name' => '${acmeDoc.name}', 'description' => '${acmeDoc.creator}', ) )));
But it gives me 500 error
what I need to change in FTL?
10-27-2019 12:47 PM
Unless you provide any details on your 500 error, little help can be provided.
I am not sure what "your format" actually is - are you trying to generate some weird JSONP response content? The example with the "die" / "json_encode" functions really does not do anything to clear that up. Do you want to generate a response that would look similar to the result of these function calls? Then what actually does the "die" function generate? (I am not aware by what framework / language that function is provided and would not know what it does)
10-28-2019 08:38 AM
Hi there. I have had the same issue. Thanks for the information
11-10-2019 05:47 PM
Hi!
I have a little progress with it.
Now my files is:
acme-documents.get.desc.xml
<webscript> <shortname>Search ACME Documents</shortname> <description>Returns metadata as JSON for all ACME documents in the repository that matches search keyword</description> <url>/tutorial/acmedocs?q={keyword}</url> <authentication>user</authentication> <format default="json">argument</format> <family>Alfresco Tutorials</family> </webscript>
acme-documents.get.js
function AcmeDocumentInfo(doc) { this.name = doc.properties["kezkz:Name"]; this.linkToCrmUrl = doc.properties["kezkz:Email"]; this.linkToCrmTitle = "Go to Alfresco"; this.assignedToEmployeeNumber = doc.properties["kezkz:Napravl"]; } function main() { var searchKeyword = json.get('srcNumber'); if (searchKeyword == null || searchKeyword.length == 0) { searchKeyword = ""; } var sort1 = { column: "@{http://www.alfresco.org/model/content/1.0}modified", ascending: false }; var sort2 = { column: "@{http://www.alfresco.org/model/content/1.0}created", ascending: false }; var paging = { maxItems: 1, skipCount: 0 }; var def = { query: "kezkz:Telefone:searchKeyword", store: "workspace://SpacesStore", language: "fts-alfresco", sort: [sort1, sort2], page: paging, }; var acmeDocNodes = search.query(def); if (acmeDocNodes == null || acmeDocNodes.length == 0) { status.code = 404; status.message = searchKeyword; status.redirect = true; } else { var acmeDocInfos = new Array(); for (i = 0; i < acmeDocNodes.length; i++) { acmeDocInfos[i] = new AcmeDocumentInfo(acmeDocNodes[i]); } model.acmeDocs = acmeDocInfos; return model; } } main();
acme-documents.get.json.ftl
{ "customerData" : <#list acmeDocs as acmeDoc> { "name" : "${acmeDoc.name}", "linkToCrmUrl" : "${acmeDoc.linkToCrmUrl}", "linkToCrmTitle" : "Go to CRM", "assignedToEmployeeNumber" : "${acmeDoc.assignedToEmployeeNumber}", } </#list> }
It works when I set manualy search query and I can find nodes in folder and receive answer, but I still can not get args from json request use them as a searchKeyword and put to a query string.
When I use json.get('srcNumber')
I receive an error "JSON is not defined"
And even if I try to set "searchKeyword" manualy I can not put this var to my search...
This part of my code: query: "kezkz:Telefone:searchKeyword",
tries to find text "searchKeyword" but not the text I defined for "var searchKeyword" earlier.
What I do wrong?
11-10-2019 05:53 PM
srcNumber - is a argument name in my GET request, which I send to Alfresco.
In the end I need to:
1. Take Tel number from GET request
2. Search datalist item by this tel number
3. Send back same data list item properties in a json format
Explore our Alfresco products with the links below. Use labels to filter content by product module.