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

import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.xcolab.model.tables.pojos.Message;
import org.xcolab.model.tables.pojos.User_;

import java.util.List;

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

@Repository
public class MessageDaoImpl implements MessageDao {

    @Autowired
    private DSLContext dslContext;

    @Override
    public Message getMessage(long messageId) {
        return dslContext.select()
                .from(MESSAGE)
                .where(MESSAGE.MESSAGE_ID.eq(messageId))
                .fetchOne().into(Message.class);
    }

    @Override
    public int countBySendingUser(long userId) {
        return dslContext.selectCount()
                .from(MESSAGE)
                .where(MESSAGE.FROM_ID.equal(userId))
                .fetchOne(0, Integer.class);
    }

    @Override
    public int countByReceivingUserArchived(long userId, boolean isArchived) {
        final byte isArchivedByte = (byte) (isArchived ? 1 : 0);
        return dslContext.selectCount()
                .from(MESSAGE_RECIPIENT_STATUS)
                .where(MESSAGE_RECIPIENT_STATUS.USER_ID.equal(userId)
                        .and(MESSAGE_RECIPIENT_STATUS.ARCHIVED.equal(isArchivedByte))
                ).fetchOne(0, Integer.class);
    }

    @Override
    public int countByReceivingUserOpened(long userId, boolean isOpened) {
        final byte isOpenedByte = (byte) (isOpened ? 1 : 0);
        return dslContext.selectCount()
                .from(MESSAGE_RECIPIENT_STATUS)
                .where(MESSAGE_RECIPIENT_STATUS.USER_ID.equal(userId)
                        .and(MESSAGE_RECIPIENT_STATUS.OPENED.equal(isOpenedByte))
                ).fetchOne(0, Integer.class);
    }

    @Override
    public List<Message> findByReceivingUserArchived(int startRecord, int limitRecord, long userId, boolean isArchived) {
        final byte isArchivedByte = (byte) (isArchived ? 1 : 0);
        return dslContext.select()
                .from(MESSAGE)
                .join(MESSAGE_RECIPIENT_STATUS).on(MESSAGE.MESSAGE_ID.equal(MESSAGE_RECIPIENT_STATUS.MESSAGE_ID))
                .where(MESSAGE_RECIPIENT_STATUS.USER_ID.equal(userId)
                        .and(MESSAGE_RECIPIENT_STATUS.ARCHIVED.equal(isArchivedByte))
                ).limit(startRecord, limitRecord).fetchInto(Message.class);
    }

    @Override
    public List<Message> findByReceivingUser(int startRecord, int limitRecord, long userId) {
        return dslContext.select()
                .from(MESSAGE)
                .join(MESSAGE_RECIPIENT_STATUS).on(MESSAGE.MESSAGE_ID.equal(MESSAGE_RECIPIENT_STATUS.MESSAGE_ID))
                .where(MESSAGE_RECIPIENT_STATUS.USER_ID.equal(userId))
                .limit(startRecord, limitRecord).fetchInto(Message.class);
    }

    @Override
    public List<Message> findBySendingUser(int startRecord, int limitRecord, long userId) {
        return dslContext.select()
                .from(MESSAGE)
                .where(MESSAGE.FROM_ID.equal(userId))
                .limit(startRecord, limitRecord).fetchInto(Message.class);
    }

    @Override
    public List<User_> getRecipients(long messageId) {
        return dslContext.select()
                .from(MESSAGE_RECIPIENT_STATUS)
                .join(USER_).on(MESSAGE_RECIPIENT_STATUS.USER_ID.eq(USER_.USER_ID))
                .fetchInto(User_.class);
    }

    @Override
    public void setArchived(long messageId, long memberId, boolean isArchived) {
        final byte isArchivedByte = (byte) (isArchived ? 1 : 0);
        dslContext.update(MESSAGE_RECIPIENT_STATUS)
                .set(MESSAGE_RECIPIENT_STATUS.ARCHIVED, isArchivedByte)
                .where(MESSAGE_RECIPIENT_STATUS.MESSAGE_ID.eq(messageId)
                        .and(MESSAGE_RECIPIENT_STATUS.USER_ID.eq(memberId)));
    }

    @Override
    public void setOpened(long messageId, long memberId, boolean isOpened) {
        final byte isOpenedByte = (byte) (isOpened ? 1 : 0);
        dslContext.update(MESSAGE_RECIPIENT_STATUS)
                .set(MESSAGE_RECIPIENT_STATUS.OPENED, isOpenedByte)
                .where(MESSAGE_RECIPIENT_STATUS.MESSAGE_ID.eq(messageId)
                        .and(MESSAGE_RECIPIENT_STATUS.USER_ID.eq(memberId)));
    }

    @Override
    public void createMessage(long messageId, long senderId, long repliesToId, String subject, String content) {
        //TODO: messageId is currently still generated by liferay
        dslContext.insertInto(MESSAGE)
                .set(MESSAGE.MESSAGE_ID, messageId)
                .set(MESSAGE.FROM_ID, senderId)
                .set(MESSAGE.REPLIES_TO, repliesToId)
                .set(MESSAGE.SUBJECT, subject)
                .set(MESSAGE.CONTENT, content);
    }

    @Override
    public void createMessageRecipient(long messageRecipientStatusId, long messageId, long recipientId) {
        //TODO: messageRecipientStatusId is currently still generated by liferay
        dslContext.insertInto(MESSAGE_RECIPIENT_STATUS)
                .set(MESSAGE_RECIPIENT_STATUS.MESSAGE_RECIPIENT_ID, messageRecipientStatusId)
                .set(MESSAGE_RECIPIENT_STATUS.MESSAGE_ID, messageId)
                .set(MESSAGE_RECIPIENT_STATUS.USER_ID, recipientId)
                .set(MESSAGE_RECIPIENT_STATUS.ARCHIVED, (byte) 0)
                .set(MESSAGE_RECIPIENT_STATUS.OPENED, (byte) 0);
    }
}
<commit_msg>[COLAB-1069] Fixed getRecipients Dao method<commit_after>package org.xcolab.domain.messaging;

import org.jooq.DSLContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.xcolab.model.tables.pojos.Message;
import org.xcolab.model.tables.pojos.User_;

import java.util.List;

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

@Repository
public class MessageDaoImpl implements MessageDao {

    @Autowired
    private DSLContext dslContext;

    @Override
    public Message getMessage(long messageId) {
        return dslContext.select()
                .from(MESSAGE)
                .where(MESSAGE.MESSAGE_ID.eq(messageId))
                .fetchOne().into(Message.class);
    }

    @Override
    public int countBySendingUser(long userId) {
        return dslContext.selectCount()
                .from(MESSAGE)
                .where(MESSAGE.FROM_ID.equal(userId))
                .fetchOne(0, Integer.class);
    }

    @Override
    public int countByReceivingUserArchived(long userId, boolean isArchived) {
        final byte isArchivedByte = (byte) (isArchived ? 1 : 0);
        return dslContext.selectCount()
                .from(MESSAGE_RECIPIENT_STATUS)
                .where(MESSAGE_RECIPIENT_STATUS.USER_ID.equal(userId)
                        .and(MESSAGE_RECIPIENT_STATUS.ARCHIVED.equal(isArchivedByte))
                ).fetchOne(0, Integer.class);
    }

    @Override
    public int countByReceivingUserOpened(long userId, boolean isOpened) {
        final byte isOpenedByte = (byte) (isOpened ? 1 : 0);
        return dslContext.selectCount()
                .from(MESSAGE_RECIPIENT_STATUS)
                .where(MESSAGE_RECIPIENT_STATUS.USER_ID.equal(userId)
                        .and(MESSAGE_RECIPIENT_STATUS.OPENED.equal(isOpenedByte))
                ).fetchOne(0, Integer.class);
    }

    @Override
    public List<Message> findByReceivingUserArchived(int startRecord, int limitRecord, long userId, boolean isArchived) {
        final byte isArchivedByte = (byte) (isArchived ? 1 : 0);
        return dslContext.select()
                .from(MESSAGE)
                .join(MESSAGE_RECIPIENT_STATUS).on(MESSAGE.MESSAGE_ID.equal(MESSAGE_RECIPIENT_STATUS.MESSAGE_ID))
                .where(MESSAGE_RECIPIENT_STATUS.USER_ID.equal(userId)
                        .and(MESSAGE_RECIPIENT_STATUS.ARCHIVED.equal(isArchivedByte))
                ).limit(startRecord, limitRecord).fetchInto(Message.class);
    }

    @Override
    public List<Message> findByReceivingUser(int startRecord, int limitRecord, long userId) {
        return dslContext.select()
                .from(MESSAGE)
                .join(MESSAGE_RECIPIENT_STATUS).on(MESSAGE.MESSAGE_ID.equal(MESSAGE_RECIPIENT_STATUS.MESSAGE_ID))
                .where(MESSAGE_RECIPIENT_STATUS.USER_ID.equal(userId))
                .limit(startRecord, limitRecord).fetchInto(Message.class);
    }

    @Override
    public List<Message> findBySendingUser(int startRecord, int limitRecord, long userId) {
        return dslContext.select()
                .from(MESSAGE)
                .where(MESSAGE.FROM_ID.equal(userId))
                .limit(startRecord, limitRecord).fetchInto(Message.class);
    }

    @Override
    public List<User_> getRecipients(long messageId) {
        return dslContext.select()
                .from(MESSAGE_RECIPIENT_STATUS)
                .join(USER_).on(MESSAGE_RECIPIENT_STATUS.USER_ID.eq(USER_.USER_ID))
                .where(MESSAGE_RECIPIENT_STATUS.MESSAGE_ID.eq(messageId))
                .fetchInto(User_.class);
    }

    @Override
    public void setArchived(long messageId, long memberId, boolean isArchived) {
        final byte isArchivedByte = (byte) (isArchived ? 1 : 0);
        dslContext.update(MESSAGE_RECIPIENT_STATUS)
                .set(MESSAGE_RECIPIENT_STATUS.ARCHIVED, isArchivedByte)
                .where(MESSAGE_RECIPIENT_STATUS.MESSAGE_ID.eq(messageId)
                        .and(MESSAGE_RECIPIENT_STATUS.USER_ID.eq(memberId)));
    }

    @Override
    public void setOpened(long messageId, long memberId, boolean isOpened) {
        final byte isOpenedByte = (byte) (isOpened ? 1 : 0);
        dslContext.update(MESSAGE_RECIPIENT_STATUS)
                .set(MESSAGE_RECIPIENT_STATUS.OPENED, isOpenedByte)
                .where(MESSAGE_RECIPIENT_STATUS.MESSAGE_ID.eq(messageId)
                        .and(MESSAGE_RECIPIENT_STATUS.USER_ID.eq(memberId)));
    }

    @Override
    public void createMessage(long messageId, long senderId, long repliesToId, String subject, String content) {
        //TODO: messageId is currently still generated by liferay
        dslContext.insertInto(MESSAGE)
                .set(MESSAGE.MESSAGE_ID, messageId)
                .set(MESSAGE.FROM_ID, senderId)
                .set(MESSAGE.REPLIES_TO, repliesToId)
                .set(MESSAGE.SUBJECT, subject)
                .set(MESSAGE.CONTENT, content);
    }

    @Override
    public void createMessageRecipient(long messageRecipientStatusId, long messageId, long recipientId) {
        //TODO: messageRecipientStatusId is currently still generated by liferay
        dslContext.insertInto(MESSAGE_RECIPIENT_STATUS)
                .set(MESSAGE_RECIPIENT_STATUS.MESSAGE_RECIPIENT_ID, messageRecipientStatusId)
                .set(MESSAGE_RECIPIENT_STATUS.MESSAGE_ID, messageId)
                .set(MESSAGE_RECIPIENT_STATUS.USER_ID, recipientId)
                .set(MESSAGE_RECIPIENT_STATUS.ARCHIVED, (byte) 0)
                .set(MESSAGE_RECIPIENT_STATUS.OPENED, (byte) 0);
    }
}

{'fraction_non_alphanumeric': 0.09002647837599294, 'fraction_numerical': 0.003000882612533098, 'mean_word_length': 2.622442455242967, 'pattern_counts': {'":': 0, '<': 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': '4235743', 'n_tokens_mistral': 3413, 'n_tokens_neox': 3274, 'n_words': 539}