forked from mendixlabs/mxcli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_microflows_builder_calls.go
More file actions
1136 lines (1006 loc) · 38.9 KB
/
cmd_microflows_builder_calls.go
File metadata and controls
1136 lines (1006 loc) · 38.9 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: Apache-2.0
// Package executor - Microflow builder: call, control flow, and client actions
package executor
import (
"fmt"
"log"
"strings"
"github.com/mendixlabs/mxcli/mdl/ast"
"github.com/mendixlabs/mxcli/mdl/types"
"github.com/mendixlabs/mxcli/model"
"github.com/mendixlabs/mxcli/sdk/javaactions"
"github.com/mendixlabs/mxcli/sdk/microflows"
)
// defaultLogNodeExpression is the quoted Mendix expression used for the log
// node when none is specified on a LOG statement. Single source of truth shared
// by the builder, the formatter, and cmd_diff_mdl.
const defaultLogNodeExpression = "'Application'"
// addLogMessageAction creates a LOG statement as a LogMessageAction.
func (fb *flowBuilder) addLogMessageAction(s *ast.LogStmt) model.ID {
logLevel := microflows.LogLevelInfo
switch s.Level {
case ast.LogTrace:
logLevel = microflows.LogLevelTrace
case ast.LogDebug:
logLevel = microflows.LogLevelDebug
case ast.LogWarning:
logLevel = microflows.LogLevelWarning
case ast.LogError:
logLevel = microflows.LogLevelError
case ast.LogCritical:
logLevel = microflows.LogLevelCritical
}
// Determine template text and parameters
// If message is a simple string literal, use it directly
// If message is a complex expression, use {1} as template and add expression as parameter
var templateText string
var templateParams []string
if len(s.Template) > 0 {
// Use provided template parameters
if lit, ok := s.Message.(*ast.LiteralExpr); ok && lit.Kind == ast.LiteralString {
templateText = fmt.Sprintf("%v", lit.Value)
} else {
templateText = fb.exprToString(s.Message)
}
// Sort parameters by index to ensure correct order
maxIndex := 0
for _, p := range s.Template {
if p.Index > maxIndex {
maxIndex = p.Index
}
}
templateParams = make([]string, maxIndex)
for _, p := range s.Template {
if p.Index > 0 && p.Index <= maxIndex {
templateParams[p.Index-1] = fb.exprToString(p.Value)
}
}
} else if lit, ok := s.Message.(*ast.LiteralExpr); ok && lit.Kind == ast.LiteralString {
// Simple string literal - use directly as template
templateText = fmt.Sprintf("%v", lit.Value)
} else {
// Complex expression - use {1} placeholder and add expression as parameter
templateText = "{1}"
templateParams = []string{fb.exprToString(s.Message)}
}
logNodeName := defaultLogNodeExpression
if s.Node != nil {
logNodeName = fb.exprToString(s.Node)
}
action := µflows.LogMessageAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
LogLevel: logLevel,
LogNodeName: logNodeName,
MessageTemplate: &model.Text{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Translations: map[string]string{
"en_US": templateText,
},
},
TemplateParameters: templateParams,
}
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
return activity.ID
}
// addCallMicroflowAction creates a CALL MICROFLOW statement.
func (fb *flowBuilder) addCallMicroflowAction(s *ast.CallMicroflowStmt) model.ID {
mfQN := s.MicroflowName.Module + "." + s.MicroflowName.Name
// Build parameter mappings for MicroflowCall
var mappings []*microflows.MicroflowCallParameterMapping
for _, arg := range s.Arguments {
// Parameter is the full qualified name: Module.Microflow.ParameterName
paramQN := mfQN + "." + arg.Name
mapping := µflows.MicroflowCallParameterMapping{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Parameter: paramQN,
Argument: fb.exprToString(arg.Value),
}
mappings = append(mappings, mapping)
}
// Create nested MicroflowCall structure
mfCall := µflows.MicroflowCall{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Microflow: mfQN,
ParameterMappings: mappings,
}
action := µflows.MicroflowCallAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ErrorHandlingType: convertErrorHandlingType(s.ErrorHandling),
MicroflowCall: mfCall,
ResultVariableName: s.OutputVariable,
UseReturnVariable: s.OutputVariable != "",
}
activityX := fb.posX
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
if s.OutputVariable != "" {
fb.registerResultVariableType(s.OutputVariable, fb.lookupMicroflowReturnType(mfQN))
}
// Build custom error handler flow if present
if s.ErrorHandling != nil && len(s.ErrorHandling.Body) > 0 {
errorY := fb.posY + VerticalSpacing
mergeID := fb.addErrorHandlerFlow(activity.ID, activityX, s.ErrorHandling.Body)
fb.handleErrorHandlerMerge(mergeID, activity.ID, errorY)
}
return activity.ID
}
// addCallJavaActionAction creates a CALL JAVA ACTION statement.
func (fb *flowBuilder) addCallJavaActionAction(s *ast.CallJavaActionStmt) model.ID {
actionQN := s.ActionName.Module + "." + s.ActionName.Name
// Try to look up the Java action definition to detect EntityTypeParameterType parameters
var jaDef *javaactions.JavaAction
if fb.backend != nil {
var err error
jaDef, err = fb.backend.ReadJavaActionByName(actionQN)
if err != nil {
log.Printf("warning: could not look up Java action %s: %v (entity type params will be empty)", actionQN, err)
}
}
// Build a map of parameter name -> param type for the Java action
entityTypeParams := make(map[string]bool)
if jaDef != nil {
for _, p := range jaDef.Parameters {
if _, ok := p.ParameterType.(*javaactions.EntityTypeParameterType); ok {
entityTypeParams[p.Name] = true
}
}
}
// Build parameter mappings with Value structure
var mappings []*microflows.JavaActionParameterMapping
for _, arg := range s.Arguments {
// Parameter qualified name format: Module.JavaAction.ParameterName
// (both Module and JavaAction are namespaces, so all levels are included)
paramQN := actionQN + "." + arg.Name
// Check if this parameter is typed to a type parameter (EntityTypeParameterType)
var value microflows.CodeActionParameterValue
if entityTypeParams[arg.Name] {
// Entity type parameter: value is the entity qualified name, not the variable reference.
// When the argument is a variable like $Email, resolve its entity type from varTypes.
valueExpr := fb.exprToString(arg.Value)
entityName := strings.Trim(valueExpr, "'")
if strings.HasPrefix(entityName, "$") {
varName := strings.TrimPrefix(entityName, "$")
if resolvedType, ok := fb.varTypes[varName]; ok {
entityName = resolvedType
}
}
value = µflows.EntityTypeCodeActionParameterValue{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Entity: entityName,
}
} else {
// Regular parameter: expression-based value
valueExpr := fb.exprToString(arg.Value)
value = µflows.BasicCodeActionParameterValue{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Argument: valueExpr,
}
}
mapping := µflows.JavaActionParameterMapping{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Parameter: paramQN,
Value: value,
}
mappings = append(mappings, mapping)
}
action := µflows.JavaActionCallAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ErrorHandlingType: convertErrorHandlingType(s.ErrorHandling),
JavaAction: actionQN,
ParameterMappings: mappings,
ResultVariableName: s.OutputVariable,
UseReturnVariable: s.OutputVariable != "",
}
activityX := fb.posX
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
// Build custom error handler flow if present
if s.ErrorHandling != nil && len(s.ErrorHandling.Body) > 0 {
errorY := fb.posY + VerticalSpacing
mergeID := fb.addErrorHandlerFlow(activity.ID, activityX, s.ErrorHandling.Body)
fb.handleErrorHandlerMerge(mergeID, activity.ID, errorY)
}
return activity.ID
}
// addCallExternalActionAction creates a CALL EXTERNAL ACTION statement.
func (fb *flowBuilder) addCallExternalActionAction(s *ast.CallExternalActionStmt) model.ID {
serviceQN := s.ServiceName.Module + "." + s.ServiceName.Name
// Build parameter mappings
var mappings []*microflows.ExternalActionParameterMapping
for _, arg := range s.Arguments {
mapping := µflows.ExternalActionParameterMapping{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ParameterName: arg.Name,
Argument: fb.exprToString(arg.Value),
}
mappings = append(mappings, mapping)
}
action := µflows.CallExternalAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ErrorHandlingType: convertErrorHandlingType(s.ErrorHandling),
ConsumedODataService: serviceQN,
Name: s.ActionName,
ParameterMappings: mappings,
ResultVariableName: s.OutputVariable,
UseReturnVariable: s.OutputVariable != "",
}
activityX := fb.posX
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
// Build custom error handler flow if present
if s.ErrorHandling != nil && len(s.ErrorHandling.Body) > 0 {
errorY := fb.posY + VerticalSpacing
mergeID := fb.addErrorHandlerFlow(activity.ID, activityX, s.ErrorHandling.Body)
fb.handleErrorHandlerMerge(mergeID, activity.ID, errorY)
}
return activity.ID
}
// addShowPageAction creates a SHOW PAGE statement.
func (fb *flowBuilder) addShowPageAction(s *ast.ShowPageStmt) model.ID {
// Use page qualified name (BY_NAME_REFERENCE) - the modern Mendix format
// uses FormSettings.Form as a string reference, not a binary UUID
pageQN := s.PageName.Module + "." + s.PageName.Name
// Build page parameter mappings
var mappings []*microflows.PageParameterMapping
for _, arg := range s.Arguments {
// Parameter qualified name format: Module.Page.ParameterName
paramQN := pageQN + "." + arg.ParamName
mapping := µflows.PageParameterMapping{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Parameter: paramQN,
Argument: fb.exprToString(arg.Value),
}
mappings = append(mappings, mapping)
}
// Determine page location
var location microflows.PageLocation
switch s.Location {
case "Popup":
location = microflows.PageLocationPopup
case "Modal":
location = microflows.PageLocationModal
default:
location = microflows.PageLocationContent
}
// Create page settings
pageSettings := µflows.PageSettings{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Location: location,
ModalForm: s.ModalForm,
}
// Create the action
// Use PageName (BY_NAME_REFERENCE) instead of PageID (BY_ID_REFERENCE)
// The modern Mendix format uses FormSettings.Form as a qualified name string
action := µflows.ShowPageAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
PageName: pageQN, // BY_NAME_REFERENCE - qualified name string
PageSettings: pageSettings,
PageParameterMappings: mappings,
}
// Set passed object if FOR syntax was used
if s.ForObject != "" {
action.PassedObject = "$" + s.ForObject
}
// Set title override if specified
if s.Title != "" {
action.OverridePageTitle = &model.Text{
BaseElement: model.BaseElement{
ID: model.ID(types.GenerateID()),
TypeName: "Texts$Text",
},
Translations: map[string]string{"en_US": s.Title},
}
}
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
return activity.ID
}
// addShowHomePageAction creates a SHOW HOME PAGE statement.
func (fb *flowBuilder) addShowHomePageAction(s *ast.ShowHomePageStmt) model.ID {
action := µflows.ShowHomePageAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
}
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
return activity.ID
}
// addShowMessageAction creates a SHOW MESSAGE statement.
func (fb *flowBuilder) addShowMessageAction(s *ast.ShowMessageStmt) model.ID {
// Build template text and parameters from message expression.
// For string literals, use the raw value directly as template text.
// For complex expressions, use {1} placeholder and add expression as parameter.
var templateText string
var templateParams []string
if lit, ok := s.Message.(*ast.LiteralExpr); ok && lit.Kind == ast.LiteralString {
templateText = fmt.Sprintf("%v", lit.Value)
} else {
templateText = "{1}"
templateParams = []string{fb.exprToString(s.Message)}
}
// Append template parameters from TemplateArgs (e.g., OBJECTS [$Var1, $Var2])
for _, arg := range s.TemplateArgs {
templateParams = append(templateParams, fb.exprToString(arg))
}
template := &model.Text{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Translations: map[string]string{"en_US": templateText},
}
msgType := microflows.MessageType(s.Type)
if msgType == "" {
msgType = microflows.MessageTypeInformation
}
action := µflows.ShowMessageAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Template: template,
Type: msgType,
TemplateParameters: templateParams,
}
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
return activity.ID
}
// addClosePageAction creates a CLOSE PAGE statement.
func (fb *flowBuilder) addClosePageAction(s *ast.ClosePageStmt) model.ID {
numPages := s.NumberOfPages
if numPages <= 0 {
numPages = 1
}
action := µflows.ClosePageAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
NumberOfPages: numPages,
}
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
return activity.ID
}
// addValidationFeedbackAction creates a VALIDATION FEEDBACK statement as a ValidationFeedbackAction.
func (fb *flowBuilder) addValidationFeedbackAction(s *ast.ValidationFeedbackStmt) model.ID {
// Build the template text from the message expression.
// For string literals, use the raw value (without quotes) since the template
// text is plain text, not a microflow expression. For complex expressions,
// use {1} placeholder with the expression as a parameter (same pattern as LogMessageAction).
var templateText string
var templateParams []string
if lit, ok := s.Message.(*ast.LiteralExpr); ok && lit.Kind == ast.LiteralString {
// Simple string literal - use raw value directly as template text
templateText = fmt.Sprintf("%v", lit.Value)
} else {
// Complex expression - use {1} placeholder and add expression as parameter
templateText = "{1}"
templateParams = []string{fb.exprToString(s.Message)}
}
// Create template with translations map (default language "en_US")
template := &model.Text{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Translations: map[string]string{"en_US": templateText},
}
// Build attribute or association name from variable type and attribute path.
// Single segment with /: attribute access ($Product/Code → "Module.Entity.Code")
// Two segments where first uses / and second uses .: association traversal
// ($Instructor/Module.Association → AssociationName = "Module.Association")
// The grammar splits "Module.Association" into two segments: {Module, /} and {Association, .}
var attributeName string
var associationName string
if entityQName, ok := fb.varTypes[s.AttributePath.Variable]; ok && len(s.AttributePath.Segments) > 0 {
segs := s.AttributePath.Segments
if len(segs) == 1 {
// Single segment: direct attribute access
attributeName = entityQName + "." + segs[0].Name
} else if len(segs) >= 2 && segs[0].Separator == "/" && segs[1].Separator == "." {
// Two+ segments starting with / then .: association qualified name
// Reconstruct "Module.AssociationName" from segments
parts := make([]string, len(segs))
for i, seg := range segs {
parts[i] = seg.Name
}
associationName = strings.Join(parts, ".")
} else {
// Fallback: treat first segment as attribute
attributeName = entityQName + "." + segs[0].Name
}
} else if entityQName, ok := fb.varTypes[s.AttributePath.Variable]; ok && len(s.AttributePath.Path) > 0 {
// Fallback for legacy Path without Segments
attributeName = entityQName + "." + s.AttributePath.Path[0]
}
// Append template parameters from TemplateArgs (e.g., OBJECTS [$Var1, $Var2])
for _, arg := range s.TemplateArgs {
templateParams = append(templateParams, fb.exprToString(arg))
}
// Strip the $ prefix from variable name for BSON storage
varName := s.AttributePath.Variable
if strings.HasPrefix(varName, "$") {
varName = varName[1:]
}
action := µflows.ValidationFeedbackAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ObjectVariable: varName,
AttributeName: attributeName,
AssociationName: associationName,
Template: template,
TemplateParameters: templateParams,
}
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
return activity.ID
}
// addRestCallAction creates a REST CALL statement as a RestCallAction.
func (fb *flowBuilder) addRestCallAction(s *ast.RestCallStmt) model.ID {
// Build HTTP configuration
httpConfig := µflows.HttpConfiguration{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
}
// Set HTTP method
switch s.Method {
case ast.HttpMethodGet:
httpConfig.HttpMethod = microflows.HttpMethodGet
case ast.HttpMethodPost:
httpConfig.HttpMethod = microflows.HttpMethodPost
case ast.HttpMethodPut:
httpConfig.HttpMethod = microflows.HttpMethodPut
case ast.HttpMethodPatch:
httpConfig.HttpMethod = microflows.HttpMethodPatch
case ast.HttpMethodDelete:
httpConfig.HttpMethod = microflows.HttpMethodDelete
default:
httpConfig.HttpMethod = microflows.HttpMethodGet
}
// Set URL template
if lit, ok := s.URL.(*ast.LiteralExpr); ok && lit.Kind == ast.LiteralString {
httpConfig.LocationTemplate = fmt.Sprintf("%v", lit.Value)
} else {
httpConfig.LocationTemplate = fb.exprToString(s.URL)
}
// Set URL template parameters
for _, param := range s.URLParams {
httpConfig.LocationParams = append(httpConfig.LocationParams, fb.exprToString(param.Value))
}
// Set custom headers
for _, header := range s.Headers {
h := µflows.HttpHeader{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Name: header.Name,
Value: fb.exprToString(header.Value),
}
httpConfig.CustomHeaders = append(httpConfig.CustomHeaders, h)
}
// Set authentication
if s.Auth != nil {
httpConfig.UseAuthentication = true
httpConfig.Username = fb.exprToString(s.Auth.Username)
httpConfig.Password = fb.exprToString(s.Auth.Password)
}
// Build request handling
var requestHandling microflows.RequestHandling
if s.Body != nil {
switch s.Body.Type {
case ast.RestBodyCustom:
// Custom body template
var template string
if lit, ok := s.Body.Template.(*ast.LiteralExpr); ok && lit.Kind == ast.LiteralString {
template = fmt.Sprintf("%v", lit.Value)
} else {
template = fb.exprToString(s.Body.Template)
}
// Extract template parameters
var templateParams []string
for _, param := range s.Body.TemplateParams {
templateParams = append(templateParams, fb.exprToString(param.Value))
}
requestHandling = µflows.CustomRequestHandling{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Template: template,
TemplateParams: templateParams,
}
case ast.RestBodyMapping:
// Export mapping
mappingQN := s.Body.MappingName.Module + "." + s.Body.MappingName.Name
requestHandling = µflows.MappingRequestHandling{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
MappingID: model.ID(mappingQN), // Use qualified name as ID for BY_NAME references
ParameterVariable: s.Body.SourceVariable,
}
default:
// No body
requestHandling = µflows.CustomRequestHandling{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Template: "",
}
}
} else {
// Default: empty custom request handling
requestHandling = µflows.CustomRequestHandling{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Template: "",
}
}
// Build result handling
var resultHandling microflows.ResultHandling
switch s.Result.Type {
case ast.RestResultString:
resultHandling = µflows.ResultHandlingString{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
}
case ast.RestResultResponse:
// Bind the full HTTP response object to the output variable. The writer
// emits the matching `DataTypes$ObjectType` bound to System.HttpResponse;
// the action-level `ResultHandlingType` is derived as "HttpResponse" from
// this concrete type.
resultHandling = µflows.ResultHandlingHttpResponse{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
VariableName: s.OutputVariable,
}
case ast.RestResultMapping:
mappingQN := s.Result.MappingName.Module + "." + s.Result.MappingName.Name
entityQN := s.Result.ResultEntity.Module + "." + s.Result.ResultEntity.Name
// Derive the output variable name from the root entity's short name so
// callers don't need to hard-code it in the MDL assignment.
s.OutputVariable = s.Result.ResultEntity.Name
// Determine whether the import mapping returns a single object or a list by
// looking at the JSON structure it references. If the root JSON element is
// an Object, the mapping produces one object; if it is an Array, a list.
singleObject := false
if fb.backend != nil {
if im, err := fb.backend.GetImportMappingByQualifiedName(s.Result.MappingName.Module, s.Result.MappingName.Name); err == nil && im.JsonStructure != "" {
// im.JsonStructure is "Module.Name" — split and look up the JSON structure.
if parts := strings.SplitN(im.JsonStructure, ".", 2); len(parts) == 2 {
if js, err := fb.backend.GetJsonStructureByQualifiedName(parts[0], parts[1]); err == nil && len(js.Elements) > 0 {
singleObject = js.Elements[0].ElementType == "Object"
}
}
}
}
resultHandling = µflows.ResultHandlingMapping{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
MappingID: model.ID(mappingQN),
ResultEntityID: model.ID(entityQN),
ResultVariable: s.OutputVariable,
SingleObject: singleObject,
}
case ast.RestResultNone:
resultHandling = µflows.ResultHandlingNone{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
}
default:
resultHandling = µflows.ResultHandlingString{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
}
}
// Build timeout expression
var timeoutExpr string
if s.Timeout != nil {
timeoutExpr = fb.exprToString(s.Timeout)
} else {
timeoutExpr = "300" // Default 5 minutes
}
action := µflows.RestCallAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
HttpConfiguration: httpConfig,
RequestHandling: requestHandling,
ResultHandling: resultHandling,
ErrorHandlingType: convertErrorHandlingType(s.ErrorHandling),
OutputVariable: s.OutputVariable,
UseReturnVariable: s.OutputVariable != "",
TimeoutExpression: timeoutExpr,
}
activityX := fb.posX
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
// Build custom error handler flow if present
if s.ErrorHandling != nil && len(s.ErrorHandling.Body) > 0 {
errorY := fb.posY + VerticalSpacing
mergeID := fb.addErrorHandlerFlow(activity.ID, activityX, s.ErrorHandling.Body)
fb.handleErrorHandlerMerge(mergeID, activity.ID, errorY)
}
return activity.ID
}
// addSendRestRequestAction creates a SEND REST REQUEST activity that calls
// a consumed REST service operation.
func (fb *flowBuilder) addSendRestRequestAction(s *ast.SendRestRequestStmt) model.ID {
// Build operation reference: Module.Service.Operation
operationQN := s.Operation.String()
// Look up the operation definition to classify parameters and body kind.
// s.Operation.Module = "MfTest", s.Operation.Name = "RC_TestApi.PostJsonTemplate"
var opDef *model.RestClientOperation
if fb.restServices != nil && s.Operation.Module != "" && strings.Contains(s.Operation.Name, ".") {
dotIdx := strings.Index(s.Operation.Name, ".")
serviceName := s.Operation.Name[:dotIdx]
opName := s.Operation.Name[dotIdx+1:]
opDef = lookupRestOperation(fb.restServices, serviceName, opName)
}
// Build OutputVariable
var outputVar *microflows.RestOutputVar
if s.OutputVariable != "" {
outputVar = µflows.RestOutputVar{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
VariableName: s.OutputVariable,
}
}
// Build BodyVariable only for EXPORT_MAPPING body kind.
// For JSON / TEMPLATE / FILE bodies, the body expression lives on the
// operation definition itself and must NOT be set here (CE7067).
var bodyVar *microflows.RestBodyVar
if s.BodyVariable != "" && shouldSetBodyVariable(opDef) {
bodyVar = µflows.RestBodyVar{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
VariableName: s.BodyVariable,
}
}
// Build parameter mappings, routing to ParameterMappings (path) or
// QueryParameterMappings (query) based on the operation definition.
paramMappings, queryParamMappings := buildRestParameterMappings(s.Parameters, opDef, operationQN)
// RestOperationCallAction does not support custom error handling (CE6035).
// ON ERROR clauses in the MDL are silently ignored for this action type.
action := µflows.RestOperationCallAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Operation: operationQN,
OutputVariable: outputVar,
BodyVariable: bodyVar,
ParameterMappings: paramMappings,
QueryParameterMappings: queryParamMappings,
}
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
return activity.ID
}
// lookupRestOperation finds a specific operation in a consumed REST service list.
func lookupRestOperation(services []*model.ConsumedRestService, serviceName, opName string) *model.RestClientOperation {
for _, svc := range services {
if svc.Name != serviceName {
continue
}
for _, op := range svc.Operations {
if op.Name == opName {
return op
}
}
}
return nil
}
// shouldSetBodyVariable returns true if a BodyVariable BSON field should be
// emitted for a call to the given operation.
// For JSON, TEMPLATE, and FILE body kinds, the body expression lives on the
// operation definition and must not be overridden by a BodyVariable (CE7067).
// For EXPORT_MAPPING, the caller provides an entity to export via BodyVariable.
// When the operation definition is unknown (nil), we preserve old behaviour and
// set BodyVariable so the caller's intent is not silently dropped.
func shouldSetBodyVariable(op *model.RestClientOperation) bool {
if op == nil {
return true // unknown operation — preserve caller intent
}
switch op.BodyType {
case "json", "template", "file":
return false
default:
// EXPORT_MAPPING or empty (no body) — only set if EXPORT_MAPPING
return op.BodyType == "EXPORT_MAPPING"
}
}
// buildRestParameterMappings splits parameter bindings from a SEND REST REQUEST
// WITH clause into path parameter mappings and query parameter mappings,
// using the operation definition to determine which is which.
// When op is nil (operation not found), all parameters fall back to query
// parameter mappings (preserves old behaviour).
func buildRestParameterMappings(
params []ast.SendRestParamDef,
op *model.RestClientOperation,
operationQN string,
) ([]*microflows.RestParameterMapping, []*microflows.RestQueryParameterMapping) {
if len(params) == 0 {
return nil, nil
}
// Build lookup sets from the operation definition.
pathParamSet := map[string]bool{}
if op != nil {
for _, p := range op.Parameters {
pathParamSet[p.Name] = true
}
}
var pathMappings []*microflows.RestParameterMapping
var queryMappings []*microflows.RestQueryParameterMapping
for _, p := range params {
if pathParamSet[p.Name] {
pathMappings = append(pathMappings, µflows.RestParameterMapping{
Parameter: operationQN + "." + p.Name,
Value: p.Expression,
})
} else {
queryMappings = append(queryMappings, µflows.RestQueryParameterMapping{
Parameter: operationQN + "." + p.Name,
Value: p.Expression,
Included: "Yes",
})
}
}
return pathMappings, queryMappings
}
// addExecuteDatabaseQueryAction creates an EXECUTE DATABASE QUERY statement.
func (fb *flowBuilder) addExecuteDatabaseQueryAction(s *ast.ExecuteDatabaseQueryStmt) model.ID {
// DynamicQuery is a Mendix expression — string literals need single quotes
dynamicQuery := s.DynamicQuery
if dynamicQuery != "" && !strings.HasPrefix(dynamicQuery, "'") {
dynamicQuery = "'" + strings.ReplaceAll(dynamicQuery, "'", "''") + "'"
}
action := µflows.ExecuteDatabaseQueryAction{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ErrorHandlingType: convertErrorHandlingType(s.ErrorHandling),
OutputVariableName: s.OutputVariable,
Query: s.QueryName,
DynamicQuery: dynamicQuery,
}
// Build parameter mappings from arguments
for _, arg := range s.Arguments {
pm := µflows.DatabaseQueryParameterMapping{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ParameterName: arg.Name,
Value: fb.exprToString(arg.Value),
}
action.ParameterMappings = append(action.ParameterMappings, pm)
}
// Build connection parameter mappings (runtime connection override)
for _, arg := range s.ConnectionArguments {
cm := µflows.DatabaseConnectionParameterMapping{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
ParameterName: arg.Name,
Value: fb.exprToString(arg.Value),
}
action.ConnectionParameterMappings = append(action.ConnectionParameterMappings, cm)
}
activityX := fb.posX
activity := µflows.ActionActivity{
BaseActivity: microflows.BaseActivity{
BaseMicroflowObject: microflows.BaseMicroflowObject{
BaseElement: model.BaseElement{ID: model.ID(types.GenerateID())},
Position: model.Point{X: fb.posX, Y: fb.posY},
Size: model.Size{Width: ActivityWidth, Height: ActivityHeight},
},
AutoGenerateCaption: true,
},
Action: action,
}
fb.objects = append(fb.objects, activity)
fb.posX += fb.spacing
// Build custom error handler flow if present
if s.ErrorHandling != nil && len(s.ErrorHandling.Body) > 0 {
errorY := fb.posY + VerticalSpacing
mergeID := fb.addErrorHandlerFlow(activity.ID, activityX, s.ErrorHandling.Body)
fb.handleErrorHandlerMerge(mergeID, activity.ID, errorY)
}
return activity.ID
}