MjmlServices.xml
26.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?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="get" noun="GlobalGrapeVarList">
<out-parameters>
<parameter name="baseLinkUrl"/>
<parameter name="currentYear"/>
</out-parameters>
<actions>
<set field="baseLinkUrl" from="!'production'.equals(System.getProperty('instance_purpose')) ? 'http://localhost:8080' : ec.web.getWebappRootUrl(true,true)" />
<set field="currentYear" from="ec.user.nowTimestamp.format('yyyy')"/>
</actions>
</service>
<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="grapesVersionName"/>
<parameter name="grapesPublishedVersionName"/>
<parameter name="htmlLocation"/>
<parameter name="htmlVersionName"/>
<parameter name="htmlPublishedVersionName"/>
<parameter name="data"/>
<parameter name="emailTemplateId"/>
<parameter name="moquiVars"/>
</out-parameters>
<actions>
<if condition="grapesLocation == 'null'"><set field="grapesLocation" from="null"/></if>
<if condition="htmlLocation == 'null'"><set field="htmlLocation" 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="grapesPublishedVersionName" from="emailTemplate?.grapesPublishedVersionName"/>
<set field="htmlLocation" from="emailTemplate?.htmlLocation"/>
<set field="htmlPublishedVersionName" from="emailTemplate?.htmlPublishedVersionName"/>
<set field="grapesJsResource" from="ec.resource.getLocationReference('dbresource://grapesjs/project')"/>
<if condition="!grapesLocation && !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')"/>
<service-call name="mjml.MjmlServices.get#GlobalGrapeVarList" out-map="context"/>
<set field="defaultLocation" value="dbresource://grapesjs/template/default.json"/>
<set field="dataRaw" from="ec.resource.getLocationReference(defaultLocation).getText()"/>
<if condition="!dataRaw"><log level="error" message="Template not found at ${defaultLocation}. Please load seed data for moqui-mjml."/></if>
<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(dataRaw)
grapesFile.move(grapesLocation)
]]></script>
<set field="htmlVersionName" from="htmlFile?.getCurrentVersion()?.getVersionName() ?: htmlFile?.getRootVersion()?.getVersionName() ?: '01'"/>
<set field="grapesVersionName" from="grapesFile?.getCurrentVersion()?.getVersionName() ?: grapesFile?.getRootVersion()?.getVersionName() ?: '01'"/>
<service-call name="update#moqui.basic.email.EmailTemplate" in-map="[emailTemplateId:emailTemplateId,grapesLocation:grapesLocation,htmlLocation:htmlLocation]"/>
<service-call name="mjml.MjmlServices.get#GlobalGrapeVarList" out-map="globalGrapeVarList"/>
<script><![CDATA[
context.putAll(globalGrapeVarList)
context.varList = globalGrapeVarList*.key.collect { '\\\$\\{'+it+'\\}' }
context.replaceVarRegexPattern = context.varList.join('|')
if (context.replaceVarRegexPattern) {
context.dataPre = context.dataRaw.replaceAll(context.replaceVarRegexPattern) { match ->
return "${match}"
}
}
context.allVarsRegexPattern = /\$\{[a-zA-Z_][a-zA-Z0-9_.]*\}/
context.errorVarList = context.dataRaw.findAll(context.allVarsRegexPattern).unique().collect { it - '${' - '}' } - globalGrapeVarList*.key
context.errorTemplateRegexPattern = errorVarList.collect { '\\\$\\{'+it+'\\}' }.join('|')
if (context.errorTemplateRegexPattern) {
context.dataPre = context.dataPre.replaceAll(context.errorTemplateRegexPattern) { match ->
def cleanMatch = match - '${' - '}'
return "\${" + cleanMatch + "!'\$\\{" + cleanMatch + "}'}"
}
}
]]></script>
<!-- <log level="warn" message="dataPre is ${dataPre}"/>-->
<script><![CDATA[
String location = defaultLocation;
freemarker.template.Template newTemplate;
Reader templateReader = null;
try {
templateReader = new StringReader(context.dataPre);
// Use the getFtlConfiguration method from ec.resource.templateRenderers.ftl
newTemplate = new freemarker.template.Template(location, templateReader, ec.resource.templateRenderers.ftl.getFtlConfiguration());
} catch (Exception e) {
throw new org.moqui.BaseArtifactException("Error while initializing template at " + location, e);
} finally {
if (templateReader != null) {
try { templateReader.close(); }
catch (Exception e) { logger.error("Error closing template reader", e); }
}
}
StringWriter sw = new StringWriter()
try {
newTemplate.createProcessingEnvironment(ec.contextStack, sw).process();
} catch (Exception e) { throw new org.moqui.BaseArtifactException("Error rendering template at " + location, e); }
context.data = sw.toString();
]]></script>
<!-- <log level="warn" message="data is ${data}"/>-->
<set field="moquiVars" from="globalGrapeVarList"/>
</then>
<else-if condition="grapesLocation && htmlLocation">
<set field="grapesFile" from="ec.resource.getLocationReference(grapesLocation)"/>
<!-- TODO: Is this a strong enough check to prevent unauthorized access? -->
<if condition="!grapesFile || grapesFile.parent?.location != grapesJsResource.location">
<return error="true" message="Resource not found"/>
</if>
<set field="grapesVersionName" from="grapesFile?.getCurrentVersion()?.getVersionName() ?: grapesFile?.getRootVersion()?.getVersionName() ?: '01'"/>
<set field="tempHtmlFile" from="ec.resource.getLocationReference(htmlLocation)"/>
<set field="htmlVersionName" from="tempHtmlFile?.getCurrentVersion()?.getVersionName() ?: tempHtmlFile?.getRootVersion()?.getVersionName() ?: '01'"/>
<set field="tempHtmlFile" from="null"/>
<service-call name="mjml.MjmlServices.get#GlobalGrapeVarList" out-map="globalGrapeVarList"/>
<set field="dataRaw" from="grapesFile.getText()"/>
<script><![CDATA[
context.putAll(globalGrapeVarList)
context.varList = globalGrapeVarList*.key.collect { '\\\$\\{'+it+'\\}' }
context.replaceVarRegexPattern = context.varList.join('|')
if (context.replaceVarRegexPattern) {
context.dataPre = context.dataRaw.replaceAll(context.replaceVarRegexPattern) { match ->
return "${match}"
}
}
context.allVarsRegexPattern = /\$\{[a-zA-Z_][a-zA-Z0-9_.]*\}/
context.errorVarList = context.dataRaw.findAll(context.allVarsRegexPattern).unique().collect { it - '${' - '}' } - globalGrapeVarList*.key
context.errorTemplateRegexPattern = errorVarList.collect { '\\\$\\{'+it+'\\}' }.join('|')
if (context.errorTemplateRegexPattern) {
context.dataPre = context.dataPre.replaceAll(context.errorTemplateRegexPattern) { match ->
def cleanMatch = match - '${' - '}'
return "\${" + cleanMatch + "!'\$\\{" + cleanMatch + "}'}"
}
}
]]></script>
<!-- <log level="warn" message="dataPre is ${dataPre}"/>-->
<script><![CDATA[
String location = grapesFile.location;
freemarker.template.Template newTemplate;
Reader templateReader = null;
try {
templateReader = new StringReader(context.dataPre);
// Use the getFtlConfiguration method from ec.resource.templateRenderers.ftl
newTemplate = new freemarker.template.Template(location, templateReader, ec.resource.templateRenderers.ftl.getFtlConfiguration());
} catch (Exception e) {
throw new org.moqui.BaseArtifactException("Error while initializing template at " + location, e);
} finally {
if (templateReader != null) {
try { templateReader.close(); }
catch (Exception e) { logger.error("Error closing template reader", e); }
}
}
StringWriter sw = new StringWriter()
try {
newTemplate.createProcessingEnvironment(ec.contextStack, sw).process();
} catch (Exception e) { throw new org.moqui.BaseArtifactException("Error rendering template at " + location, e); }
context.data = sw.toString();
]]></script>
<!-- <log level="warn" message="data is ${data}"/>-->
<set field="moquiVars" from="globalGrapeVarList"/>
</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" required="true"/>
<parameter name="data" required="true"/>
<parameter name="moquiVars" required="true"/>
<parameter name="html" allow-html="any" required="true"/>
<parameter name="publish" default="false" type="Boolean" required="true"/>
</in-parameters>
<out-parameters>
<parameter name="htmlLocation"/>
<parameter name="htmlVersionName"/>
<parameter name="htmlPublishedVersionName"/>
<parameter name="grapesLocation"/>
<parameter name="grapesVersionName"/>
<parameter name="grapesPublishedVersionName"/>
</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="grapesPublishedVersionName" from="emailTemplate?.grapesPublishedVersionName"/>
<set field="htmlLocation" from="emailTemplate?.htmlLocation"/>
<set field="htmlPublishedVersionName" from="emailTemplate?.htmlPublishedVersionName"/>
<set field="dataMap" from="new groovy.json.JsonSlurper().parseText(ec.web.secureRequestParameters._requestBodyText).data"/>
<!-- <log level="warn" message="dataMap is ${dataMap}"/>-->
<set field="moquiVars" from="new groovy.json.JsonSlurper().parseText(ec.web.secureRequestParameters._requestBodyText).moquiVars"/>
<set field="data" from="groovy.json.JsonOutput.toJson(dataMap)"/>
<set field="grapesJsResource" from="ec.resource.getLocationReference('dbresource://grapesjs/project')"/>
<if condition="!htmlLocation && !grapesLocation">
<then>
<!-- TODO: This should work as of 2024 May 14, but isn't used anywhere and is untested. There will need to be work done based on the dataMap code
<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><![CDATA[
context.replaceMoquiVarRegexPattern = context.moquiVars*.value.join('|')
if (context.replaceMoquiVarRegexPattern) {
context.dataPre = data.replaceAll(context.replaceMoquiVarRegexPattern) { match ->
def output = moquiVars.find { it.value == match }?.key
return '${' + output + '}'
}
}
]]></script>
<script>grapesFile.putText(dataPre)</script>
<set field="grapesVersionName" from="grapesFile?.getCurrentVersion()?.getVersionName()"/>
<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 && 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>
<set field="htmlVersionName" from="htmlFile?.getCurrentVersion()?.getVersionName()"/>
<if condition="publish">
<service-call name="update#moqui.basic.email.EmailTemplate" in-map="[emailTemplateId:emailTemplateId,
htmlPublishedVersionName:htmlVersionName,grapesPublishedVersionName:grapesVersionName]"/>
<set field="htmlPublishedVersionName" from="htmlVersionName"/>
<set field="grapesPublishedVersionName" from="grapesVersionName"/>
</if>
</else-if>
<else>
<return error="true" message="Resource not found"/>
</else>
</if>
</actions>
</service>
<service verb="get" noun="AfterMjmlStore">
<description>Get the data after storing the MJML data</description>
<in-parameters>
<parameter name="emailTemplateId" required="true"/>
</in-parameters>
<out-parameters>
<parameter name="htmlLocation"/>
<parameter name="htmlVersionName"/>
<parameter name="htmlPublishedVersionName"/>
<parameter name="grapesLocation"/>
<parameter name="grapesVersionName"/>
<parameter name="grapesPublishedVersionName"/>
</out-parameters>
<actions>
<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="grapesVersionName" from="ec.resource.getLocationReference(grapesLocation)?.getCurrentVersion()?.getVersionName()"/>
<set field="grapesPublishedVersionName" from="emailTemplate?.grapesPublishedVersionName"/>
<set field="htmlLocation" from="emailTemplate?.htmlLocation"/>
<set field="htmlVersionName" from="ec.resource.getLocationReference(htmlLocation)?.getCurrentVersion()?.getVersionName()"/>
<set field="htmlPublishedVersionName" from="emailTemplate?.htmlPublishedVersionName"/>
</actions>
</service>
<service verb="remove" noun="ExcessMjmlEmailResourceHistories" authenticate="anonymous-all">
<in-parameters>
<parameter name="runDate" default="Timestamp.from(java.time.Instant.now())" required="true" type="Timestamp"/>
</in-parameters>
<out-parameters>
<parameter name="removedHistoryList"/>
</out-parameters>
<actions>
<entity-find entity-name="moqui.basic.email.EmailTemplate" list="emailTemplateList">
<econdition field-name="grapesLocation" operator="is-not-null"/>
<econdition field-name="htmlLocation" operator="is-not-null"/>
<select-field field-name="emailTemplateId,grapesLocation,htmlLocation,grapesPublishedVersionName,htmlPublishedVersionName"/>
<order-by field-name="-lastUpdatedStamp"/>
</entity-find>
<set field="resourceList" from="[]"/>
<iterate list="emailTemplateList" entry="emailTemplate">
<set field="grapesFile" from="ec.resource.getLocationReference(emailTemplate.grapesLocation)"/>
<if condition="grapesFile">
<script><![CDATA[
def versionWhitelist = []
if (grapesFile?.getCurrentVersion()?.getVersionName()) versionWhitelist.add(grapesFile.getCurrentVersion().getVersionName())
if (emailTemplate?.grapesPublishedVersionName) versionWhitelist.add(emailTemplate.grapesPublishedVersionName)
resourceList.add([location:grapesFile.location,resourceId:grapesFile.dbResourceId,versionWhitelist:versionWhitelist.size()>0?versionWhitelist:null])]]></script>
</if>
<set field="htmlFile" from="ec.resource.getLocationReference(emailTemplate.htmlLocation)"/>
<if condition="htmlFile">
<script><![CDATA[
def versionWhitelist = []
if (htmlFile?.getCurrentVersion()?.getVersionName()) versionWhitelist.add(htmlFile.getCurrentVersion().getVersionName())
if (emailTemplate?.htmlPublishedVersionName) versionWhitelist.add(emailTemplate.htmlPublishedVersionName)
resourceList.add([location:htmlFile.location,resourceId:htmlFile.dbResourceId,versionWhitelist:versionWhitelist.size()>0?versionWhitelist:null])]]></script>
</if>
</iterate>
<set field="oneHourAgo" from="Timestamp.from(runDate.toInstant().minus(1, java.time.temporal.ChronoUnit.HOURS))"/>
<set field="removedHistoryList" from="[]"/>
<iterate list="resourceList" entry="resource">
<entity-find entity-name="moqui.resource.DbResourceFileHistory" list="resourceHistoryPreList">
<econdition field-name="resourceId" from="resource.resourceId"/>
<econdition field-name="versionDate" operator="greater" from="oneHourAgo"/>
<econdition field-name="versionDate" operator="less-equals" from="runDate"/>
<select-field field-name="resourceId,versionName,versionDate,previousVersionName,userId"/>
<order-by field-name="-versionDate"/>
</entity-find>
<set field="skippedUserIds" from="[]"/>
<set field="resourceHistoryList" from="[]"/>
<iterate list="resourceHistoryPreList" entry="resourceHistory">
<!-- Add all but the single latest history per userId to resourceHistoryList -->
<if condition="resource.versionWhitelist.contains(resourceHistory.versionName) || !skippedUserIds.contains(resourceHistory.userId)">
<!-- <log level="warn" message="${resource.resourceId} ${resourceHistory.versionName} userId ${resourceHistory.userId}"/>-->
<set field="skippedUserIds" from="skippedUserIds + [resourceHistory.userId]"/><continue/></if>
<set field="resourceHistoryList" from="resourceHistoryList + [resourceHistory]"/>
</iterate>
<iterate list="resourceHistoryList" entry="resourceHistory">
<entity-find-one entity-name="moqui.resource.DbResourceFileHistory" value-field="usedResourceHistory" auto-field-map="[resourceId:resource.resourceId,previousVersionName:resourceHistory.versionName]" for-update="true">
<select-field field-name="resourceId,versionName,versionDate,previousVersionName,userId"/></entity-find-one>
<!-- <log level="warn" message="set versionName ${usedResourceHistory.versionName} of previousVersionName ${usedResourceHistory.previousVersionName} to ${resourceHistory.previousVersionName}"/>-->
<if condition="usedResourceHistory">
<set field="usedResourceHistory.previousVersionName" from="resourceHistory?.previousVersionName"/>
<log level="info" message="Removed resource history ${resourceHistory.resourceId} version ${resourceHistory.versionName}"/>
<set field="removedHistoryList" from="removedHistoryList + [resourceId:resource.resourceId, versionName:resourceHistory.versionName,versionDate:resourceHistory.versionDate,userId:resourceHistory.userId]"/>
<entity-update value-field="usedResourceHistory"/>
<entity-delete value-field="resourceHistory"/>
</if>
</iterate>
</iterate>
</actions>
</service>
</services>