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!");
});
}
Aggregations