moqui / moqui-framework

Use Moqui Framework to build enterprise applications based on Java. It includes tools for databases (relational, graph, document), local and web services, web and other UI with screens and forms, security, file/resource access, scripts, templates, l10n, caching, logging, search, rules, workflow, multi-instance, and integration.
http://www.moqui.org
Other
279 stars 200 forks source link

Possible Fix for Template render error when using entity-find-count with having-econditions #579

Open gurveenbagga opened 1 year ago

gurveenbagga commented 1 year ago

When using having-econditions with entity-find-count, encountered below error

06:48:40.952 ERROR   MoquiJob-1      o.moqui.i.c.r.FtlTemplateRenderer Error from FTL in render
freemarker.core.NonHashException: For "...[...]" left-hand operand: Expected a hash, but this has evaluated to a sequence (wrapper: org.moqui.util.MNode$FtlNodeListWrapper):
==> .node["having-econditions"]  [in template "classpath://template/XmlActions.groovy.ftl" at line 212, column 65]

----
FTL stack trace ("~" means nesting-related):
        - Failed at: #list .node["having-econditions"]["*"...  [in template "classpath://template/XmlActions.groovy.ftl" in macro "entity-find-count" at line 212, column 58]
        - Reached through: #recurse  [in template "classpath://template/XmlActions.groovy.ftl" in macro "actions" at line 22, column 1]
        - Reached through: #visit xmlActionsRoot  [in template "classpath://template/XmlActions.groovy.ftl" at line 19, column 1]
----
        at org.moqui.impl.actions.XmlAction.getGroovyString(XmlAction.java:119) ~[moqui_temp2194216266458823044WEB-INF_lib_moqui-framework-3.1.0-rc1.jar.:3.1.0-rc1]
        at org.moqui.impl.actions.XmlAction.writeGroovyWithLines(XmlAction.java:85) ~[moqui_temp2194216266458823044WEB-INF_lib_moqui-framework-3.1.0-rc1.jar.:3.1.0-rc1]
        at org.moqui.impl.actions.XmlAction.makeGroovyClass(XmlAction.java:104) ~[moqui_temp2194216266458823044WEB-INF_lib_moqui-framework-3.1.0-rc1.jar.:3.1.0-rc1]
        at org.moqui.impl.actions.XmlAction.getGroovyClass(XmlAction.java:94) ~[moqui_temp2194216266458823044WEB-INF_lib_moqui-framework-3.1.0-rc1.jar.:3.1.0-rc1]
        at org.moqui.impl.actions.XmlAction.run(XmlAction.java:61) ~[moqui_temp2194216266458823044WEB-INF_lib_moqui-framework-3.1.0-rc1.jar.:3.1.0-rc1]
        at org.moqui.impl.service.runner.InlineServiceRunner.runService(InlineServiceRunner.java:59) ~[moqui_temp2194216266458823044WEB-INF_lib_moqui-framework-3.1.0-rc1.jar.:3.1.0-rc1]
        at org.moqui.impl.service.ServiceCallSyncImpl.callSingle(ServiceCallSyncImpl.java:322) ~[moqui_temp2194216266458823044WEB-INF_lib_moqui-framework-3.1.0-rc1.jar.:3.1.0-rc1]
        at org.moqui.impl.service.ServiceCallSyncImpl.call(ServiceCallSyncImpl.java:125) ~[moqui_temp2194216266458823044WEB-INF_lib_moqui-framework-3.1.0-rc1.jar.:3.1.0-rc1]
        at org.moqui.impl.service.ServiceCallJobImpl$ServiceJobCallable.call(ServiceCallJobImpl.groovy:244) ~[moqui_temp2194216266458823044WEB-INF_lib_moqui-framework-3.1.0-rc1.jar.:3.1.0-rc1]
        at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) ~[?:?]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) ~[?:?]
        at java.lang.Thread.run(Thread.java:829) ~[?:?]

Possible Fix

While exploring the similar behavior and analysing the XmlActions.groovy.ftl for the use of having-econditions in entity-find, replacing the code of line [1]

<#t><#if .node["having-econditions"]?has_content><#list .node["having-econditions"]["*"] as havingCond>.havingCondition(<#visit havingCond/>)</#list></#if>

with

<#t><#if .node["having-econditions"]?has_content><#list .node["having-econditions"][0]?children as havingCond>.havingCondition(<#visit havingCond/>)</#list></#if>

worked and produced the desired result.

I would like to suggest this change in XmlActions.groovy.ftl. If this looks okay, can I add this an improvement PR?

[1] https://github.com/moqui/moqui-framework/blob/master/framework/template/XmlActions.groovy.ftl#L214