com.khartec.waltz.jobs.sample.InvolvementGenerator.java Source code

Java tutorial

Introduction

Here is the source code for com.khartec.waltz.jobs.sample.InvolvementGenerator.java

Source

/*
 *  This file is part of Waltz.
 *
 *     Waltz is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Waltz is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Waltz.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.khartec.waltz.jobs.sample;

import com.khartec.waltz.model.EntityKind;
import com.khartec.waltz.model.EntityReference;
import com.khartec.waltz.model.ImmutableEntityReference;
import com.khartec.waltz.model.application.ApplicationKind;
import com.khartec.waltz.model.involvement.InvolvementKind;
import com.khartec.waltz.schema.tables.records.InvolvementRecord;
import com.khartec.waltz.service.DIConfiguration;
import org.jooq.DSLContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import static com.khartec.waltz.common.ListUtilities.concat;
import static com.khartec.waltz.common.ListUtilities.randomPick;
import static com.khartec.waltz.schema.tables.Application.APPLICATION;
import static com.khartec.waltz.schema.tables.Involvement.INVOLVEMENT;
import static com.khartec.waltz.schema.tables.OrganisationalUnit.ORGANISATIONAL_UNIT;
import static com.khartec.waltz.schema.tables.Person.PERSON;

public class InvolvementGenerator {

    private static final Random rnd = new Random();

    public static void main(String[] args) {

        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(DIConfiguration.class);

        DSLContext dsl = ctx.getBean(DSLContext.class);

        List<String> developers = getEmployeeIdsByTitle(dsl, "%developer%");
        List<String> managers = getEmployeeIdsByTitle(dsl, "%manager%");
        List<String> analysts = getEmployeeIdsByTitle(dsl, "%analyst%");
        List<String> administrators = getEmployeeIdsByTitle(dsl, "%administrator%");
        List<String> qa = getEmployeeIdsByTitle(dsl, "%qa%");
        List<String> directors = getEmployeeIdsByTitle(dsl, "%director%");
        ;

        List<Long> orgUnitIds = dsl.select(ORGANISATIONAL_UNIT.ID).from(ORGANISATIONAL_UNIT)
                .fetch(ORGANISATIONAL_UNIT.ID);

        List<Long> inHouseApps = getAppIdsByKind(dsl, ApplicationKind.IN_HOUSE);
        List<Long> hostedApps = getAppIdsByKind(dsl, ApplicationKind.INTERNALLY_HOSTED);
        List<Long> externalApps = getAppIdsByKind(dsl, ApplicationKind.EXTERNALLY_HOSTED);
        List<Long> eucApps = getAppIdsByKind(dsl, ApplicationKind.EUC);

        List<InvolvementRecord> devInvolvements = inHouseApps.stream().map(id -> toAppRef(id))
                .flatMap(appRef -> mkInvolvments(appRef, developers, InvolvementKind.DEVELOPER, 7))
                .collect(Collectors.toList());

        List<InvolvementRecord> qaInvolvements = concat(inHouseApps, hostedApps).stream().map(id -> toAppRef(id))
                .flatMap(appRef -> mkInvolvments(appRef, qa, InvolvementKind.QA, 3)).collect(Collectors.toList());

        List<InvolvementRecord> projectManagerInvolvements = concat(inHouseApps, externalApps, hostedApps, eucApps)
                .stream().map(id -> toAppRef(id))
                .flatMap(appRef -> mkInvolvments(appRef, managers, InvolvementKind.PROJECT_MANAGER, 1))
                .collect(Collectors.toList());

        List<InvolvementRecord> supportManagerInvolvments = concat(inHouseApps, externalApps, hostedApps).stream()
                .map(id -> toAppRef(id))
                .flatMap(appRef -> mkInvolvments(appRef, managers, InvolvementKind.SUPPORT_MANAGER, 1))
                .collect(Collectors.toList());

        List<InvolvementRecord> analystInvolvments = concat(inHouseApps, externalApps, hostedApps).stream()
                .map(id -> toAppRef(id))
                .flatMap(appRef -> mkInvolvments(appRef, analysts, InvolvementKind.BUSINESS_ANALYST, 3))
                .collect(Collectors.toList());

        List<InvolvementRecord> ouArchitects = orgUnitIds.stream()
                .map(id -> new InvolvementRecord(EntityKind.ORG_UNIT.name(), id,
                        InvolvementKind.IT_ARCHITECT.name(), randomPick(directors), "RANDOM_GENERATOR"))
                .collect(Collectors.toList());

        List<InvolvementRecord> ouSponsors = orgUnitIds.stream()
                .map(id -> new InvolvementRecord(EntityKind.ORG_UNIT.name(), id,
                        InvolvementKind.BUSINESS_SPONSOR.name(), randomPick(directors), "RANDOM_GENERATOR"))
                .collect(Collectors.toList());

        dsl.delete(INVOLVEMENT).execute();

        dsl.batchInsert(devInvolvements).execute();
        dsl.batchInsert(qaInvolvements).execute();
        dsl.batchInsert(supportManagerInvolvments).execute();
        dsl.batchInsert(projectManagerInvolvements).execute();
        dsl.batchInsert(analystInvolvments).execute();
        dsl.batchInsert(ouArchitects).execute();
        dsl.batchInsert(ouSponsors).execute();

        System.out.println("Done");

    }

    private static List<Long> getAppIdsByKind(DSLContext dsl, ApplicationKind kind) {
        return dsl.select(APPLICATION.ID).from(APPLICATION).where(APPLICATION.KIND.eq(kind.name()))
                .fetch(APPLICATION.ID);
    }

    private static List<String> getEmployeeIdsByTitle(DSLContext dsl, String title) {
        return dsl.select(PERSON.EMPLOYEE_ID).from(PERSON).where(PERSON.TITLE.like(title))
                .fetch(PERSON.EMPLOYEE_ID);
    }

    private static Stream<InvolvementRecord> mkInvolvments(EntityReference appRef, List<String> employeeIds,
            InvolvementKind kind, int upperBound) {
        int count = rnd.nextInt(upperBound) + 1;
        return IntStream.range(0, count).mapToObj(i -> new InvolvementRecord(appRef.kind().name(), appRef.id(),
                kind.name(), randomPick(employeeIds), "RANDOM_GENERATOR"));
    }

    private static EntityReference toAppRef(Long id) {
        return ImmutableEntityReference.builder().id(id).kind(EntityKind.APPLICATION).build();
    }

}