Skip to content

Commit

Permalink
Migrate attestation runs to use the involvement group table
Browse files Browse the repository at this point in the history
#CTCTOWALTZ-2760
finos#6637
  • Loading branch information
jessica-woodland-scott-db committed Jun 7, 2023
1 parent 7293a5c commit 74907ca
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,22 +284,12 @@ public SyncRecipientsResponse reassignRecipients() {

private Set<Tuple2<Long, Long>> getAttestationRunIdToInvKindId() {
return dsl
.select(ATTESTATION_RUN.ID, ATTESTATION_RUN.INVOLVEMENT_KIND_IDS)
.select(ATTESTATION_RUN.ID, INVOLVEMENT_GROUP_ENTRY.INVOLVEMENT_KIND_ID)
.from(ATTESTATION_RUN)
.where(ATTESTATION_RUN.INVOLVEMENT_KIND_IDS.isNotNull())
.and(ATTESTATION_RUN.INVOLVEMENT_KIND_IDS.ne(""))
.fetch()
.stream()
.flatMap(r -> {
Long runId = r.get(ATTESTATION_RUN.ID);
String invKinds = r.get(ATTESTATION_RUN.INVOLVEMENT_KIND_IDS);
return splitThenMap(
invKinds,
";",
d -> tuple(runId, Long.valueOf(d.trim())))
.stream();
})
.collect(toSet());
.innerJoin(INVOLVEMENT_GROUP_ENTRY)
.on(ATTESTATION_RUN.RECIPIENT_INVOLVEMENT_GROUP_ID.eq(INVOLVEMENT_GROUP_ENTRY.INVOLVEMENT_GROUP_ID))
.where(ATTESTATION_RUN.RECIPIENT_INVOLVEMENT_GROUP_ID.isNotNull())
.fetchSet(r -> tuple(r.get(ATTESTATION_RUN.ID), r.get(INVOLVEMENT_GROUP_ENTRY.INVOLVEMENT_KIND_ID)));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.finos.waltz.data.attestation;

import org.finos.waltz.data.involvement_group.InvolvementGroupDao;
import org.finos.waltz.schema.tables.records.AttestationRunRecord;
import org.finos.waltz.data.InlineSelectFieldFactory;
import org.finos.waltz.model.EntityKind;
Expand All @@ -32,17 +33,19 @@

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static java.util.Collections.emptyList;
import static org.finos.waltz.schema.Tables.SURVEY_RUN;
import static org.finos.waltz.schema.tables.AttestationInstance.ATTESTATION_INSTANCE;
import static org.finos.waltz.schema.tables.AttestationInstanceRecipient.ATTESTATION_INSTANCE_RECIPIENT;
import static org.finos.waltz.schema.tables.AttestationRun.ATTESTATION_RUN;
import static org.finos.waltz.common.Checks.checkNotNull;
import static org.finos.waltz.common.DateTimeUtilities.*;
import static org.finos.waltz.common.ListUtilities.newArrayList;
import static org.finos.waltz.common.StringUtilities.join;
import static org.finos.waltz.common.StringUtilities.splitThenMap;

@Repository
public class AttestationRunDao {
Expand All @@ -69,18 +72,22 @@ public class AttestationRunDao {

private static final String ID_SEPARATOR = ";";

private static final RecordMapper<Record, AttestationRun> TO_DOMAIN_MAPPER = r -> {
private static AttestationRun mkAttestationRun(Record r, Map<Long, List<Long>> attestationInvolvementGroupKindIds) {

AttestationRunRecord record = r.into(ATTESTATION_RUN);

Long recipientInvolvementGroupId = record.getRecipientInvolvementGroupId();
List<Long> recipients = attestationInvolvementGroupKindIds.getOrDefault(recipientInvolvementGroupId, emptyList());


Optional<EntityReference> attestedEntityRef = Optional.empty();
if(record.getAttestedEntityKind() != null && record.getAttestedEntityId() != null) {
if (record.getAttestedEntityKind() != null && record.getAttestedEntityId() != null) {
attestedEntityRef = Optional.of(EntityReference.mkRef(
EntityKind.valueOf(record.getAttestedEntityKind()),
record.getAttestedEntityId(),
r.getValue(ATTESTED_ENTITY_NAME_FIELD)));
}


return ImmutableAttestationRun.builder()
.id(record.getId())
.targetEntityKind(EntityKind.valueOf(record.getTargetEntityKind()))
Expand All @@ -92,10 +99,7 @@ public class AttestationRunDao {
record.getSelectorEntityId(),
r.getValue(ENTITY_NAME_FIELD)),
HierarchyQueryScope.valueOf(record.getSelectorHierarchyScope())))
.involvementKindIds(splitThenMap(
record.getInvolvementKindIds(),
ID_SEPARATOR,
Long::valueOf))
.involvementKindIds(recipients)
.issuedBy(record.getIssuedBy())
.issuedOn(toLocalDate(record.getIssuedOn()))
.dueDate(toLocalDate(record.getDueDate()))
Expand Down Expand Up @@ -128,48 +132,61 @@ public AttestationRunDao(DSLContext dsl) {


public AttestationRun getById(long attestationRunId) {
return dsl.select(ATTESTATION_RUN.fields())

Map<Long, List<Long>> involvementsByGroupId = InvolvementGroupDao.findAllInvolvementsByGroupId(dsl);

return dsl
.select(ATTESTATION_RUN.fields())
.select(ENTITY_NAME_FIELD)
.select(ATTESTED_ENTITY_NAME_FIELD)
.from(ATTESTATION_RUN)
.where(ATTESTATION_RUN.ID.eq(attestationRunId))
.fetchOne(TO_DOMAIN_MAPPER);
.fetchOne(r -> mkAttestationRun(r, involvementsByGroupId));
}


public List<AttestationRun> findAll() {

Map<Long, List<Long>> involvementsByGroupId = InvolvementGroupDao.findAllInvolvementsByGroupId(dsl);

return dsl.select(ATTESTATION_RUN.fields())
.select(ENTITY_NAME_FIELD)
.select(ATTESTED_ENTITY_NAME_FIELD)
.from(ATTESTATION_RUN)
.fetch(TO_DOMAIN_MAPPER);
.fetch(r -> mkAttestationRun(r, involvementsByGroupId));
}


public List<AttestationRun> findByRecipient(String userId) {

Map<Long, List<Long>> involvementsByGroupId = InvolvementGroupDao.findAllInvolvementsByGroupId(dsl);

return dsl.selectDistinct(ATTESTATION_RUN.fields())
.select(ENTITY_NAME_FIELD)
.select(ATTESTED_ENTITY_NAME_FIELD)
.from(ATTESTATION_RUN)
.innerJoin(ATTESTATION_INSTANCE)
.on(ATTESTATION_INSTANCE.ATTESTATION_RUN_ID.eq(ATTESTATION_RUN.ID))
.on(ATTESTATION_INSTANCE.ATTESTATION_RUN_ID.eq(ATTESTATION_RUN.ID))
.innerJoin(ATTESTATION_INSTANCE_RECIPIENT)
.on(ATTESTATION_INSTANCE_RECIPIENT.ATTESTATION_INSTANCE_ID.eq(ATTESTATION_INSTANCE.ID))
.on(ATTESTATION_INSTANCE_RECIPIENT.ATTESTATION_INSTANCE_ID.eq(ATTESTATION_INSTANCE.ID))
.where(ATTESTATION_INSTANCE_RECIPIENT.USER_ID.eq(userId))
.fetch(TO_DOMAIN_MAPPER);
.fetch(r -> mkAttestationRun(r, involvementsByGroupId));
}


public List<AttestationRun> findByEntityReference(EntityReference ref) {

Map<Long, List<Long>> involvementsByGroupId = InvolvementGroupDao.findAllInvolvementsByGroupId(dsl);

return dsl.select(ATTESTATION_RUN.fields())
.select(ENTITY_NAME_FIELD)
.select(ATTESTED_ENTITY_NAME_FIELD)
.from(ATTESTATION_RUN)
.innerJoin(ATTESTATION_INSTANCE)
.on(ATTESTATION_INSTANCE.ATTESTATION_RUN_ID.eq(ATTESTATION_RUN.ID))
.on(ATTESTATION_INSTANCE.ATTESTATION_RUN_ID.eq(ATTESTATION_RUN.ID))
.where(ATTESTATION_INSTANCE.PARENT_ENTITY_KIND.eq(ref.kind().name()))
.and(ATTESTATION_INSTANCE.PARENT_ENTITY_ID.eq(ref.id()))
.fetch(TO_DOMAIN_MAPPER);
.fetch(r -> mkAttestationRun(r, involvementsByGroupId));
}


Expand All @@ -187,14 +204,16 @@ public List<AttestationRunResponseSummary> findResponseSummaries() {

public List<AttestationRun> findByIdSelector(Select<Record1<Long>> selector) {

Map<Long, List<Long>> involvementsByGroupId = InvolvementGroupDao.findAllInvolvementsByGroupId(dsl);

return dsl.select(ATTESTATION_RUN.fields())
.select(ENTITY_NAME_FIELD)
.select(ATTESTED_ENTITY_NAME_FIELD)
.from(ATTESTATION_RUN)
.innerJoin(ATTESTATION_INSTANCE)
.on(ATTESTATION_INSTANCE.ATTESTATION_RUN_ID.eq(ATTESTATION_RUN.ID))
.where(ATTESTATION_INSTANCE.ID.in(selector))
.fetch(TO_DOMAIN_MAPPER);
.fetch(r -> mkAttestationRun(r, involvementsByGroupId));
}

public Long create(String userId, AttestationRunCreateCommand command) {
Expand All @@ -207,7 +226,6 @@ public Long create(String userId, AttestationRunCreateCommand command) {
record.setSelectorEntityKind(command.selectionOptions().entityReference().kind().name());
record.setSelectorEntityId(command.selectionOptions().entityReference().id());
record.setSelectorHierarchyScope(command.selectionOptions().scope().name());
record.setInvolvementKindIds(join(command.involvementKindIds(), ID_SEPARATOR));
record.setIssuedBy(userId);
record.setIssuedOn(toSqlDate(command.issuedOn()));
record.setDueDate(toSqlDate(command.dueDate()));
Expand All @@ -229,13 +247,16 @@ public int getEntityCount(Select<Record1<Long>> idSelector) {


public Set<AttestationRun> findPendingRuns() {

Map<Long, List<Long>> involvementsByGroupId = InvolvementGroupDao.findAllInvolvementsByGroupId(dsl);

return dsl
.select(ATTESTATION_RUN.fields())
.select(ENTITY_NAME_FIELD)
.select(ATTESTED_ENTITY_NAME_FIELD)
.from(ATTESTATION_RUN)
.where(ATTESTATION_RUN.STATUS.eq(AttestationStatus.PENDING.name()))
.fetchSet(TO_DOMAIN_MAPPER);
.fetchSet(r -> mkAttestationRun(r, involvementsByGroupId));
}


Expand All @@ -256,6 +277,21 @@ public int updateStatusForRunIds(Set<Long> runIds, AttestationStatus newStatus)
.where(ATTESTATION_RUN.ID.in(runIds))
.execute();
}
}

public Long getRecipientInvolvementGroupId(long attestationRunId) {
return dsl
.select(ATTESTATION_RUN.RECIPIENT_INVOLVEMENT_GROUP_ID)
.from(ATTESTATION_RUN)
.where(ATTESTATION_RUN.ID.eq(attestationRunId))
.fetchOne(ATTESTATION_RUN.RECIPIENT_INVOLVEMENT_GROUP_ID);
}

public int updateRecipientInvolvementGroupId(long attestationRunId, Long recipientInvGroupId) {
return dsl
.update(ATTESTATION_RUN)
.set(ATTESTATION_RUN.RECIPIENT_INVOLVEMENT_GROUP_ID, recipientInvGroupId)
.where(ATTESTATION_RUN.ID.eq(attestationRunId))
.execute();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import org.jooq.RecordMapper;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -78,6 +80,17 @@ public Long createInvolvementGroup(InvolvementGroupCreateCommand cmd) {
return invGroupId;
}


public static Map<Long, List<Long>> findAllInvolvementsByGroupId(DSLContext dsl) {
return dsl
.select(INVOLVEMENT_GROUP_ENTRY.INVOLVEMENT_GROUP_ID, INVOLVEMENT_GROUP_ENTRY.INVOLVEMENT_KIND_ID)
.from(INVOLVEMENT_GROUP_ENTRY)
.fetchGroups(
r -> r.get(INVOLVEMENT_GROUP_ENTRY.INVOLVEMENT_GROUP_ID),
r -> r.get(INVOLVEMENT_GROUP_ENTRY.INVOLVEMENT_KIND_ID));
}


public void updateInvolvements(Long groupId, Set<Long> involvementKindIds) {

dsl.transaction(ctx -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.finos.waltz.data.survey;

import org.finos.waltz.common.DateTimeUtilities;
import org.finos.waltz.data.involvement_group.InvolvementGroupDao;
import org.finos.waltz.model.EntityKind;
import org.finos.waltz.model.EntityReference;
import org.finos.waltz.model.HierarchyQueryScope;
Expand Down Expand Up @@ -94,7 +95,7 @@ public SurveyRunDao(DSLContext dsl) {

public SurveyRun getById(long id) {

Map<Long, List<Long>> surveyInvolvementGroupKindIds = findSurveyInvolvementGroupKindIds();
Map<Long, List<Long>> surveyInvolvementGroupKindIds = InvolvementGroupDao.findAllInvolvementsByGroupId(dsl);

return dsl
.select(SURVEY_RUN.fields())
Expand All @@ -106,7 +107,7 @@ public SurveyRun getById(long id) {

public List<SurveyRun> findForRecipient(long personId) {

Map<Long, List<Long>> surveyInvolvementGroupKindIds = findSurveyInvolvementGroupKindIds();
Map<Long, List<Long>> surveyInvolvementGroupKindIds = InvolvementGroupDao.findAllInvolvementsByGroupId(dsl);

return dsl
.select(SURVEY_RUN.fields())
Expand Down Expand Up @@ -218,7 +219,7 @@ public int issue(long surveyRunId) {

public List<SurveyRun> findBySurveyInstanceIdSelector(Select<Record1<Long>> idSelector) {

Map<Long, List<Long>> surveyInvolvementGroupKindIds = findSurveyInvolvementGroupKindIds();
Map<Long, List<Long>> surveyInvolvementGroupKindIds = InvolvementGroupDao.findAllInvolvementsByGroupId(dsl);

return dsl
.selectDistinct(SURVEY_RUN.fields())
Expand All @@ -231,7 +232,7 @@ public List<SurveyRun> findBySurveyInstanceIdSelector(Select<Record1<Long>> idSe

public List<SurveyRun> findByTemplateId(long templateId) {

Map<Long, List<Long>> surveyInvolvementGroupKindIds = findSurveyInvolvementGroupKindIds();
Map<Long, List<Long>> surveyInvolvementGroupKindIds = InvolvementGroupDao.findAllInvolvementsByGroupId(dsl);

return dsl
.select(SURVEY_RUN.fields())
Expand Down Expand Up @@ -273,11 +274,4 @@ public int updateRecipientInvolvementGroupId(long surveyRunId, Long recipientInv
.execute();
}

private Map<Long, List<Long>> findSurveyInvolvementGroupKindIds() {
return dsl
.select(ige.INVOLVEMENT_GROUP_ID, ige.INVOLVEMENT_KIND_ID)
.from(ige)
.fetchGroups(r -> r.get(ige.INVOLVEMENT_GROUP_ID), r -> r.get(ige.INVOLVEMENT_KIND_ID));
}

}
59 changes: 59 additions & 0 deletions waltz-schema/src/main/resources/liquibase/db.changelog-1.51.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,63 @@
columnName="owner_inv_kind_ids"/>
</changeSet>


<!-- 6637: migrate attestation involvement_kind_ids to their own involvement_group -->
<changeSet id="20230607-6637-11"
author="woodjes">
<addColumn tableName="attestation_run">
<column name="recipient_involvement_group_id"
type="${id.type}">
<constraints nullable="true"/>
</column>
</addColumn>
</changeSet>

<!-- 6637: migrate survey_run recipients -->
<changeSet id="20230607-6637-12"
author="woodjes">
<sql>
insert into involvement_group (name, external_id, provenance)
select concat('Recipients for attestation run: ', name), concat('RECIPIENTS_ATTESTATION_RUN_', id), 'waltz'
from attestation_run
where involvement_kind_ids is not null
and involvement_kind_ids != '';
</sql>
</changeSet>

<changeSet id="20230607-6637-13"
author="woodjes">
<sql>
update attestation_run
set recipient_involvement_group_id = (select id
from involvement_group ig
where ig.external_id =
concat('RECIPIENTS_ATTESTATION_RUN_', attestation_run.id))
where involvement_kind_ids is not null
and involvement_kind_ids != '';
</sql>
</changeSet>


<changeSet id="20230607-6637-14"
author="woodjes">
<sql>
insert into involvement_group_entry (involvement_group_id, involvement_kind_id)
select ar.recipient_involvement_group_id, ik.id
from attestation_run ar
inner join involvement_kind ik
on concat(ar.involvement_kind_ids, ';') like concat('%', ik.id, ';%')
where involvement_kind_ids is not null
and involvement_kind_ids != '';
</sql>
</changeSet>


<changeSet id="20230607-6637-15"
author="woodjes">
<comment>6637: migrate attestation_run recipients to involvement group, drop column</comment>
<dropColumn tableName="attestation_run"
columnName="involvement_kind_ids"/>
</changeSet>

</databaseChangeLog>
Loading

0 comments on commit 74907ca

Please sign in to comment.