Search in sources :

Example 1 with SurveyQuestionResponseRecord

use of org.finos.waltz.schema.tables.records.SurveyQuestionResponseRecord in project waltz by khartec.

the class SurveyQuestionResponseDao method mkRecord.

private SurveyQuestionResponseRecord mkRecord(SurveyInstanceQuestionResponse response) {
    SurveyQuestionResponse questionResponse = response.questionResponse();
    Optional<EntityReference> entityResponse = questionResponse.entityResponse();
    SurveyQuestionResponseRecord record = dsl.newRecord(SURVEY_QUESTION_RESPONSE);
    record.setSurveyInstanceId(response.surveyInstanceId());
    record.setQuestionId(questionResponse.questionId());
    record.setPersonId(response.personId());
    record.setLastUpdatedAt(Timestamp.valueOf(response.lastUpdatedAt()));
    record.setComment(questionResponse.comment().map(c -> ifEmpty(c, null)).orElse(null));
    record.setStringResponse(questionResponse.stringResponse().map(s -> ifEmpty(s, null)).orElse(null));
    record.setNumberResponse(questionResponse.numberResponse().map(BigDecimal::valueOf).orElse(null));
    record.setBooleanResponse(questionResponse.booleanResponse().orElse(null));
    record.setDateResponse(questionResponse.dateResponse().map(DateTimeUtilities::toSqlDate).orElse(null));
    record.setEntityResponseKind(entityResponse.map(er -> er.kind().name()).orElse(null));
    record.setEntityResponseId(entityResponse.map(EntityReference::id).orElse(null));
    record.setListResponseConcat(questionResponse.listResponse().filter(l -> !l.isEmpty()).map(l -> join(l, "; ")).orElse(null));
    return record;
}
Also used : StringUtilities.join(org.finos.waltz.common.StringUtilities.join) DSL(org.jooq.impl.DSL) Tables(org.finos.waltz.schema.Tables) EntityKind(org.finos.waltz.model.EntityKind) Autowired(org.springframework.beans.factory.annotation.Autowired) ListUtilities.newArrayList(org.finos.waltz.common.ListUtilities.newArrayList) EntityReference.mkRef(org.finos.waltz.model.EntityReference.mkRef) SURVEY_INSTANCE(org.finos.waltz.schema.tables.SurveyInstance.SURVEY_INSTANCE) Checks.checkTrue(org.finos.waltz.common.Checks.checkTrue) BigDecimal(java.math.BigDecimal) SetUtilities.map(org.finos.waltz.common.SetUtilities.map) SURVEY_QUESTION_LIST_RESPONSE(org.finos.waltz.schema.Tables.SURVEY_QUESTION_LIST_RESPONSE) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) org.jooq(org.jooq) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) Repository(org.springframework.stereotype.Repository) SURVEY_QUESTION_RESPONSE(org.finos.waltz.schema.tables.SurveyQuestionResponse.SURVEY_QUESTION_RESPONSE) CollectionUtilities.first(org.finos.waltz.common.CollectionUtilities.first) StringUtilities.ifEmpty(org.finos.waltz.common.StringUtilities.ifEmpty) org.finos.waltz.model.survey(org.finos.waltz.model.survey) SurveyQuestionListResponseRecord(org.finos.waltz.schema.tables.records.SurveyQuestionListResponseRecord) Optional.ofNullable(java.util.Optional.ofNullable) ListUtilities.isEmpty(org.finos.waltz.common.ListUtilities.isEmpty) Timestamp(java.sql.Timestamp) Set(java.util.Set) Collectors(java.util.stream.Collectors) Date(java.sql.Date) SURVEY_RUN(org.finos.waltz.schema.Tables.SURVEY_RUN) SurveyQuestionResponseRecord(org.finos.waltz.schema.tables.records.SurveyQuestionResponseRecord) List(java.util.List) Checks.checkNotNull(org.finos.waltz.common.Checks.checkNotNull) Tuple.tuple(org.jooq.lambda.tuple.Tuple.tuple) DateTimeUtilities(org.finos.waltz.common.DateTimeUtilities) InlineSelectFieldFactory(org.finos.waltz.data.InlineSelectFieldFactory) EntityReference(org.finos.waltz.model.EntityReference) Optional(java.util.Optional) SurveyQuestionResponseRecord(org.finos.waltz.schema.tables.records.SurveyQuestionResponseRecord) DateTimeUtilities(org.finos.waltz.common.DateTimeUtilities) EntityReference(org.finos.waltz.model.EntityReference) BigDecimal(java.math.BigDecimal)

Example 2 with SurveyQuestionResponseRecord

use of org.finos.waltz.schema.tables.records.SurveyQuestionResponseRecord in project waltz by khartec.

the class SurveyRunGenerator method create.

@Override
public Map<String, Integer> create(ApplicationContext ctx) {
    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.findAllActive();
    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);
    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());
        ImmutableInstancesAndRecipientsCreateCommand createCmd = ImmutableInstancesAndRecipientsCreateCommand.builder().surveyRunId(surveyRunId).dueDate(toLocalDate(nowUtcTimestamp())).approvalDueDate(toLocalDate(nowUtcTimestamp())).excludedRecipients(emptySet()).build();
        surveyRunService.createSurveyInstancesAndRecipients(createCmd);
        List<SurveyInstanceQuestionResponse> surveyInstanceQuestionResponses = mkRandomSurveyRunResponses(surveyRunId, surveyInstanceService, surveyQuestionService);
        Map<SurveyInstanceStatus, Set<Long>> surveyInstanceStatusMap = surveyInstanceQuestionResponses.stream().mapToLong(response -> response.surveyInstanceId()).distinct().mapToObj(id -> tuple(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(DateTimeUtilities.nowUtcTimestamp());
            return record;
        }).filter(Objects::nonNull).collect(toList())).execute();
        if (SurveyRunStatus.valueOf(surveyRunRecord.getStatus()) == SurveyRunStatus.COMPLETED) {
            surveyRunRecord.setStatus(SurveyRunStatus.COMPLETED.name());
            surveyRunRecord.store();
            surveyCompletedCount.incrementAndGet();
            // update 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);
            }
        } 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());
    return null;
}
Also used : SurveyQuestionService(org.finos.waltz.service.survey.SurveyQuestionService) IntStream(java.util.stream.IntStream) SurveyRunService(org.finos.waltz.service.survey.SurveyRunService) java.util(java.util) DSL(org.jooq.impl.DSL) SURVEY_INSTANCE_RECIPIENT(org.finos.waltz.schema.tables.SurveyInstanceRecipient.SURVEY_INSTANCE_RECIPIENT) EntityKind(org.finos.waltz.model.EntityKind) InvolvementKindDao(org.finos.waltz.data.involvement_kind.InvolvementKindDao) LoggerFactory(org.slf4j.LoggerFactory) AppGroup(org.finos.waltz.model.app_group.AppGroup) SurveyQuestionService(org.finos.waltz.service.survey.SurveyQuestionService) AppGroupDao(org.finos.waltz.data.app_group.AppGroupDao) Person(org.finos.waltz.model.person.Person) Condition(org.jooq.Condition) SurveyRunRecord(org.finos.waltz.schema.tables.records.SurveyRunRecord) BigDecimal(java.math.BigDecimal) Tuple2(org.jooq.lambda.tuple.Tuple2) Checks.checkFalse(org.finos.waltz.common.Checks.checkFalse) Record1(org.jooq.Record1) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvolvementKind(org.finos.waltz.model.involvement_kind.InvolvementKind) DSLContext(org.jooq.DSLContext) SURVEY_QUESTION_RESPONSE(org.finos.waltz.schema.tables.SurveyQuestionResponse.SURVEY_QUESTION_RESPONSE) Select(org.jooq.Select) SurveyTemplateDao(org.finos.waltz.data.survey.SurveyTemplateDao) Logger(org.slf4j.Logger) Collections.emptySet(java.util.Collections.emptySet) CollectionUtilities.isEmpty(org.finos.waltz.common.CollectionUtilities.isEmpty) org.finos.waltz.model.survey(org.finos.waltz.model.survey) SURVEY_INSTANCE(org.finos.waltz.schema.Tables.SURVEY_INSTANCE) PersonDao(org.finos.waltz.data.person.PersonDao) ApplicationContext(org.springframework.context.ApplicationContext) Collectors(java.util.stream.Collectors) SURVEY_RUN(org.finos.waltz.schema.Tables.SURVEY_RUN) RandomUtilities(org.finos.waltz.common.RandomUtilities) ListUtilities.map(org.finos.waltz.common.ListUtilities.map) SurveyQuestionResponseRecord(org.finos.waltz.schema.tables.records.SurveyQuestionResponseRecord) Tuple.tuple(org.jooq.lambda.tuple.Tuple.tuple) DateTimeUtilities(org.finos.waltz.common.DateTimeUtilities) LocalDate(java.time.LocalDate) RandomUtilities.randomPick(org.finos.waltz.common.RandomUtilities.randomPick) SurveyInstanceService(org.finos.waltz.service.survey.SurveyInstanceService) PERSON(org.finos.waltz.schema.tables.Person.PERSON) HierarchyQueryScope(org.finos.waltz.model.HierarchyQueryScope) SurveyQuestionResponseRecord(org.finos.waltz.schema.tables.records.SurveyQuestionResponseRecord) Collections.emptySet(java.util.Collections.emptySet) InvolvementKindDao(org.finos.waltz.data.involvement_kind.InvolvementKindDao) AppGroup(org.finos.waltz.model.app_group.AppGroup) SurveyInstanceService(org.finos.waltz.service.survey.SurveyInstanceService) AppGroupDao(org.finos.waltz.data.app_group.AppGroupDao) InvolvementKind(org.finos.waltz.model.involvement_kind.InvolvementKind) SurveyTemplateDao(org.finos.waltz.data.survey.SurveyTemplateDao) DSLContext(org.jooq.DSLContext) BigDecimal(java.math.BigDecimal) SurveyRunService(org.finos.waltz.service.survey.SurveyRunService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SurveyRunRecord(org.finos.waltz.schema.tables.records.SurveyRunRecord) Person(org.finos.waltz.model.person.Person)

Example 3 with SurveyQuestionResponseRecord

use of org.finos.waltz.schema.tables.records.SurveyQuestionResponseRecord in project waltz by khartec.

the class SurveyQuestionResponseDao method saveResponse.

public void saveResponse(SurveyInstanceQuestionResponse response) {
    checkNotNull(response, "response cannot be null");
    checkNotNull(response.questionResponse(), "response.questionResponse() cannot be null");
    SurveyQuestionResponseRecord record = mkRecord(response);
    Condition responseExistsCondition = DSL.exists(DSL.select(SURVEY_QUESTION_RESPONSE.fields()).from(SURVEY_QUESTION_RESPONSE).where(SURVEY_QUESTION_RESPONSE.SURVEY_INSTANCE_ID.eq(response.surveyInstanceId()).and(SURVEY_QUESTION_RESPONSE.QUESTION_ID.eq(response.questionResponse().questionId()))));
    // save survey_question_response record
    Boolean responseExists = dsl.select(DSL.when(responseExistsCondition, true).otherwise(false)).fetchOne(Record1::value1);
    dsl.transaction(configuration -> {
        DSLContext txDsl = DSL.using(configuration);
        if (responseExists) {
            txDsl.executeUpdate(record);
        } else {
            txDsl.executeInsert(record);
        }
        response.questionResponse().listResponse().ifPresent(list -> saveListResponse(txDsl, response.surveyInstanceId(), response.questionResponse().questionId(), list));
        response.questionResponse().entityListResponse().ifPresent(list -> saveEntityListResponse(txDsl, response.surveyInstanceId(), response.questionResponse().questionId(), list));
    });
}
Also used : SurveyQuestionResponseRecord(org.finos.waltz.schema.tables.records.SurveyQuestionResponseRecord)

Aggregations

SurveyQuestionResponseRecord (org.finos.waltz.schema.tables.records.SurveyQuestionResponseRecord)3 BigDecimal (java.math.BigDecimal)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Collectors (java.util.stream.Collectors)2 DateTimeUtilities (org.finos.waltz.common.DateTimeUtilities)2 EntityKind (org.finos.waltz.model.EntityKind)2 org.finos.waltz.model.survey (org.finos.waltz.model.survey)2 SURVEY_RUN (org.finos.waltz.schema.Tables.SURVEY_RUN)2 SURVEY_QUESTION_RESPONSE (org.finos.waltz.schema.tables.SurveyQuestionResponse.SURVEY_QUESTION_RESPONSE)2 DSL (org.jooq.impl.DSL)2 Tuple.tuple (org.jooq.lambda.tuple.Tuple.tuple)2 Date (java.sql.Date)1 Timestamp (java.sql.Timestamp)1 LocalDate (java.time.LocalDate)1 java.util (java.util)1 Collections.emptySet (java.util.Collections.emptySet)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Optional.ofNullable (java.util.Optional.ofNullable)1