use of com.khartec.waltz.schema.tables.records.SurveyRunRecord in project waltz by khartec.
the class SurveyRunGenerator method main.
public static void main(String[] args) {
try {
final AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DIConfiguration.class);
final DSLContext dsl = ctx.getBean(DSLContext.class);
List<Person> owners = dsl.selectFrom(PERSON).limit(NUMBER_OF_RUNS).fetch().map(PersonDao.personMapper);
checkFalse(isEmpty(owners), "No person found, please generate person data first");
final SurveyTemplateDao surveyTemplateDao = ctx.getBean(SurveyTemplateDao.class);
List<SurveyTemplate> surveyTemplates = surveyTemplateDao.findAll(owners.get(0).id().get());
checkFalse(isEmpty(surveyTemplates), "No template found, please generate templates first");
final AppGroupDao appGroupDao = ctx.getBean(AppGroupDao.class);
List<AppGroup> appGroups = appGroupDao.findPublicGroups();
checkFalse(isEmpty(appGroups), "No public app group found, please generate app groups first");
final InvolvementKindDao involvementKindDao = ctx.getBean(InvolvementKindDao.class);
List<InvolvementKind> involvementKinds = involvementKindDao.findAll();
final SurveyRunService surveyRunService = ctx.getBean(SurveyRunService.class);
final SurveyInstanceService surveyInstanceService = ctx.getBean(SurveyInstanceService.class);
final SurveyQuestionService surveyQuestionService = ctx.getBean(SurveyQuestionService.class);
deleteSurveyRunsAndResponses(dsl);
AtomicInteger surveyCompletedCount = new AtomicInteger(0);
IntStream.range(0, NUMBER_OF_RUNS).forEach(idx -> {
SurveyTemplate surveyTemplate = surveyTemplates.get(random.nextInt(surveyTemplates.size()));
Person owner = owners.get(random.nextInt(owners.size()));
SurveyRunRecord surveyRunRecord = mkRandomSurveyRunRecord(dsl, appGroups, involvementKinds, surveyTemplate, owner);
surveyRunRecord.store();
long surveyRunId = surveyRunRecord.getId();
LOG.debug("Survey Run: {} / {} / {}", surveyRunRecord.getStatus(), surveyRunId, surveyRunRecord.getName());
surveyRunService.createSurveyInstancesAndRecipients(surveyRunId, Collections.emptyList());
List<SurveyInstanceQuestionResponse> surveyInstanceQuestionResponses = mkRandomSurveyRunResponses(surveyRunId, surveyInstanceService, surveyQuestionService);
Map<SurveyInstanceStatus, Set<Long>> surveyInstanceStatusMap = surveyInstanceQuestionResponses.stream().mapToLong(response -> response.surveyInstanceId()).distinct().mapToObj(id -> Tuple.tuple(ArrayUtilities.randomPick(SurveyInstanceStatus.NOT_STARTED, SurveyInstanceStatus.IN_PROGRESS, SurveyInstanceStatus.COMPLETED), id)).collect(groupingBy(t -> t.v1, mapping(t -> t.v2, toSet())));
dsl.batchInsert(surveyInstanceQuestionResponses.stream().map(r -> {
if (surveyInstanceStatusMap.containsKey(SurveyInstanceStatus.NOT_STARTED) && surveyInstanceStatusMap.get(SurveyInstanceStatus.NOT_STARTED).contains(r.surveyInstanceId())) {
// don't create response for NOT_STARTED
return null;
}
SurveyQuestionResponse questionResponse = r.questionResponse();
SurveyQuestionResponseRecord record = new SurveyQuestionResponseRecord();
record.setSurveyInstanceId(r.surveyInstanceId());
record.setPersonId(r.personId());
record.setQuestionId(questionResponse.questionId());
record.setBooleanResponse(questionResponse.booleanResponse().orElse(null));
record.setNumberResponse(questionResponse.numberResponse().map(BigDecimal::valueOf).orElse(null));
record.setStringResponse(questionResponse.stringResponse().orElse(null));
record.setComment(r.questionResponse().comment().orElse(null));
record.setLastUpdatedAt(Timestamp.valueOf(nowUtc()));
return record;
}).filter(Objects::nonNull).collect(toList())).execute();
if (SurveyRunStatus.valueOf(surveyRunRecord.getStatus()) == SurveyRunStatus.COMPLETED) {
surveyRunRecord.setStatus(SurveyRunStatus.COMPLETED.name());
surveyRunRecord.store();
surveyCompletedCount.incrementAndGet();
// save instances to COMPLETED
if (surveyInstanceStatusMap.containsKey(SurveyInstanceStatus.COMPLETED)) {
Set<Long> completedInstanceIds = surveyInstanceStatusMap.get(SurveyInstanceStatus.COMPLETED);
dsl.update(SURVEY_INSTANCE).set(SURVEY_INSTANCE.STATUS, SurveyInstanceStatus.COMPLETED.name()).where(SURVEY_INSTANCE.ID.in(completedInstanceIds)).execute();
LOG.debug(" --- {} instances: {}", SurveyInstanceStatus.COMPLETED, completedInstanceIds);
}
// save instances to EXPIRED
if (surveyInstanceStatusMap.containsKey(SurveyInstanceStatus.NOT_STARTED) || surveyInstanceStatusMap.containsKey(SurveyInstanceStatus.IN_PROGRESS)) {
Set<Long> expiredInstanceIds = surveyInstanceStatusMap.entrySet().stream().filter(e -> e.getKey() != SurveyInstanceStatus.COMPLETED).flatMap(e -> e.getValue().stream()).collect(toSet());
dsl.update(SURVEY_INSTANCE).set(SURVEY_INSTANCE.STATUS, SurveyInstanceStatus.EXPIRED.name()).where(SURVEY_INSTANCE.ID.in(expiredInstanceIds)).execute();
LOG.debug(" --- {} instances: {}", SurveyInstanceStatus.EXPIRED, expiredInstanceIds);
}
} else {
surveyInstanceStatusMap.forEach(((status, instanceIds) -> {
dsl.update(SURVEY_INSTANCE).set(SURVEY_INSTANCE.STATUS, status.name()).where(SURVEY_INSTANCE.ID.in(instanceIds)).execute();
LOG.debug(" --- {} instances: {}", status.name(), instanceIds);
}));
}
});
LOG.debug("Generated: {} survey runs, in which {} are completed", NUMBER_OF_RUNS, surveyCompletedCount.get());
} catch (Exception e) {
e.printStackTrace();
}
}
use of com.khartec.waltz.schema.tables.records.SurveyRunRecord in project waltz by khartec.
the class SurveyRunGenerator method mkRandomSurveyRunRecord.
private static SurveyRunRecord mkRandomSurveyRunRecord(DSLContext dsl, List<AppGroup> appGroups, List<InvolvementKind> involvementKinds, SurveyTemplate surveyTemplate, Person owner) {
SurveyRunRecord surveyRunRecord = dsl.newRecord(SURVEY_RUN);
surveyRunRecord.setOwnerId(owner.id().get());
surveyRunRecord.setContactEmail(owner.email());
surveyRunRecord.setSurveyTemplateId(surveyTemplate.id().get());
surveyRunRecord.setName(String.format("%s %s %s", ArrayUtilities.randomPick(SURVEY_RUN_PREFIXES), surveyTemplate.name(), SURVEY_RUN_SUFFIX));
surveyRunRecord.setDescription(surveyTemplate.description());
surveyRunRecord.setSelectorEntityKind(EntityKind.APP_GROUP.name());
surveyRunRecord.setSelectorEntityId(appGroups.get(random.nextInt(appGroups.size())).id().get());
surveyRunRecord.setSelectorHierarchyScope(HierarchyQueryScope.EXACT.name());
Collections.shuffle(involvementKinds, random);
surveyRunRecord.setInvolvementKindIds(involvementKinds.stream().limit(random.nextInt(MAX_INVOLVEMENT_KINDS_PER_RUN) + 1).map(kind -> kind.id().get().toString()).collect(joining(ID_SEPARATOR)));
surveyRunRecord.setIssuanceKind(ArrayUtilities.randomPick(SurveyIssuanceKind.values()).name());
LocalDate issuedOn = LocalDate.now().minusDays(random.nextInt(MAX_SURVEY_AGE_IN_DAYS));
surveyRunRecord.setIssuedOn(java.sql.Date.valueOf(issuedOn));
LocalDate dueDate = random.nextBoolean() ? issuedOn.plusDays(random.nextInt(MAX_SURVEY_LIFESPAN_IN_DAYS)) : null;
surveyRunRecord.setDueDate(dueDate == null ? null : java.sql.Date.valueOf(dueDate));
surveyRunRecord.setStatus(ArrayUtilities.randomPick(SurveyRunStatus.ISSUED.name(), SurveyRunStatus.COMPLETED.name()));
return surveyRunRecord;
}
use of com.khartec.waltz.schema.tables.records.SurveyRunRecord in project waltz by khartec.
the class SurveyRunDao method create.
public long create(long ownerId, SurveyRunCreateCommand command) {
checkNotNull(command, "command cannot be null");
SurveyRunRecord record = dsl.newRecord(SURVEY_RUN);
record.setSurveyTemplateId(command.surveyTemplateId());
record.setName(command.name());
record.setDescription(command.description());
record.setSelectorEntityKind(command.selectionOptions().entityReference().kind().name());
record.setSelectorEntityId(command.selectionOptions().entityReference().id());
record.setSelectorHierarchyScope(command.selectionOptions().scope().name());
record.setInvolvementKindIds(join(command.involvementKindIds(), ID_SEPARATOR));
record.setDueDate(command.dueDate().map(Date::valueOf).orElse(null));
record.setIssuanceKind(command.issuanceKind().name());
record.setOwnerId(ownerId);
record.setContactEmail(command.contactEmail().orElse(null));
record.setStatus(SurveyRunStatus.DRAFT.name());
record.store();
return record.getId();
}
Aggregations