<commit_before>package org.xcolab.service.members.domain.messaging;

import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Record1;
import org.jooq.SelectQuery;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import org.xcolab.model.tables.pojos.Member;
import org.xcolab.model.tables.pojos.Message;
import org.xcolab.model.tables.records.MessageRecord;
import org.xcolab.service.members.exceptions.NotFoundException;
import org.xcolab.service.members.wrappers.MessageReceived;
import org.xcolab.service.utils.PaginationHelper;
import org.xcolab.service.utils.PaginationHelper.SortColumn;

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

import static org.xcolab.model.Tables.MEMBER;
import static org.xcolab.model.Tables.MESSAGE;
import static org.xcolab.model.Tables.MESSAGE_RECIPIENT_STATUS;

@Repository
public class MessageDaoImpl implements MessageDao {

    @Autowired
    private DSLContext dslContext;

    @Override
    public Message getMessage(long messageId) throws NotFoundException {
        final Record record = dslContext.select()
                .from(MESSAGE)
                .where(MESSAGE.MESSAGE_ID.eq(messageId))
                .fetchOne();
        if (record == null) {
            throw new NotFoundException("Message with id " + messageId + "does not exist");
        }
        return record.into(Message.class);
    }

    @Override
    public int countByGiven(Long senderId, Long recipientId, Boolean isArchived, Boolean isOpened, Timestamp sinceDate) {
        final SelectQuery<Record1<Integer>> query = dslContext.selectCount()
                .from(MESSAGE).getQuery();

        if (recipientId != null || isArchived != null || isOpened != null) {
            query.addJoin(MESSAGE_RECIPIENT_STATUS, MESSAGE.MESSAGE_ID.eq(MESSAGE_RECIPIENT_STATUS.MESSAGE_ID));
        }
        if (senderId != null) {
            query.addConditions(MESSAGE.FROM_ID.eq(senderId));
        }
        if (recipientId != null) {
            query.addConditions(MESSAGE_RECIPIENT_STATUS.USER_ID.eq(recipientId));
        }
        if (isArchived != null) {
            query.addConditions(MESSAGE_RECIPIENT_STATUS.ARCHIVED.eq(isArchived));
        }
        if (isOpened != null) {
            query.addConditions(MESSAGE_RECIPIENT_STATUS.OPENED.eq(isOpened));
        }
        if (sinceDate != null) {
            query.addConditions(MESSAGE.CREATE_DATE.gt(sinceDate));
        }

        return query.fetchOne(0, Integer.class);
    }

    @Override
    public List<Message> findByGiven(PaginationHelper paginationHelper,
            Long senderId, Long recipientId, Boolean isArchived, Boolean isOpened, Timestamp sinceDate) {
        final SelectQuery<Record> query = dslContext.select()
                .from(MESSAGE).getQuery();

        if (recipientId != null || isArchived != null || isOpened != null) {
            query.addJoin(MESSAGE_RECIPIENT_STATUS, MESSAGE.MESSAGE_ID.eq(MESSAGE_RECIPIENT_STATUS.MESSAGE_ID));
        }
        if (senderId != null) {
            query.addConditions(MESSAGE.FROM_ID.eq(senderId));
        }
        if (recipientId != null) {
            query.addConditions(MESSAGE_RECIPIENT_STATUS.USER_ID.eq(recipientId));
        }
        if (isArchived != null) {
            query.addConditions(MESSAGE_RECIPIENT_STATUS.ARCHIVED.eq(isArchived));
        }
        if (isOpened != null) {
            query.addConditions(MESSAGE_RECIPIENT_STATUS.OPENED.eq(isOpened));
        }
        if (sinceDate != null) {
            query.addConditions(MESSAGE.CREATE_DATE.gt(sinceDate));
        }

        for (SortColumn sortColumn : paginationHelper.getSortColumns()) {
            switch (sortColumn.getColumnName()) {
                case "senderId":
                    query.addOrderBy(sortColumn.isAscending()
                            ? MESSAGE.FROM_ID.asc()
                            : MESSAGE.FROM_ID.desc());
                    break;
                case "recipientId":
                    query.addOrderBy(sortColumn.isAscending()
                            ? MESSAGE_RECIPIENT_STATUS.MESSAGE_RECIPIENT_ID.asc()
                            : MESSAGE_RECIPIENT_STATUS.MESSAGE_RECIPIENT_ID.desc());
                    break;
                case "isArchived":
                    query.addOrderBy(sortColumn.isAscending()
                            ? MESSAGE_RECIPIENT_STATUS.ARCHIVED.asc()<commit_msg>[COLAB-1341] Excluded broken messages without recipients from MessageDao<commit_after>package org.xcolab.service.members.domain.messaging;

import org.jooq.DSLContext;
import org.jooq.Record;
import org.jooq.Record1;
import org.jooq.SelectQuery;
import org.jooq.impl.DSL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import org.xcolab.model.tables.pojos.Member;
import org.xcolab.model.tables.pojos.Message;
import org.xcolab.model.tables.records.MessageRecord;
import org.xcolab.service.members.exceptions.NotFoundException;
import org.xcolab.service.members.wrappers.MessageReceived;
import org.xcolab.service.utils.PaginationHelper;
import org.xcolab.service.utils.PaginationHelper.SortColumn;

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

import static org.xcolab.model.Tables.MEMBER;
import static org.xcolab.model.Tables.MESSAGE;
import static org.xcolab.model.Tables.MESSAGE_RECIPIENT_STATUS;

@Repository
public class MessageDaoImpl implements MessageDao {

    @Autowired
    private DSLContext dslContext;

    @Override
    public Message getMessage(long messageId) throws NotFoundException {
        final Record record = dslContext.select()
                .from(MESSAGE)
                .where(MESSAGE.MESSAGE_ID.eq(messageId))
                .fetchOne();
        if (record == null) {
            throw new NotFoundException("Message with id " + messageId + "does not exist");
        }
        return record.into(Message.class);
    }

    @Override
    public int countByGiven(Long senderId, Long recipientId, Boolean isArchived, Boolean isOpened, Timestamp sinceDate) {
        final SelectQuery<Record1<Integer>> query = dslContext.selectCount()
                .from(MESSAGE)
                .join(MESSAGE_RECIPIENT_STATUS).on(MESSAGE.MESSAGE_ID.eq(MESSAGE_RECIPIENT_STATUS.MESSAGE_ID))
                .getQuery();

        if (senderId != null) {
            query.addConditions(MESSAGE.FROM_ID.eq(senderId));
        }
        if (recipientId != null) {
            query.addConditions(MESSAGE_RECIPIENT_STATUS.USER_ID.eq(recipientId));
        }
        if (isArchived != null) {
            query.addConditions(MESSAGE_RECIPIENT_STATUS.ARCHIVED.eq(isArchived));
        }
        if (isOpened != null) {
            query.addConditions(MESSAGE_RECIPIENT_STATUS.OPENED.eq(isOpened));
        }
        if (sinceDate != null) {
            query.addConditions(MESSAGE.CREATE_DATE.gt(sinceDate));
        }

        return query.fetchOne(0, Integer.class);
    }

    @Override
    public List<Message> findByGiven(PaginationHelper paginationHelper,
            Long senderId, Long recipientId, Boolean isArchived, Boolean isOpened, Timestamp sinceDate) {
        final SelectQuery<Record> query = dslContext.select()
                .from(MESSAGE)
                .join(MESSAGE_RECIPIENT_STATUS).on(MESSAGE.MESSAGE_ID.eq(MESSAGE_RECIPIENT_STATUS.MESSAGE_ID))
                .getQuery();

        if (senderId != null) {
            query.addConditions(MESSAGE.FROM_ID.eq(senderId));
        }
        if (recipientId != null) {
            query.addConditions(MESSAGE_RECIPIENT_STATUS.USER_ID.eq(recipientId));
        }
        if (isArchived != null) {
            query.addConditions(MESSAGE_RECIPIENT_STATUS.ARCHIVED.eq(isArchived));
        }
        if (isOpened != null) {
            query.addConditions(MESSAGE_RECIPIENT_STATUS.OPENED.eq(isOpened));
        }
        if (sinceDate != null) {
            query.addConditions(MESSAGE.CREATE_DATE.gt(sinceDate));
        }

        for (SortColumn sortColumn : paginationHelper.getSortColumns()) {
            switch (sortColumn.getColumnName()) {
                case "senderId":
                    query.addOrderBy(sortColumn.isAscending()
                            ? MESSAGE.FROM_ID.asc()
                            : MESSAGE.FROM_ID.desc());
                    break;
                case "recipientId":
                    query.addOrderBy(sortColumn.isAscending()
                            ? MESSAGE_RECIPIENT_STATUS.MESSAGE_RECIPIENT_ID.asc()
                            : MESSAGE_RECIPIENT_STATUS.MESSAGE_RECIPIENT_ID.desc());
                    break;
                case "isArchived":
                    query.addOrderBy(sortColumn.isAscending()
                            ? MESSAGE_RECIPIENT_STATUS.ARCHIVED.asc()
{'fraction_non_alphanumeric': 0.09985430908887145, 'fraction_numerical': 0.001120699316373417, 'mean_word_length': 2.5120031483667846, 'pattern_counts': {'":': 6, '<': 11, '<?xml version=': 0, '>': 11, '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': '4675795', 'n_tokens_mistral': 2631, 'n_tokens_neox': 2575, 'n_words': 400}