<commit_before>package ch.difty.scipamato.publ.persistence.newstudies;

import static ch.difty.scipamato.publ.db.tables.NewStudy.NEW_STUDY;
import static ch.difty.scipamato.publ.db.tables.NewStudyTopic.NEW_STUDY_TOPIC;
import static ch.difty.scipamato.publ.db.tables.Newsletter.NEWSLETTER;
import static ch.difty.scipamato.publ.db.tables.NewsletterTopic.NEWSLETTER_TOPIC;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.jooq.*;
import org.jooq.impl.DSL;
import org.springframework.stereotype.Repository;

import ch.difty.scipamato.common.AssertAs;
import ch.difty.scipamato.publ.db.tables.NewsletterTopic;
import ch.difty.scipamato.publ.entity.NewStudy;
import ch.difty.scipamato.publ.entity.NewStudyTopic;

@Repository
public class JooqNewStudyRepo implements NewStudyRepository {

    private final DSLContext dsl;

    public JooqNewStudyRepo(final DSLContext dsl) {
        this.dsl = dsl;
    }

    @Override
    public List<NewStudyTopic> findNewStudyTopicsForNewsletter(final int newsletterId, String languageCode) {
        AssertAs.notNull(languageCode, "languageCode");

        // References to aliased tables
        final ch.difty.scipamato.publ.db.tables.NewStudyTopic newStudyTopicTable = NEW_STUDY_TOPIC.as("nst");
        final ch.difty.scipamato.publ.db.tables.NewsletterTopic newsletterTopicTable = NEWSLETTER_TOPIC.as("nt");
        final ch.difty.scipamato.publ.db.tables.NewStudy newStudyTable = NEW_STUDY.as("ns");

        // References to aliased table fields (to avoid ambiguous field names
        final Field<Integer> newStudyTopicSortField = newStudyTopicTable.SORT.as("nstSort");
        final Field<Integer> newStudySortField = newStudyTable.SORT.as("nsSort");

        // Fetch the result set (Grouped NewStudyTopics with associated NewStudies)
        final Result<Record13<Integer, String, Integer, Integer, Integer, Long, Integer, String, String, String, Integer, Timestamp, Timestamp>> fetch = dsl
            .select(newStudyTopicSortField, newsletterTopicTable.TITLE, newStudyTable.NEWSLETTER_ID,
                newStudyTable.NEWSLETTER_TOPIC_ID, newStudySortField, newStudyTable.PAPER_NUMBER, newStudyTable.YEAR,
                newStudyTable.AUTHORS, newStudyTable.HEADLINE, newStudyTable.DESCRIPTION, newStudyTable.VERSION,
                newStudyTable.CREATED, newStudyTable.LAST_MODIFIED)
            .from(newStudyTopicTable)
            .leftOuterJoin(newsletterTopicTable)
            .on(newStudyTopicTable.NEWSLETTER_TOPIC_ID.eq(newsletterTopicTable.ID))
            .leftOuterJoin(newStudyTable)
            .on(newStudyTopicTable.NEWSLETTER_ID
                .eq(newStudyTable.NEWSLETTER_ID)
                .and(newStudyTopicTable.NEWSLETTER_TOPIC_ID.eq(newStudyTable.NEWSLETTER_TOPIC_ID)))
            .where(newStudyTopicTable.NEWSLETTER_ID.eq(newsletterId))
            .and(DSL
                .isnull(newsletterTopicTable.LANG_CODE, languageCode)
                .eq(languageCode))
            .orderBy(newStudyTopicSortField, newsletterTopicTable.TITLE, newStudySortField, newStudyTable.DESCRIPTION)
            .fetch();
        final Map<Record, Result<Record13<Integer, String, Integer, Integer, Integer, Long, Integer, String, String, String, Integer, Timestamp, Timestamp>>> resultSet = fetch
            .intoGroups(new Field[] { newStudyTopicSortField, newsletterTopicTable.TITLE });

        return processDbRecords(resultSet, newsletterTopicTable, newStudyTopicSortField, newStudyTable,
            newStudySortField);
    }

    /*
     * Walks through the recordset and extracts the NewStudyTopic with all assocated NewStudies<commit_msg>Fix typo<commit_after>package ch.difty.scipamato.publ.persistence.newstudies;

import static ch.difty.scipamato.publ.db.tables.NewStudy.NEW_STUDY;
import static ch.difty.scipamato.publ.db.tables.NewStudyTopic.NEW_STUDY_TOPIC;
import static ch.difty.scipamato.publ.db.tables.Newsletter.NEWSLETTER;
import static ch.difty.scipamato.publ.db.tables.NewsletterTopic.NEWSLETTER_TOPIC;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import org.jooq.*;
import org.jooq.impl.DSL;
import org.springframework.stereotype.Repository;

import ch.difty.scipamato.common.AssertAs;
import ch.difty.scipamato.publ.db.tables.NewsletterTopic;
import ch.difty.scipamato.publ.entity.NewStudy;
import ch.difty.scipamato.publ.entity.NewStudyTopic;

@Repository
public class JooqNewStudyRepo implements NewStudyRepository {

    private final DSLContext dsl;

    public JooqNewStudyRepo(final DSLContext dsl) {
        this.dsl = dsl;
    }

    @Override
    public List<NewStudyTopic> findNewStudyTopicsForNewsletter(final int newsletterId, String languageCode) {
        AssertAs.notNull(languageCode, "languageCode");

        // References to aliased tables
        final ch.difty.scipamato.publ.db.tables.NewStudyTopic newStudyTopicTable = NEW_STUDY_TOPIC.as("nst");
        final ch.difty.scipamato.publ.db.tables.NewsletterTopic newsletterTopicTable = NEWSLETTER_TOPIC.as("nt");
        final ch.difty.scipamato.publ.db.tables.NewStudy newStudyTable = NEW_STUDY.as("ns");

        // References to aliased table fields (to avoid ambiguous field names
        final Field<Integer> newStudyTopicSortField = newStudyTopicTable.SORT.as("nstSort");
        final Field<Integer> newStudySortField = newStudyTable.SORT.as("nsSort");

        // Fetch the result set (Grouped NewStudyTopics with associated NewStudies)
        final Result<Record13<Integer, String, Integer, Integer, Integer, Long, Integer, String, String, String, Integer, Timestamp, Timestamp>> fetch = dsl
            .select(newStudyTopicSortField, newsletterTopicTable.TITLE, newStudyTable.NEWSLETTER_ID,
                newStudyTable.NEWSLETTER_TOPIC_ID, newStudySortField, newStudyTable.PAPER_NUMBER, newStudyTable.YEAR,
                newStudyTable.AUTHORS, newStudyTable.HEADLINE, newStudyTable.DESCRIPTION, newStudyTable.VERSION,
                newStudyTable.CREATED, newStudyTable.LAST_MODIFIED)
            .from(newStudyTopicTable)
            .leftOuterJoin(newsletterTopicTable)
            .on(newStudyTopicTable.NEWSLETTER_TOPIC_ID.eq(newsletterTopicTable.ID))
            .leftOuterJoin(newStudyTable)
            .on(newStudyTopicTable.NEWSLETTER_ID
                .eq(newStudyTable.NEWSLETTER_ID)
                .and(newStudyTopicTable.NEWSLETTER_TOPIC_ID.eq(newStudyTable.NEWSLETTER_TOPIC_ID)))
            .where(newStudyTopicTable.NEWSLETTER_ID.eq(newsletterId))
            .and(DSL
                .isnull(newsletterTopicTable.LANG_CODE, languageCode)
                .eq(languageCode))
            .orderBy(newStudyTopicSortField, newsletterTopicTable.TITLE, newStudySortField, newStudyTable.DESCRIPTION)
            .fetch();
        final Map<Record, Result<Record13<Integer, String, Integer, Integer, Integer, Long, Integer, String, String, String, Integer, Timestamp, Timestamp>>> resultSet = fetch
            .intoGroups(new Field[] { newStudyTopicSortField, newsletterTopicTable.TITLE });

        return processDbRecords(resultSet, newsletterTopicTable, newStudyTopicSortField, newStudyTable,
            newStudySortField);
    }

    /*
     * Walks through the recordset and extracts the NewStudyTopic with all associated NewStudies
{'fraction_non_alphanumeric': 0.08945945945945946, 'fraction_numerical': 0.001081081081081081, 'mean_word_length': 3.8499344692005244, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5050013', 'n_tokens_mistral': 2441, 'n_tokens_neox': 2283, 'n_words': 384}