29
29
import com .renomad .minum .web .WebFramework ;
30
30
import java .net .URI ;
31
31
import java .nio .file .Path ;
32
+ import java .util .List ;
32
33
import java .util .Map ;
33
34
import java .util .stream .Collectors ;
34
35
import ru .ewc .checklogic .ServerConfiguration ;
35
36
import ru .ewc .checklogic .ServerInstance ;
36
37
import ru .ewc .checklogic .testing .CheckSuite ;
38
+ import ru .ewc .decisions .api .ComputationContext ;
39
+ import ru .ewc .decisions .api .OutputTracker ;
37
40
import ru .ewc .decisions .input .CombinedCsvFileReader ;
38
41
39
42
/**
@@ -70,7 +73,7 @@ public StatePage(
70
73
@ Override
71
74
public void register (final WebFramework web ) {
72
75
web .registerPath (GET , "state" , this ::statePage );
73
- web .registerPath (POST , "state" , this ::createState );
76
+ web .registerPath (POST , "state" , this ::postRouter );
74
77
web .registerPath (DELETE , "state" , this ::resetState );
75
78
}
76
79
@@ -82,17 +85,60 @@ private Response statePage(final Request request) {
82
85
"templates/state.html" ,
83
86
Map .of (
84
87
"state" , stored .asHtmlList (),
85
- "includes" , this .listOfIncludes ()
88
+ "includes" , this .listOfIncludes (),
89
+ "tables" , this .listOfTables ()
86
90
)
87
91
)
88
92
);
89
93
}
90
94
91
- private Response createState (final Request request ) {
95
+ private Response postRouter (final Request request ) {
92
96
assert request .requestLine ().getMethod ().equals (RequestLine .Method .POST );
93
97
final String include = request .body ().asString ("include" );
94
- this .context .createState (include , this .testSuite ());
95
- return Response .htmlOk ("OK" , Map .of ("HX-Redirect" , "/state" ));
98
+ final String table = request .body ().asString ("table" );
99
+ final ComputationContext computation = this .context .computation ();
100
+ final Response result ;
101
+ if (StatePage .isSpecified (include )) {
102
+ this .testSuite ().findAndPerform (include , computation );
103
+ result = Response .htmlOk ("OK" , Map .of ("HX-Redirect" , "/state" ));
104
+ } else if (StatePage .isSpecified (table )) {
105
+ final OutputTracker <String > tracker = computation .startTracking ();
106
+ final Map <String , String > outcomes = computation .decisionFor (table );
107
+ result = Response .htmlOk (
108
+ this .processors .renderTemplateWith (
109
+ "templates/outcomes.html" ,
110
+ Map .of (
111
+ "outcomes" , StatePage .asTable (outcomes ),
112
+ "events" , StatePage .asCollapsible (tracker .events ())
113
+ )
114
+ )
115
+ );
116
+ } else {
117
+ result = Response .htmlOk ("OK" , Map .of ("HX-Redirect" , "/state" ));
118
+ }
119
+ return result ;
120
+ }
121
+
122
+ private static boolean isSpecified (final String include ) {
123
+ return !include .isBlank ();
124
+ }
125
+
126
+ private static String asCollapsible (final List <String > events ) {
127
+ return events .stream ()
128
+ .map ("<li>%s</li>" ::formatted )
129
+ .collect (Collectors .joining ());
130
+ }
131
+
132
+ private static String asTable (final Map <String , String > outcomes ) {
133
+ return outcomes .entrySet ().stream ()
134
+ .sorted (Map .Entry .comparingByKey ())
135
+ .map (
136
+ entry -> "<tr><td>%s</td><td>%s</td></tr>" .formatted (
137
+ entry .getKey (),
138
+ entry .getValue ()
139
+ )
140
+ )
141
+ .collect (Collectors .joining ());
96
142
}
97
143
98
144
private Response resetState (final Request request ) {
@@ -119,4 +165,11 @@ private String listOfIncludes() {
119
165
.map (name -> "<option value=\" %s\" >%s</option>" .formatted (name , name ))
120
166
.collect (Collectors .joining ());
121
167
}
168
+
169
+ private String listOfTables () {
170
+ return this .context .computation ().tableNames ().stream ()
171
+ .sorted ()
172
+ .map (name -> "<option value=\" %s\" >%s</option>" .formatted (name , name ))
173
+ .collect (Collectors .joining ());
174
+ }
122
175
}
0 commit comments