<commit_before>package org.xcolab.portlets.proposals.discussion;

import org.xcolab.client.comment.pojo.Comment;
import org.xcolab.client.contest.ContestClientUtil;
import org.xcolab.client.contest.pojo.Contest;
import org.xcolab.client.contest.pojo.phases.ContestPhase;
import org.xcolab.client.members.MembersClient;
import org.xcolab.client.proposals.exceptions.ProposalNotFoundException;
import org.xcolab.client.proposals.pojo.Proposal;
import org.xcolab.jspTags.discussion.DiscussionPermissions;
import org.xcolab.portlets.proposals.utils.context.ProposalsContextUtil;
import org.xcolab.portlets.proposals.wrappers.ProposalTab;

import javax.portlet.PortletRequest;

public class ProposalDiscussionPermissions extends DiscussionPermissions {

    private final String discussionTabName;
    private final Long proposalId;
    private final Long contestPhaseId;
    private final PortletRequest request;

    public ProposalDiscussionPermissions(PortletRequest request) {
        super(request);
        discussionTabName = getTabName(request);
        proposalId = getProposalId(request);
        contestPhaseId = getContestPhaseId(request);
        this.request = request;
    }

    private String getTabName(PortletRequest request) {
        String discussionTabName = request.getParameter("tab");
        if (discussionTabName == null) {
            final String pageToDisplay = request.getParameter("pageToDisplay");
            if (pageToDisplay != null && pageToDisplay.startsWith("proposalDetails_")) {
                discussionTabName = pageToDisplay.replace("proposalDetails_", "");
            }
        }
        return discussionTabName;
    }

    private Long getProposalId(PortletRequest request) {
        Long proposalId = null;
        try {
            String proposalIdParameter = request.getParameter("proposalId");
            if (proposalIdParameter != null) {
                proposalId = Long.parseLong(proposalIdParameter);
            } else {
                proposalIdParameter = request.getParameter("planId");
                if (proposalIdParameter != null) {
                    proposalId = Long.parseLong(proposalIdParameter);
                }
            }
        } catch (NumberFormatException ignored) {
        }
        return proposalId;
    }

    private Long getContestPhaseId(PortletRequest request) {
        Long phaseId = null;
        try {
            String contestPhaseIdParameter = request.getParameter("phaseId");
            if (contestPhaseIdParameter != null) {
                phaseId = Long.parseLong(contestPhaseIdParameter);
            } else if (proposalId != null && proposalId > 0) {
                phaseId = ProposalsContextUtil.getClients(request).getProposalClient().getLatestContestPhaseInProposal(proposalId).getContestPhasePK();
            }
        } catch (NumberFormatException  ignored) {
        }
        return phaseId;
    }

    @Override
    public boolean getCanSeeAddCommentButton() {
        boolean canSeeAddCommentButton = false;
        boolean isEvaluationTabActive = discussionTabName != null
                && discussionTabName.equals(ProposalTab.EVALUATION.name());

        if (isEvaluationTabActive) {
            boolean isIdsInitialized = proposalId != null && contestPhaseId != null;
            if (isIdsInitialized) {
                if(isLoggedIn) {
                    canSeeAddCommentButton =
                            isAllowedToAddCommentsToProposalEvaluationInContestPhase();
                }else{
                    return false;
                }
            }
        } else {
            canSeeAddCommentButton = true;
        }
        return canSeeAddCommentButton;
    }

    @Override
    public boolean getCanAdminMessage(Comment comment) {
        if (comment.getAuthorId() == memberId && proposalId != null) {
            try {
                Proposal proposal = ProposalsContextUtil.getClients(request).getProposalClient().getProposal(proposalId);


                return proposal.isUserAmongFellows(memberId) || getCanAdminAll();
            } catch (ProposalNotFoundException ignored) {
            }
        }
        return comment.getAuthorId() == memberId || getCanAdminAll();
    }

    private boolean isAllowedToAddCommentsToProposalEvaluationInContestPhase() {

        boolean isUserAllowed = false;
        try {
            Proposal proposal = ProposalsContextUtil.getClients(request).getProposalClient().getProposal(proposalId);
            isUserAllowed = isUserFellowOrJudgeOrAdvisor(proposal)
                    || isUserProposalAuthorOrTeamMember(proposal)
                    || getCanAdminAll();
        } catch (ProposalNotFoundException ignored) {
        }
        return isUserAllowed;
    }

    private boolean isUserFellowOrJudgeOrAdvisor(Proposal proposal) {
        ContestPhase contestPhase = ContestClientUtil.getContestPhase(contestPhaseId);
        Proposal proposalWrapper = new Proposal(proposal, contestPhase);


        Contest contestWrapper =  proposalWrapper.getContest();

        boolean isJudge = proposalWrapper.isUserAmongSelectedJudge(
                MembersClient.getMemberUnchecked(memberId));<commit_msg>more bugfixes for evaluation tab<commit_after>package org.xcolab.portlets.proposals.discussion;

import org.xcolab.client.comment.pojo.Comment;
import org.xcolab.client.contest.ContestClientUtil;
import org.xcolab.client.contest.pojo.Contest;
import org.xcolab.client.contest.pojo.phases.ContestPhase;
import org.xcolab.client.members.MembersClient;
import org.xcolab.client.proposals.exceptions.ProposalNotFoundException;
import org.xcolab.client.proposals.pojo.Proposal;
import org.xcolab.jspTags.discussion.DiscussionPermissions;
import org.xcolab.portlets.proposals.utils.context.ProposalsContextUtil;
import org.xcolab.portlets.proposals.wrappers.ProposalTab;

import javax.portlet.PortletRequest;

public class ProposalDiscussionPermissions extends DiscussionPermissions {

    private final String discussionTabName;
    private final Long proposalId;
    private final Long contestPhaseId;
    private final PortletRequest request;

    public ProposalDiscussionPermissions(PortletRequest request) {
        super(request);
        discussionTabName = getTabName(request);
        proposalId = getProposalId(request);
        contestPhaseId = getContestPhaseId(request);
        this.request = request;
    }

    private String getTabName(PortletRequest request) {
        String discussionTabName = request.getParameter("tab");
        if (discussionTabName == null) {
            final String pageToDisplay = request.getParameter("pageToDisplay");
            if (pageToDisplay != null && pageToDisplay.startsWith("proposalDetails_")) {
                discussionTabName = pageToDisplay.replace("proposalDetails_", "");
            }
        }
        return discussionTabName;
    }

    private Long getProposalId(PortletRequest request) {
        Long proposalId = null;
        try {
            String proposalIdParameter = request.getParameter("proposalId");
            if (proposalIdParameter != null) {
                proposalId = Long.parseLong(proposalIdParameter);
            } else {
                proposalIdParameter = request.getParameter("planId");
                if (proposalIdParameter != null) {
                    proposalId = Long.parseLong(proposalIdParameter);
                }
            }
        } catch (NumberFormatException ignored) {
        }
        return proposalId;
    }

    private Long getContestPhaseId(PortletRequest request) {
        Long phaseId = null;
        try {
            String contestPhaseIdParameter = request.getParameter("phaseId");
            if (contestPhaseIdParameter != null) {
                phaseId = Long.parseLong(contestPhaseIdParameter);
            } else if (proposalId != null && proposalId > 0) {
                phaseId = ProposalsContextUtil.getClients(request).getProposalClient().getLatestContestPhaseInProposal(proposalId).getContestPhasePK();
            }
        } catch (NumberFormatException  ignored) {
        }
        return phaseId;
    }

    @Override
    public boolean getCanSeeAddCommentButton() {
        boolean canSeeAddCommentButton = false;
        boolean isEvaluationTabActive = discussionTabName != null
                && discussionTabName.equals(ProposalTab.EVALUATION.name());

        if (isEvaluationTabActive) {
            boolean isIdsInitialized = proposalId != null && contestPhaseId != null;
            if (isIdsInitialized) {
                if(isLoggedIn) {
                    canSeeAddCommentButton =
                            isAllowedToAddCommentsToProposalEvaluationInContestPhase();
                }else{
                    return false;
                }
            }
        } else {
            canSeeAddCommentButton = true;
        }
        return canSeeAddCommentButton;
    }

    @Override
    public boolean getCanAdminMessage(Comment comment) {
        if (comment.getAuthorId() == memberId && proposalId != null) {
            try {
                Proposal proposal = ProposalsContextUtil.getClients(request).getProposalClient().getProposal(proposalId);


                return proposal.isUserAmongFellows(memberId) || getCanAdminAll();
            } catch (ProposalNotFoundException ignored) {
            }
        }
        return comment.getAuthorId() == memberId || getCanAdminAll();
    }

    private boolean isAllowedToAddCommentsToProposalEvaluationInContestPhase() {

        boolean isUserAllowed = false;
        try {
            Proposal proposal = ProposalsContextUtil.getClients(request).getProposalClient().getProposal(proposalId);
            isUserAllowed = isUserFellowOrJudgeOrAdvisor(proposal)
                    || isUserProposalAuthorOrTeamMember(proposal)
                    || getCanAdminAll();
        } catch (ProposalNotFoundException ignored) {
        }
        return isUserAllowed;
    }

    private boolean isUserFellowOrJudgeOrAdvisor(Proposal proposal) {
        ContestPhase contestPhase = ProposalsContextUtil.getClients(request).getContestClient().getContestPhase(contestPhaseId);
        Proposal proposalWrapper = new Proposal(proposal, contestPhase);


        Contest contestWrapper =  proposalWrapper.getContest();

        boolean isJudge = proposalWrapper.isUserAmongSelectedJudge(
                MembersClient.getMemberUnchecked(memberId));
{'fraction_non_alphanumeric': 0.08045206397854611, 'fraction_numerical': 0.00019155253328225267, 'mean_word_length': 2.672880759760816, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '146643', 'n_tokens_mistral': 2749, 'n_tokens_neox': 2679, 'n_words': 467}