forked from mendixlabs/mxcli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_microflows_create.go
More file actions
276 lines (253 loc) · 9.88 KB
/
cmd_microflows_create.go
File metadata and controls
276 lines (253 loc) · 9.88 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
// SPDX-License-Identifier: Apache-2.0
// Package executor - CREATE MICROFLOW command
package executor
import (
"fmt"
"strings"
"github.com/mendixlabs/mxcli/mdl/ast"
mdlerrors "github.com/mendixlabs/mxcli/mdl/errors"
"github.com/mendixlabs/mxcli/mdl/types"
"github.com/mendixlabs/mxcli/model"
"github.com/mendixlabs/mxcli/sdk/microflows"
)
// isBuiltinModuleEntity returns true for modules whose entities are defined
// internally by the Mendix runtime and are therefore not present in the MPR's
// domain models. These types are serialized using the qualified name reference
// ("System.Workflow", "System.User", etc.) and resolved at runtime.
func isBuiltinModuleEntity(moduleName string) bool {
return moduleName == "System"
}
// execCreateMicroflow handles CREATE MICROFLOW statements.
// loadRestServices returns all consumed REST services, or nil if no backend.
func loadRestServices(ctx *ExecContext) ([]*model.ConsumedRestService, error) {
if !ctx.Connected() {
return nil, nil
}
svcs, err := ctx.Backend.ListConsumedRestServices()
return svcs, err
}
func execCreateMicroflow(ctx *ExecContext, s *ast.CreateMicroflowStmt) error {
e := ctx.executor
if !ctx.ConnectedForWrite() {
return mdlerrors.NewNotConnectedWrite()
}
// Find or auto-create module
module, err := findOrCreateModule(ctx, s.Name.Module)
if err != nil {
return err
}
// Resolve folder if specified
containerID := module.ID
if s.Folder != "" {
folderID, err := resolveFolder(ctx, module.ID, s.Folder)
if err != nil {
return mdlerrors.NewBackend("resolve folder "+s.Folder, err)
}
containerID = folderID
}
// Check if microflow with same name already exists in this module
var existingID model.ID
var existingContainerID model.ID
var existingAllowedRoles []model.ID
preserveAllowedRoles := false
existingMicroflows, err := ctx.Backend.ListMicroflows()
if err != nil {
return mdlerrors.NewBackend("check existing microflows", err)
}
for _, existing := range existingMicroflows {
if existing.Name == s.Name.Name && getModuleID(ctx, existing.ContainerID) == module.ID {
if !s.CreateOrModify {
return mdlerrors.NewAlreadyExistsMsg("microflow", s.Name.Module+"."+s.Name.Name, "microflow '"+s.Name.Module+"."+s.Name.Name+"' already exists (use create or replace to overwrite)")
}
existingID = existing.ID
existingContainerID = existing.ContainerID
existingAllowedRoles = cloneRoleIDs(existing.AllowedModuleRoles)
preserveAllowedRoles = true
break
}
}
// For CREATE OR REPLACE/MODIFY, reuse the existing ID to preserve references
qualifiedName := s.Name.Module + "." + s.Name.Name
microflowID := model.ID(types.GenerateID())
if existingID != "" {
microflowID = existingID
// Keep the original folder unless a new folder is explicitly specified
if s.Folder == "" {
containerID = existingContainerID
}
} else if dropped := consumeDroppedMicroflow(ctx, qualifiedName); dropped != nil {
// A prior DROP MICROFLOW in the same session removed the unit. Reuse
// its original UnitID and (unless a new folder is specified)
// ContainerID so that Studio Pro sees the rewrite as an in-place
// update rather than a delete+insert pair, which produces
// ".mpr does not look like a Mendix Studio Pro project file" errors.
microflowID = dropped.ID
if s.Folder == "" && dropped.ContainerID != "" {
containerID = dropped.ContainerID
}
// consumeDroppedMicroflow removed the cache entry, so we own this
// slice — no need to clone it again.
existingAllowedRoles = dropped.AllowedRoles
preserveAllowedRoles = true
}
// Build the microflow
mf := µflows.Microflow{
BaseElement: model.BaseElement{
ID: microflowID,
},
ContainerID: containerID,
Name: s.Name.Name,
Documentation: s.Documentation,
AllowConcurrentExecution: true, // Default: allow concurrent execution
MarkAsUsed: false,
Excluded: s.Excluded,
}
if preserveAllowedRoles {
mf.AllowedModuleRoles = existingAllowedRoles
}
// Build entity resolver function for parameter/return types
entityResolver := func(qn ast.QualifiedName) model.ID {
// Get all domain models and build module name map
dms, err := ctx.Backend.ListDomainModels()
if err != nil {
return ""
}
modules, _ := ctx.Backend.ListModules()
moduleNames := make(map[model.ID]string)
for _, m := range modules {
moduleNames[m.ID] = m.Name
}
// Search for entity in all domain models
for _, dm := range dms {
modName := moduleNames[dm.ContainerID]
if modName != qn.Module {
continue
}
for _, ent := range dm.Entities {
if ent.Name == qn.Name {
return ent.ID
}
}
}
return ""
}
// Validate and add parameters
for _, p := range s.Parameters {
// Validate entity references for List and Entity types.
// Built-in modules (e.g. System) are not stored in the MPR domain models;
// their types are serialized by qualified name and resolved at runtime.
if p.Type.EntityRef != nil && !isBuiltinModuleEntity(p.Type.EntityRef.Module) {
entityID := entityResolver(*p.Type.EntityRef)
if entityID == "" {
return mdlerrors.NewNotFoundMsg("entity", p.Type.EntityRef.Module+"."+p.Type.EntityRef.Name,
fmt.Sprintf("entity '%s.%s' not found for parameter '%s'", p.Type.EntityRef.Module, p.Type.EntityRef.Name, p.Name))
}
}
// Validate enumeration references for Enumeration types
if p.Type.Kind == ast.TypeEnumeration && p.Type.EnumRef != nil {
if found := findEnumeration(ctx, p.Type.EnumRef.Module, p.Type.EnumRef.Name); found == nil {
return mdlerrors.NewNotFoundMsg("enumeration", p.Type.EnumRef.Module+"."+p.Type.EnumRef.Name,
fmt.Sprintf("enumeration '%s.%s' not found for parameter '%s'", p.Type.EnumRef.Module, p.Type.EnumRef.Name, p.Name))
}
}
param := µflows.MicroflowParameter{
BaseElement: model.BaseElement{
ID: model.ID(types.GenerateID()),
},
ContainerID: mf.ID,
Name: p.Name,
Type: convertASTToMicroflowDataType(p.Type, entityResolver),
}
mf.Parameters = append(mf.Parameters, param)
}
// Validate and set return type
if s.ReturnType != nil {
// Validate entity references for return type.
// Built-in modules (e.g. System) are not stored in the MPR domain models;
// their types are serialized by qualified name and resolved at runtime.
if s.ReturnType.Type.EntityRef != nil && !isBuiltinModuleEntity(s.ReturnType.Type.EntityRef.Module) {
entityID := entityResolver(*s.ReturnType.Type.EntityRef)
if entityID == "" {
return mdlerrors.NewNotFoundMsg("entity", s.ReturnType.Type.EntityRef.Module+"."+s.ReturnType.Type.EntityRef.Name,
fmt.Sprintf("entity '%s.%s' not found for return type", s.ReturnType.Type.EntityRef.Module, s.ReturnType.Type.EntityRef.Name))
}
}
// Validate enumeration references for return type
if s.ReturnType.Type.Kind == ast.TypeEnumeration && s.ReturnType.Type.EnumRef != nil {
if found := findEnumeration(ctx, s.ReturnType.Type.EnumRef.Module, s.ReturnType.Type.EnumRef.Name); found == nil {
return mdlerrors.NewNotFoundMsg("enumeration", s.ReturnType.Type.EnumRef.Module+"."+s.ReturnType.Type.EnumRef.Name,
fmt.Sprintf("enumeration '%s.%s' not found for return type", s.ReturnType.Type.EnumRef.Module, s.ReturnType.Type.EnumRef.Name))
}
}
mf.ReturnType = convertASTToMicroflowDataType(s.ReturnType.Type, entityResolver)
// Set return variable name if provided (AS $VarName)
if s.ReturnType.Variable != "" {
mf.ReturnVariableName = s.ReturnType.Variable
}
} else {
mf.ReturnType = µflows.VoidType{}
}
// Build flow graph from body statements
// Initialize variable types from parameters
varTypes := make(map[string]string)
declaredVars := make(map[string]string)
for _, p := range s.Parameters {
if p.Type.EntityRef != nil {
entityQN := p.Type.EntityRef.Module + "." + p.Type.EntityRef.Name
if p.Type.Kind == ast.TypeListOf {
// Store "List of Module.Entity" for list parameters
varTypes[p.Name] = "List of " + entityQN
} else {
// Store "Module.Entity" for single entity parameters
varTypes[p.Name] = entityQN
}
} else {
// Primitive type parameters are also considered declared
declaredVars[p.Name] = p.Type.Kind.String()
}
}
// Get hierarchy for resolving page/microflow references
hierarchy, _ := getHierarchy(ctx)
restServices, _ := loadRestServices(ctx)
builder := &flowBuilder{
posX: 200,
posY: 200,
baseY: 200, // Base Y for happy path
spacing: HorizontalSpacing,
varTypes: varTypes,
declaredVars: declaredVars,
measurer: &layoutMeasurer{varTypes: varTypes},
backend: ctx.Backend,
hierarchy: hierarchy,
restServices: restServices,
}
mf.ObjectCollection = builder.buildFlowGraph(s.Body, s.ReturnType)
// Check for validation errors
if errors := builder.GetErrors(); len(errors) > 0 {
// Report all errors to the user
var errMsg strings.Builder
errMsg.WriteString(fmt.Sprintf("microflow '%s.%s' has validation errors:\n", s.Name.Module, s.Name.Name))
for _, err := range errors {
errMsg.WriteString(fmt.Sprintf(" - %s\n", err))
}
return fmt.Errorf("%s", errMsg.String())
}
// Create or update the microflow
if existingID != "" {
if err := ctx.Backend.UpdateMicroflow(mf); err != nil {
return mdlerrors.NewBackend("update microflow", err)
}
fmt.Fprintf(ctx.Output, "Replaced microflow: %s.%s\n", s.Name.Module, s.Name.Name)
} else {
if err := ctx.Backend.CreateMicroflow(mf); err != nil {
return mdlerrors.NewBackend("create microflow", err)
}
fmt.Fprintf(ctx.Output, "Created microflow: %s.%s\n", s.Name.Module, s.Name.Name)
}
// Track the created microflow so it can be resolved by subsequent page creations
returnEntityName := extractEntityFromReturnType(mf.ReturnType)
e.trackCreatedMicroflow(s.Name.Module, s.Name.Name, mf.ID, containerID, returnEntityName)
// Invalidate hierarchy cache so the new microflow's container is visible
invalidateHierarchy(ctx)
return nil
}