cancel
Showing results for 
Search instead for 
Did you mean: 

Freemarker scripts... how to detect missing properties?

theorbix
Confirmed Champ
Confirmed Champ
I'm writing a custom Web Script that returns the result of a query in JSON format.

This is the Freemarker script I use to generate the JSON output:


<#assign x = 0>
{"recordsReturned":${search.itemsPerPage},
       "totalRecords":${search.totalResults},
       "startIndex":0,
       "sort":null,
       "dir":"asc",
       "records":[
      
<#list search.results as row>         
 
           {"id":"${row.id}",
           "name":"${row.name}",
           "icon":"${row.icon16}",
           "urlcontext":"${url.context}",
           "description":"${row.properties.description}",
           "author":"${row.properties.creator}"
           }
<#assign x = x+ 1>
<#if x != search.itemsPerPage>
,
</#if>

</#list>
       ]
}

The problem is that sometimes the documents resulting from the search do not contain the description property (I don't know why…. this should be a default property of all documents contained in Alfresco), and this causes an exception in the execution of the Freemarker script:

freemarker.core.InvalidReferenceException - Expression row.properties.description is undefined on line 15, column 34 in org/alfresco/repository/search/keywordsearch.get.json.ftl. 

How can I avoid this error? Is there a way, in a Freemarker script, to check if a note property exist?
1 ACCEPTED ANSWER

theorbix
Confirmed Champ
Confirmed Champ
Yep! One minute after posting, I found the correct syntax in another .ftl sample script:


            <#if row.properties.description?? == true>
           "description":"${row.properties.description}",
           </#if>

Sometimes I should RTFM… Smiley Happy

View answer in original post

6 REPLIES 6

pmonks
Star Contributor
Star Contributor
Freemarker includes a "missing value test operator" that probably does what you're after - it's described at http://freemarker.sourceforge.net/docs/dgui_template_exp.html#dgui_template_exp_missing_test.

If you're doing any significant amount of Freemarker scripting, you should keep the (superb!) Freemarker reference documentation close by - it lives at http://freemarker.sourceforge.net/docs/.

Cheers,
Peter

theorbix
Confirmed Champ
Confirmed Champ
Yep! One minute after posting, I found the correct syntax in another .ftl sample script:


            <#if row.properties.description?? == true>
           "description":"${row.properties.description}",
           </#if>

Sometimes I should RTFM… Smiley Happy

pmonks
Star Contributor
Star Contributor
Sometimes I should RTFM… Smiley Happy
Particularly the Freemarker manual - it's one of the best examples of open source documentation I've ever seen!   :wink:

Cheers,
Peter

mikef
Champ in-the-making
Champ in-the-making
You can also use this:

${myVar?default('default text')}

to output a variable if it exists, or a specified default value if it doesn’t. Saves on using lots of 'if/else’ statements.

mikeh
Star Contributor
Star Contributor

satishvarmadand
Champ in-the-making
Champ in-the-making
Hi,
  I am getting stuck with freemarker templates. I am using opensearch and i can successfully do a search. But the searcg results contain default metadata like description, author etc …

<name>${row.properties.creator}</name>

So how can i get the complete meta-data set for a node. How can i find out all the row properties?

I actually extended the default metadata, so i would like get back the new extended metadata values like category (my custom meta-data) in the open search results.

Is this possible? what values should i need to update in the ftl template - "keywordsearch.get.atom.ftl"

can anyone help me

Thanks