<commit_before>package org.xcolab.service.proposal.service.proposalattribute;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.xcolab.client.comment.exceptions.ThreadNotFoundException;
import org.xcolab.client.comment.pojo.CommentThread;
import org.xcolab.client.comment.util.ThreadClientUtil;
import org.xcolab.model.tables.pojos.Proposal;
import org.xcolab.model.tables.pojos.ProposalAttribute;
import org.xcolab.model.tables.pojos.ProposalVersion;
import org.xcolab.service.proposal.domain.proposal.ProposalDao;
import org.xcolab.service.proposal.domain.proposalattribute.ProposalAttributeDao;
import org.xcolab.service.proposal.domain.proposalversion.ProposalVersionDao;
import org.xcolab.service.contest.exceptions.NotFoundException;

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

@Service
public class ProposalAttributeService {

    private final ProposalAttributeDao proposalAttributeDao;

    private final ProposalDao proposalDao;


    private final ProposalVersionDao proposalVersionDao;

    @Autowired
    public ProposalAttributeService(ProposalDao proposalDao,
            ProposalAttributeDao proposalAttributeDao, ProposalVersionDao proposalVersionDao) {
        this.proposalAttributeDao = proposalAttributeDao;
        this.proposalDao = proposalDao;
        this.proposalVersionDao = proposalVersionDao;
    }

    public ProposalAttribute setAttribute(ProposalAttribute proposalAttribute, Long authorId) {

        try {
            Proposal proposal = proposalDao.get(proposalAttribute.getProposalId());

            int currentVersion = proposalVersionDao.findMaxVersion(proposalAttribute.getProposalId());
            Integer version = proposalAttribute.getVersion();
            boolean isNewVersion = false;
            if (version == null || version < currentVersion) {
                version = currentVersion + 1;
                isNewVersion = true;
            }

            List<ProposalAttribute> currentProposalAttributes =
                    proposalAttributeDao.findByGiven(proposal.getProposalId(),
                            null, null, currentVersion);

            // for each attribute, if it isn't the one that we are changing, simply
            // update it to the most recent version
            // if it is the one that we are changing then leave old one as it is and
            // create new one for new proposal version
            for (ProposalAttribute attribute : currentProposalAttributes) {
                ProposalAttributeDetectUpdateAlgorithm updateAlgorithm =
                        new ProposalAttributeDetectUpdateAlgorithm(attribute);
                if (!updateAlgorithm.hasBeenUpdated(proposalAttribute.getName(),
                        zeroIfNull(proposalAttribute.getAdditionalId()),
                        zeroIfNull(proposalAttribute.getNumericValue()),
                        zeroIfNull(proposalAttribute.getRealValue()))) {
                    // clone the attribute and set its version to the new value
                    attribute.setVersion(version);
                    proposalAttributeDao.update(attribute);
                } else {
                }
            }

            // set new value for provided attribute
            proposalAttribute.setVersion(version);
            ProposalAttribute attribute = proposalAttributeDao
                    .create(proposalAttribute);//setAttributeValue(proposalId, newVersion,
            // attributeName, additionalId, stringValue, numericValue, realValue);

            Timestamp updatedDate = new Timestamp((new Date()).getTime());
            proposal.setUpdatedDate(updatedDate);

            // create newly created version descriptor
            if (isNewVersion) {
                createProposalVersionDescription(authorId, proposalAttribute.getProposalId(),
                        version, proposalAttribute.getName(), proposalAttribute.getAdditionalId(),
                        updatedDate);
            }
            proposalDao.update(proposal);

            // Update the proposal name in the discussion category
            if (proposalAttribute.getName().equals(ProposalAttributeKeys.NAME)) {
                try {
                    CommentThread thread = ThreadClientUtil.getThread(proposal.getDiscussionId());

                    thread.setTitle(
                            String.format("%s %s", getProposalNameFromOldTitle(thread.getTitle()),
                                    proposalAttribute.getStringValue()));<commit_msg>[COLAB-2283] comment out useless shit<commit_after>package org.xcolab.service.proposal.service.proposalattribute;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.xcolab.client.comment.exceptions.ThreadNotFoundException;
import org.xcolab.client.comment.pojo.CommentThread;
import org.xcolab.client.comment.util.ThreadClientUtil;
import org.xcolab.model.tables.pojos.Proposal;
import org.xcolab.model.tables.pojos.ProposalAttribute;
import org.xcolab.model.tables.pojos.ProposalVersion;
import org.xcolab.service.proposal.domain.proposal.ProposalDao;
import org.xcolab.service.proposal.domain.proposalattribute.ProposalAttributeDao;
import org.xcolab.service.proposal.domain.proposalversion.ProposalVersionDao;
import org.xcolab.service.contest.exceptions.NotFoundException;

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

@Service
public class ProposalAttributeService {

    private final ProposalAttributeDao proposalAttributeDao;

    private final ProposalDao proposalDao;


    private final ProposalVersionDao proposalVersionDao;

    @Autowired
    public ProposalAttributeService(ProposalDao proposalDao,
            ProposalAttributeDao proposalAttributeDao, ProposalVersionDao proposalVersionDao) {
        this.proposalAttributeDao = proposalAttributeDao;
        this.proposalDao = proposalDao;
        this.proposalVersionDao = proposalVersionDao;
    }

    public ProposalAttribute setAttribute(ProposalAttribute proposalAttribute, Long authorId) {

        try {
            Proposal proposal = proposalDao.get(proposalAttribute.getProposalId());

            int currentVersion = proposalVersionDao.findMaxVersion(proposalAttribute.getProposalId());
            Integer version = proposalAttribute.getVersion();
            boolean isNewVersion = false;
            if (version == null || version < currentVersion) {
                version = currentVersion + 1;
                isNewVersion = true;
            }

            List<ProposalAttribute> currentProposalAttributes =
                    proposalAttributeDao.findByGiven(proposal.getProposalId(),
                            null, null, currentVersion);

            // for each attribute, if it isn't the one that we are changing, simply
            // update it to the most recent version
            // if it is the one that we are changing then leave old one as it is and
            // create new one for new proposal version
            // TODO: [COLAB-2414] Fix the update detection to only update updated attributes.
            // for (ProposalAttribute attribute : currentProposalAttributes) {
            //     ProposalAttributeDetectUpdateAlgorithm updateAlgorithm =
            //             new ProposalAttributeDetectUpdateAlgorithm(attribute);
            //     if (!updateAlgorithm.hasBeenUpdated(proposalAttribute.getName(),
            //             zeroIfNull(proposalAttribute.getAdditionalId()),
            //             zeroIfNull(proposalAttribute.getNumericValue()),
            //             zeroIfNull(proposalAttribute.getRealValue()))) {
            //         // clone the attribute and set its version to the new value
            //         attribute.setVersion(version);
            //         proposalAttributeDao.update(attribute);
            //     } else {
            //     }
            // }

            // set new value for provided attribute
            proposalAttribute.setVersion(version);
            ProposalAttribute attribute = proposalAttributeDao
                    .create(proposalAttribute);//setAttributeValue(proposalId, newVersion,
            // attributeName, additionalId, stringValue, numericValue, realValue);

            Timestamp updatedDate = new Timestamp((new Date()).getTime());
            proposal.setUpdatedDate(updatedDate);

            // create newly created version descriptor
            if (isNewVersion) {
                createProposalVersionDescription(authorId, proposalAttribute.getProposalId(),
                        version, proposalAttribute.getName(), proposalAttribute.getAdditionalId(),
                        updatedDate);
            }
            proposalDao.update(proposal);

            // Update the proposal name in the discussion category
            if (proposalAttribute.getName().equals(ProposalAttributeKeys.NAME)) {
                try {
                    CommentThread thread = ThreadClientUtil.getThread(proposal.getDiscussionId());

                    thread.setTitle(
                            String.format("%s %s", getProposalNameFromOldTitle(thread.getTitle()),
                                    proposalAttribute.getStringValue()));
{'fraction_non_alphanumeric': 0.07589237860435202, 'fraction_numerical': 0.0010719262514738985, 'mean_word_length': 2.5194266314598264, 'pattern_counts': {'":': 0, '<': 7, '<?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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5569491', 'n_tokens_mistral': 2196, 'n_tokens_neox': 2183, 'n_words': 462}