Search in sources :

Example 1 with Tuple5

use of org.jooq.lambda.tuple.Tuple5 in project waltz by khartec.

the class AssessmentRatingBulkImport method load.

public void load(String filename, AssessmentRatingBulkImportConfig config) throws IOException {
    InputStream inputStream = IOUtilities.getFileResource(filename).getInputStream();
    Workbook workbook = new XSSFWorkbook(inputStream);
    Sheet sheet = workbook.getSheetAt(config.sheetPosition());
    Set<AssessmentRatingEntry> existingRatings = dsl.select(ASSESSMENT_RATING.ENTITY_ID, ASSESSMENT_RATING.ENTITY_KIND, ASSESSMENT_RATING.RATING_ID, ASSESSMENT_RATING.DESCRIPTION).from(ASSESSMENT_RATING).where(ASSESSMENT_RATING.ASSESSMENT_DEFINITION_ID.eq(config.assessmentDefinitionId())).fetchSet(r -> ImmutableAssessmentRatingEntry.builder().entity(readRef(r, ASSESSMENT_RATING.ENTITY_KIND, ASSESSMENT_RATING.ENTITY_ID)).ratingId(r.get(ASSESSMENT_RATING.RATING_ID)).description(r.get(ASSESSMENT_RATING.DESCRIPTION)).build());
    EntityKind subjectKind = EntityKind.valueOf(dsl.select(ASSESSMENT_DEFINITION.ENTITY_KIND).from(ASSESSMENT_DEFINITION).where(ASSESSMENT_DEFINITION.ID.eq(config.assessmentDefinitionId())).fetchOne(ASSESSMENT_DEFINITION.ENTITY_KIND));
    Aliases<Long> ratingAliases = mkRatingAliases(config);
    Map<String, Long> externalIdToEntityIdMap = loadExternalIdToEntityIdMap(subjectKind);
    StreamUtilities.Siphon<Tuple5<String, String, String, Long, Optional<Long>>> noEntityFoundSiphon = mkSiphon(t -> t.v4 == null);
    StreamUtilities.Siphon<Tuple5<String, String, String, Long, Optional<Long>>> noRatingFoundSiphon = mkSiphon(t -> !t.v5.isPresent());
    Set<AssessmentRatingEntry> requiredRatings = streamRows(sheet).skip(config.numberOfHeaderRows()).map(r -> tuple(strVal(r, Columns.A), strVal(r, Columns.B), strVal(r, Columns.C))).map(t -> t.concat(tuple(externalIdToEntityIdMap.get(t.v1), ratingAliases.lookup(t.v2)))).filter(noEntityFoundSiphon).filter(noRatingFoundSiphon).map(t -> ImmutableAssessmentRatingEntry.builder().entity(mkRef(subjectKind, t.v4)).ratingId(t.v5.get()).description(t.v3).build()).collect(toSet());
    noEntityFoundSiphon.getResults().forEach(t -> System.out.printf("Couldn't find an entity id for row: %s%n", t.limit3()));
    noRatingFoundSiphon.getResults().forEach(t -> System.out.printf("Couldn't find a rating id for row: %s%n", t.limit3()));
    DiffResult<AssessmentRatingEntry> diff = DiffResult.mkDiff(existingRatings, requiredRatings, AssessmentRatingEntry::entity, Object::equals);
    dsl.transaction(ctx -> {
        DSLContext tx = ctx.dsl();
        int[] insertedRecords = diff.otherOnly().stream().map(r -> mkAssessmentRatingRecord(config.assessmentDefinitionId(), r, config.updateUser())).collect(Collectors.collectingAndThen(toSet(), tx::batchInsert)).execute();
        LOG.debug(format("inserted new assessment ratings for %d records", IntStream.of(insertedRecords).sum()));
        int[] updatedRecords = diff.differingIntersection().stream().map(r -> dsl.update(ASSESSMENT_RATING).set(ASSESSMENT_RATING.DESCRIPTION, r.description()).set(ASSESSMENT_RATING.RATING_ID, r.ratingId()).set(ASSESSMENT_RATING.LAST_UPDATED_AT, DateTimeUtilities.nowUtcTimestamp()).set(ASSESSMENT_RATING.LAST_UPDATED_BY, config.updateUser()).where(ASSESSMENT_RATING.ASSESSMENT_DEFINITION_ID.eq(config.assessmentDefinitionId()).and(ASSESSMENT_RATING.ENTITY_KIND.eq(r.entity().kind().name()).and(ASSESSMENT_RATING.ENTITY_ID.eq(r.entity().id()))))).collect(Collectors.collectingAndThen(toSet(), tx::batch)).execute();
        LOG.debug(format("Updated ratings or descriptions for %d records", IntStream.of(updatedRecords).sum()));
        if (config.mode().equals(SynchronisationMode.FULL)) {
            int[] removedRecords = diff.waltzOnly().stream().map(r -> dsl.deleteFrom(ASSESSMENT_RATING).where(ASSESSMENT_RATING.ASSESSMENT_DEFINITION_ID.eq(config.assessmentDefinitionId()).and(ASSESSMENT_RATING.ENTITY_ID.eq(r.entity().id()).and(ASSESSMENT_RATING.ENTITY_KIND.eq(r.entity().kind().name()))))).collect(Collectors.collectingAndThen(toSet(), tx::batch)).execute();
            LOG.debug(format("Deleted assessment ratings for %d records", IntStream.of(removedRecords).sum()));
        }
    // throw new  IllegalArgumentException("BBooooooOOOOOooMMMMMMMM!");
    });
}
Also used : IntStream(java.util.stream.IntStream) XlsUtilities.strVal(org.finos.waltz.jobs.XlsUtilities.strVal) Tables(org.finos.waltz.schema.Tables) EntityKind(org.finos.waltz.model.EntityKind) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) StreamUtilities.mkSiphon(org.finos.waltz.common.StreamUtilities.mkSiphon) DIConfiguration(org.finos.waltz.service.DIConfiguration) EntityReference.mkRef(org.finos.waltz.model.EntityReference.mkRef) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Tuple5(org.jooq.lambda.tuple.Tuple5) XlsUtilities.streamRows(org.finos.waltz.jobs.XlsUtilities.streamRows) Map(java.util.Map) DSLContext(org.jooq.DSLContext) JooqUtilities.readRef(org.finos.waltz.data.JooqUtilities.readRef) org.finos.waltz.common(org.finos.waltz.common) Collectors.toSet(java.util.stream.Collectors.toSet) Sheet(org.apache.poi.ss.usermodel.Sheet) Logger(org.slf4j.Logger) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) String.format(java.lang.String.format) Workbook(org.apache.poi.ss.usermodel.Workbook) Component(org.springframework.stereotype.Component) Tuple.tuple(org.jooq.lambda.tuple.Tuple.tuple) Optional(java.util.Optional) AssessmentRatingRecord(org.finos.waltz.schema.tables.records.AssessmentRatingRecord) InputStream(java.io.InputStream) InputStream(java.io.InputStream) DSLContext(org.jooq.DSLContext) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Workbook(org.apache.poi.ss.usermodel.Workbook) Tuple5(org.jooq.lambda.tuple.Tuple5) XSSFWorkbook(org.apache.poi.xssf.usermodel.XSSFWorkbook) Sheet(org.apache.poi.ss.usermodel.Sheet) EntityKind(org.finos.waltz.model.EntityKind)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 String.format (java.lang.String.format)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Collectors.toSet (java.util.stream.Collectors.toSet)1 IntStream (java.util.stream.IntStream)1 Sheet (org.apache.poi.ss.usermodel.Sheet)1 Workbook (org.apache.poi.ss.usermodel.Workbook)1 XSSFWorkbook (org.apache.poi.xssf.usermodel.XSSFWorkbook)1 org.finos.waltz.common (org.finos.waltz.common)1 StreamUtilities.mkSiphon (org.finos.waltz.common.StreamUtilities.mkSiphon)1 JooqUtilities.readRef (org.finos.waltz.data.JooqUtilities.readRef)1 XlsUtilities.strVal (org.finos.waltz.jobs.XlsUtilities.strVal)1 XlsUtilities.streamRows (org.finos.waltz.jobs.XlsUtilities.streamRows)1 EntityKind (org.finos.waltz.model.EntityKind)1 EntityReference.mkRef (org.finos.waltz.model.EntityReference.mkRef)1 Tables (org.finos.waltz.schema.Tables)1