Search in sources :

Example 1 with MEASURABLE_RATING_REPLACEMENT

use of org.finos.waltz.schema.Tables.MEASURABLE_RATING_REPLACEMENT in project waltz by khartec.

the class AppMeasurableDecommGenerator method create.

@Override
public Map<String, Integer> create(ApplicationContext ctx) {
    DSLContext dsl = getDsl(ctx);
    MeasurableCategoryDao categoryDao = ctx.getBean(MeasurableCategoryDao.class);
    ApplicationDao applicationDao = ctx.getBean(ApplicationDao.class);
    MeasurableRatingDao ratingDao = ctx.getBean(MeasurableRatingDao.class);
    first(categoryDao.findByExternalId("CAPABILITY")).id().ifPresent(categoryId -> {
        Collection<MeasurableRating> ratings = ratingDao.findByCategory(categoryId);
        int[] rc = ratings.stream().filter(e -> randomTrue(0.1)).map(r -> {
            MeasurableRatingPlannedDecommissionRecord record = dsl.newRecord(MEASURABLE_RATING_PLANNED_DECOMMISSION);
            record.setEntityId(r.entityReference().id());
            record.setEntityKind(r.entityReference().kind().name());
            record.setMeasurableId(r.measurableId());
            record.setPlannedDecommissionDate(mkFutureDate());
            record.setCreatedBy("test");
            record.setCreatedAt(DateTimeUtilities.nowUtcTimestamp());
            record.setUpdatedBy("test");
            record.setUpdatedAt(DateTimeUtilities.nowUtcTimestamp());
            return record;
        }).collect(collectingAndThen(Collectors.toSet(), dsl::batchInsert)).execute();
        List<Application> allApps = applicationDao.findAll();
        Map<Long, Collection<MeasurableRating>> allRatingsByMeasurableId = groupBy(ratings, MeasurableRating::measurableId);
        dsl.selectFrom(MEASURABLE_RATING_PLANNED_DECOMMISSION).fetchGroups(r -> r.get(MEASURABLE_RATING_PLANNED_DECOMMISSION.MEASURABLE_ID)).entrySet().stream().flatMap(e -> {
            Set<Long> appIdsForMeasurable = map(allRatingsByMeasurableId.get(e.getValue()), r -> r.entityReference().id());
            Set<Long> decommingAppIds = map(e.getValue(), MeasurableRatingPlannedDecommissionRecord::getEntityId);
            Set<Long> possibleReplacements = minus(appIdsForMeasurable, decommingAppIds);
            Set<Long> replacementAppIds = new HashSet<>();
            if (possibleReplacements.size() > 1) {
                Tuple2<Long, List<Long>> firstPick = pickAndRemove(new ArrayList<>(possibleReplacements));
                Tuple2<Long, List<Long>> secondPick = pickAndRemove(firstPick.v2);
                replacementAppIds.add(firstPick.v1);
                replacementAppIds.add(secondPick.v1);
            }
            replacementAppIds.add(randomPick(allApps).id().get());
            return e.getValue().stream().filter(r -> randomTrue(0.7)).map(r -> {
                MeasurableRatingReplacementRecord record = dsl.newRecord(MEASURABLE_RATING_REPLACEMENT);
                record.setDecommissionId(r.getId());
                record.setEntityId(randomPick(replacementAppIds));
                record.setEntityKind(EntityKind.APPLICATION.name());
                record.setPlannedCommissionDate(mkFutureDate());
                record.setCreatedBy("test");
                record.setCreatedAt(DateTimeUtilities.nowUtcTimestamp());
                record.setUpdatedBy("test");
                record.setUpdatedAt(DateTimeUtilities.nowUtcTimestamp());
                return record;
            });
        }).collect(collectingAndThen(Collectors.toSet(), dsl::batchInsert)).execute();
    });
    log("hello %s", "world");
    return null;
}
Also used : MeasurableCategoryDao(org.finos.waltz.data.measurable_category.MeasurableCategoryDao) java.util(java.util) MeasurableRatingPlannedDecommissionRecord(org.finos.waltz.schema.tables.records.MeasurableRatingPlannedDecommissionRecord) MeasurableRating(org.finos.waltz.model.measurable_rating.MeasurableRating) EntityKind(org.finos.waltz.model.EntityKind) SetUtilities.minus(org.finos.waltz.common.SetUtilities.minus) LoggingUtilities(org.finos.waltz.common.LoggingUtilities) DIConfiguration(org.finos.waltz.service.DIConfiguration) Collectors.collectingAndThen(java.util.stream.Collectors.collectingAndThen) DateTimeUtilities.toSqlDate(org.finos.waltz.common.DateTimeUtilities.toSqlDate) SetUtilities.map(org.finos.waltz.common.SetUtilities.map) MeasurableRatingDao(org.finos.waltz.data.measurable_rating.MeasurableRatingDao) Tuple2(org.jooq.lambda.tuple.Tuple2) MapUtilities.groupBy(org.finos.waltz.common.MapUtilities.groupBy) DSLContext(org.jooq.DSLContext) MEASURABLE_RATING_REPLACEMENT(org.finos.waltz.schema.Tables.MEASURABLE_RATING_REPLACEMENT) CollectionUtilities.first(org.finos.waltz.common.CollectionUtilities.first) Application(org.finos.waltz.model.application.Application) ApplicationContext(org.springframework.context.ApplicationContext) Collectors(java.util.stream.Collectors) MeasurableRatingReplacementRecord(org.finos.waltz.schema.tables.records.MeasurableRatingReplacementRecord) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Date(java.sql.Date) ApplicationDao(org.finos.waltz.data.application.ApplicationDao) RandomUtilities(org.finos.waltz.common.RandomUtilities) MEASURABLE_RATING_PLANNED_DECOMMISSION(org.finos.waltz.schema.Tables.MEASURABLE_RATING_PLANNED_DECOMMISSION) DateTimeUtilities(org.finos.waltz.common.DateTimeUtilities) DateTimeUtilities.today(org.finos.waltz.common.DateTimeUtilities.today) MeasurableRatingDao(org.finos.waltz.data.measurable_rating.MeasurableRatingDao) DSLContext(org.jooq.DSLContext) MeasurableRatingPlannedDecommissionRecord(org.finos.waltz.schema.tables.records.MeasurableRatingPlannedDecommissionRecord) ApplicationDao(org.finos.waltz.data.application.ApplicationDao) MeasurableRating(org.finos.waltz.model.measurable_rating.MeasurableRating) MeasurableCategoryDao(org.finos.waltz.data.measurable_category.MeasurableCategoryDao) MeasurableRatingReplacementRecord(org.finos.waltz.schema.tables.records.MeasurableRatingReplacementRecord) Application(org.finos.waltz.model.application.Application)

Aggregations

Date (java.sql.Date)1 java.util (java.util)1 Collectors (java.util.stream.Collectors)1 Collectors.collectingAndThen (java.util.stream.Collectors.collectingAndThen)1 CollectionUtilities.first (org.finos.waltz.common.CollectionUtilities.first)1 DateTimeUtilities (org.finos.waltz.common.DateTimeUtilities)1 DateTimeUtilities.toSqlDate (org.finos.waltz.common.DateTimeUtilities.toSqlDate)1 DateTimeUtilities.today (org.finos.waltz.common.DateTimeUtilities.today)1 LoggingUtilities (org.finos.waltz.common.LoggingUtilities)1 MapUtilities.groupBy (org.finos.waltz.common.MapUtilities.groupBy)1 RandomUtilities (org.finos.waltz.common.RandomUtilities)1 SetUtilities.map (org.finos.waltz.common.SetUtilities.map)1 SetUtilities.minus (org.finos.waltz.common.SetUtilities.minus)1 ApplicationDao (org.finos.waltz.data.application.ApplicationDao)1 MeasurableCategoryDao (org.finos.waltz.data.measurable_category.MeasurableCategoryDao)1 MeasurableRatingDao (org.finos.waltz.data.measurable_rating.MeasurableRatingDao)1 EntityKind (org.finos.waltz.model.EntityKind)1 Application (org.finos.waltz.model.application.Application)1 MeasurableRating (org.finos.waltz.model.measurable_rating.MeasurableRating)1 MEASURABLE_RATING_PLANNED_DECOMMISSION (org.finos.waltz.schema.Tables.MEASURABLE_RATING_PLANNED_DECOMMISSION)1