Search in sources :

Example 91 with DSLContext

use of org.jooq.DSLContext 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();
    }
}
Also used : SurveyQuestionService(com.khartec.waltz.service.survey.SurveyQuestionService) IntStream(java.util.stream.IntStream) AppGroup(com.khartec.waltz.model.app_group.AppGroup) java.util(java.util) DSL(org.jooq.impl.DSL) DateTimeUtilities.nowUtc(com.khartec.waltz.common.DateTimeUtilities.nowUtc) LoggerFactory(org.slf4j.LoggerFactory) HierarchyQueryScope(com.khartec.waltz.model.HierarchyQueryScope) Condition(org.jooq.Condition) CollectionUtilities.randomPick(com.khartec.waltz.common.CollectionUtilities.randomPick) EntityKind(com.khartec.waltz.model.EntityKind) SurveyQuestionService(com.khartec.waltz.service.survey.SurveyQuestionService) BigDecimal(java.math.BigDecimal) SurveyRunService(com.khartec.waltz.service.survey.SurveyRunService) DIConfiguration(com.khartec.waltz.service.DIConfiguration) InvolvementKind(com.khartec.waltz.model.involvement_kind.InvolvementKind) Record1(org.jooq.Record1) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SurveyInstanceService(com.khartec.waltz.service.survey.SurveyInstanceService) PersonDao(com.khartec.waltz.data.person.PersonDao) DSLContext(org.jooq.DSLContext) SurveyQuestionResponseRecord(com.khartec.waltz.schema.tables.records.SurveyQuestionResponseRecord) com.khartec.waltz.model.survey(com.khartec.waltz.model.survey) Select(org.jooq.Select) SurveyTemplateDao(com.khartec.waltz.data.survey.SurveyTemplateDao) Logger(org.slf4j.Logger) Timestamp(java.sql.Timestamp) Checks.checkFalse(com.khartec.waltz.common.Checks.checkFalse) PERSON(com.khartec.waltz.schema.tables.Person.PERSON) Collectors(java.util.stream.Collectors) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) AppGroupDao(com.khartec.waltz.data.app_group.AppGroupDao) Tuple(org.jooq.lambda.tuple.Tuple) CollectionUtilities.isEmpty(com.khartec.waltz.common.CollectionUtilities.isEmpty) Tables(com.khartec.waltz.schema.Tables) Person(com.khartec.waltz.model.person.Person) LocalDate(java.time.LocalDate) InvolvementKindDao(com.khartec.waltz.data.involvement_kind.InvolvementKindDao) ArrayUtilities(com.khartec.waltz.common.ArrayUtilities) SurveyRunRecord(com.khartec.waltz.schema.tables.records.SurveyRunRecord) SurveyQuestionResponseRecord(com.khartec.waltz.schema.tables.records.SurveyQuestionResponseRecord) InvolvementKindDao(com.khartec.waltz.data.involvement_kind.InvolvementKindDao) AppGroup(com.khartec.waltz.model.app_group.AppGroup) SurveyInstanceService(com.khartec.waltz.service.survey.SurveyInstanceService) AppGroupDao(com.khartec.waltz.data.app_group.AppGroupDao) InvolvementKind(com.khartec.waltz.model.involvement_kind.InvolvementKind) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) SurveyTemplateDao(com.khartec.waltz.data.survey.SurveyTemplateDao) DSLContext(org.jooq.DSLContext) BigDecimal(java.math.BigDecimal) SurveyRunService(com.khartec.waltz.service.survey.SurveyRunService) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SurveyRunRecord(com.khartec.waltz.schema.tables.records.SurveyRunRecord) Person(com.khartec.waltz.model.person.Person)

Example 92 with DSLContext

use of org.jooq.DSLContext in project waltz by khartec.

the class SurveyTemplateGenerator method main.

public static void main(String[] args) {
    try {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DIConfiguration.class);
        DSLContext dsl = ctx.getBean(DSLContext.class);
        SurveyTemplateService surveyTemplateService = ctx.getBean(SurveyTemplateService.class);
        SurveyQuestionService surveyQuestionService = ctx.getBean(SurveyQuestionService.class);
        dsl.deleteFrom(SURVEY_TEMPLATE).execute();
        dsl.deleteFrom(SURVEY_QUESTION).execute();
        ReleaseLifecycleStatusChangeCommand statusChangeCommand = ImmutableReleaseLifecycleStatusChangeCommand.builder().newStatus(ReleaseLifecycleStatus.ACTIVE).build();
        SurveyTemplateChangeCommand appSurvey = mkAppSurvey();
        long aid = surveyTemplateService.create("admin", appSurvey);
        List<SurveyQuestion> appQs = mkAppQuestions(aid);
        appQs.forEach(surveyQuestionService::create);
        surveyTemplateService.updateStatus("admin", aid, statusChangeCommand);
        SurveyTemplateChangeCommand projectSurvey = mkProjectSurvey();
        long pid = surveyTemplateService.create("admin", projectSurvey);
        List<SurveyQuestion> projQs = mkProjQuestions(pid);
        projQs.forEach(surveyQuestionService::create);
        surveyTemplateService.updateStatus("admin", pid, statusChangeCommand);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SurveyQuestionService(com.khartec.waltz.service.survey.SurveyQuestionService) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ImmutableReleaseLifecycleStatusChangeCommand(com.khartec.waltz.model.ImmutableReleaseLifecycleStatusChangeCommand) ReleaseLifecycleStatusChangeCommand(com.khartec.waltz.model.ReleaseLifecycleStatusChangeCommand) DSLContext(org.jooq.DSLContext) SurveyTemplateService(com.khartec.waltz.service.survey.SurveyTemplateService)

Example 93 with DSLContext

use of org.jooq.DSLContext in project waltz by khartec.

the class UserRoleDao method updateRoles.

public boolean updateRoles(String userName, List<Role> newRoles) {
    try {
        dsl.transaction(config -> {
            LOG.info("Removing existing roles for: " + userName);
            DSL.using(config).delete(USER_ROLE).where(USER_ROLE.USER_NAME.eq(userName)).execute();
            LOG.info("Inserting roles for " + userName + " / " + newRoles);
            DSLContext batcher = DSL.using(config);
            List<Query> inserts = map(newRoles, r -> batcher.insertInto(USER_ROLE, USER_ROLE.USER_NAME, USER_ROLE.ROLE).values(userName, r.name()));
            batcher.batch(inserts).execute();
        });
        return true;
    } catch (Exception e) {
        return false;
    }
}
Also used : Query(org.jooq.Query) DSLContext(org.jooq.DSLContext)

Example 94 with DSLContext

use of org.jooq.DSLContext in project waltz by khartec.

the class AuthSourceHarness method main.

public static void main(String[] args) {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DIConfiguration.class);
    DSLContext dsl = ctx.getBean(DSLContext.class);
    AuthoritativeSourceService svc = ctx.getBean(AuthoritativeSourceService.class);
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) DSLContext(org.jooq.DSLContext) AuthoritativeSourceService(com.khartec.waltz.service.authoritative_source.AuthoritativeSourceService)

Example 95 with DSLContext

use of org.jooq.DSLContext in project waltz by khartec.

the class ChangeInitiativeHarness method main.

public static void main(String[] args) throws ParseException {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DIConfiguration.class);
    DSLContext dsl = ctx.getBean(DSLContext.class);
    ChangeInitiativeDao dao = ctx.getBean(ChangeInitiativeDao.class);
    ChangeInitiative changeInitiative = dao.getById(1L);
    System.out.println(changeInitiative);
    SelectConditionStep<Record1<Long>> ouHier = dsl.selectDistinct(ENTITY_HIERARCHY.ANCESTOR_ID).from(ENTITY_HIERARCHY).where(ENTITY_HIERARCHY.ID.eq(210L).and(ENTITY_HIERARCHY.KIND.eq(EntityKind.ORG_UNIT.name())));
    SelectConditionStep<Record1<Long>> baseIdSelector = dsl.selectDistinct(CHANGE_INITIATIVE.ID).from(CHANGE_INITIATIVE).where(CHANGE_INITIATIVE.ORGANISATIONAL_UNIT_ID.in(ouHier));
    SelectConditionStep<Record1<Long>> ciHier = dsl.selectDistinct(ENTITY_HIERARCHY.ANCESTOR_ID).from(ENTITY_HIERARCHY).where(ENTITY_HIERARCHY.ID.in(baseIdSelector).and(ENTITY_HIERARCHY.KIND.eq(EntityKind.CHANGE_INITIATIVE.name())));
    dsl.select(CHANGE_INITIATIVE.NAME).from(CHANGE_INITIATIVE).where(CHANGE_INITIATIVE.ID.in(ciHier)).forEach(System.out::println);
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) ChangeInitiativeDao(com.khartec.waltz.data.change_initiative.ChangeInitiativeDao) ChangeInitiative(com.khartec.waltz.model.change_initiative.ChangeInitiative) DSLContext(org.jooq.DSLContext) Record1(org.jooq.Record1)

Aggregations

DSLContext (org.jooq.DSLContext)109 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)55 Connection (java.sql.Connection)23 SQLException (java.sql.SQLException)17 List (java.util.List)17 DIConfiguration (com.khartec.waltz.service.DIConfiguration)14 Collectors (java.util.stream.Collectors)14 EntityKind (com.khartec.waltz.model.EntityKind)11 ArrayList (java.util.ArrayList)9 DSL (org.jooq.impl.DSL)9 EntityReference (com.khartec.waltz.model.EntityReference)8 Timestamp (java.sql.Timestamp)8 Random (java.util.Random)8 IntStream (java.util.stream.IntStream)8 Application (com.khartec.waltz.model.application.Application)7 LogicalFlowDao (com.khartec.waltz.data.logical_flow.LogicalFlowDao)6 OrganisationalUnit (com.khartec.waltz.model.orgunit.OrganisationalUnit)6 Field (org.jooq.Field)6 Record1 (org.jooq.Record1)6 Test (org.junit.Test)6