Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bulk involvement fixes #6461

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@

import org.finos.waltz.common.FunctionUtilities;
import org.finos.waltz.model.EntityKind;
import org.finos.waltz.schema.Tables;
import org.jooq.DSLContext;
import org.jooq.Record2;
import org.jooq.SelectConditionStep;
import org.jooq.SelectOrderByStep;
import org.jooq.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

Expand All @@ -34,6 +30,7 @@
import static java.lang.String.format;
import static java.util.stream.Collectors.toMap;
import static org.finos.waltz.common.Checks.checkNotNull;
import static org.finos.waltz.schema.Tables.*;
import static org.jooq.lambda.tuple.Tuple.tuple;

/**
Expand All @@ -44,11 +41,13 @@ public class EntityAliasPopulator {

private final DSLContext dsl;


@Autowired
public EntityAliasPopulator(DSLContext dsl) {
this.dsl = dsl;
}


public Map<String, Long> fetchEntityIdLookupMap(EntityKind entityKind, Set<String> identifiers) {
checkNotNull(entityKind, "entityKind cannot be null");

Expand All @@ -57,48 +56,80 @@ public Map<String, Long> fetchEntityIdLookupMap(EntityKind entityKind, Set<Strin
return fetchApplicationAliasToIdMap(identifiers);
case PERSON:
return FunctionUtilities.time("fetch alias map people", () -> fetchPersonAliasToIdMap(identifiers));
case CHANGE_INITIATIVE:
return FunctionUtilities.time("fetch alias map change initiatives", () -> fetchChangeInitiativeAliasToIdMap(identifiers));
case ORG_UNIT:
return FunctionUtilities.time("fetch alias map org units", () -> fetchOrgUnitAliasToIdMap(identifiers));
default:
throw new IllegalArgumentException(format("Cannot find lookup map for id for entity kind: %s", entityKind));
}
}


private Map<String, Long> fetchChangeInitiativeAliasToIdMap(Set<String> aliases) {
return resolveSimpleAliases(aliases, CHANGE_INITIATIVE, CHANGE_INITIATIVE.EXTERNAL_ID, CHANGE_INITIATIVE.ID);
}


private Map<String, Long> fetchOrgUnitAliasToIdMap(Set<String> aliases) {
return resolveSimpleAliases(aliases, ORGANISATIONAL_UNIT, ORGANISATIONAL_UNIT.EXTERNAL_ID, ORGANISATIONAL_UNIT.ID);
}


private Map<String, Long> fetchApplicationAliasToIdMap(Set<String> aliases) {
return dsl
.select(Tables.APPLICATION.ASSET_CODE, Tables.APPLICATION.ID)
.from(Tables.APPLICATION)
.where(Tables.APPLICATION.ASSET_CODE.in(aliases))
.fetchSet(r -> tuple(r.get(Tables.APPLICATION.ASSET_CODE), r.get(Tables.APPLICATION.ID)))
.stream()
.collect(toMap(t -> t.v1, t -> t.v2, (v1, v2) -> v1));
return resolveSimpleAliases(aliases, APPLICATION, APPLICATION.ASSET_CODE, APPLICATION.ID);
}


private Map<String, Long> fetchPersonAliasToIdMap(Set<String> aliases) {

SelectConditionStep<Record2<String, Long>> emails = dsl
.select(Tables.PERSON.EMAIL, Tables.PERSON.ID)
.from(Tables.PERSON)
.where(Tables.PERSON.IS_REMOVED.isFalse()
.and(Tables.PERSON.EMAIL.in(aliases)));
Condition isActive = PERSON.IS_REMOVED.isFalse();

SelectConditionStep<Record2<String, Long>> name = dsl
.select(Tables.PERSON.DISPLAY_NAME, Tables.PERSON.ID)
.from(Tables.PERSON)
.where(Tables.PERSON.IS_REMOVED.isFalse()
.and(Tables.PERSON.DISPLAY_NAME.in(aliases)));
SelectConditionStep<Record2<String, Long>> emails = mkQry(aliases, PERSON, PERSON.EMAIL, PERSON.ID)
.and(isActive);

SelectConditionStep<Record2<String, Long>> empId = dsl
.select(Tables.PERSON.EMPLOYEE_ID, Tables.PERSON.ID)
.from(Tables.PERSON)
.where(Tables.PERSON.IS_REMOVED.isFalse()
.and(Tables.PERSON.EMPLOYEE_ID.in(aliases)));
SelectConditionStep<Record2<String, Long>> name = mkQry(aliases, PERSON, PERSON.DISPLAY_NAME, PERSON.ID)
.and(isActive);

SelectConditionStep<Record2<String, Long>> empId = mkQry(aliases, PERSON, PERSON.EMPLOYEE_ID, PERSON.ID)
.and(isActive);

SelectOrderByStep<Record2<String, Long>> qry = emails.union(name.union(empId));

return qry
.fetchSet(r -> tuple(r.get(0, String.class), r.get(Tables.PERSON.ID)))
.fetchSet(r -> tuple(r.get(0, String.class), r.get(PERSON.ID)))
.stream()
.collect(toMap(t -> t.v1, t -> t.v2, (v1, v2) -> v1));
}


// --- HELPERS ---

private Map<String, Long> resolveSimpleAliases(Set<String> aliases,
Table<?> table,
Field<String> externalIdField,
Field<Long> idField) {
return mkQry(aliases, table, externalIdField, idField)
.fetchSet(r -> tuple(
r.get(externalIdField),
r.get(idField)))
.stream()
.collect(toMap(
t -> t.v1,
t -> t.v2,
(v1, v2) -> v1));
}



private SelectConditionStep<Record2<String, Long>> mkQry(Set<String> aliases,
Table<?> table,
Field<String> externalIdField,
Field<Long> idField) {
return dsl
.select(externalIdField, idField)
.from(table)
.where(externalIdField.in(aliases));
}

}