2121
2222import org .finos .waltz .model .EntityKind ;
2323import org .finos .waltz .model .EntityLifecycleStatus ;
24+ import org .finos .waltz .schema .Tables ;
25+ import org .finos .waltz .schema .tables .EntityHierarchy ;
26+ import org .finos .waltz .schema .tables .Measurable ;
2427import org .finos .waltz .web .WebUtilities ;
2528import org .jooq .DSLContext ;
29+ import org .jooq .Field ;
2630import org .jooq .Record ;
2731import org .jooq .SelectConditionStep ;
28- import org .slf4j .Logger ;
29- import org .slf4j .LoggerFactory ;
32+ import org .jooq .SelectOnConditionStep ;
33+ import org .jooq .impl .DSL ;
34+ import org .jooq .lambda .tuple .Tuple3 ;
3035import org .springframework .beans .factory .annotation .Autowired ;
3136import org .springframework .stereotype .Service ;
3237
38+ import java .util .ArrayList ;
39+ import java .util .List ;
40+ import java .util .stream .Collectors ;
41+ import java .util .stream .Stream ;
42+
43+ import static org .finos .waltz .common .Checks .checkNotNull ;
3344import static org .finos .waltz .schema .Tables .ENTITY_HIERARCHY ;
3445import static org .finos .waltz .schema .Tables .INVOLVEMENT ;
3546import static org .finos .waltz .schema .tables .InvolvementKind .INVOLVEMENT_KIND ;
3647import static org .finos .waltz .schema .tables .Measurable .MEASURABLE ;
3748import static org .finos .waltz .schema .tables .MeasurableCategory .MEASURABLE_CATEGORY ;
3849import static org .finos .waltz .schema .tables .Person .PERSON ;
39- import static org .finos . waltz . common . Checks . checkNotNull ;
50+ import static org .jooq . lambda . tuple . Tuple . tuple ;
4051import static spark .Spark .get ;
4152
4253
4354@ Service
4455public class MeasurableCategoryExtractor extends DirectQueryBasedDataExtractor {
4556
46- private static final Logger LOG = LoggerFactory .getLogger (MeasurableCategoryExtractor .class );
47-
4857
4958 @ Autowired
5059 public MeasurableCategoryExtractor (DSLContext dsl ) {
@@ -54,21 +63,77 @@ public MeasurableCategoryExtractor(DSLContext dsl) {
5463
5564 @ Override
5665 public void register () {
66+ String flatPath = WebUtilities .mkPath ("data-extract" , "measurable-category" , "flat" , ":id" );
67+ get (flatPath , (request , response ) -> {
68+ long categoryId = WebUtilities .getId (request );
69+
70+ EntityHierarchy eh = ENTITY_HIERARCHY .as ("eh" );
71+ Measurable m = Tables .MEASURABLE .as ("m" );
72+ Field <Integer > maxLevelField = DSL .max (eh .LEVEL ).as ("maxLevel" );
73+ Integer maxLevel = dsl
74+ .select (maxLevelField )
75+ .from (eh )
76+ .where (eh .KIND .eq (EntityKind .MEASURABLE .name ()))
77+ .and (eh .ID .in (DSL
78+ .select (m .ID )
79+ .from (m )
80+ .where (m .MEASURABLE_CATEGORY_ID .eq (categoryId ))
81+ .and (m .ENTITY_LIFECYCLE_STATUS .ne (EntityLifecycleStatus .REMOVED .name ()))))
82+ .fetchOne (maxLevelField );
83+
84+ ArrayList <Tuple3 <Integer , EntityHierarchy , Measurable >> ehAndMeasurableTablesForLevel = new ArrayList <>(maxLevel );
85+
86+ for (int i = 0 ; i < maxLevel ; i ++) {
87+ ehAndMeasurableTablesForLevel .add (i , tuple (i + 1 , ENTITY_HIERARCHY .as ("eh" + i ), Tables .MEASURABLE .as ("l" + i )));
88+ }
89+
90+ List <Field <?>> fields = ehAndMeasurableTablesForLevel
91+ .stream ()
92+ .flatMap (t -> Stream .of (
93+ _m (t ).NAME .as ("Level " + t .v1 + " Name" ),
94+ _m (t ).EXTERNAL_ID .as ("Level " + t .v1 + " External Id" ),
95+ _m (t ).ID .as ("Level " + t .v1 + " Waltz Id" )))
96+ .collect (Collectors .toList ());
97+
98+ Tuple3 <Integer , EntityHierarchy , Measurable > l1 = ehAndMeasurableTablesForLevel .get (0 );
99+
100+ SelectOnConditionStep <Record > qry = dsl
101+ .select (fields )
102+ .from (_m (l1 ))
103+ .innerJoin (_eh (l1 ))
104+ .on (_eh (l1 ).ID .eq (_m (l1 ).ID )
105+ .and (_eh (l1 ).KIND .eq (EntityKind .MEASURABLE .name ()))
106+ .and (_eh (l1 ).DESCENDANT_LEVEL .eq (_lvl (l1 )))
107+ .and (_m (l1 ).MEASURABLE_CATEGORY_ID .eq (categoryId ))
108+ .and (_m (l1 ).ENTITY_LIFECYCLE_STATUS .ne (EntityLifecycleStatus .REMOVED .name ())));
109+
110+ for (int i = 1 ; i < maxLevel ; i ++) {
111+ Tuple3 <Integer , EntityHierarchy , Measurable > curr = ehAndMeasurableTablesForLevel .get (i );
112+ Tuple3 <Integer , EntityHierarchy , Measurable > prev = ehAndMeasurableTablesForLevel .get (i - 1 );
113+ qry = qry
114+ .leftJoin (_eh (curr ))
115+ .on (_eh (curr ).ANCESTOR_ID .eq (_eh (prev ).ID )
116+ .and (_eh (curr ).KIND .eq (EntityKind .MEASURABLE .name ())
117+ .and (_eh (curr ).DESCENDANT_LEVEL .eq (_lvl (curr )))))
118+ .leftJoin (_m (curr ))
119+ .on (_m (curr ).ID .eq (_eh (curr ).ID )
120+ .and (_m (curr ).ENTITY_LIFECYCLE_STATUS .ne (EntityLifecycleStatus .REMOVED .name ())));
121+ }
122+
123+ return writeExtract (
124+ mkSuggestedFilename (categoryId ),
125+ qry ,
126+ request ,
127+ response );
128+ });
57129
58- String path = WebUtilities .mkPath ("data-extract" , "measurable-category" , ":id" );
59- get (path , (request , response ) -> {
130+
131+
132+
133+ String parentChildPath = WebUtilities .mkPath ("data-extract" , "measurable-category" , ":id" );
134+ get (parentChildPath , (request , response ) -> {
60135 long categoryId = WebUtilities .getId (request );
61- String categoryName = dsl
62- .select (MEASURABLE_CATEGORY .NAME )
63- .from (MEASURABLE_CATEGORY )
64- .where (MEASURABLE_CATEGORY .ID .eq (categoryId ))
65- .fetchOne (MEASURABLE_CATEGORY .NAME );
66-
67- checkNotNull (categoryName , "category cannot be null" );
68- String suggestedFilename = categoryName
69- .replace ("." , "-" )
70- .replace (" " , "-" )
71- .replace ("," , "-" );
136+ String suggestedFilename = mkSuggestedFilename (categoryId );
72137
73138 SelectConditionStep <Record > data = dsl
74139 .select (
@@ -103,4 +168,31 @@ public void register() {
103168 });
104169 }
105170
171+ private String mkSuggestedFilename (long categoryId ) {
172+ String categoryName = dsl
173+ .select (MEASURABLE_CATEGORY .NAME )
174+ .from (MEASURABLE_CATEGORY )
175+ .where (MEASURABLE_CATEGORY .ID .eq (categoryId ))
176+ .fetchOne (MEASURABLE_CATEGORY .NAME );
177+
178+ checkNotNull (categoryName , "category cannot be null" );
179+ String suggestedFilename = categoryName
180+ .replace ("." , "-" )
181+ .replace (" " , "-" )
182+ .replace ("," , "-" );
183+ return suggestedFilename ;
184+ }
185+
186+
187+ private static Measurable _m (Tuple3 <Integer , EntityHierarchy , Measurable > l1 ) {
188+ return l1 .v3 ;
189+ }
190+
191+ private static EntityHierarchy _eh (Tuple3 <Integer , EntityHierarchy , Measurable > l1 ) {
192+ return l1 .v2 ;
193+ }
194+
195+ private static Integer _lvl (Tuple3 <Integer , EntityHierarchy , Measurable > l1 ) {
196+ return l1 .v1 ;
197+ }
106198}
0 commit comments