cancel
Showing results for 
Search instead for 
Did you mean: 

JSON - Can't convert the date to string

bengrah
Champ on-the-rise
Champ on-the-rise
Hi all,

This is what I suspect, a very easy issue to solve. I just can't see what I'm doing wrong here.

I've got a webscript which has some JSON on the front end:

<#assign loopNumber = 0 />

<#escape x as jsonUtils.encodeJSONString(x)>
{
   "recentDocs": [
      <#list results as recentDocItem>
         {
            "document-name":"${recentDocItem.properties["cm:name"]}",
            "document-location":"${recentDocItem.displayPath}",
            "document-modified":"${recentDocItem.properties["cm:modified"]?datetime}"
         <#if loopNumber < 9>
         },
         <#else>
         }
         </#if>
         
         <#assign loopNumber = loopNumber + 1 />
      </#list>
   ]
}
</#escape>

I'm becoming unstuck when getting the date:
"document-modified":"${recentDocItem.properties["cm:modified"]?datetime}"

I just want to get the date modified of a node and display it in one of my JSON values, but every time I do I get the following error:

 org.springframework.extensions.webscripts.WebScriptException: 10120061 Wrapped Exception (with status template): 10122344 Error during processing of the template 'Can't convert the date to string, because it is not known which parts of the date variable are in use. Use ?date, ?time or ?datetime built-in, or ?string.<format> or ?string(format) built-in with this date.'. Please contact your system administrator.
   at org.springframework.extensions.webscripts.AbstractWebScript.createStatusException(AbstractWebScript.java:1047)
   at org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:171)
   at org.alfresco.repo.web.scripts.RepositoryContainer$2.execute(RepositoryContainer.java:400)
   at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:388)
   at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecute(RepositoryContainer.java:462)
   at org.alfresco.repo.web.scripts.RepositoryContainer.transactionedExecuteAs(RepositoryContainer.java:500)
   at org.alfresco.repo.web.scripts.RepositoryContainer.executeScript(RepositoryContainer.java:316)
   at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:372)
   at org.springframework.extensions.webscripts.AbstractRuntime.executeScript(AbstractRuntime.java:209)
   at org.springframework.extensions.webscripts.servlet.WebScriptServlet.service(WebScriptServlet.java:118)
   at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.alfresco.web.app.servlet.GlobalLocalizationFilter.doFilter(GlobalLocalizationFilter.java:58)
   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:470)
   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
   at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861)
   at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
   at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584)
   at java.lang.Thread.run(Thread.java:662)
Caused by: org.alfresco.service.cmr.repository.TemplateException: 10122344 Error during processing of the template 'Can't convert the date to string, because it is not known which parts of the date variable are in use. Use ?date, ?time or ?datetime built-in, or ?string.<format> or ?string(format) built-in with this date.'. Please contact your system administrator.
   at org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:206)
   at org.springframework.extensions.webscripts.AbstractWebScript.renderTemplate(AbstractWebScript.java:876)
   at org.springframework.extensions.webscripts.DeclarativeWebScript.renderFormatTemplate(DeclarativeWebScript.java:267)
   at org.springframework.extensions.webscripts.DeclarativeWebScript.execute(DeclarativeWebScript.java:147)
   … 25 more
Caused by: freemarker.template.TemplateModelException: Can't convert the date to string, because it is not known which parts of the date variable are in use. Use ?date, ?time or ?datetime built-in, or ?string.<format> or ?string(format) built-in with this date.
   at freemarker.core.Environment.formatDate(Environment.java:776)
   at freemarker.core.Expression.getStringValue(Expression.java:105)
   at freemarker.core.Expression.getStringValue(Expression.java:93)
   at freemarker.core.DollarVariable.accept(DollarVariable.java:76)
   at freemarker.core.Environment.visit(Environment.java:221)
   at freemarker.core.MixedContent.accept(MixedContent.java:92)
   at freemarker.core.Environment.visit(Environment.java:221)
   at freemarker.core.IteratorBlock$Context.runLoop(IteratorBlock.java:179)
   at freemarker.core.Environment.visit(Environment.java:428)
   at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
   at freemarker.core.Environment.visit(Environment.java:221)
   at freemarker.core.MixedContent.accept(MixedContent.java:92)
   at freemarker.core.Environment.visit(Environment.java:221)
   at freemarker.core.EscapeBlock.accept(EscapeBlock.java:84)
   at freemarker.core.Environment.visit(Environment.java:221)
   at freemarker.core.MixedContent.accept(MixedContent.java:92)
   at freemarker.core.Environment.visit(Environment.java:221)
   at freemarker.core.Environment.process(Environment.java:199)
   at org.alfresco.repo.template.FreeMarkerProcessor.process(FreeMarkerProcessor.java:202)
   … 28 more



Any ideas?

Thanks,
Ben.
7 REPLIES 7

abarisone
Star Contributor
Star Contributor
Hi,
as reported by the error try this:
${recentDocItem.properties["cm:modified"]?string("dd-MM-yyyy HH:mm:ss")}
This allows you to format your dates according to your needs.

Regards,
Andrea

bengrah
Champ on-the-rise
Champ on-the-rise
HI abarisone

Thanks for that, that worked a treat! Why do the regular built-ins like ?datetime etc. not working in this instance? They usually work in HTML templates?

Thanks,
Ben.

abarisone
Star Contributor
Star Contributor
Hi,
for more info about Freemarker's built-ins for dates take a look here http://freemarker.sourceforge.net/docs/ref_builtins_date.html

Regards,
Andrea

muralidharand
Star Contributor
Star Contributor
Hi Patrick,
Is it working now?
Are you changing directly,  ftl, lib.js and min.js files?
I would not recommend this. Please use the extension module to extend it; Otherwise, when you're upgrading to newer version is a pain for you.

muralidharand
Star Contributor
Star Contributor
Your custom date is a date object or a string object?
http://freemarker.sourceforge.net/docs/ref_builtins_string.html#ref_builtin_string_date

Extension module : http://docs.alfresco.com/4.0/index.jsp?topic=%2Fcom.alfresco.enterprise.doc%2Ftasks%2Ftu-share-JS-cu...
For the client javascript,  you can extend it or override only the required function.

One of my customization, I've overridden onCreateNewItem of ObjectRenderer class.

Alfresco.ObjectRenderer.prototype.onCreateNewItem =  function ObjectRenderer_onCreateNewItem()
{
}


Hope this helps you !

muralidharand
Star Contributor
Star Contributor
How the date object is represented in  your java class?
Are you able to  print the date in the FTL AS IS ( i.e without any type conversion / formatting)?
For testing purpose, is it possible for you to change the date datatype to string/ text and check it out once.

muralidharand
Star Contributor
Star Contributor
Hi Patrick,
Ok, I understood. You've used the node properties to fill the custom  dates.
My hunch is that
 node.properties["gha:RapportPublicatieDatum"] 
returning null.
So, Just add  null check  to the 
 node.properties["gha:RapportPublicatieDatum"] 

( I've not tested the syntax or the code)
if  (node.properties["gha:RapportPublicatieDatum"] != null && node.properties["gha:RapportPublicatieDatum"] != 'undefined')
{
    publicatiedatumRapport: node.properties["gha:RapportPublicatieDatum"]
}
else
{
   publicatiedatumRapport: new Date()
}

Or simply hard-code the date in search.lib.js to check.

Hope this will help you !