McpServices.mcp.ExecuteScreenAction.xml
6.66 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
<?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, 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.
-->
<services xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://moqui.org/xsd/service-3.xsd">
<service verb="mcp" noun="ExecuteScreenAction" authenticate="true" allow-remote="true" transaction-timeout="60">
<description>Execute a screen action (transition or CRUD convention) with parameters.</description>
<in-parameters>
<parameter name="path" required="true"/>
<parameter name="action" required="true"/>
<parameter name="parameters" type="Map"/>
</in-parameters>
<out-parameters>
<parameter name="result" type="Map"/>
</out-parameters>
<actions>
<script><![CDATA[
import org.moqui.context.ExecutionContext
ExecutionContext ec = context.ec
def actionResult = null
def actionError = null
def actionParams = parameters ?: [:]
try {
// First, resolve the screen path
def resolveResult = ec.service.sync().name("McpServices.mcp#ResolveScreenPath")
.parameter("path", path).call()
def screenPath = resolveResult.screenPath
def screenDef = resolveResult.screenDef
if (!screenDef) {
actionError = "Could not resolve screen for path '${path}'"
result = [actionResult: actionResult, actionError: actionError]
return
}
if (action == "submit") {
// Special handling for form submit - just acknowledge receipt
actionResult = [
action: "submit",
status: "success",
message: "Form parameters submitted",
parametersProcessed: actionParams.keySet()
]
} else {
// Check if action matches CRUD convention for ProductPrice
def actionPrefix = action?.take(6)
if (actionPrefix && actionPrefix in ['update', 'create', 'delete']) {
// Convention: updateProductPrice -> update#mantle.product.ProductPrice
def serviceName = "${actionPrefix}#mantle.product.ProductPrice"
ec.logger.info("ExecuteScreenAction: Calling service by convention: ${serviceName}")
try {
def serviceCallResult = ec.service.sync().name(serviceName).parameters(actionParams).call()
actionResult = [
action: action,
status: "executed",
message: "Executed service ${serviceName}",
result: serviceCallResult
]
} catch (Exception e) {
actionError = "Service call failed: ${e.message}"
}
} else {
// Look for matching transition on screen
def foundTransition = null
def allTransitions = screenDef.getAllTransitions()
if (allTransitions) {
for (def transition : allTransitions) {
if (transition.getName() == action) {
foundTransition = transition
break
}
}
}
if (foundTransition) {
// Try to find and execute the transition's service
def serviceName = null
if (foundTransition.xmlTransition) {
def serviceCallNode = foundTransition.xmlTransition.first("service-call")
if (serviceCallNode) serviceName = serviceCallNode.attribute("name")
}
if (serviceName) {
ec.logger.info("ExecuteScreenAction: Executing transition '${action}' service: ${serviceName}")
try {
def serviceCallResult = ec.service.sync().name(serviceName).parameters(actionParams).call()
actionResult = [
action: action,
status: "executed",
message: "Executed service ${serviceName}",
result: serviceCallResult
]
} catch (Exception e) {
actionError = "Service call failed: ${e.message}"
}
} else {
actionResult = [
action: action,
status: "success",
message: "Transition '${action}' ready (no direct service)"
]
}
} else {
actionError = "Transition '${action}' not found on screen"
}
}
}
} catch (Exception e) {
actionError = "Action execution failed: ${e.message}"
ec.logger.warn("ExecuteScreenAction error: ${e.message}")
}
result = [actionResult: actionResult, actionError: actionError]
]]></script>
</actions>
</service>
</services>