Search in sources :

Example 1 with ScenarioRatingItemRecord

use of org.finos.waltz.schema.tables.records.ScenarioRatingItemRecord in project waltz by khartec.

the class ScenarioPopulator method mkRecordsFromRow.

/**
 * Each row may potentially yeild up to two records to be inserted:
 * <ul>
 *     <li>Current app (if scenario is before decomm date)</li>
 *     <li>Replacement app (if after scenario effective date)</li>
 * </ul>
 *
 * Note: We rely upon the equivalence of records to prevent duplicates (hence passing
 * in a single copy of now).
 *
 * @param tx  Transactional DSL context
 * @param r  Row from the initial query
 * @param now  timestamp representing now
 * @return  Stream of 0-2 records
 */
private static Stream<ScenarioRatingItemRecord> mkRecordsFromRow(DSLContext tx, Record r, Timestamp now) {
    Stream.Builder<ScenarioRatingItemRecord> sbuilder = Stream.builder();
    if (r.get(inclCurrent)) {
        ScenarioRatingItemRecord item = mkBaseRecord(tx, r, now);
        item.setDomainItemKind(r.get(mr.ENTITY_KIND));
        item.setDomainItemId(r.get(mr.ENTITY_ID));
        item.setRating(r.get(mr.RATING));
        item.setDescription(mkDecomMessage(r));
        sbuilder.add(item);
    }
    if (r.get(inclReplacement)) {
        ScenarioRatingItemRecord item = mkBaseRecord(tx, r, now);
        item.setDomainItemKind(r.get(repl.ENTITY_KIND));
        item.setDomainItemId(r.get(repl.ENTITY_ID));
        item.setRating("G");
        item.setDescription(mkCommMessage(r));
        sbuilder.add(item);
    }
    return sbuilder.build();
}
Also used : ScenarioRatingItemRecord(org.finos.waltz.schema.tables.records.ScenarioRatingItemRecord) Stream(java.util.stream.Stream)

Example 2 with ScenarioRatingItemRecord

use of org.finos.waltz.schema.tables.records.ScenarioRatingItemRecord in project waltz by khartec.

the class RoadmapGenerator method populateScenario.

private void populateScenario(ApplicationContext ctx, Scenario scenario) {
    ScenarioAxisItemDao scenarioAxisItemDao = getScenarioAxisItemDao(ctx);
    MeasurableRatingService measurableRatingService = getMeasurableRatingService(ctx);
    scenario.id().ifPresent(scenarioId -> {
        Collection<ScenarioAxisItem> axisItems = scenarioAxisItemDao.findForScenarioId(scenarioId);
        Map<AxisOrientation, Collection<ScenarioAxisItem>> byOrientation = groupBy(ScenarioAxisItem::axisOrientation, axisItems);
        IdSelectionOptions options = mkOpts(scenario.entityReference());
        Map<Long, Collection<MeasurableRating>> ratingsByMeasurableId = groupBy(MeasurableRating::measurableId, measurableRatingService.findByMeasurableIdSelector(options));
        List<ScenarioRatingItemRecord> scenarioRatingItems = IntStream.range(0, randomIntBetween(10, 300)).mapToObj(i -> tuple(randomPick(byOrientation.get(AxisOrientation.COLUMN)), randomPick(byOrientation.get(AxisOrientation.ROW)))).map(t -> t.map1(d -> d.domainItem().id()).map2(d -> d.domainItem().id())).map(t -> t.concat(tuple(randomPick(ratingsByMeasurableId.get(t.v1)), randomPick(ratingsByMeasurableId.get(t.v2))))).map(t -> {
            MeasurableRating rating = t.v3 != null ? t.v3 : t.v4;
            ScenarioRatingItemRecord record = getDsl(ctx).newRecord(SCENARIO_RATING_ITEM);
            record.setScenarioId(scenarioId);
            record.setColumnId(t.v1);
            record.setColumnKind(EntityKind.MEASURABLE.name());
            record.setRowId(t.v2);
            record.setRowKind(EntityKind.MEASURABLE.name());
            record.setDomainItemId(rating.entityReference().id());
            record.setDomainItemKind(rating.entityReference().kind().name());
            record.setRating(String.valueOf(rating.rating()));
            record.setLastUpdatedBy("admin");
            record.setLastUpdatedAt(DateTimeUtilities.nowUtcTimestamp());
            return record;
        }).collect(Collectors.toList());
        getDsl(ctx).batchInsert(scenarioRatingItems).execute();
    });
}
Also used : IntStream(java.util.stream.IntStream) ScenarioAxisItemDao(org.finos.waltz.data.scenario.ScenarioAxisItemDao) java.util(java.util) IdSelectionOptions(org.finos.waltz.model.IdSelectionOptions) DSL(org.jooq.impl.DSL) MeasurableRating(org.finos.waltz.model.measurable_rating.MeasurableRating) Tables(org.finos.waltz.schema.Tables) EntityKind(org.finos.waltz.model.EntityKind) RandomUtilities.randomIntBetween(org.finos.waltz.common.RandomUtilities.randomIntBetween) SetUtilities.asSet(org.finos.waltz.common.SetUtilities.asSet) EntityReference.mkRef(org.finos.waltz.model.EntityReference.mkRef) Scenario(org.finos.waltz.model.scenario.Scenario) SelectConditionStep(org.jooq.SelectConditionStep) ObjectUtilities.any(org.finos.waltz.common.ObjectUtilities.any) MeasurableRatingDao(org.finos.waltz.data.measurable_rating.MeasurableRatingDao) Measurable(org.finos.waltz.model.measurable.Measurable) Record1(org.jooq.Record1) MapUtilities.groupBy(org.finos.waltz.common.MapUtilities.groupBy) DSLContext(org.jooq.DSLContext) ROADMAP(org.finos.waltz.schema.tables.Roadmap.ROADMAP) MeasurableDao(org.finos.waltz.data.measurable.MeasurableDao) ScenarioDao(org.finos.waltz.data.scenario.ScenarioDao) IdSelectionOptions.mkOpts(org.finos.waltz.model.IdSelectionOptions.mkOpts) AxisOrientation(org.finos.waltz.model.AxisOrientation) MeasurableRatingService(org.finos.waltz.service.measurable_rating.MeasurableRatingService) ApplicationContext(org.springframework.context.ApplicationContext) Collectors(java.util.stream.Collectors) ScenarioRatingItemRecord(org.finos.waltz.schema.tables.records.ScenarioRatingItemRecord) ScenarioAxisItem(org.finos.waltz.model.scenario.ScenarioAxisItem) ListUtilities.filter(org.finos.waltz.common.ListUtilities.filter) Tuple.tuple(org.jooq.lambda.tuple.Tuple.tuple) DateTimeUtilities(org.finos.waltz.common.DateTimeUtilities) RoadmapDao(org.finos.waltz.data.roadmap.RoadmapDao) RandomUtilities.randomPick(org.finos.waltz.common.RandomUtilities.randomPick) EntityReference(org.finos.waltz.model.EntityReference) Roadmap(org.finos.waltz.model.roadmap.Roadmap) MeasurableIdSelectorFactory(org.finos.waltz.data.measurable.MeasurableIdSelectorFactory) MeasurableRatingService(org.finos.waltz.service.measurable_rating.MeasurableRatingService) ScenarioAxisItem(org.finos.waltz.model.scenario.ScenarioAxisItem) MeasurableRating(org.finos.waltz.model.measurable_rating.MeasurableRating) ScenarioRatingItemRecord(org.finos.waltz.schema.tables.records.ScenarioRatingItemRecord) ScenarioAxisItemDao(org.finos.waltz.data.scenario.ScenarioAxisItemDao) AxisOrientation(org.finos.waltz.model.AxisOrientation) IdSelectionOptions(org.finos.waltz.model.IdSelectionOptions)

Example 3 with ScenarioRatingItemRecord

use of org.finos.waltz.schema.tables.records.ScenarioRatingItemRecord in project waltz by khartec.

the class ScenarioRatingImporter method mkScenarioRatingRecords.

private List<ScenarioRatingItemRecord> mkScenarioRatingRecords(List<Measurable> measurables, Map<String, RatingSchemeItem> ratingsByName, List<ScenarioRatingRow> rows, Scenario scenario) {
    Collection<ScenarioAxisItem> scenarioAxes = scenarioAxisItemDao.findForScenarioId(scenario.id().get());
    Map<Long, Measurable> measurablesById = indexBy(m -> m.id().get(), measurables);
    Map<String, ScenarioAxisItem> scenarioAxesByName = scenarioAxes.stream().map(sa -> Tuple.tuple(lower(measurablesById.get(sa.domainItem().id()).name()), sa)).collect(toMap(t -> t.v1(), t -> t.v2()));
    return rows.stream().map(r -> {
        ScenarioAxisItem columnAxis = scenarioAxesByName.get(lower(r.column()));
        checkNotNull(columnAxis, "columnAxis cannot be null");
        checkTrue(columnAxis.axisOrientation().equals(AxisOrientation.COLUMN), "column does not match a oolumn axis");
        ScenarioAxisItem rowAxis = scenarioAxesByName.get(lower(r.row()));
        checkNotNull(rowAxis, "rowAxis cannot be null");
        checkTrue(rowAxis.axisOrientation().equals(AxisOrientation.ROW), "row does not match a row axis");
        Application app = assetCodeToApplicationMap.get(r.assetCode());
        checkNotNull(app, String.format("Application with asset code[%s] cannot be null", r.assetCode()));
        RatingSchemeItem rating = ratingsByName.get(lower(r.rating()));
        checkNotNull(rating, String.format("rating [%s] cannot be null", r.rating()));
        ScenarioRatingItemRecord record = new ScenarioRatingItemRecord();
        record.setScenarioId(scenario.id().get());
        record.setRating(rating.rating().toString());
        record.setDomainItemKind(EntityKind.APPLICATION.name());
        record.setDomainItemId(app.id().get());
        record.setRowKind(rowAxis.domainItem().kind().name());
        record.setRowId(rowAxis.domainItem().id());
        record.setColumnKind(columnAxis.domainItem().kind().name());
        record.setColumnId(columnAxis.domainItem().id());
        record.setLastUpdatedAt(DateTimeUtilities.nowUtcTimestamp());
        record.setLastUpdatedBy(r.providedBy());
        record.setDescription(r.description());
        return record;
    }).collect(toList());
}
Also used : ICsvMapReader(org.supercsv.io.ICsvMapReader) ScenarioAxisItemDao(org.finos.waltz.data.scenario.ScenarioAxisItemDao) APPLICATION(org.finos.waltz.schema.tables.Application.APPLICATION) EntityKind(org.finos.waltz.model.EntityKind) RatingScheme(org.finos.waltz.model.rating.RatingScheme) DIConfiguration(org.finos.waltz.service.DIConfiguration) StringUtilities.lower(org.finos.waltz.common.StringUtilities.lower) CsvPreference(org.supercsv.prefs.CsvPreference) Scenario(org.finos.waltz.model.scenario.Scenario) ArrayList(java.util.ArrayList) Checks.checkTrue(org.finos.waltz.common.Checks.checkTrue) Measurable(org.finos.waltz.model.measurable.Measurable) RatingSchemeItem(org.finos.waltz.model.rating.RatingSchemeItem) SCENARIO_RATING_ITEM(org.finos.waltz.schema.tables.ScenarioRatingItem.SCENARIO_RATING_ITEM) Map(java.util.Map) DSLContext(org.jooq.DSLContext) Application(org.finos.waltz.model.application.Application) MeasurableDao(org.finos.waltz.data.measurable.MeasurableDao) ScenarioDao(org.finos.waltz.data.scenario.ScenarioDao) Collection(java.util.Collection) IOException(java.io.IOException) AxisOrientation(org.finos.waltz.model.AxisOrientation) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) MapUtilities.indexBy(org.finos.waltz.common.MapUtilities.indexBy) ScenarioRatingItemRecord(org.finos.waltz.schema.tables.records.ScenarioRatingItemRecord) CsvMapReader(org.supercsv.io.CsvMapReader) ApplicationDao(org.finos.waltz.data.application.ApplicationDao) List(java.util.List) Checks.checkNotNull(org.finos.waltz.common.Checks.checkNotNull) ExternalIdValue(org.finos.waltz.model.external_identifier.ExternalIdValue) ScenarioAxisItem(org.finos.waltz.model.scenario.ScenarioAxisItem) Tuple(org.jooq.lambda.tuple.Tuple) DateTimeUtilities(org.finos.waltz.common.DateTimeUtilities) RatingSchemeDAO(org.finos.waltz.data.rating_scheme.RatingSchemeDAO) RoadmapDao(org.finos.waltz.data.roadmap.RoadmapDao) Roadmap(org.finos.waltz.model.roadmap.Roadmap) Measurable(org.finos.waltz.model.measurable.Measurable) ScenarioAxisItem(org.finos.waltz.model.scenario.ScenarioAxisItem) ScenarioRatingItemRecord(org.finos.waltz.schema.tables.records.ScenarioRatingItemRecord) Application(org.finos.waltz.model.application.Application) RatingSchemeItem(org.finos.waltz.model.rating.RatingSchemeItem)

Example 4 with ScenarioRatingItemRecord

use of org.finos.waltz.schema.tables.records.ScenarioRatingItemRecord in project waltz by khartec.

the class ScenarioRatingImporter method updateRatingsForScenario.

private void updateRatingsForScenario(List<Measurable> measurables, Roadmap roadmap, Map<String, RatingSchemeItem> ratingsByName, List<ScenarioRatingRow> rows, Map<String, Scenario> scenariosByName, String scenarioName) {
    // fetch scenario and axis items
    Scenario scenario = scenariosByName.get(lower(scenarioName));
    checkNotNull(scenario, "scenario [" + scenarioName + "] cannot be null");
    // ensure all scenario axes
    ensureScenarioAxes(measurables, roadmap, scenario, rows);
    // convert rows to scenario rating items
    List<ScenarioRatingItemRecord> records = mkScenarioRatingRecords(measurables, ratingsByName, rows, scenario);
    removeExistingRatings(dsl, scenario);
    // insert new ones
    int[] inserts = dsl.batchInsert(records).execute();
    System.out.printf("Inserted %s records for scenario %s \n", inserts.length, scenarioName);
}
Also used : ScenarioRatingItemRecord(org.finos.waltz.schema.tables.records.ScenarioRatingItemRecord) Scenario(org.finos.waltz.model.scenario.Scenario)

Example 5 with ScenarioRatingItemRecord

use of org.finos.waltz.schema.tables.records.ScenarioRatingItemRecord in project waltz by khartec.

the class ScenarioPopulator method mkBaseRecord.

/**
 * Creates a new scenario rating item record and populates the base fields
 * from the given query result row.
 *
 * @param tx  Transactional dsl context
 * @param r  Query result row
 * @param now  timestamp representing now
 * @return  Paritally completed ScenarioRatingItemRecord
 */
private static ScenarioRatingItemRecord mkBaseRecord(DSLContext tx, Record r, Timestamp now) {
    ScenarioRatingItemRecord item = tx.newRecord(SCENARIO_RATING_ITEM);
    item.setScenarioId(r.get(s.ID));
    item.setRowKind(r.get(rai.DOMAIN_ITEM_KIND));
    item.setRowId(r.get(rai.DOMAIN_ITEM_ID));
    item.setColumnKind(r.get(cai.DOMAIN_ITEM_KIND));
    item.setColumnId(r.get(cai.DOMAIN_ITEM_ID));
    item.setLastUpdatedAt(now);
    item.setLastUpdatedBy("admin");
    return item;
}
Also used : ScenarioRatingItemRecord(org.finos.waltz.schema.tables.records.ScenarioRatingItemRecord)

Aggregations

ScenarioRatingItemRecord (org.finos.waltz.schema.tables.records.ScenarioRatingItemRecord)5 Scenario (org.finos.waltz.model.scenario.Scenario)3 Collectors (java.util.stream.Collectors)2 DateTimeUtilities (org.finos.waltz.common.DateTimeUtilities)2 MeasurableDao (org.finos.waltz.data.measurable.MeasurableDao)2 RoadmapDao (org.finos.waltz.data.roadmap.RoadmapDao)2 ScenarioAxisItemDao (org.finos.waltz.data.scenario.ScenarioAxisItemDao)2 ScenarioDao (org.finos.waltz.data.scenario.ScenarioDao)2 AxisOrientation (org.finos.waltz.model.AxisOrientation)2 EntityKind (org.finos.waltz.model.EntityKind)2 Measurable (org.finos.waltz.model.measurable.Measurable)2 Roadmap (org.finos.waltz.model.roadmap.Roadmap)2 ScenarioAxisItem (org.finos.waltz.model.scenario.ScenarioAxisItem)2 DSLContext (org.jooq.DSLContext)2 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1