Search in sources :

Example 1 with MEASURABLE_CATEGORY

use of com.khartec.waltz.schema.tables.MeasurableCategory.MEASURABLE_CATEGORY in project waltz by khartec.

the class MeasurablesGenerator method generateRegions.

private static void generateRegions(DSLContext dsl) throws IOException {
    List<String> lines = readLines(OrgUnitGenerator.class.getResourceAsStream("/regions.csv"));
    System.out.println("Deleting existing Regions & Countries ...");
    int deletedCount = dsl.deleteFrom(MEASURABLE).where(MEASURABLE.MEASURABLE_CATEGORY_ID.in(DSL.select(MEASURABLE_CATEGORY.ID).from(MEASURABLE_CATEGORY).where(MEASURABLE_CATEGORY.EXTERNAL_ID.eq(REGION_CATEGORY_EXTERNAL_ID)))).and(MEASURABLE.PROVENANCE.eq("demo")).execute();
    System.out.println("Deleted: " + deletedCount + " existing Regions & Countries");
    Map<String, Map<String, Set<String>>> regionHierarchy = lines.stream().skip(1).map(line -> StringUtils.splitPreserveAllTokens(line, ",")).filter(cells -> notEmpty(cells[0]) && notEmpty(cells[6]) && notEmpty(cells[5])).map(cells -> Tuple.tuple(cells[0], cells[6], cells[5])).collect(groupingBy(t -> t.v3, groupingBy(t -> t.v2, mapping(t -> t.v1, toSet()))));
    final long measurableCategoryId = dsl.select(MEASURABLE_CATEGORY.ID).from(MEASURABLE_CATEGORY).where(MEASURABLE_CATEGORY.EXTERNAL_ID.eq(REGION_CATEGORY_EXTERNAL_ID)).fetchAny().value1();
    AtomicInteger insertCount = new AtomicInteger(0);
    regionHierarchy.forEach((region, subRegionMap) -> {
        final long regionId = dsl.insertInto(MEASURABLE).set(createRegion(null, region, measurableCategoryId, false)).returning(MEASURABLE.ID).fetchOne().getId();
        insertCount.incrementAndGet();
        subRegionMap.forEach((subRegion, countries) -> {
            final long subRegionId = dsl.insertInto(MEASURABLE).set(createRegion(regionId, subRegion, measurableCategoryId, true)).returning(MEASURABLE.ID).fetchOne().getId();
            insertCount.incrementAndGet();
            insertCount.addAndGet(dsl.batchInsert(countries.stream().map(country -> createRegion(subRegionId, country, measurableCategoryId, true)).collect(toList())).execute().length);
        });
    });
    System.out.println("Inserted: " + insertCount + " Regions & Countries");
}
Also used : DSL(org.jooq.impl.DSL) MeasurableRecord(com.khartec.waltz.schema.tables.records.MeasurableRecord) StringUtilities.notEmpty(com.khartec.waltz.common.StringUtilities.notEmpty) Set(java.util.Set) IOException(java.io.IOException) MEASURABLE(com.khartec.waltz.schema.tables.Measurable.MEASURABLE) StringUtils(org.apache.commons.lang3.StringUtils) Collectors(java.util.stream.Collectors) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) List(java.util.List) Tuple(org.jooq.lambda.tuple.Tuple) DIConfiguration(com.khartec.waltz.service.DIConfiguration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) MEASURABLE_CATEGORY(com.khartec.waltz.schema.tables.MeasurableCategory.MEASURABLE_CATEGORY) DSLContext(org.jooq.DSLContext) IOUtilities.readLines(com.khartec.waltz.common.IOUtilities.readLines) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map)

Aggregations

IOUtilities.readLines (com.khartec.waltz.common.IOUtilities.readLines)1 StringUtilities.notEmpty (com.khartec.waltz.common.StringUtilities.notEmpty)1 MEASURABLE (com.khartec.waltz.schema.tables.Measurable.MEASURABLE)1 MEASURABLE_CATEGORY (com.khartec.waltz.schema.tables.MeasurableCategory.MEASURABLE_CATEGORY)1 MeasurableRecord (com.khartec.waltz.schema.tables.records.MeasurableRecord)1 DIConfiguration (com.khartec.waltz.service.DIConfiguration)1 IOException (java.io.IOException)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors (java.util.stream.Collectors)1 StringUtils (org.apache.commons.lang3.StringUtils)1 DSLContext (org.jooq.DSLContext)1 DSL (org.jooq.impl.DSL)1 Tuple (org.jooq.lambda.tuple.Tuple)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1