<commit_before>package cz.metacentrum.perun.cabinet.bl.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cz.metacentrum.perun.cabinet.bl.AuthorshipManagerBl;
import cz.metacentrum.perun.cabinet.bl.CabinetManagerBl;
import cz.metacentrum.perun.cabinet.dao.ThanksManagerDao;
import cz.metacentrum.perun.cabinet.model.Author;
import cz.metacentrum.perun.cabinet.model.Thanks;
import cz.metacentrum.perun.cabinet.model.ThanksForGUI;
import cz.metacentrum.perun.cabinet.bl.CabinetException;
import cz.metacentrum.perun.cabinet.bl.ErrorCodes;
import cz.metacentrum.perun.cabinet.bl.PerunManagerBl;
import cz.metacentrum.perun.cabinet.bl.ThanksManagerBl;
import cz.metacentrum.perun.core.api.PerunSession;
import cz.metacentrum.perun.core.api.exceptions.InternalErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Class for handling Thanks entity in Cabinet.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
public class ThanksManagerBlImpl implements ThanksManagerBl {

	private ThanksManagerDao thanksManagerDao;
	private AuthorshipManagerBl authorshipManagerBl;
	private CabinetManagerBl cabinetManagerBl;

	private static Logger log = LoggerFactory.getLogger(ThanksManagerBlImpl.class);

	// setters -------------------------

	@Autowired
	public void setThanksManagerDao(ThanksManagerDao thanksManagerDao) {
		this.thanksManagerDao = thanksManagerDao;
	}

	@Autowired
	public void setAuthorshipManagerBl(AuthorshipManagerBl authorshipManagerBl) {
		this.authorshipManagerBl = authorshipManagerBl;
	}

	@Autowired
	public void setCabinetManagerBl(CabinetManagerBl cabinetManagerBl) {
		this.cabinetManagerBl = cabinetManagerBl;
	}

	public ThanksManagerDao getThanksManagerDao() {
		return thanksManagerDao;
	}

	public AuthorshipManagerBl getAuthorshipManagerBl() {
		return authorshipManagerBl;
	}

	public CabinetManagerBl getCabinetManagerBl() {
		return cabinetManagerBl;
	}


// methods -------------------------

	public Thanks createThanks(PerunSession sess, Thanks t) throws CabinetException, InternalErrorException {
		if (t.getCreatedDate() == null) {
			t.setCreatedDate(new Date());
		}
		if (thanksExist(t)) {
			throw new CabinetException("Can't create duplicate thanks.", ErrorCodes.THANKS_ALREADY_EXISTS);
		}

		t = getThanksManagerDao().createThanks(sess, t);
		log.debug("{} created.", t);

		// recalculate thanks for all publication's authors
		List<Author> authors = new ArrayList<Author>();
		authors = getAuthorshipManagerBl().getAuthorsByPublicationId(t.getPublicationId());
		for (Author a : authors) {
			getCabinetManagerBl().setThanksAttribute(a.getId());
		}
		return t;
	}

	@Override
	public void deleteThanks(PerunSession sess, Thanks thanks) throws InternalErrorException, CabinetException {
		// recalculate thanks for all publication's authors
		List<Author> authors = getAuthorshipManagerBl().getAuthorsByPublicationId(thanks.getPublicationId());
		for (Author a : authors) {
			getCabinetManagerBl().setThanksAttribute(a.getId());
		}

		getThanksManagerDao().deleteThanks(sess, thanks);
		log.debug("{} deleted.", thanks);
	}

	@Override
	public boolean thanksExist(Thanks thanks) throws InternalErrorException {
		return getThanksManagerDao().thanksExist(thanks);
	}

	@Override
	public Thanks getThanksById(int id) throws CabinetException, InternalErrorException {
		return getThanksManagerDao().getThanksById(id);
	}

	@Override
	public List<Thanks> getThanksByPublicationId(int publicationId) throws InternalErrorException {
		return getThanksManagerDao().getThanksByPublicationId(publicationId);
	}

	@Override
	public List<ThanksForGUI> getRichThanksByPublicationId(int publicationId) throws InternalErrorException {<commit_msg>CABINET: Prevent deadlock in calculating priority coefficient<commit_after>package cz.metacentrum.perun.cabinet.bl.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import cz.metacentrum.perun.cabinet.bl.AuthorshipManagerBl;
import cz.metacentrum.perun.cabinet.bl.CabinetManagerBl;
import cz.metacentrum.perun.cabinet.dao.ThanksManagerDao;
import cz.metacentrum.perun.cabinet.model.Author;
import cz.metacentrum.perun.cabinet.model.Thanks;
import cz.metacentrum.perun.cabinet.model.ThanksForGUI;
import cz.metacentrum.perun.cabinet.bl.CabinetException;
import cz.metacentrum.perun.cabinet.bl.ErrorCodes;
import cz.metacentrum.perun.cabinet.bl.ThanksManagerBl;
import cz.metacentrum.perun.core.api.PerunSession;
import cz.metacentrum.perun.core.api.exceptions.InternalErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Class for handling Thanks entity in Cabinet.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 */
public class ThanksManagerBlImpl implements ThanksManagerBl {

	private ThanksManagerDao thanksManagerDao;
	private AuthorshipManagerBl authorshipManagerBl;
	private CabinetManagerBl cabinetManagerBl;

	private static Logger log = LoggerFactory.getLogger(ThanksManagerBlImpl.class);

	// setters -------------------------

	@Autowired
	public void setThanksManagerDao(ThanksManagerDao thanksManagerDao) {
		this.thanksManagerDao = thanksManagerDao;
	}

	@Autowired
	public void setAuthorshipManagerBl(AuthorshipManagerBl authorshipManagerBl) {
		this.authorshipManagerBl = authorshipManagerBl;
	}

	@Autowired
	public void setCabinetManagerBl(CabinetManagerBl cabinetManagerBl) {
		this.cabinetManagerBl = cabinetManagerBl;
	}

	public ThanksManagerDao getThanksManagerDao() {
		return thanksManagerDao;
	}

	public AuthorshipManagerBl getAuthorshipManagerBl() {
		return authorshipManagerBl;
	}

	public CabinetManagerBl getCabinetManagerBl() {
		return cabinetManagerBl;
	}


	// methods -------------------------

	public Thanks createThanks(PerunSession sess, Thanks t) throws CabinetException, InternalErrorException {
		if (t.getCreatedDate() == null) {
			t.setCreatedDate(new Date());
		}
		if (thanksExist(t)) {
			throw new CabinetException("Can't create duplicate thanks.", ErrorCodes.THANKS_ALREADY_EXISTS);
		}

		t = getThanksManagerDao().createThanks(sess, t);
		log.debug("{} created.", t);

		// recalculate thanks for all publication's authors
		List<Author> authors = new ArrayList<Author>();
		authors = getAuthorshipManagerBl().getAuthorsByPublicationId(t.getPublicationId());
		// sort to prevent locking
		Collections.sort(authors);
		for (Author a : authors) {
			getCabinetManagerBl().setThanksAttribute(a.getId());
		}
		return t;
	}

	@Override
	public void deleteThanks(PerunSession sess, Thanks thanks) throws InternalErrorException, CabinetException {
		// recalculate thanks for all publication's authors
		List<Author> authors = getAuthorshipManagerBl().getAuthorsByPublicationId(thanks.getPublicationId());
		// sort to prevent locking
		Collections.sort(authors);
		for (Author a : authors) {
			getCabinetManagerBl().setThanksAttribute(a.getId());
		}

		getThanksManagerDao().deleteThanks(sess, thanks);
		log.debug("{} deleted.", thanks);
	}

	@Override
	public boolean thanksExist(Thanks thanks) throws InternalErrorException {
		return getThanksManagerDao().thanksExist(thanks);
	}

	@Override
	public Thanks getThanksById(int id) throws CabinetException, InternalErrorException {
		return getThanksManagerDao().getThanksById(id);
	}

	@Override
	public List<Thanks> getThanksByPublicationId(int publicationId) throws InternalErrorException {
		return getThanksManagerDao().getThanksByPublicationId(publicationId);
	}

	@Override
	public List<ThanksForGUI> getRichThanksByPublicationId(int publicationId) throws InternalErrorException {
{'fraction_non_alphanumeric': 0.1080802882141019, 'fraction_numerical': 0.000514668039114771, 'mean_word_length': 5.6153191489361705, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '2148568', 'n_tokens_mistral': 2451, 'n_tokens_neox': 2347, 'n_words': 456}