<commit_before>package pl.edu.pw.ii.pik01.seeknresolve.service.bug;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pl.edu.pw.ii.pik01.seeknresolve.domain.dto.BugDTO;
import pl.edu.pw.ii.pik01.seeknresolve.domain.dto.BugDetailsDTO;
import pl.edu.pw.ii.pik01.seeknresolve.domain.dto.CommentDTO;
import pl.edu.pw.ii.pik01.seeknresolve.domain.entity.*;
import pl.edu.pw.ii.pik01.seeknresolve.domain.repository.*;
import pl.edu.pw.ii.pik01.seeknresolve.service.bug.generator.BugTagGenerator;
import pl.edu.pw.ii.pik01.seeknresolve.service.common.DtosFactory;

import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceException;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BugService {
    private BugRepository bugRepository;
    private ProjectRepository projectRepository;
    private UserRepository userRepository;
    private CommentRepository commentRepository;
    private UserProjectRoleRepository userProjectRoleRepository;
    private BugTagGenerator bugTagGenerator;

    @Autowired
    public BugService(BugRepository bugRepository, ProjectRepository projectRepository,
                      UserRepository userRepository, CommentRepository commentRepository,
                      UserProjectRoleRepository userProjectRoleRepository, BugTagGenerator bugTagGenerator) {
        this.bugRepository = bugRepository;
        this.projectRepository = projectRepository;
        this.userRepository = userRepository;
        this.commentRepository = commentRepository;
        this.userProjectRoleRepository = userProjectRoleRepository;
        this.bugTagGenerator = bugTagGenerator;
    }

    @Transactional(readOnly = true)
    public List<BugDTO> getAllPermittedBugs(User user) {
        List<UserProjectRole> projectRoles = userProjectRoleRepository.findByUser(user);
        return projectRoles.stream()
                .map(projectRole -> projectRole.getProject().getBugs())
                .flatMap(bugList -> bugList.stream())
                .map(bug -> DtosFactory.createBugDTO(bug))
                .collect(Collectors.toList());
    }

    @Transactional
    public BugDTO createAndSaveNewBug(BugDTO bugDTO) {
        Bug savedBug = bugRepository.save(createBugFromDTO(bugDTO));
        if(savedBug == null) {
            throw new PersistenceException("Cannot save bug with tag: " + bugDTO.getTag());
        }
        return DtosFactory.createBugDTO(savedBug);
    }

    private Bug createBugFromDTO(BugDTO bugDTO) {
        Bug bug = new Bug();
        Project project = projectRepository.findOne(bugDTO.getProjectId());
        bug.setTag(generateNextTagForBug(project));
        bug.setProject(project);
        bug.setReporter(userRepository.findOne(bugDTO.getReporterId()));
        updateBugDataFromDTO(bug, bugDTO);
        return bug;
    }

    private String generateNextTagForBug(Project project) {
        String nextTag = bugTagGenerator.generateTag(project);
        incrementBugNumberAndSaveProject(project);
        return nextTag;
    }

    private void incrementBugNumberAndSaveProject(Project project) {
        project.incrementLastBugNumber();<commit_msg>Temporary fix for bug create.<commit_after>package pl.edu.pw.ii.pik01.seeknresolve.service.bug;

import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pl.edu.pw.ii.pik01.seeknresolve.domain.dto.BugDTO;
import pl.edu.pw.ii.pik01.seeknresolve.domain.dto.BugDetailsDTO;
import pl.edu.pw.ii.pik01.seeknresolve.domain.dto.CommentDTO;
import pl.edu.pw.ii.pik01.seeknresolve.domain.entity.*;
import pl.edu.pw.ii.pik01.seeknresolve.domain.repository.*;
import pl.edu.pw.ii.pik01.seeknresolve.service.bug.generator.BugTagGenerator;
import pl.edu.pw.ii.pik01.seeknresolve.service.common.DtosFactory;

import javax.persistence.EntityNotFoundException;
import javax.persistence.PersistenceException;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BugService {
    private BugRepository bugRepository;
    private ProjectRepository projectRepository;
    private UserRepository userRepository;
    private CommentRepository commentRepository;
    private UserProjectRoleRepository userProjectRoleRepository;
    private BugTagGenerator bugTagGenerator;

    @Autowired
    public BugService(BugRepository bugRepository, ProjectRepository projectRepository,
                      UserRepository userRepository, CommentRepository commentRepository,
                      UserProjectRoleRepository userProjectRoleRepository, BugTagGenerator bugTagGenerator) {
        this.bugRepository = bugRepository;
        this.projectRepository = projectRepository;
        this.userRepository = userRepository;
        this.commentRepository = commentRepository;
        this.userProjectRoleRepository = userProjectRoleRepository;
        this.bugTagGenerator = bugTagGenerator;
    }

    @Transactional(readOnly = true)
    public List<BugDTO> getAllPermittedBugs(User user) {
        List<UserProjectRole> projectRoles = userProjectRoleRepository.findByUser(user);
        return projectRoles.stream()
                .map(projectRole -> projectRole.getProject().getBugs())
                .flatMap(bugList -> bugList.stream())
                .map(bug -> DtosFactory.createBugDTO(bug))
                .collect(Collectors.toList());
    }

    @Transactional
    public BugDTO createAndSaveNewBug(BugDTO bugDTO) {
        Bug savedBug = bugRepository.save(createBugFromDTO(bugDTO));
        if(savedBug == null) {
            throw new PersistenceException("Cannot save bug with tag: " + bugDTO.getTag());
        }
        return DtosFactory.createBugDTO(savedBug);
    }

    private Bug createBugFromDTO(BugDTO bugDTO) {
        Bug bug = new Bug();
        Project project = projectRepository.findOne(bugDTO.getProjectId());
        bug.setTag(generateNextTagForBug(project));
        bug.setProject(project);
        bug.setReporter(userRepository.findOne(bugDTO.getReporterId()));
        updateBugDataFromDTO(bug, bugDTO);
        bug.setDateCreated(DateTime.now());
        return bug;
    }

    private String generateNextTagForBug(Project project) {
        String nextTag = bugTagGenerator.generateTag(project);
        incrementBugNumberAndSaveProject(project);
        return nextTag;
    }

    private void incrementBugNumberAndSaveProject(Project project) {
        project.incrementLastBugNumber();
{'fraction_non_alphanumeric': 0.08793180798564379, 'fraction_numerical': 0.004785404516225512, 'mean_word_length': 3.7703281027104136, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '1119725', 'n_tokens_mistral': 1851, 'n_tokens_neox': 1826, 'n_words': 304}