77import org .dddjava .jig .domain .model .documents .stationery .JigDocumentContext ;
88import org .dddjava .jig .domain .model .information .JigRepository ;
99import org .dddjava .jig .domain .model .knowledge .insight .Insights ;
10+ import org .dddjava .jig .domain .model .knowledge .insight .MethodInsight ;
11+ import org .dddjava .jig .domain .model .knowledge .insight .PackageInsight ;
12+ import org .dddjava .jig .domain .model .knowledge .insight .TypeInsight ;
1013import org .thymeleaf .TemplateEngine ;
1114import org .thymeleaf .context .Context ;
1215
1316import java .nio .file .Path ;
1417import java .util .List ;
1518import java .util .Locale ;
1619import java .util .Map ;
20+ import java .util .stream .Collectors ;
1721
1822
1923@ HandleDocument
@@ -34,11 +38,25 @@ public List<Path> invoke(JigRepository repository, JigDocument jigDocument) {
3438 Insights result = jigService .insights (repository );
3539 var jigDocumentWriter = new JigDocumentWriter (jigDocument , jigDocumentContext .outputDirectory ());
3640
41+ String packagesJson = result .packageInsightList ().stream ()
42+ .map (this ::formatPackageJson )
43+ .collect (Collectors .joining ("," , "[" , "]" ));
44+
45+ String typesJson = result .typeInsightList ().stream ()
46+ .map (this ::formatTypeJson )
47+ .collect (Collectors .joining ("," , "[" , "]" ));
48+
49+ String methodsJson = result .methodInsightList ().stream ()
50+ .map (this ::formatMethodJson )
51+ .collect (Collectors .joining ("," , "[" , "]" ));
52+
53+ String insightJson = """
54+ {"packages": %s, "types": %s, "methods": %s}
55+ """ .formatted (packagesJson , typesJson , methodsJson );
56+
3757 Map <String , Object > contextMap = Map .of (
3858 "title" , jigDocumentWriter .jigDocument ().label (),
39- "packageInsightList" , result .packageInsightList (),
40- "typeInsightList" , result .typeInsightList (),
41- "methodInsightList" , result .methodInsightList ()
59+ "insightJson" , insightJson
4260 );
4361
4462 Context context = new Context (Locale .ROOT , contextMap );
@@ -48,4 +66,53 @@ public List<Path> invoke(JigRepository repository, JigDocument jigDocument) {
4866 writer -> templateEngine .process (template , context , writer ));
4967 return jigDocumentWriter .outputFilePaths ();
5068 }
69+
70+ private String formatPackageJson (PackageInsight insight ) {
71+ return """
72+ {"fqn": "%s", "label": "%s", "numberOfTypes": %d, "numberOfMethods": %d, "numberOfUsingTypes": %d, "cyclomaticComplexity": %d, "size": %d}
73+ """ .formatted (
74+ escape (insight .fqn ()),
75+ escape (insight .label ()),
76+ insight .numberOfTypes (),
77+ insight .numberOfMethods (),
78+ insight .numberOfUsingTypes (),
79+ insight .cyclomaticComplexity (),
80+ insight .size ());
81+ }
82+
83+ private String formatTypeJson (TypeInsight insight ) {
84+ return """
85+ {"fqn": "%s", "label": "%s", "numberOfMethods": %d, "numberOfUsingTypes": %d, "cyclomaticComplexity": %d, "size": %d, "packageFqn": "%s"}
86+ """ .formatted (
87+ escape (insight .fqn ()),
88+ escape (insight .label ()),
89+ insight .numberOfMethods (),
90+ insight .numberOfUsingTypes (),
91+ insight .cyclomaticComplexity (),
92+ insight .size (),
93+ escape (insight .packageFqn ()));
94+ }
95+
96+ private String formatMethodJson (MethodInsight insight ) {
97+ return """
98+ {"fqn": "%s", "label": "%s", "cyclomaticComplexity": %d, "numberOfUsingTypes": %d, "numberOfUsingMethods": %d, "numberOfUsingFields": %d, "size": %d, "packageFqn": "%s", "typeFqn": "%s"}
99+ """ .formatted (
100+ escape (insight .fqn ()),
101+ escape (insight .label ()),
102+ insight .cyclomaticComplexity (),
103+ insight .numberOfUsingTypes (),
104+ insight .numberOfUsingMethods (),
105+ insight .numberOfUsingFields (),
106+ insight .size (),
107+ escape (insight .packageFqn ()),
108+ escape (insight .typeFqn ()));
109+ }
110+
111+ private String escape (String string ) {
112+ return string
113+ .replace ("\\ " , "\\ \\ " )
114+ .replace ("\" " , "\\ \" " )
115+ .replace ("\r " , "\\ r" )
116+ .replace ("\n " , "\\ n" );
117+ }
51118}
0 commit comments