MjmlServices.xml 9.62 KB
<?xml version="1.0" encoding="UTF-8"?>
<!--
This software is in the public domain under CC0 1.0 Universal plus a 
Grant of Patent License.

To the extent possible under law, the author(s) have dedicated all
copyright and related and neighboring rights to this software to the
public domain worldwide. This software is distributed without any
warranty.

You should have received a copy of the CC0 Public Domain Dedication
along with this software (see the LICENSE.md file). If not, see
<http://creativecommons.org/publicdomain/zero/1.0/>.
-->
<services xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://moqui.org/xsd/service-definition-3.xsd">

    <service verb="load" noun="GrapeJs">
        <description>Load GrapesJs resource. Can be adapted to entity other than EmailTemplate, but must have data for the grapesLocation and htmlLocation to ensure safety of read and write of resources.</description>
        <in-parameters>
            <parameter name="grapesLocation"/>
            <parameter name="htmlLocation"/>
            <parameter name="emailTemplateId" required="true"/>
        </in-parameters>
        <out-parameters>
            <parameter name="grapesLocation"/>
            <parameter name="htmlLocation"/>
            <parameter name="data"/>
        </out-parameters>
        <actions>
            <if condition="grapesLocation == 'null'"><set field="grapesLocation" from="null"/></if>
            <if condition="htmlLocation == 'null'"><set field="htmlLocation" from="null"/></if>
<!--            <log level="warn" message="resourceId is ${resourceId} resourceId.getClass().getName() ${resourceId.getClass().getName()} resourceId == 'null' ${resourceId == 'null'} resourceId == null ${resourceId == null}"/>-->
<!--            <log level="warn" message="load context.toString() ${context.toString()}"/>-->
            <entity-find-one entity-name="moqui.basic.email.EmailTemplate" value-field="emailTemplate" auto-field-map="[emailTemplateId:emailTemplateId]"/>
            <if condition="!emailTemplate"><return error="true" message="Resource not found"/></if>
            <set field="grapesLocation" from="emailTemplate?.grapesLocation"/>
            <set field="htmlLocation" from="emailTemplate?.htmlLocation"/>

            <set field="grapesJsResource" from="ec.resource.getLocationReference('dbresource://grapesjs/project')"/>
            <if condition="!grapesLocation &amp;&amp; !htmlLocation">
                <then>
                    <set field="htmlFile" from="grapesJsResource.makeFile(java.util.UUID.randomUUID().toString()+'.html')"/>
                    <set field="grapesFile" from="grapesJsResource.makeFile(java.util.UUID.randomUUID().toString()+'.json')"/>

                    <set field="data" from="ec.resource.getLocationReference('dbresource://grapesjs/template/default.json').getText()"/>
                    <set field="htmlLocation" from="grapesJsResource.location + '/' + htmlFile?.dbResourceId + '.html'"/>
                    <set field="grapesLocation" from="grapesJsResource.location + '/' + grapesFile?.dbResourceId + '.json'"/>
                    <script><![CDATA[
                        htmlFile.move(htmlLocation)
                        grapesFile.putText(data)
                        grapesFile.move(grapesLocation)
                        ]]></script>

                    <service-call name="update#moqui.basic.email.EmailTemplate" in-map="[emailTemplateId:emailTemplateId,grapesLocation:grapesLocation,htmlLocation:htmlLocation]"/>
                </then>
                <else-if condition="grapesLocation &amp;&amp; htmlLocation">
                    <set field="putDbResource" from="ec.resource.getLocationReference(grapesLocation)"/>
                    <!-- TODO: Is this a strong enough check to prevent unauthorized access? -->
                    <if condition="!putDbResource || putDbResource.parent?.location != grapesJsResource.location">
                        <return error="true" message="Resource not found"/>
                    </if>

                    <set field="data" from="putDbResource.getText()"/>
                </else-if>
                <else>
                    <return error="true" message="Resource not found"/>
                </else>
            </if>
        </actions>
    </service>
    <service verb="store" noun="GrapeJs" authenticate="anonymous-all">
        <description>Store GrapesJs resource. Can be adapted to entity other than EmailTemplate, but must have data for the grapesLocation and htmlLocation to ensure safety of read and write of resources.</description>
        <in-parameters>
            <parameter name="htmlLocation"/>
            <parameter name="grapesLocation"/>
            <parameter name="emailTemplateId"/>
            <parameter name="data"/>
            <parameter name="html" allow-html="any"/>
        </in-parameters>
        <out-parameters>
            <parameter name="htmlLocation"/>
            <parameter name="grapesLocation"/>
        </out-parameters>
        <actions>
            <if condition="htmlLocation == 'null'"><set field="htmlLocation" from="null"/></if>
            <if condition="grapesLocation == 'null'"><set field="grapesLocation" from="null"/></if>
            <entity-find-one entity-name="moqui.basic.email.EmailTemplate" value-field="emailTemplate" auto-field-map="[emailTemplateId:emailTemplateId]"/>
            <if condition="!emailTemplate"><return error="true" message="Resource not found"/></if>
            <set field="grapesLocation" from="emailTemplate?.grapesLocation"/>
            <set field="htmlLocation" from="emailTemplate?.htmlLocation"/>
            <set field="data" from="groovy.json.JsonOutput.toJson(new groovy.json.JsonSlurper().parseText(ec.web.secureRequestParameters._requestBodyText).data)"/>

            <set field="grapesJsResource" from="ec.resource.getLocationReference('dbresource://grapesjs/project')"/>
            <if condition="!htmlLocation &amp;&amp; !grapesLocation">
                <then>
<!--                TODO: This should work, but isn't used anywhere and is untested.
                    <set field="htmlFile" from="grapesJsResource.makeFile(java.util.UUID.randomUUID().toString()+'.html')"/>
                    <set field="grapesFile" from="grapesJsResource.makeFile(java.util.UUID.randomUUID().toString()+'.json')"/>

                    <set field="htmlLocation" from="grapesJsResource.location + '/' + htmlFile?.dbResourceId + '.html'"/>
                    <set field="grapesLocation" from="grapesJsResource.location + '/' + grapesFile?.dbResourceId + '.json'"/>
                    <if condition="ec.resource.getLocationReference(htmlLocation).parent?.location != grapesJsResource.location">
                        <return error="true" message="Resource not found"/>
                    </if>
                    <if condition="ec.resource.getLocationReference(grapesLocation).parent?.location != grapesJsResource.location">
                        <return error="true" message="Resource not found"/>
                    </if>
                    <script><![CDATA[
                        htmlFile.putText(html)
                        htmlFile.move(htmlLocation)
                        grapesFile.putText(data)
                        grapesFile.move(grapesLocation)
                        ]]></script>-->
                    <return error="true" message="Resource not found"/>
                </then>
                <else-if condition="grapesLocation">
                    <set field="grapesFile" from="ec.resource.getLocationReference(grapesLocation)"/>
                    <if condition="!grapesFile || grapesFile.parent?.location != grapesJsResource.location">
                        <return error="true" message="Resource not found"/>
                    </if>
                    <script>grapesFile.putText(data)</script>

                    <set field="htmlFile" from="null"/>
                    <if condition="htmlLocation"><then>
                        <set field="htmlFile" from="ec.resource.getLocationReference(htmlLocation)"/>
                    </then><else>
                        <entity-find entity-name="moqui.basic.email.EmailTemplate" list="emailTemplateList">
                            <econdition field-name="grapesLocation"/>
                            <order-by field-name="-lastUpdatedStamp"/></entity-find>
                        <set field="emailTemplate" from="emailTemplateList.getFirst()"/>
                        <if condition="emailTemplateList.size() == 1 &amp;&amp; emailTemplate.htmlLocation"><then>
                            <set field="htmlFile" from="ec.resource.getLocationReference(emailTemplate.htmlLocation)"/>
                        </then><else>
                            <set field="htmlFile" from="grapesJsResource.makeFile(java.util.UUID.randomUUID().toString()+'.html')"/>
                            <set field="htmlLocation" from="grapesJsResource.location + '/' + htmlFile?.dbResourceId + '.html'"/>
                            <script><![CDATA[htmlFile.move(htmlLocation)]]></script>

                            <service-call name="update#moqui.basic.email.EmailTemplate" in-map="[emailTemplateId:emailTemplateId,htmlLocation:htmlLocation]"/>
                        </else></if>
                    </else></if>
                    <if condition="!htmlFile || htmlFile.parent?.location != grapesJsResource.location">
                        <return error="true" message="Resource not found"/>
                    </if>
                    <script><![CDATA[htmlFile.putText(html)]]></script>
                </else-if>
                <else>
                    <return error="true" message="Resource not found"/>
                </else>
            </if>
        </actions>
    </service>


</services>