<commit_before>/*
 * Waltz - Enterprise Architecture
 * Copyright (C) 2016, 2017, 2018, 2019 Waltz open source project
 * See README.md for more information
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific
 *
 */

package com.khartec.waltz.web.endpoints.extracts;

import com.khartec.waltz.common.ListUtilities;
import com.khartec.waltz.data.GenericSelector;
import com.khartec.waltz.data.GenericSelectorFactory;
import com.khartec.waltz.data.InlineSelectFieldFactory;
import com.khartec.waltz.model.EntityKind;
import com.khartec.waltz.model.EntityLifecycleStatus;
import com.khartec.waltz.model.EntityReference;
import com.khartec.waltz.model.IdSelectionOptions;
import org.jooq.*;
import org.jooq.impl.DSL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.khartec.waltz.model.IdSelectionOptions.mkOpts;
import static com.khartec.waltz.schema.Tables.*;
import static com.khartec.waltz.schema.tables.Application.APPLICATION;
import static com.khartec.waltz.web.WebUtilities.*;
import static spark.Spark.get;


@Service
public class SurveyRunExtractor extends DirectQueryBasedDataExtractor {

    private static final Logger LOG = LoggerFactory.getLogger(SurveyRunExtractor.class);
    private static List<Field> SURVEY_RESPONSE_FIELDS;
    private static List<Field> SURVEY_INSTANCE_FIELDS;

    private final GenericSelectorFactory genericSelectorFactory = new GenericSelectorFactory();


    static {
        Field<String> ENTITY_RESPONSE_NAME_FIELD = InlineSelectFieldFactory.mkNameField(
                SURVEY_QUESTION_RESPONSE.ENTITY_RESPONSE_ID,
                SURVEY_QUESTION_RESPONSE.ENTITY_RESPONSE_KIND);

        Field<String> answer = DSL.concat(SURVEY_QUESTION_RESPONSE.STRING_RESPONSE.coalesce(""),
                SURVEY_QUESTION_RESPONSE.NUMBER_RESPONSE.cast(String.class).coalesce(""),
                SURVEY_QUESTION_RESPONSE.BOOLEAN_RESPONSE.cast(String.class).coalesce(""),
                SURVEY_QUESTION_RESPONSE.DATE_RESPONSE.cast(String.class).coalesce(""),
                ENTITY_RESPONSE_NAME_FIELD.coalesce(""),
                SURVEY_QUESTION_RESPONSE.LIST_RESPONSE_CONCAT.coalesce("")).as("Answer");

        SURVEY_RESPONSE_FIELDS = ListUtilities.asList(
                SURVEY_QUESTION.SECTION_NAME.as("Section"),
                SURVEY_QUESTION.QUESTION_TEXT.as("Question"),
                answer,
                SURVEY_QUESTION_RESPONSE.COMMENT.as("Comment"));

        SURVEY_INSTANCE_FIELDS = ListUtilities.asList(
                SURVEY_INSTANCE.DUE_DATE.as("Due Date"),
                SURVEY_INSTANCE.SUBMITTED_BY.as("Submitted By"),
                SURVEY_INSTANCE.SUBMITTED_AT.as("Submitted At"),
                SURVEY_INSTANCE.APPROVED_BY.as("Approved By"),
                SURVEY_INSTANCE.APPROVED_AT.as("Approved At"));
    }

    @Autowired
    public SurveyRunExtractor(DSLContext dsl) {
        super(dsl);
    }

    @Override
    public void register() {
        String surveyPath = mkPath("data-extract", "surveys", ":kind", ":id");
        String surveyRunPath = mkPath("data-extract", "survey-run", ":id");
        String surveyInstanceResponsesPath = mkPath("data-extract", "survey-run-response", "instance", ":id");
        String surveyRunResponsesPath = mkPath("data-extract", "survey-run-response", ":id");

        get(surveyRunPath, (request, response) -> {
            long runId = getId(request);

            LOG.info("Survey run has been exported successfully");
            return writeExtract(
                    mkFilename(getSurveyRunNameById(runId)),
                    getSurveyRunInstances(runId),
                    request,
                    response);
        });

        get(surveyInstanceResponsesPath, (request, response) -> {
            long instanceId = getId(request);

            LOG.info("Survey instance with responses has been exported successfully");
            return writeExtract(
                    mkFilename(getSurveyRunNameByInstanceId(instanceId)),
                    getSurveyInstanceResponses(instanceId),
                    request,
                    response);
        });

        get(surveyRunResponsesPath, (request, response) -> {
            long runId = getId(request);

            LOG.info("Survey run with responses has been exported successfully");
            return writeExtract(
                    mkFilename(getSurveyRunNameById(runId)),
                    getSurveyRunWithResponses(runId),
                    request,
                    response);
        });
        
        get(surveyPath, (request, response) -> {
            EntityReference ref = getEntityReference(request);
            
            LOG.info("Survey run with responses has been exported successfully");
            return writeExtract(
                    mkFilename("test"),
                    getSurveysForEntity(ref),
                    request,
                    response);
        });
    }


    private SelectWhereStep<?> getSurveysForEntity(EntityReference entityReference) {

        IdSelectionOptions idSelectionOptions = mkOpts(entityReference);

        GenericSelector genericSelectorForChangeInitiativeIds = genericSelectorFactory.applyForKind(EntityKind.CHANGE_INITIATIVE, idSelectionOptions);
        GenericSelector genericSelectorForApplications = genericSelectorFactory.applyForKind(EntityKind.APPLICATION, idSelectionOptions);

        SelectConditionStep<Record> applicationSurveySelect = DSL.select(
                ORGANISATIONAL_UNIT.NAME.as("Org Unit"),
                APPLICATION.ASSET_CODE.as("Entity Id"),
                APPLICATION.NAME.as("Entity Name"),
                SURVEY_INSTANCE.ENTITY_KIND.as("Entity Kind"),
                SURVEY_INSTANCE.ID.as("Survey Instance Id"),
                SURVEY_TEMPLATE.NAME.as("Survey Template"),
                SURVEY_RUN.NAME.as("Survey Run"),
                PERSON.DISPLAY_NAME.as("Survey Owner"),
                SURVEY_INSTANCE.STATUS.as("Status"))
                .select(SURVEY_INSTANCE_FIELDS)
                .from(SURVEY_INSTANCE)
                .innerJoin(SURVEY_RUN).on(SURVEY_INSTANCE.SURVEY_RUN_ID.eq(SURVEY_RUN.ID))
                .innerJoin(SURVEY_TEMPLATE).on(SURVEY_RUN.SURVEY_TEMPLATE_ID.eq(SURVEY_TEMPLATE.ID)
                        .and(SURVEY_TEMPLATE.TARGET_ENTITY_KIND.eq(EntityKind.APPLICATION.name())))
                .innerJoin(APPLICATION).on(SURVEY_INSTANCE.ENTITY_KIND.eq(EntityKind.APPLICATION.name())
                        .and(APPLICATION.ID.eq(SURVEY_INSTANCE.ENTITY_ID)))
                .innerJoin(ORGANISATIONAL_UNIT).on(APPLICATION.ORGANISATIONAL_UNIT_ID.eq(ORGANISATIONAL_UNIT.ID))
                .leftJoin(PERSON).on(SURVEY_RUN.OWNER_ID.eq(PERSON.ID))
                .where(SURVEY_INSTANCE.ORIGINAL_INSTANCE_ID.isNull()
                        .and(APPLICATION.ENTITY_LIFECYCLE_STATUS.eq(EntityLifecycleStatus.ACTIVE.name())
                                .and(APPLICATION.IS_REMOVED.isFalse()
                                        .and(APPLICATION.ID.in(genericSelectorForApplications.selector())))));


        SelectConditionStep<Record> changeInitiativeSurveySelect = DSL.select(
                ORGANISATIONAL_UNIT.NAME.as("Org Unit"),
                CHANGE_INITIATIVE.EXTERNAL_ID.as("Entity Id"),
                CHANGE_INITIATIVE.NAME.as("Entity Name"),
                SURVEY_INSTANCE.ENTITY_KIND.as("Entity Kind"),
                SURVEY_INSTANCE.ID.as("Survey Instance Id"),
                SURVEY_TEMPLATE.NAME.as("Survey Template"),
                SURVEY_RUN.NAME.as("Survey Run"),
                PERSON.DISPLAY_NAME.as("Survey Owner"),
                SURVEY_INSTANCE.STATUS.as("Status"))
                .select(SURVEY_INSTANCE_FIELDS)
                .from(SURVEY_INSTANCE)
                .innerJoin(SURVEY_RUN).on(SURVEY_INSTANCE.SURVEY_RUN_ID.eq(SURVEY_RUN.ID))
                .innerJoin(SURVEY_TEMPLATE).on(SURVEY_RUN.SURVEY_TEMPLATE_ID.eq(SURVEY_TEMPLATE.ID)
                        .and(SURVEY_TEMPLATE.TARGET_ENTITY_KIND.eq(EntityKind.CHANGE_INITIATIVE.name())))
                .innerJoin(CHANGE_INITIATIVE).on(SURVEY_INSTANCE.ENTITY_KIND.eq(EntityKind.CHANGE_INITIATIVE.name())
                        .and(CHANGE_INITIATIVE.ID.eq(SURVEY_INSTANCE.ENTITY_ID)))
                .innerJoin(ORGANISATIONAL_UNIT).on(CHANGE_INITIATIVE.ORGANISATIONAL_UNIT_ID.eq(ORGANISATIONAL_UNIT.ID))
                .leftJoin(PERSON).on(SURVEY_RUN.OWNER_ID.eq(PERSON.ID))
                .where(SURVEY_INSTANCE.ORIGINAL_INSTANCE_ID.isNull()
                        .and(CHANGE_INITIATIVE.ID.in(genericSelectorForChangeInitiativeIds.selector())));

        return dsl
                .selectFrom(applicationSurveySelect
                        .union(changeInitiativeSurveySelect)
                        .asTable());
    }


    private SelectConditionStep<?> getSurveyRunInstances(long surveyRunId) {
        return dsl
                .select(APPLICATION.NAME.coalesce(CHANGE_INITIATIVE.NAME).as("Entity Name"),<commit_msg>Surveys: concat descriptions in the instance overview<commit_after>/*
 * Waltz - Enterprise Architecture
 * Copyright (C) 2016, 2017, 2018, 2019 Waltz open source project
 * See README.md for more information
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific
 *
 */

package com.khartec.waltz.web.endpoints.extracts;

import com.khartec.waltz.common.ListUtilities;
import com.khartec.waltz.data.GenericSelector;
import com.khartec.waltz.data.GenericSelectorFactory;
import com.khartec.waltz.data.InlineSelectFieldFactory;
import com.khartec.waltz.model.EntityKind;
import com.khartec.waltz.model.EntityLifecycleStatus;
import com.khartec.waltz.model.EntityReference;
import com.khartec.waltz.model.IdSelectionOptions;
import org.jooq.*;
import org.jooq.impl.DSL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.khartec.waltz.model.IdSelectionOptions.mkOpts;
import static com.khartec.waltz.schema.Tables.*;
import static com.khartec.waltz.schema.tables.Application.APPLICATION;
import static com.khartec.waltz.web.WebUtilities.*;
import static spark.Spark.get;


@Service
public class SurveyRunExtractor extends DirectQueryBasedDataExtractor {

    private static final Logger LOG = LoggerFactory.getLogger(SurveyRunExtractor.class);
    private static List<Field> SURVEY_RESPONSE_FIELDS;
    private static List<Field> SURVEY_INSTANCE_FIELDS;

    private final GenericSelectorFactory genericSelectorFactory = new GenericSelectorFactory();


    static {
        Field<String> ENTITY_RESPONSE_NAME_FIELD = InlineSelectFieldFactory.mkNameField(
                SURVEY_QUESTION_RESPONSE.ENTITY_RESPONSE_ID,
                SURVEY_QUESTION_RESPONSE.ENTITY_RESPONSE_KIND);

        Field<String> answer = DSL.concat(SURVEY_QUESTION_RESPONSE.STRING_RESPONSE.coalesce(""),
                SURVEY_QUESTION_RESPONSE.NUMBER_RESPONSE.cast(String.class).coalesce(""),
                SURVEY_QUESTION_RESPONSE.BOOLEAN_RESPONSE.cast(String.class).coalesce(""),
                SURVEY_QUESTION_RESPONSE.DATE_RESPONSE.cast(String.class).coalesce(""),
                ENTITY_RESPONSE_NAME_FIELD.coalesce(""),
                SURVEY_QUESTION_RESPONSE.LIST_RESPONSE_CONCAT.coalesce("")).as("Answer");

        SURVEY_RESPONSE_FIELDS = ListUtilities.asList(
                SURVEY_QUESTION.SECTION_NAME.as("Section"),
                SURVEY_QUESTION.QUESTION_TEXT.as("Question"),
                answer,
                SURVEY_QUESTION_RESPONSE.COMMENT.as("Comment"));

        SURVEY_INSTANCE_FIELDS = ListUtilities.asList(
                SURVEY_INSTANCE.DUE_DATE.as("Due Date"),
                SURVEY_INSTANCE.SUBMITTED_BY.as("Submitted By"),
                SURVEY_INSTANCE.SUBMITTED_AT.as("Submitted At"),
                SURVEY_INSTANCE.APPROVED_BY.as("Approved By"),
                SURVEY_INSTANCE.APPROVED_AT.as("Approved At"));
    }

    @Autowired
    public SurveyRunExtractor(DSLContext dsl) {
        super(dsl);
    }

    @Override
    public void register() {
        String surveyPath = mkPath("data-extract", "surveys", ":kind", ":id");
        String surveyRunPath = mkPath("data-extract", "survey-run", ":id");
        String surveyInstanceResponsesPath = mkPath("data-extract", "survey-run-response", "instance", ":id");
        String surveyRunResponsesPath = mkPath("data-extract", "survey-run-response", ":id");

        get(surveyRunPath, (request, response) -> {
            long runId = getId(request);

            LOG.info("Survey run has been exported successfully");
            return writeExtract(
                    mkFilename(getSurveyRunNameById(runId)),
                    getSurveyRunInstances(runId),
                    request,
                    response);
        });

        get(surveyInstanceResponsesPath, (request, response) -> {
            long instanceId = getId(request);

            LOG.info("Survey instance with responses has been exported successfully");
            return writeExtract(
                    mkFilename(getSurveyRunNameByInstanceId(instanceId)),
                    getSurveyInstanceResponses(instanceId),
                    request,
                    response);
        });

        get(surveyRunResponsesPath, (request, response) -> {
            long runId = getId(request);

            LOG.info("Survey run with responses has been exported successfully");
            return writeExtract(
                    mkFilename(getSurveyRunNameById(runId)),
                    getSurveyRunWithResponses(runId),
                    request,
                    response);
        });
        
        get(surveyPath, (request, response) -> {
            EntityReference ref = getEntityReference(request);
            
            LOG.info("Survey run with responses has been exported successfully");
            return writeExtract(
                    mkFilename("test"),
                    getSurveysForEntity(ref),
                    request,
                    response);
        });
    }


    private SelectWhereStep<?> getSurveysForEntity(EntityReference entityReference) {

        IdSelectionOptions idSelectionOptions = mkOpts(entityReference);

        GenericSelector genericSelectorForChangeInitiativeIds = genericSelectorFactory.applyForKind(EntityKind.CHANGE_INITIATIVE, idSelectionOptions);
        GenericSelector genericSelectorForApplications = genericSelectorFactory.applyForKind(EntityKind.APPLICATION, idSelectionOptions);

        SelectConditionStep<Record> applicationSurveySelect = DSL.select(
                ORGANISATIONAL_UNIT.NAME.as("Org Unit"),
                APPLICATION.ASSET_CODE.as("Entity Id"),
                APPLICATION.NAME.as("Entity Name"),
                SURVEY_INSTANCE.ENTITY_KIND.as("Entity Kind"),
                SURVEY_INSTANCE.ID.as("Survey Instance Id"),
                SURVEY_TEMPLATE.NAME.as("Survey Template"),
                SURVEY_RUN.NAME.as("Survey Run"),
                PERSON.DISPLAY_NAME.as("Survey Owner"),
                SURVEY_INSTANCE.STATUS.as("Status"))
                .select(SURVEY_INSTANCE_FIELDS)
                .from(SURVEY_INSTANCE)
                .innerJoin(SURVEY_RUN).on(SURVEY_INSTANCE.SURVEY_RUN_ID.eq(SURVEY_RUN.ID))
                .innerJoin(SURVEY_TEMPLATE).on(SURVEY_RUN.SURVEY_TEMPLATE_ID.eq(SURVEY_TEMPLATE.ID)
                        .and(SURVEY_TEMPLATE.TARGET_ENTITY_KIND.eq(EntityKind.APPLICATION.name())))
                .innerJoin(APPLICATION).on(SURVEY_INSTANCE.ENTITY_KIND.eq(EntityKind.APPLICATION.name())
                        .and(APPLICATION.ID.eq(SURVEY_INSTANCE.ENTITY_ID)))
                .innerJoin(ORGANISATIONAL_UNIT).on(APPLICATION.ORGANISATIONAL_UNIT_ID.eq(ORGANISATIONAL_UNIT.ID))
                .leftJoin(PERSON).on(SURVEY_RUN.OWNER_ID.eq(PERSON.ID))
                .where(SURVEY_INSTANCE.ORIGINAL_INSTANCE_ID.isNull()
                        .and(APPLICATION.ENTITY_LIFECYCLE_STATUS.eq(EntityLifecycleStatus.ACTIVE.name())
                                .and(APPLICATION.IS_REMOVED.isFalse()
                                        .and(APPLICATION.ID.in(genericSelectorForApplications.selector())))));


        SelectConditionStep<Record> changeInitiativeSurveySelect = DSL.select(
                ORGANISATIONAL_UNIT.NAME.as("Org Unit"),
                CHANGE_INITIATIVE.EXTERNAL_ID.as("Entity Id"),
                CHANGE_INITIATIVE.NAME.as("Entity Name"),
                SURVEY_INSTANCE.ENTITY_KIND.as("Entity Kind"),
                SURVEY_INSTANCE.ID.as("Survey Instance Id"),
                SURVEY_TEMPLATE.NAME.as("Survey Template"),
                SURVEY_RUN.NAME.as("Survey Run"),
                PERSON.DISPLAY_NAME.as("Survey Owner"),
                SURVEY_INSTANCE.STATUS.as("Status"))
                .select(SURVEY_INSTANCE_FIELDS)
                .from(SURVEY_INSTANCE)
                .innerJoin(SURVEY_RUN).on(SURVEY_INSTANCE.SURVEY_RUN_ID.eq(SURVEY_RUN.ID))
                .innerJoin(SURVEY_TEMPLATE).on(SURVEY_RUN.SURVEY_TEMPLATE_ID.eq(SURVEY_TEMPLATE.ID)
                        .and(SURVEY_TEMPLATE.TARGET_ENTITY_KIND.eq(EntityKind.CHANGE_INITIATIVE.name())))
                .innerJoin(CHANGE_INITIATIVE).on(SURVEY_INSTANCE.ENTITY_KIND.eq(EntityKind.CHANGE_INITIATIVE.name())
                        .and(CHANGE_INITIATIVE.ID.eq(SURVEY_INSTANCE.ENTITY_ID)))
                .innerJoin(ORGANISATIONAL_UNIT).on(CHANGE_INITIATIVE.ORGANISATIONAL_UNIT_ID.eq(ORGANISATIONAL_UNIT.ID))
                .leftJoin(PERSON).on(SURVEY_RUN.OWNER_ID.eq(PERSON.ID))
                .where(SURVEY_INSTANCE.ORIGINAL_INSTANCE_ID.isNull()
                        .and(CHANGE_INITIATIVE.ID.in(genericSelectorForChangeInitiativeIds.selector())));

        return dsl
                .selectFrom(applicationSurveySelect.union(changeInitiativeSurveySelect)
                        .asTable());
    }


    private SelectConditionStep<?> getSurveyRunInstances(long surveyRunId) {
        return dsl
                .select(APPLICATION.NAME.coalesce(CHANGE_INITIATIVE.NAME).as("Entity Name"),
{'fraction_non_alphanumeric': 0.09786523304974164, 'fraction_numerical': 0.002296570802233937, 'mean_word_length': 2.6439710916698362, 'pattern_counts': {'":': 10, '<': 19, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5221656', 'n_tokens_mistral': 6040, 'n_tokens_neox': 5782, 'n_words': 798}