Skip to content

Commit

Permalink
Merge pull request finos#6784 from deutschebank/db-contrib/waltz-6763…
Browse files Browse the repository at this point in the history
…-survey-section-rework

Db contrib/waltz 6763 survey section rework
  • Loading branch information
jessica-woodland-scott-db authored Sep 26, 2023
2 parents 483ad51 + 69e0e23 commit 50cdd08
Show file tree
Hide file tree
Showing 14 changed files with 528 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ public List<Involvement> findAllByEmployeeId(String employeeId) {


public List<Person> findPeopleByEntityReference(EntityReference ref) {
return dsl.selectDistinct(PERSON.fields())
return dsl
.selectDistinct(PERSON.fields())
.from(PERSON)
.innerJoin(INVOLVEMENT)
.on(INVOLVEMENT.ENTITY_ID.eq(ref.id()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,12 @@ public int deleteForSurveyRun(long surveyRunId) {
public int updateStatus(long instanceId, SurveyInstanceStatus newStatus) {
checkNotNull(newStatus, "newStatus cannot be null");

return dsl.update(si)
return dsl
.update(si)
.set(si.STATUS, newStatus.name())
.where(si.STATUS.notEqual(newStatus.name())
.and(si.ID.eq(instanceId)))
.and(si.ID.eq(instanceId))
.and(si.ORIGINAL_INSTANCE_ID.isNull()))
.execute();
}

Expand Down Expand Up @@ -366,34 +368,53 @@ public int updateOwningRoleForSurveyRun(long surveyRunId, String role) {
}


public int updateSubmitted(long instanceId, String userName) {
public int markSubmitted(long instanceId, String userName) {
checkNotNull(userName, "userName cannot be null");

return dsl.update(si)
return dsl
.update(si)
.set(si.STATUS, SurveyInstanceStatus.COMPLETED.name())
.set(si.SUBMITTED_AT, Timestamp.valueOf(nowUtc()))
.set(si.SUBMITTED_BY, userName)
.where(si.ID.eq(instanceId))
.where(si.ID.eq(instanceId)
.and(si.ORIGINAL_INSTANCE_ID.isNull())
.and(si.STATUS.in(
SurveyInstanceStatus.NOT_STARTED.name(),
SurveyInstanceStatus.IN_PROGRESS.name())))
.execute();
}


public int markApproved(long instanceId, String userName) {
checkNotNull(userName, "userName cannot be null");

return dsl.update(si)
return dsl
.update(si)
.set(si.APPROVED_AT, Timestamp.valueOf(nowUtc()))
.set(si.APPROVED_BY, userName)
.set(si.STATUS, SurveyInstanceStatus.APPROVED.name())
.where(si.ID.eq(instanceId))
.where(si.ID.eq(instanceId)
.and(si.ORIGINAL_INSTANCE_ID.isNull())
.and(si.STATUS.eq(SurveyInstanceStatus.COMPLETED.name())))
.execute();
}


public void clearApproved(long instanceId) {
dsl.update(si)
public int reopenSurvey(long instanceId) {
return dsl
.update(si)
.set(si.STATUS, SurveyInstanceStatus.IN_PROGRESS.name())
.set(si.APPROVED_AT, (Timestamp) null)
.set(si.APPROVED_BY, (String) null)
.where(si.ID.eq(instanceId))
.set(si.SUBMITTED_AT, (Timestamp) null)
.set(si.SUBMITTED_BY, (String) null)
.set(si.ISSUED_ON, toSqlDate(nowUtcTimestamp())) //update the issued on to the current date
.where(si.ID.eq(instanceId)
.and(si.ORIGINAL_INSTANCE_ID.isNull())
.and(si.STATUS.in(
SurveyInstanceStatus.APPROVED.name(),
SurveyInstanceStatus.REJECTED.name(),
SurveyInstanceStatus.WITHDRAWN.name())))
.execute();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,18 @@
import org.finos.waltz.model.survey.SurveyInstanceStatus;
import org.finos.waltz.model.survey.SurveyIssuanceKind;
import org.finos.waltz.model.survey.SurveyRunStatus;
import org.finos.waltz.schema.tables.SurveyInstance;
import org.finos.waltz.schema.tables.records.SurveyInstanceRecord;
import org.finos.waltz.schema.tables.records.SurveyRunRecord;
import org.jooq.AggregateFunction;
import org.jooq.Condition;
import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.Record;
import org.jooq.Record1;
import org.jooq.Select;
import org.jooq.SelectConditionStep;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

Expand Down Expand Up @@ -78,10 +83,13 @@ public class SurveyViewDao {
.as("external_id");

private static final String ID_SEPARATOR = ";";

private static final Condition IS_ORIGINAL_INSTANCE_CONDITION = SURVEY_INSTANCE.ORIGINAL_INSTANCE_ID.isNull();

private static SurveyInstanceInfo mkSurveyInstanceInfo(Record r, Map<Long, List<Long>> surveyInvolvementGroupKindIds) {
private static final SurveyInstance historicalVersion = SURVEY_INSTANCE.as("historicalVersion");

private static SurveyInstanceInfo mkSurveyInstanceInfo(Record r,
Map<Long, List<Long>> surveyInvolvementGroupKindIds,
Map<Long, Integer> historicalVersions) {

SurveyInstanceRecord instanceRecord = r.into(SURVEY_INSTANCE);
ImmutableSurveyInstance surveyInstance = ImmutableSurveyInstance.builder()
Expand Down Expand Up @@ -140,6 +148,8 @@ private static SurveyInstanceInfo mkSurveyInstanceInfo(Record r, Map<Long, List<
.status(SurveyRunStatus.valueOf(runRecord.getStatus()))
.build();

Integer historicalVersionCount = historicalVersions.getOrDefault(surveyInstance.id().get(), 0);

return ImmutableSurveyInstanceInfo.builder()
.surveyInstance(surveyInstance)
.surveyRun(run)
Expand All @@ -149,6 +159,7 @@ private static SurveyInstanceInfo mkSurveyInstanceInfo(Record r, Map<Long, List<
r.get(SURVEY_TEMPLATE.NAME),
r.get(SURVEY_TEMPLATE.DESCRIPTION),
r.get(SURVEY_TEMPLATE.EXTERNAL_ID)))
.historicalVersionsCount(historicalVersionCount)
.build();
};

Expand All @@ -166,6 +177,7 @@ public SurveyViewDao(DSLContext dsl) {
public SurveyInstanceInfo getById(long instanceId) {

Map<Long, List<Long>> surveyInvolvementGroupKindIds = findSurveyInvolvementGroupKindIds();
Map<Long, Integer> historicalVersions = getHistoricalVersionCounts(historicalVersion.ORIGINAL_INSTANCE_ID.eq(instanceId));

return dsl
.select(SURVEY_INSTANCE.fields())
Expand All @@ -181,14 +193,16 @@ public SurveyInstanceInfo getById(long instanceId) {
.innerJoin(SURVEY_RUN).on(SURVEY_INSTANCE.SURVEY_RUN_ID.eq(SURVEY_RUN.ID))
.innerJoin(SURVEY_TEMPLATE).on(SURVEY_RUN.SURVEY_TEMPLATE_ID.eq(SURVEY_TEMPLATE.ID))
.where(SURVEY_INSTANCE.ID.eq(instanceId))
.fetchOne(r -> mkSurveyInstanceInfo(r, surveyInvolvementGroupKindIds));
.fetchOne(r -> mkSurveyInstanceInfo(r, surveyInvolvementGroupKindIds, historicalVersions));
}


public Set<SurveyInstanceInfo> findForRecipient(long personId) {

Map<Long, List<Long>> surveyInvolvementGroupKindIds = findSurveyInvolvementGroupKindIds();

Map<Long, Integer> historicalVersions = getHistoricalVersionCounts(DSL.trueCondition());

return dsl
.select(SURVEY_INSTANCE.fields())
.select(SURVEY_RUN.fields())
Expand All @@ -208,7 +222,7 @@ public Set<SurveyInstanceInfo> findForRecipient(long personId) {
.and(IS_ORIGINAL_INSTANCE_CONDITION)
.and(SURVEY_INSTANCE.STATUS.ne(SurveyInstanceStatus.WITHDRAWN.name()))
.and(SURVEY_TEMPLATE.STATUS.eq(ReleaseLifecycleStatus.ACTIVE.name()))
.fetchSet(r -> mkSurveyInstanceInfo(r, surveyInvolvementGroupKindIds));
.fetchSet(r -> mkSurveyInstanceInfo(r, surveyInvolvementGroupKindIds, historicalVersions));
}


Expand All @@ -218,6 +232,7 @@ public Set<SurveyInstanceInfo> findForOwner(Long personId) {
Condition isRunOwnerOrHasOwnerInvolvement = SURVEY_INSTANCE_OWNER.PERSON_ID.eq(personId).or(SURVEY_RUN.OWNER_ID.eq(personId));

Map<Long, List<Long>> surveyInvolvementGroupKindIds = findSurveyInvolvementGroupKindIds();
Map<Long, Integer> historicalVersions = getHistoricalVersionCounts(DSL.trueCondition());

SelectConditionStep<Record> selectSurveysByOwningInvolvement = dsl
.select(SURVEY_INSTANCE.fields())
Expand Down Expand Up @@ -259,7 +274,7 @@ public Set<SurveyInstanceInfo> findForOwner(Long personId) {

return selectSurveysByOwningInvolvement
.union(selectSurveysByOwningRole)
.fetchSet(r -> mkSurveyInstanceInfo(r, surveyInvolvementGroupKindIds));
.fetchSet(r -> mkSurveyInstanceInfo(r, surveyInvolvementGroupKindIds, historicalVersions));
}


Expand All @@ -281,4 +296,44 @@ private Map<Long, List<Long>> findSurveyInvolvementGroupKindIds() {
r -> r.get(INVOLVEMENT_GROUP_ENTRY.INVOLVEMENT_GROUP_ID),
r -> r.get(INVOLVEMENT_GROUP_ENTRY.INVOLVEMENT_KIND_ID));
}

public Set<SurveyInstanceInfo> findBySurveyInstanceIdSelector(Select<Record1<Long>> selector) {
Map<Long, List<Long>> surveyInvolvementGroupKindIds = findSurveyInvolvementGroupKindIds();

Map<Long, Integer> historicalVersions = getHistoricalVersionCounts(historicalVersion.ORIGINAL_INSTANCE_ID.in(selector));

SelectConditionStep<Record> qry = dsl
.select(SURVEY_INSTANCE.fields())
.select(SURVEY_RUN.fields())
.select(SURVEY_TEMPLATE.NAME,
SURVEY_TEMPLATE.ID,
SURVEY_TEMPLATE.DESCRIPTION,
SURVEY_TEMPLATE.EXTERNAL_ID)
.select(ENTITY_NAME_FIELD)
.select(QUALIFIER_NAME_FIELD)
.select(EXTERNAL_ID_FIELD)
.from(SURVEY_INSTANCE)
.innerJoin(SURVEY_RUN).on(SURVEY_INSTANCE.SURVEY_RUN_ID.eq(SURVEY_RUN.ID))
.innerJoin(SURVEY_TEMPLATE).on(SURVEY_RUN.SURVEY_TEMPLATE_ID.eq(SURVEY_TEMPLATE.ID))
.where(SURVEY_INSTANCE.ID.in(selector)
.and(IS_ORIGINAL_INSTANCE_CONDITION));

return qry
.fetchSet(r -> mkSurveyInstanceInfo(r, surveyInvolvementGroupKindIds, historicalVersions));
}

private Map<Long, Integer> getHistoricalVersionCounts(Condition condition) {

AggregateFunction<Integer> surveyCount = DSL.count();

return dsl
.select(historicalVersion.ORIGINAL_INSTANCE_ID, surveyCount)
.from(historicalVersion)
.where(historicalVersion.ORIGINAL_INSTANCE_ID.isNotNull())
.and(condition)
.groupBy(historicalVersion.ORIGINAL_INSTANCE_ID)
.fetchMap(
r -> r.get(historicalVersion.ORIGINAL_INSTANCE_ID),
r -> r.get(surveyCount));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ public abstract class SurveyInstanceInfo {
public abstract SurveyInstance surveyInstance();
public abstract SurveyRun surveyRun();
public abstract EntityReference surveyTemplateRef();
public abstract Integer historicalVersionsCount();

}
5 changes: 3 additions & 2 deletions waltz-ng/client/survey/components/survey-section.html
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,9 @@ <h4>

<!-- SURVEY INSTANCE LIST -->
<div ng-if="$ctrl.visibility.mode == 'list'">
<waltz-survey-instance-list parent-entity-ref="$ctrl.parentEntityRef">
</waltz-survey-instance-list>
<waltz-svelte-component component="$ctrl.SurveyInstanceList"
primary-entity-ref="$ctrl.parentEntityRef">
</waltz-svelte-component>
</div>


Expand Down
4 changes: 3 additions & 1 deletion waltz-ng/client/survey/components/survey-section.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {displayError} from "../../common/error-utils";
import {isSurveyTargetKind} from "../survey-utils";
import toasts from "../../svelte-stores/toast-store";
import SystemRoles from "../../user/system-roles";
import SurveyInstanceList from "./svelte/SurveyInstanceList.svelte";


const initialState = {
Expand All @@ -43,7 +44,8 @@ const initialState = {
recipientInvolvementKinds: [],
ownerInvolvementKinds: []
},
templateQuery: ""
templateQuery: "",
SurveyInstanceList
};


Expand Down
Loading

0 comments on commit 50cdd08

Please sign in to comment.