Skip to content

Commit

Permalink
Merge pull request #6924 from deutschebank/db-contrib/waltz-6922-surv…
Browse files Browse the repository at this point in the history
…ey-col-options-and-filters

Report grids: Survey col options and filters
  • Loading branch information
davidwatkins73 authored Jan 4, 2024
2 parents 51f0b30 + 44d391f commit 3dcf19a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@
import org.finos.waltz.model.report_grid.ReportGridKind;
import org.finos.waltz.model.report_grid.ReportGridMemberRole;
import org.finos.waltz.model.report_grid.ReportGridUpdateCommand;
import org.finos.waltz.model.survey.SurveyInstanceStatus;
import org.finos.waltz.model.survey.SurveyQuestionFieldType;
import org.finos.waltz.model.usage_info.UsageKind;
import org.finos.waltz.schema.Tables;
import org.finos.waltz.schema.tables.ChangeInitiative;
import org.finos.waltz.schema.tables.SurveyInstance;
import org.finos.waltz.schema.tables.records.ReportGridColumnDefinitionRecord;
import org.finos.waltz.schema.tables.records.ReportGridDerivedColumnDefinitionRecord;
import org.finos.waltz.schema.tables.records.ReportGridFixedColumnDefinitionRecord;
Expand All @@ -80,6 +82,7 @@
import org.jooq.SelectJoinStep;
import org.jooq.SelectOrderByStep;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.impl.DSL;
import org.jooq.lambda.tuple.Tuple2;
import org.jooq.lambda.tuple.Tuple3;
Expand Down Expand Up @@ -138,8 +141,6 @@
import static org.finos.waltz.data.JooqUtilities.fieldsWithout;
import static org.finos.waltz.model.EntityReference.mkRef;
import static org.finos.waltz.model.report_grid.CellOption.mkCellOption;
import static org.finos.waltz.model.survey.SurveyInstanceStatus.APPROVED;
import static org.finos.waltz.model.survey.SurveyInstanceStatus.COMPLETED;
import static org.finos.waltz.schema.Tables.APPLICATION;
import static org.finos.waltz.schema.Tables.APPLICATION_GROUP;
import static org.finos.waltz.schema.Tables.APPLICATION_GROUP_ENTRY;
Expand Down Expand Up @@ -861,6 +862,10 @@ private Set<ReportGridCell> findCellDataByGridCondition(Condition gridCondition,
colsByKind.getOrDefault(EntityKind.DATA_TYPE, emptySet()),
d -> d.additionalColumnOptions() == AdditionalColumnOptions.NONE);

Map<AdditionalColumnOptions, Collection<ReportGridFixedColumnDefinition>> surveyQuestionColsBySurveyStatusOptions = groupBy(
colsByKind.getOrDefault(EntityKind.SURVEY_QUESTION, emptySet()),
d -> d.additionalColumnOptions());


// FIELD REF COL DEFS

Expand All @@ -883,7 +888,7 @@ private Set<ReportGridCell> findCellDataByGridCondition(Condition gridCondition,
.map(d -> tuple(d, fieldReferencesById.get(d.entityFieldReference().id().get())))
.collect(groupingBy(t -> t.v2.entityKind(), toSet()));


return union(
fetchAssessmentData(genericSelector, colsByKind.get(EntityKind.ASSESSMENT_DEFINITION)),
fetchInvolvementData(genericSelector, colsByKind.get(EntityKind.INVOLVEMENT_KIND)),
Expand Down Expand Up @@ -1570,6 +1575,7 @@ private Set<ReportGridCell> fetchChangeInitiativeFieldReferenceData(GenericSelec

private Set<ReportGridCell> fetchSurveyFieldReferenceData(GenericSelector selector,
Set<Tuple2<ReportGridFixedColumnDefinition, EntityFieldReference>> surveyInstanceInfo) {

if (isEmpty(surveyInstanceInfo)) {
return emptySet();
} else {
Expand All @@ -1584,14 +1590,18 @@ private Set<ReportGridCell> fetchSurveyFieldReferenceData(GenericSelector select
r -> r.v1.columnEntityId(),
v -> v.v2);

Set<Long> surveyTemplateIds = fieldReferencesByTemplateId.keySet();
Map<AdditionalColumnOptions, Collection<Long>> templateIdsByColOptions = groupBy(
surveyInstanceInfo,
d -> d.v1.additionalColumnOptions(),
d -> d.v1.columnEntityId());

Set<Long> surveyTemplateIds = fieldReferencesByTemplateId.keySet();

Field<Long> latestInstance = DSL
.firstValue(SURVEY_INSTANCE.ID)
.over()
.partitionBy(SURVEY_INSTANCE.ENTITY_ID, SURVEY_INSTANCE.ENTITY_KIND, SURVEY_RUN.SURVEY_TEMPLATE_ID)
.orderBy(SURVEY_INSTANCE.SUBMITTED_AT.desc().nullsLast())
.orderBy(SURVEY_INSTANCE.ISSUED_ON.desc(), SURVEY_INSTANCE.SUBMITTED_AT.desc())
.as("latest_instance");

Table<Record> surveyInfo = dsl
Expand All @@ -1612,10 +1622,11 @@ private Set<ReportGridCell> fetchSurveyFieldReferenceData(GenericSelector select
.select(SURVEY_RUN.SURVEY_TEMPLATE_ID)
.from(SURVEY_INSTANCE)
.innerJoin(SURVEY_RUN).on(SURVEY_INSTANCE.SURVEY_RUN_ID.eq(SURVEY_RUN.ID))
.where(SURVEY_INSTANCE.ENTITY_ID.in(selector.selector())
.and(SURVEY_INSTANCE.ENTITY_KIND.eq(selector.kind().name())
.and(SURVEY_RUN.SURVEY_TEMPLATE_ID.in(surveyTemplateIds)
.and(SURVEY_INSTANCE.ORIGINAL_INSTANCE_ID.isNull()))))
.where(determineInstanceStatusCondition(SURVEY_RUN.SURVEY_TEMPLATE_ID, SURVEY_INSTANCE, templateIdsByColOptions)
.and(SURVEY_INSTANCE.ENTITY_ID.in(selector.selector())
.and(SURVEY_INSTANCE.ENTITY_KIND.eq(selector.kind().name())
.and(SURVEY_RUN.SURVEY_TEMPLATE_ID.in(surveyTemplateIds)
.and(SURVEY_INSTANCE.ORIGINAL_INSTANCE_ID.isNull())))))
.asTable();


Expand Down Expand Up @@ -1648,11 +1659,14 @@ private Set<ReportGridCell> fetchSurveyFieldReferenceData(GenericSelector select
return null;
}

CellOption option = determineOptionForSurveyInstance(fieldRef.fieldName(), textValue);

return ImmutableReportGridCell
.builder()
.subjectId(surveyRecord.get(SURVEY_INSTANCE.ENTITY_ID))
.columnDefinitionId(templateAndFieldRefToDefIdMap.get(tuple(templateId, fieldRef.id().get()))) // FIX
.textValue(textValue)
.options(asSet(option))
.build();
});
})
Expand All @@ -1662,6 +1676,7 @@ private Set<ReportGridCell> fetchSurveyFieldReferenceData(GenericSelector select
}



private Set<ReportGridCell> fetchOrgUnitFieldReferenceData(GenericSelector selector,
Set<Tuple2<ReportGridFixedColumnDefinition, EntityFieldReference>> requiredOrgUnitColumns) {

Expand Down Expand Up @@ -2223,11 +2238,16 @@ private Set<ReportGridCell> fetchSurveyQuestionResponseData(GenericSelector sele
ReportGridFixedColumnDefinition::columnEntityId,
ReportGridFixedColumnDefinition::gridColumnId);

Map<AdditionalColumnOptions, Collection<Long>> questionIdsByColOptions = groupBy(
cols,
ReportGridFixedColumnDefinition::additionalColumnOptions,
ReportGridFixedColumnDefinition::columnEntityId);

Field<Long> latestInstance = DSL
.firstValue(SURVEY_INSTANCE.ID)
.over()
.partitionBy(SURVEY_INSTANCE.ENTITY_ID, SURVEY_INSTANCE.ENTITY_KIND, SURVEY_QUESTION.ID)
.orderBy(SURVEY_INSTANCE.SUBMITTED_AT.desc().nullsLast())
.orderBy(SURVEY_INSTANCE.ISSUED_ON.desc(), SURVEY_INSTANCE.SUBMITTED_AT.desc())
.as("latest_instance");

Table<Record> responsesWithQuestionTypeAndEntity = dsl
Expand All @@ -2252,10 +2272,11 @@ private Set<ReportGridCell> fetchSurveyQuestionResponseData(GenericSelector sele
.on(SURVEY_QUESTION_RESPONSE.SURVEY_INSTANCE_ID.eq(SURVEY_INSTANCE.ID))
.innerJoin(SURVEY_QUESTION)
.on(SURVEY_QUESTION.ID.eq(SURVEY_QUESTION_RESPONSE.QUESTION_ID))
.where(SURVEY_INSTANCE.STATUS.in(APPROVED.name(), COMPLETED.name())
.where(determineInstanceStatusCondition(SURVEY_QUESTION.ID, SURVEY_INSTANCE, questionIdsByColOptions)
.and(SURVEY_QUESTION.ID.in(questionIdToDefIdMap.keySet()))
.and(SURVEY_INSTANCE.ENTITY_ID.in(selector.selector()))
.and(SURVEY_INSTANCE.ENTITY_KIND.eq(selector.kind().name())))
.and(SURVEY_INSTANCE.ENTITY_KIND.eq(selector.kind().name()))
.and(SURVEY_INSTANCE.ORIGINAL_INSTANCE_ID.isNull()))
.asTable();


Expand Down Expand Up @@ -2290,9 +2311,7 @@ private Set<ReportGridCell> fetchSurveyQuestionResponseData(GenericSelector sele
List<String> listResponses = responsesByInstanceQuestionKey.getOrDefault(tuple(instanceId, questionId), emptyList());

// if a question is not mandatory and left blank present as not provided
Set<CellOption> options = isEmpty(response)
? emptySet()
: asSet(determineOptionForSurveyQuestion(fieldType, response));
Set<CellOption> options = asSet(determineOptionForSurveyQuestion(fieldType, entityName, response, listResponses));

return ImmutableReportGridCell.builder()
.subjectId(r.get(SURVEY_INSTANCE.ENTITY_ID))
Expand All @@ -2305,11 +2324,31 @@ private Set<ReportGridCell> fetchSurveyQuestionResponseData(GenericSelector sele
}
}

private Condition determineInstanceStatusCondition(TableField<? extends Record, Long> idField,
SurveyInstance surveyInstance,
Map<AdditionalColumnOptions, Collection<Long>> colOptionsById) {

Collection<Long> questionsWithCompletedAndApprovedOnly = colOptionsById.getOrDefault(AdditionalColumnOptions.COMPLETED_AND_APPROVED_ONLY, emptySet());
Collection<Long> questionsExcludingWithdrawn = colOptionsById.getOrDefault(AdditionalColumnOptions.EXCLUDE_WITHDRAWN, emptySet());
Collection<Long> allStatuses = colOptionsById.getOrDefault(AdditionalColumnOptions.NONE, emptySet());

Condition completedApprovedCondition = idField.in(questionsWithCompletedAndApprovedOnly).and(surveyInstance.STATUS.in(asSet(SurveyInstanceStatus.COMPLETED.name(), SurveyInstanceStatus.APPROVED.name())));
Condition excludeWithdrawnCondition = idField.in(questionsExcludingWithdrawn).and(surveyInstance.STATUS.ne(SurveyInstanceStatus.WITHDRAWN.name()));
Condition allStatusesCondition = idField.in(allStatuses);

return completedApprovedCondition
.or(excludeWithdrawnCondition)
.or(allStatusesCondition);
}


private CellOption determineOptionForSurveyQuestion(String fieldType, String response) {
private CellOption determineOptionForSurveyQuestion(String fieldType,
String response,
String entityName,
List<String> listResponses) {

if (isEmpty(response)) {
mkCellOption("NA", "N/A");
if (isEmpty(response) && isEmpty(entityName) && isEmpty(listResponses)) {
return mkCellOption("NA", "N/A");
}

SurveyQuestionFieldType type = SurveyQuestionFieldType.valueOf(fieldType);
Expand All @@ -2323,6 +2362,16 @@ private CellOption determineOptionForSurveyQuestion(String fieldType, String res
}
}

private CellOption determineOptionForSurveyInstance(String fieldName, String textValue) {

if (fieldName.equalsIgnoreCase("status")) {
SurveyInstanceStatus status = SurveyInstanceStatus.valueOf(textValue);
return mkCellOption(status.name(), status.prettyName());
} else {
return CellOption.defaultCellOption();
}
}


private String determineDisplayText(String fieldType,
String entityName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ public enum AdditionalColumnOptions {
NONE(),
PICK_HIGHEST(asSet(EntityKind.MEASURABLE)),
PICK_LOWEST(asSet(EntityKind.MEASURABLE)),
ROLLUP(asSet(EntityKind.DATA_TYPE));
ROLLUP(asSet(EntityKind.DATA_TYPE)),
COMPLETED_AND_APPROVED_ONLY(asSet(EntityKind.SURVEY_INSTANCE, EntityKind.SURVEY_QUESTION)),
EXCLUDE_WITHDRAWN(asSet(EntityKind.SURVEY_INSTANCE, EntityKind.SURVEY_QUESTION));

private final Set<EntityKind> allowedKinds;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,21 @@

public enum SurveyInstanceStatus {

NOT_STARTED,
IN_PROGRESS,
COMPLETED,
REJECTED,
WITHDRAWN,
APPROVED
NOT_STARTED("Not Started"),
IN_PROGRESS("In Progress"),
COMPLETED("Completed"),
REJECTED("Rejected"),
WITHDRAWN("Withdrawn"),
APPROVED("Approved");

private final String prettyName;


SurveyInstanceStatus(String prettyName){

this.prettyName = prettyName;

}

public String prettyName(){ return prettyName; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
export let onCancel = () => console.log("Close");
export let onRemove = () => console.log("Remove");
$: columnOptionsCall = reportGridStore.findAdditionalColumnOptionsForKind(column.columnEntityKind);
$: optionsKind = !_.isEmpty(column.entityFieldReference) ? column.entityFieldReference.entityKind : column.columnEntityKind
$: columnOptionsCall = reportGridStore.findAdditionalColumnOptionsForKind(optionsKind);
$: allowedColumnOptions = _.map($columnOptionsCall?.data, d => additionalColumnOptions[d]) || [additionalColumnOptions.NONE];
let working = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,16 @@ export const additionalColumnOptions = {
key: "ROLLUP",
name: "Rollup",

},
COMPLETED_AND_APPROVED_ONLY: {
key: "COMPLETED_AND_APPROVED_ONLY",
name: "Completed and Approved Only",
},
EXCLUDE_WITHDRAWN: {
key: "EXCLUDE_WITHDRAWN",
name: "Exclude Withdrawn",
}
};
}


export function determineDefaultColumnOptions(columnEntityKind) {
Expand Down

0 comments on commit 3dcf19a

Please sign in to comment.