3030import org .finos .waltz .model .survey .SurveyInstanceStatus ;
3131import org .finos .waltz .model .survey .SurveyIssuanceKind ;
3232import org .finos .waltz .model .survey .SurveyRunStatus ;
33+ import org .finos .waltz .schema .tables .SurveyInstance ;
3334import org .finos .waltz .schema .tables .records .SurveyInstanceRecord ;
3435import org .finos .waltz .schema .tables .records .SurveyRunRecord ;
36+ import org .jooq .AggregateFunction ;
3537import org .jooq .Condition ;
3638import org .jooq .DSLContext ;
3739import org .jooq .Field ;
3840import org .jooq .Record ;
41+ import org .jooq .Record1 ;
42+ import org .jooq .Select ;
3943import org .jooq .SelectConditionStep ;
44+ import org .jooq .impl .DSL ;
4045import org .springframework .beans .factory .annotation .Autowired ;
4146import org .springframework .stereotype .Repository ;
4247
@@ -78,10 +83,13 @@ public class SurveyViewDao {
7883 .as ("external_id" );
7984
8085 private static final String ID_SEPARATOR = ";" ;
81-
8286 private static final Condition IS_ORIGINAL_INSTANCE_CONDITION = SURVEY_INSTANCE .ORIGINAL_INSTANCE_ID .isNull ();
8387
84- private static SurveyInstanceInfo mkSurveyInstanceInfo (Record r , Map <Long , List <Long >> surveyInvolvementGroupKindIds ) {
88+ private static final SurveyInstance historicalVersion = SURVEY_INSTANCE .as ("historicalVersion" );
89+
90+ private static SurveyInstanceInfo mkSurveyInstanceInfo (Record r ,
91+ Map <Long , List <Long >> surveyInvolvementGroupKindIds ,
92+ Map <Long , Integer > historicalVersions ) {
8593
8694 SurveyInstanceRecord instanceRecord = r .into (SURVEY_INSTANCE );
8795 ImmutableSurveyInstance surveyInstance = ImmutableSurveyInstance .builder ()
@@ -140,6 +148,8 @@ private static SurveyInstanceInfo mkSurveyInstanceInfo(Record r, Map<Long, List<
140148 .status (SurveyRunStatus .valueOf (runRecord .getStatus ()))
141149 .build ();
142150
151+ Integer historicalVersionCount = historicalVersions .getOrDefault (surveyInstance .id ().get (), 0 );
152+
143153 return ImmutableSurveyInstanceInfo .builder ()
144154 .surveyInstance (surveyInstance )
145155 .surveyRun (run )
@@ -149,6 +159,7 @@ private static SurveyInstanceInfo mkSurveyInstanceInfo(Record r, Map<Long, List<
149159 r .get (SURVEY_TEMPLATE .NAME ),
150160 r .get (SURVEY_TEMPLATE .DESCRIPTION ),
151161 r .get (SURVEY_TEMPLATE .EXTERNAL_ID )))
162+ .historicalVersionsCount (historicalVersionCount )
152163 .build ();
153164 };
154165
@@ -166,6 +177,7 @@ public SurveyViewDao(DSLContext dsl) {
166177 public SurveyInstanceInfo getById (long instanceId ) {
167178
168179 Map <Long , List <Long >> surveyInvolvementGroupKindIds = findSurveyInvolvementGroupKindIds ();
180+ Map <Long , Integer > historicalVersions = getHistoricalVersionCounts (historicalVersion .ORIGINAL_INSTANCE_ID .eq (instanceId ));
169181
170182 return dsl
171183 .select (SURVEY_INSTANCE .fields ())
@@ -181,14 +193,16 @@ public SurveyInstanceInfo getById(long instanceId) {
181193 .innerJoin (SURVEY_RUN ).on (SURVEY_INSTANCE .SURVEY_RUN_ID .eq (SURVEY_RUN .ID ))
182194 .innerJoin (SURVEY_TEMPLATE ).on (SURVEY_RUN .SURVEY_TEMPLATE_ID .eq (SURVEY_TEMPLATE .ID ))
183195 .where (SURVEY_INSTANCE .ID .eq (instanceId ))
184- .fetchOne (r -> mkSurveyInstanceInfo (r , surveyInvolvementGroupKindIds ));
196+ .fetchOne (r -> mkSurveyInstanceInfo (r , surveyInvolvementGroupKindIds , historicalVersions ));
185197 }
186198
187199
188200 public Set <SurveyInstanceInfo > findForRecipient (long personId ) {
189201
190202 Map <Long , List <Long >> surveyInvolvementGroupKindIds = findSurveyInvolvementGroupKindIds ();
191203
204+ Map <Long , Integer > historicalVersions = getHistoricalVersionCounts (DSL .trueCondition ());
205+
192206 return dsl
193207 .select (SURVEY_INSTANCE .fields ())
194208 .select (SURVEY_RUN .fields ())
@@ -208,7 +222,7 @@ public Set<SurveyInstanceInfo> findForRecipient(long personId) {
208222 .and (IS_ORIGINAL_INSTANCE_CONDITION )
209223 .and (SURVEY_INSTANCE .STATUS .ne (SurveyInstanceStatus .WITHDRAWN .name ()))
210224 .and (SURVEY_TEMPLATE .STATUS .eq (ReleaseLifecycleStatus .ACTIVE .name ()))
211- .fetchSet (r -> mkSurveyInstanceInfo (r , surveyInvolvementGroupKindIds ));
225+ .fetchSet (r -> mkSurveyInstanceInfo (r , surveyInvolvementGroupKindIds , historicalVersions ));
212226 }
213227
214228
@@ -218,6 +232,7 @@ public Set<SurveyInstanceInfo> findForOwner(Long personId) {
218232 Condition isRunOwnerOrHasOwnerInvolvement = SURVEY_INSTANCE_OWNER .PERSON_ID .eq (personId ).or (SURVEY_RUN .OWNER_ID .eq (personId ));
219233
220234 Map <Long , List <Long >> surveyInvolvementGroupKindIds = findSurveyInvolvementGroupKindIds ();
235+ Map <Long , Integer > historicalVersions = getHistoricalVersionCounts (DSL .trueCondition ());
221236
222237 SelectConditionStep <Record > selectSurveysByOwningInvolvement = dsl
223238 .select (SURVEY_INSTANCE .fields ())
@@ -259,7 +274,7 @@ public Set<SurveyInstanceInfo> findForOwner(Long personId) {
259274
260275 return selectSurveysByOwningInvolvement
261276 .union (selectSurveysByOwningRole )
262- .fetchSet (r -> mkSurveyInstanceInfo (r , surveyInvolvementGroupKindIds ));
277+ .fetchSet (r -> mkSurveyInstanceInfo (r , surveyInvolvementGroupKindIds , historicalVersions ));
263278 }
264279
265280
@@ -281,4 +296,44 @@ private Map<Long, List<Long>> findSurveyInvolvementGroupKindIds() {
281296 r -> r .get (INVOLVEMENT_GROUP_ENTRY .INVOLVEMENT_GROUP_ID ),
282297 r -> r .get (INVOLVEMENT_GROUP_ENTRY .INVOLVEMENT_KIND_ID ));
283298 }
299+
300+ public Set <SurveyInstanceInfo > findBySurveyInstanceIdSelector (Select <Record1 <Long >> selector ) {
301+ Map <Long , List <Long >> surveyInvolvementGroupKindIds = findSurveyInvolvementGroupKindIds ();
302+
303+ Map <Long , Integer > historicalVersions = getHistoricalVersionCounts (historicalVersion .ORIGINAL_INSTANCE_ID .in (selector ));
304+
305+ SelectConditionStep <Record > qry = dsl
306+ .select (SURVEY_INSTANCE .fields ())
307+ .select (SURVEY_RUN .fields ())
308+ .select (SURVEY_TEMPLATE .NAME ,
309+ SURVEY_TEMPLATE .ID ,
310+ SURVEY_TEMPLATE .DESCRIPTION ,
311+ SURVEY_TEMPLATE .EXTERNAL_ID )
312+ .select (ENTITY_NAME_FIELD )
313+ .select (QUALIFIER_NAME_FIELD )
314+ .select (EXTERNAL_ID_FIELD )
315+ .from (SURVEY_INSTANCE )
316+ .innerJoin (SURVEY_RUN ).on (SURVEY_INSTANCE .SURVEY_RUN_ID .eq (SURVEY_RUN .ID ))
317+ .innerJoin (SURVEY_TEMPLATE ).on (SURVEY_RUN .SURVEY_TEMPLATE_ID .eq (SURVEY_TEMPLATE .ID ))
318+ .where (SURVEY_INSTANCE .ID .in (selector )
319+ .and (IS_ORIGINAL_INSTANCE_CONDITION ));
320+
321+ return qry
322+ .fetchSet (r -> mkSurveyInstanceInfo (r , surveyInvolvementGroupKindIds , historicalVersions ));
323+ }
324+
325+ private Map <Long , Integer > getHistoricalVersionCounts (Condition condition ) {
326+
327+ AggregateFunction <Integer > surveyCount = DSL .count ();
328+
329+ return dsl
330+ .select (historicalVersion .ORIGINAL_INSTANCE_ID , surveyCount )
331+ .from (historicalVersion )
332+ .where (historicalVersion .ORIGINAL_INSTANCE_ID .isNotNull ())
333+ .and (condition )
334+ .groupBy (historicalVersion .ORIGINAL_INSTANCE_ID )
335+ .fetchMap (
336+ r -> r .get (historicalVersion .ORIGINAL_INSTANCE_ID ),
337+ r -> r .get (surveyCount ));
338+ }
284339}
0 commit comments