Skip to content

Commit

Permalink
Merge pull request #6096 from deutschebank/db-contrib/waltz-6082-over…
Browse files Browse the repository at this point in the history
…lay-diagram-tweaks

Db contrib/waltz 6082 overlay diagram tweaks
  • Loading branch information
davidwatkins73 authored Jun 14, 2022
2 parents 2ddf3a9 + 7cf883a commit 35deb55
Show file tree
Hide file tree
Showing 44 changed files with 1,294 additions and 559 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package org.finos.waltz.data.aggregate_overlay_diagram;

import org.finos.waltz.common.DateTimeUtilities;
import org.finos.waltz.model.aggregate_overlay_diagram.AggregateOverlayDiagramPreset;
import org.finos.waltz.model.aggregate_overlay_diagram.ImmutableAggregateOverlayDiagramPreset;
import org.finos.waltz.model.aggregate_overlay_diagram.OverlayDiagramPresetCreateCommand;
import org.finos.waltz.schema.tables.records.AggregateOverlayDiagramPresetRecord;
import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Set;

import static org.finos.waltz.common.DateTimeUtilities.toLocalDateTime;
import static org.finos.waltz.schema.Tables.AGGREGATE_OVERLAY_DIAGRAM_PRESET;

@Repository
public class AggregateOverlayDiagramPresetDao {

protected static final RecordMapper<? super Record, ? extends AggregateOverlayDiagramPreset> TO_DOMAIN_MAPPER = r -> {
AggregateOverlayDiagramPresetRecord record = r.into(AGGREGATE_OVERLAY_DIAGRAM_PRESET);
return ImmutableAggregateOverlayDiagramPreset.builder()
.id(record.getId())
.name(record.getName())
.diagramId(record.getDiagramId())
.description(record.getDescription())
.externalId(record.getExternalId())
.overlayConfig(record.getOverlayConfig())
.filterConfig(record.getFilterConfig())
.lastUpdatedAt(toLocalDateTime(record.getLastUpdatedAt()))
.lastUpdatedBy(record.getLastUpdatedBy())
.provenance(record.getProvenance())
.build();
};

private final DSLContext dsl;

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

public Set<AggregateOverlayDiagramPreset> findPresetsForDiagram(Long diagramId) {
return dsl
.select(AGGREGATE_OVERLAY_DIAGRAM_PRESET.fields())
.from(AGGREGATE_OVERLAY_DIAGRAM_PRESET)
.where(AGGREGATE_OVERLAY_DIAGRAM_PRESET.DIAGRAM_ID.eq(diagramId))
.fetchSet(TO_DOMAIN_MAPPER::map);
}

public int create(OverlayDiagramPresetCreateCommand createCommand, String username) {

AggregateOverlayDiagramPresetRecord record = dsl.newRecord(AGGREGATE_OVERLAY_DIAGRAM_PRESET);

record.setDiagramId(createCommand.diagramId());
record.setName(createCommand.name());
record.setDescription(createCommand.description());
record.setExternalId(createCommand.externalId());
record.setOverlayConfig(createCommand.overlayConfig());
record.setFilterConfig(createCommand.filterConfig());
record.setLastUpdatedAt(DateTimeUtilities.nowUtcTimestamp());
record.setLastUpdatedBy(username);
record.setProvenance("waltz");

return dsl
.insertInto(AGGREGATE_OVERLAY_DIAGRAM_PRESET)
.set(record)
.execute();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,59 @@
import org.finos.waltz.model.EntityReference;
import org.finos.waltz.model.aggregate_overlay_diagram.AggregateOverlayDiagram;
import org.finos.waltz.model.aggregate_overlay_diagram.ImmutableAggregateOverlayDiagram;
import org.finos.waltz.schema.tables.Application;
import org.finos.waltz.schema.tables.MeasurableRating;
import org.finos.waltz.schema.tables.MeasurableRatingPlannedDecommission;
import org.finos.waltz.schema.tables.MeasurableRatingReplacement;
import org.finos.waltz.schema.tables.records.AggregateOverlayDiagramRecord;
import org.jooq.*;
import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.Record;
import org.jooq.Record1;
import org.jooq.Record2;
import org.jooq.Record3;
import org.jooq.RecordMapper;
import org.jooq.Select;
import org.jooq.SelectConditionStep;
import org.jooq.impl.DSL;
import org.jooq.lambda.tuple.Tuple2;

import java.sql.Date;
import java.time.LocalDate;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static java.lang.String.format;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;
import static java.util.stream.Collectors.*;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.mapping;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toSet;
import static org.finos.waltz.common.DateTimeUtilities.toLocalDateTime;
import static org.finos.waltz.common.MapUtilities.groupBy;
import static org.finos.waltz.common.SetUtilities.fromCollection;
import static org.finos.waltz.data.JooqUtilities.readRef;
import static org.finos.waltz.schema.Tables.*;
import static org.finos.waltz.schema.Tables.AGGREGATE_OVERLAY_DIAGRAM;
import static org.finos.waltz.schema.Tables.AGGREGATE_OVERLAY_DIAGRAM_CELL_DATA;
import static org.finos.waltz.schema.Tables.APPLICATION;
import static org.finos.waltz.schema.Tables.ENTITY_HIERARCHY;
import static org.finos.waltz.schema.Tables.MEASURABLE_RATING;
import static org.finos.waltz.schema.Tables.MEASURABLE_RATING_PLANNED_DECOMMISSION;
import static org.finos.waltz.schema.Tables.MEASURABLE_RATING_REPLACEMENT;
import static org.finos.waltz.schema.tables.EntityRelationship.ENTITY_RELATIONSHIP;
import static org.jooq.lambda.tuple.Tuple.tuple;

public class AggregateOverlayDiagramUtilities {

private static final MeasurableRating mr = MEASURABLE_RATING;
private static final Application app = APPLICATION;
private static final MeasurableRatingPlannedDecommission mrpd = MEASURABLE_RATING_PLANNED_DECOMMISSION;
private static final MeasurableRatingReplacement mrp = MEASURABLE_RATING_REPLACEMENT;

protected static final RecordMapper<? super Record, ? extends AggregateOverlayDiagram> TO_DOMAIN_MAPPER = r -> {
AggregateOverlayDiagramRecord record = r.into(AGGREGATE_OVERLAY_DIAGRAM);
return ImmutableAggregateOverlayDiagram.builder()
Expand Down Expand Up @@ -83,7 +112,8 @@ protected static SelectConditionStep<Record3<String, String, Long>> selectCellMa
protected static Map<String, Set<Long>> loadCellExtIdToAggregatedEntities(DSLContext dsl,
long diagramId,
EntityKind aggregatedEntityKind,
Select<Record1<Long>> inScopeEntityIdSelector) {
Select<Record1<Long>> inScopeEntityIdSelector,
Optional<LocalDate> targetStateDate) {

Set<Tuple2<String, EntityReference>> cellMappings = loadExpandedCellMappingsForDiagram(dsl, diagramId);

Expand All @@ -103,7 +133,8 @@ protected static Map<String, Set<Long>> loadCellExtIdToAggregatedEntities(DSLCon
dsl,
aggregatedEntityKind,
inScopeEntityIdSelector,
backingMeasurableEntityIds);
backingMeasurableEntityIds,
targetStateDate);

return cellBackingEntitiesByCellExtId
.entrySet()
Expand Down Expand Up @@ -135,11 +166,12 @@ protected static Map<String, Set<Long>> loadCellExtIdToAggregatedEntities(DSLCon
private static Map<Long, List<Long>> findMeasurableIdToAggregatedEntityIdMap(DSLContext dsl,
EntityKind aggregatedEntityKind,
Select<Record1<Long>> inScopeEntityIdSelector,
Set<Long> backingEntityIds) {
Set<Long> backingEntityIds,
Optional<LocalDate> targetStateDate) {

switch (aggregatedEntityKind) {
case APPLICATION:
return loadMeasurableToAppIdsMap(dsl, inScopeEntityIdSelector, backingEntityIds);
return loadMeasurableToAppIdsMap(dsl, inScopeEntityIdSelector, backingEntityIds, targetStateDate);
case CHANGE_INITIATIVE:
return loadMeasurableToCIIdsMap(dsl, inScopeEntityIdSelector, backingEntityIds);
default:
Expand Down Expand Up @@ -181,7 +213,71 @@ private static Map<Long, List<Long>> loadMeasurableToCIIdsMap(DSLContext dsl,

private static Map<Long, List<Long>> loadMeasurableToAppIdsMap(DSLContext dsl,
Select<Record1<Long>> inScopeEntityIdSelector,
Set<Long> backingEntityReferences) {
Set<Long> backingEntityReferences,
Optional<LocalDate> targetStateDate) {
return targetStateDate
.map(targetDate -> loadMeasurableToAppIdsMapUsingTargetState(
dsl,
inScopeEntityIdSelector,
backingEntityReferences,
targetDate))
.orElse(loadMeasurableToAppIdsMapIgnoringTargetDate(
dsl,
inScopeEntityIdSelector,
backingEntityReferences));
}


private static Map<Long, List<Long>> loadMeasurableToAppIdsMapUsingTargetState(DSLContext dsl,
Select<Record1<Long>> inScopeEntityIdSelector,
Set<Long> backingEntityReferences,
LocalDate targetStateDate) {
Date targetDate = targetStateDate == null
? null
: Date.valueOf(targetStateDate);


Field<Date> decommDate = DSL.coalesce(
mrpd.PLANNED_DECOMMISSION_DATE,
app.PLANNED_RETIREMENT_DATE);


Field<Long> appIdCol = DSL
.when(decommDate.isNull().or(decommDate.gt(targetDate)),
mr.ENTITY_ID)
.when(mrp.PLANNED_COMMISSION_DATE.lt(targetDate),
mrp.ENTITY_ID)
.otherwise((Long) null);

SelectConditionStep<Record2<Long, Long>> qry = dsl
.select(mr.MEASURABLE_ID,
appIdCol)
.from(mr)
.innerJoin(app)
.on(mr.ENTITY_ID
.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))))
.leftJoin(mrp)
.on(mrp.DECOMMISSION_ID.eq(mrpd.ID))
.where(mr.ENTITY_ID.in(inScopeEntityIdSelector))
.and(mr.MEASURABLE_ID.in(backingEntityReferences));

return qry
.fetchGroups(
mr.MEASURABLE_ID,
appIdCol);
}


private static Map<Long, List<Long>> loadMeasurableToAppIdsMapIgnoringTargetDate(DSLContext dsl,
Select<Record1<Long>> inScopeEntityIdSelector,
Set<Long> backingEntityReferences) {
return dsl
.selectDistinct(MEASURABLE_RATING.MEASURABLE_ID, MEASURABLE_RATING.ENTITY_ID)
.from(MEASURABLE_RATING)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static java.lang.String.format;
Expand All @@ -31,13 +33,15 @@ public AggregatedEntitiesWidgetDao(DSLContext dsl) {

public Set<AggregatedEntitiesWidgetDatum> findWidgetData(long diagramId,
EntityKind aggregatedEntityKind,
Select<Record1<Long>> inScopeEntityIdSelector) {
Select<Record1<Long>> inScopeEntityIdSelector,
Optional<LocalDate> targetStateDate) {

Map<String, Set<Long>> cellExtIdsToAggregatedEntities = loadCellExtIdToAggregatedEntities(
dsl,
diagramId,
aggregatedEntityKind,
inScopeEntityIdSelector);
inScopeEntityIdSelector,
targetStateDate);

Map<Long, String> entityIdToNameMap = loadEntityIdToNameMap(aggregatedEntityKind, inScopeEntityIdSelector);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static java.util.Collections.emptySet;
Expand Down Expand Up @@ -62,13 +64,15 @@ public AppCostWidgetDao(DSLContext dsl) {
public Set<CostWidgetDatum> findWidgetData(long diagramId,
Set<Long> costKindIds,
long allocationSchemeId,
Select<Record1<Long>> inScopeApplicationSelector) {
Select<Record1<Long>> inScopeApplicationSelector,
Optional<LocalDate> targetStateDate) {

Map<String, Set<Long>> cellExtIdsToBackingEntities = loadCellExtIdToAggregatedEntities(
dsl,
diagramId,
EntityKind.APPLICATION,
inScopeApplicationSelector);
inScopeApplicationSelector,
targetStateDate);

Set<MeasurableCostEntry> costData = fetchCostData(
dsl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.time.LocalDate;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static java.util.stream.Collectors.toSet;
Expand Down Expand Up @@ -41,7 +42,8 @@ public Set<CountWidgetDatum> findWidgetData(long diagramId,
dsl,
diagramId,
EntityKind.APPLICATION,
inScopeApplicationSelector);
inScopeApplicationSelector,
Optional.empty());

Set<Long> appIds = cellExtIdsToAggregatedEntities.values()
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
import org.finos.waltz.model.aggregate_overlay_diagram.overlay.AssessmentRatingsWidgetDatum;
import org.finos.waltz.model.aggregate_overlay_diagram.overlay.ImmutableAssessmentRatingCount;
import org.finos.waltz.model.aggregate_overlay_diagram.overlay.ImmutableAssessmentRatingsWidgetDatum;
import org.finos.waltz.model.utils.IdUtilities;
import org.finos.waltz.schema.tables.AssessmentRating;
import org.jooq.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

Expand Down Expand Up @@ -41,13 +41,15 @@ public AssessmentRatingWidgetDao(DSLContext dsl,
public Set<AssessmentRatingsWidgetDatum> findWidgetData(long diagramId,
EntityKind aggregatedEntityKind,
Long assessmentId,
Select<Record1<Long>> inScopeEntityIdSelector) {
Select<Record1<Long>> inScopeEntityIdSelector,
Optional<LocalDate> targetStateDate) {

Map<String, Set<Long>> cellExtIdsToAggregatedEntities = loadCellExtIdToAggregatedEntities(
dsl,
diagramId,
aggregatedEntityKind,
inScopeEntityIdSelector);
inScopeEntityIdSelector,
targetStateDate);

Set<Long> diagramEntityIds = cellExtIdsToAggregatedEntities.values()
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.time.LocalDate;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

import static java.util.stream.Collectors.toSet;
Expand Down Expand Up @@ -42,7 +43,8 @@ public Set<TargetCostWidgetDatum> findWidgetData(long diagramId,
dsl,
diagramId,
EntityKind.APPLICATION,
inScopeApplicationSelector);
inScopeApplicationSelector,
Optional.empty());

Set<Long> diagramAppIds = cellExtIdsToAggregatedEntities
.values()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,8 @@ public static void main(String[] args) throws ParseException {
long schemeId = 1L;
long allocationSchemeId = 1L;

ApplicationIdSelectorFactory selectorFactory = new ApplicationIdSelectorFactory();
ApplicationIdSelectorFactory selectorFactory = new ApplicationIdSelectorFactory();
Select<Record1<Long>> appIds = selectorFactory.apply(selectionOptions);
Set<CostWidgetDatum> widgetData = dao.findWidgetData(diagramId, SetUtilities.asSet(schemeId), allocationSchemeId, appIds);
System.out.println(widgetData);

}

Expand Down
Loading

0 comments on commit 35deb55

Please sign in to comment.