Skip to content

Commit

Permalink
Merge pull request #6732 from deutschebank/db-contrib/waltz-6728-add-…
Browse files Browse the repository at this point in the history
…id-to-measurable-rating

Db contrib/waltz 6728 add id to measurable rating
  • Loading branch information
davidwatkins73 authored Aug 25, 2023
2 parents 31d225c + 7b148b1 commit 5df4579
Show file tree
Hide file tree
Showing 34 changed files with 719 additions and 352 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -304,11 +304,7 @@ private static Map<Long, List<Long>> loadMeasurableToAppIdsMapUsingTargetState(D
.eq(app.ID)
.and(mr.ENTITY_KIND.eq(EntityKind.APPLICATION.name())))
.leftJoin(mrpd)
.on(mr.ENTITY_ID
.eq(mrpd.ENTITY_ID)
.and(mr.ENTITY_KIND
.eq(mrpd.ENTITY_KIND)
.and(mr.MEASURABLE_ID.eq(mrpd.MEASURABLE_ID))))
.on(mr.ID.eq(mrpd.MEASURABLE_RATING_ID))
.leftJoin(mrp)
.on(mrp.DECOMMISSION_ID.eq(mrpd.ID))
.where(mr.ENTITY_ID.in(inScopeEntityIdSelector))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,15 @@ public class AppChangesWidgetDao {
private static final MeasurableRatingReplacement mrr = MeasurableRatingReplacement.MEASURABLE_RATING_REPLACEMENT;

private static final Field<String> ENTITY_NAME_FIELD = InlineSelectFieldFactory.mkNameField(
mrpd.ENTITY_ID,
mrpd.ENTITY_KIND,
mr.ENTITY_ID,
mr.ENTITY_KIND,
newArrayList(EntityKind.APPLICATION))
.as("entity_name");


private static final Field<String> REPLACEMENT_ENTITY_NAME_FIELD = InlineSelectFieldFactory.mkNameField(
mrr.ENTITY_ID,
mrr.ENTITY_KIND,
newArrayList(EntityKind.APPLICATION))
.as("entity_name");

Expand Down Expand Up @@ -227,20 +234,21 @@ private Map<Long, List<Tuple2<EntityReference, LocalDate>>> fetchDecommissionDat
.withDayOfMonth(1);

return dsl
.select(mrpd.ENTITY_ID,
mrpd.MEASURABLE_ID,
.select(mr.ENTITY_ID,
mr.MEASURABLE_ID,
mrpd.PLANNED_DECOMMISSION_DATE)
.select(ENTITY_NAME_FIELD)
.from(mrpd)
.where(mrpd.ENTITY_ID.in(inScopeEntityIdSelector)
.and(mrpd.MEASURABLE_ID.in(backingMeasurableEntityIds))
.and(mrpd.ENTITY_KIND.eq(EntityKind.APPLICATION.name()))
.innerJoin(mr).on(mrpd.MEASURABLE_RATING_ID.eq(mr.ID))
.where(mr.ENTITY_ID.in(inScopeEntityIdSelector)
.and(mr.MEASURABLE_ID.in(backingMeasurableEntityIds))
.and(mr.ENTITY_KIND.eq(EntityKind.APPLICATION.name()))
.and(mrpd.PLANNED_DECOMMISSION_DATE.ge(toSqlDate(startOfYear)))
.and(targetDateCondition))
.fetchGroups(
r -> r.get(mrpd.MEASURABLE_ID),
r -> r.get(mr.MEASURABLE_ID),
r -> tuple(
mkRef(EntityKind.APPLICATION, r.get(mrpd.ENTITY_ID), r.get(ENTITY_NAME_FIELD)),
mkRef(EntityKind.APPLICATION, r.get(mr.ENTITY_ID), r.get(ENTITY_NAME_FIELD)),
toLocalDate(r.get(mrpd.PLANNED_DECOMMISSION_DATE))));
}

Expand All @@ -256,21 +264,22 @@ private Map<Long, List<Tuple2<EntityReference, LocalDate>>> fetchReplacementAppI

return dsl
.select(mrr.ENTITY_ID,
mrpd.MEASURABLE_ID,
mr.MEASURABLE_ID,
mrr.PLANNED_COMMISSION_DATE)
.select(ENTITY_NAME_FIELD)
.select(REPLACEMENT_ENTITY_NAME_FIELD)
.from(mrr)
.innerJoin(mrpd)
.on(mrr.DECOMMISSION_ID.eq(mrpd.ID))
.innerJoin(mr).on(mrpd.MEASURABLE_RATING_ID.eq(mr.ID))
.where(mrr.ENTITY_ID.in(inScopeEntityIdSelector)
.and(mrr.ENTITY_KIND.eq(EntityKind.APPLICATION.name()))
.and(mrpd.MEASURABLE_ID.in(backingMeasurableEntityIds))
.and(mr.MEASURABLE_ID.in(backingMeasurableEntityIds))
.and(mrr.PLANNED_COMMISSION_DATE.ge(toSqlDate(startOfYear)))
.and(targetDateCondition))
.fetchGroups(
r -> r.get(mrpd.MEASURABLE_ID),
r -> r.get(mr.MEASURABLE_ID),
r -> tuple(
mkRef(EntityKind.APPLICATION, r.get(mrpd.ENTITY_ID), r.get(ENTITY_NAME_FIELD)),
mkRef(EntityKind.APPLICATION, r.get(mrr.ENTITY_ID), r.get(ENTITY_NAME_FIELD)),
toLocalDate(r.get(mrr.PLANNED_COMMISSION_DATE))));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,7 @@ private Set<MeasurableCostEntry> fetchCostData(DSLContext dsl,
c.AMOUNT)
.from(mr)
.innerJoin(ck).on(ck.ID.in(costKindIds))
.leftJoin(a).on(mr.ENTITY_ID.eq(a.ENTITY_ID)
.and(mr.ENTITY_KIND.eq(a.ENTITY_KIND))
.and(mr.MEASURABLE_ID.eq(a.MEASURABLE_ID))
.leftJoin(a).on(mr.ID.eq(a.MEASURABLE_RATING_ID)
.and(a.ALLOCATION_SCHEME_ID.eq(allocationSchemeId)))
.leftJoin(c).on(mr.ENTITY_ID.eq(c.ENTITY_ID)
.and(mr.ENTITY_KIND.eq(c.ENTITY_KIND))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
import org.finos.waltz.model.allocation.MeasurablePercentageChange;
import org.finos.waltz.schema.tables.records.AllocationRecord;
import org.jooq.DSLContext;
import org.jooq.DeleteConditionStep;
import org.jooq.Record;
import org.jooq.Record3;
import org.jooq.RecordMapper;
import org.jooq.SelectConditionStep;
import org.jooq.UpdateConditionStep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

Expand All @@ -41,8 +41,8 @@
import java.util.Optional;

import static org.finos.waltz.common.MapUtilities.groupBy;
import static org.finos.waltz.data.JooqUtilities.readRef;
import static org.finos.waltz.schema.Tables.ALLOCATION;
import static org.finos.waltz.schema.Tables.MEASURABLE_RATING;

@Repository
public class AllocationDao {
Expand All @@ -53,9 +53,9 @@ public class AllocationDao {
public static final RecordMapper<Record, Allocation> TO_DOMAIN_MAPPER = record -> {
AllocationRecord allocationRecord = record.into(ALLOCATION);
return ImmutableAllocation.builder()
.id(allocationRecord.getId())
.schemeId(allocationRecord.getAllocationSchemeId())
.measurableId(allocationRecord.getMeasurableId())
.entityReference(readRef(allocationRecord, ALLOCATION.ENTITY_KIND, ALLOCATION.ENTITY_ID))
.measurableRatingId(allocationRecord.getMeasurableRatingId())
.percentage(allocationRecord.getAllocationPercentage())
.lastUpdatedAt(allocationRecord.getLastUpdatedAt().toLocalDateTime())
.lastUpdatedBy(allocationRecord.getLastUpdatedBy())
Expand All @@ -76,8 +76,9 @@ public List<Allocation> findByEntity(EntityReference ref) {
return dsl
.select(ALLOCATION.fields())
.from(ALLOCATION)
.where(ALLOCATION.ENTITY_KIND.eq(ref.kind().name()))
.and(ALLOCATION.ENTITY_ID.eq(ref.id()))
.innerJoin(MEASURABLE_RATING).on(ALLOCATION.MEASURABLE_RATING_ID.eq(MEASURABLE_RATING.ID))
.where(MEASURABLE_RATING.ENTITY_KIND.eq(ref.kind().name()))
.and(MEASURABLE_RATING.ENTITY_ID.eq(ref.id()))
.fetch(TO_DOMAIN_MAPPER);
}

Expand All @@ -87,9 +88,10 @@ public List<Allocation> findByEntityAndScheme(EntityReference ref,
return dsl
.select(ALLOCATION.fields())
.from(ALLOCATION)
.innerJoin(MEASURABLE_RATING).on(ALLOCATION.MEASURABLE_RATING_ID.eq(MEASURABLE_RATING.ID))
.where(ALLOCATION.ALLOCATION_SCHEME_ID.eq(schemeId))
.and(ALLOCATION.ENTITY_KIND.eq(ref.kind().name()))
.and(ALLOCATION.ENTITY_ID.eq(ref.id()))
.and(MEASURABLE_RATING.ENTITY_KIND.eq(ref.kind().name()))
.and(MEASURABLE_RATING.ENTITY_ID.eq(ref.id()))
.fetch(TO_DOMAIN_MAPPER);
}

Expand All @@ -98,24 +100,13 @@ public List<Allocation> findByMeasurableIdAndScheme(long measurableId, long sche
return dsl
.select(ALLOCATION.fields())
.from(ALLOCATION)
.where(ALLOCATION.MEASURABLE_ID.eq(measurableId))
.innerJoin(MEASURABLE_RATING).on(ALLOCATION.MEASURABLE_RATING_ID.eq(MEASURABLE_RATING.ID))
.where(MEASURABLE_RATING.MEASURABLE_ID.eq(measurableId))
.and(ALLOCATION.ALLOCATION_SCHEME_ID.eq(schemeId))
.fetch(TO_DOMAIN_MAPPER);
}


private SelectConditionStep<Record3<Long, Long, String>> findAllocationsBySchemeId(long schemeId){
return dsl
.select(ALLOCATION.MEASURABLE_ID,
ALLOCATION.ENTITY_ID,
ALLOCATION.ENTITY_KIND)
.from(ALLOCATION)
.where(ALLOCATION.ALLOCATION_SCHEME_ID.eq(schemeId));
}


public Boolean updateAllocations(EntityReference ref,
long scheme,
public Boolean updateAllocations(long scheme,
Collection<MeasurablePercentageChange> changes,
String username) {

Expand All @@ -126,28 +117,33 @@ public Boolean updateAllocations(EntityReference ref,
dsl.transaction(tx -> {
DSLContext txDsl = tx.dsl();

Collection<AllocationRecord> recordsToDelete = mkRecordsFromChanges(
ref,
Collection<MeasurablePercentageChange> toRemove = changesByOp.get(Operation.REMOVE);
Collection<MeasurablePercentageChange> toUpdate = changesByOp.get(Operation.UPDATE);
Collection<MeasurablePercentageChange> toInsert = changesByOp.get(Operation.ADD);

Collection<DeleteConditionStep<AllocationRecord>> recordsToDelete = mkRemovalsFromChanges(
txDsl,
scheme,
changesByOp.get(Operation.REMOVE),
username);
toRemove);

Collection<AllocationRecord> recordsToUpdate = mkRecordsFromChanges(
ref,
Collection<UpdateConditionStep<AllocationRecord>> recordsToUpdate = mkUpdatesFromChanges(
txDsl,
scheme,
changesByOp.get(Operation.UPDATE),
toUpdate,
username);

Collection<AllocationRecord> recordsToInsert = mkRecordsFromChanges(
ref,
txDsl,
scheme,
changesByOp.get(Operation.ADD),
toInsert,
username);

txDsl.batchDelete(recordsToDelete)
txDsl.batch(recordsToDelete)
.execute();
txDsl.batchUpdate(recordsToUpdate)

txDsl.batch(recordsToUpdate)
.execute();

txDsl.batchInsert(recordsToInsert)
.execute();
});
Expand All @@ -158,29 +154,72 @@ public Boolean updateAllocations(EntityReference ref,

// -- HELPERS ----

private static Collection<AllocationRecord> mkRecordsFromChanges(EntityReference ref,
long scheme,
Collection<MeasurablePercentageChange> changes,
String username) {
private static Collection<AllocationRecord> mkRecordsFromChanges(DSLContext tx,
long scheme,
Collection<MeasurablePercentageChange> changes,
String username) {
return CollectionUtilities.map(
ListUtilities.ensureNotNull(changes),
c -> mkRecordFromChange(ref, scheme, c, username));
c -> mkRecordFromChange(tx, scheme, c, username));
}

private static Collection<DeleteConditionStep<AllocationRecord>> mkRemovalsFromChanges(DSLContext tx,
long scheme,
Collection<MeasurablePercentageChange> changes) {
return CollectionUtilities.map(
ListUtilities.ensureNotNull(changes),
c -> mkRemovalFromChange(tx, c.measurablePercentage().measurableRatingId(), scheme));
}

private static AllocationRecord mkRecordFromChange(EntityReference ref, long scheme, MeasurablePercentageChange c, String username) {
AllocationRecord record = new AllocationRecord();
private static Collection<UpdateConditionStep<AllocationRecord>> mkUpdatesFromChanges(DSLContext tx,
long scheme,
Collection<MeasurablePercentageChange> changes,
String username) {
return CollectionUtilities.map(
ListUtilities.ensureNotNull(changes),
c -> mkUpdateFromChange(tx, scheme, c, username));
}


private static AllocationRecord mkRecordFromChange(DSLContext tx,
long scheme,
MeasurablePercentageChange c,
String username) {
AllocationRecord record = tx.newRecord(ALLOCATION);
record.setAllocationSchemeId(scheme);
record.setEntityId(ref.id());
record.setEntityKind(ref.kind().name());
record.setMeasurableId(c.measurablePercentage().measurableId());
record.setMeasurableRatingId(c.measurablePercentage().measurableRatingId());
record.setAllocationPercentage(c.measurablePercentage().percentage());
record.setLastUpdatedBy(username);
record.setLastUpdatedAt(DateTimeUtilities.nowUtcTimestamp());
record.setProvenance("waltz");
return record;
}


private static DeleteConditionStep<AllocationRecord> mkRemovalFromChange(DSLContext tx,
long measurableRatingId,
long scheme) {
return tx
.deleteFrom(ALLOCATION)
.where(ALLOCATION.ALLOCATION_SCHEME_ID.eq(scheme)
.and(ALLOCATION.MEASURABLE_RATING_ID.eq(measurableRatingId)));
}


private static UpdateConditionStep<AllocationRecord> mkUpdateFromChange(DSLContext tx,
long scheme,
MeasurablePercentageChange c,
String username) {
return tx
.update(ALLOCATION)
.set(ALLOCATION.ALLOCATION_PERCENTAGE, c.measurablePercentage().percentage())
.set(ALLOCATION.LAST_UPDATED_AT, DateTimeUtilities.nowUtcTimestamp())
.set(ALLOCATION.LAST_UPDATED_BY, username)
.where(ALLOCATION.ALLOCATION_SCHEME_ID.eq(scheme)
.and(ALLOCATION.MEASURABLE_RATING_ID.eq(c.measurablePercentage().measurableRatingId())));

}

}


Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,12 @@ public String getRequiredRatingEditRole(EntityReference reference) {

Condition condition = mkCondition(reference);

Condition measurableToRatingJoinCondition = MEASURABLE_RATING.MEASURABLE_ID.eq(MEASURABLE_RATING_PLANNED_DECOMMISSION.MEASURABLE_ID)
.and(MEASURABLE_RATING_PLANNED_DECOMMISSION.ENTITY_ID.eq(MEASURABLE_RATING.ENTITY_ID)
.and(MEASURABLE_RATING_PLANNED_DECOMMISSION.ENTITY_KIND.eq(MEASURABLE_RATING.ENTITY_KIND)));

return dsl
.selectDistinct(MEASURABLE_CATEGORY.RATING_EDITOR_ROLE)
.from(MEASURABLE_CATEGORY)
.innerJoin(MEASURABLE).on(MEASURABLE_CATEGORY.ID.eq(MEASURABLE.MEASURABLE_CATEGORY_ID))
.leftJoin(MEASURABLE_RATING).on(MEASURABLE_RATING.MEASURABLE_ID.eq(MEASURABLE.ID))
.leftJoin(MEASURABLE_RATING_PLANNED_DECOMMISSION).on(measurableToRatingJoinCondition)
.leftJoin(MEASURABLE_RATING_PLANNED_DECOMMISSION).on(MEASURABLE_RATING.ID.eq(MEASURABLE_RATING_PLANNED_DECOMMISSION.ID))
.where(condition)
.fetchOne()
.get(MEASURABLE_CATEGORY.RATING_EDITOR_ROLE);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,11 @@ private Select<Record1<Long>> mkForDirectEntityKind(IdSelectionOptions options)
checkTrue(options.scope() == HierarchyQueryScope.EXACT, "Can only calculate application based selectors with exact scopes");

SelectConditionStep<Record1<Long>> measurablesViaReplacements = DSL
.select(MEASURABLE_RATING_PLANNED_DECOMMISSION.MEASURABLE_ID)
.from(MEASURABLE_RATING_PLANNED_DECOMMISSION)
.innerJoin(MEASURABLE_RATING_REPLACEMENT)
.select(MEASURABLE_RATING.MEASURABLE_ID)
.from(MEASURABLE_RATING_REPLACEMENT)
.innerJoin(MEASURABLE_RATING_PLANNED_DECOMMISSION)
.on(MEASURABLE_RATING_PLANNED_DECOMMISSION.ID.eq(MEASURABLE_RATING_REPLACEMENT.DECOMMISSION_ID))
.innerJoin(MEASURABLE_RATING).on(MEASURABLE_RATING_PLANNED_DECOMMISSION.MEASURABLE_RATING_ID.eq(MEASURABLE_RATING.ID))
.where(MEASURABLE_RATING_REPLACEMENT.ENTITY_ID.eq(options.entityReference().id())
.and(MEASURABLE_RATING_REPLACEMENT.ENTITY_KIND.eq(options.entityReference().kind().name())));

Expand Down
Loading

0 comments on commit 5df4579

Please sign in to comment.