<commit_before>package com.df.interview.companiesdk;

import com.df.interview.companiesdk.model.Company;
import com.df.interview.companiesdk.model.CompanyName;
import com.df.interview.companiesdk.model.Owner;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.RollbackException;
import javax.validation.ConstraintViolationException;
import javax.validation.Valid;
import java.util.List;
import java.util.function.Function;
import java.util.logging.Logger;
import java.util.stream.Collectors;

public class DB {
    private static Logger LOG = Logger.getLogger(DB.class.getName());
    private static final DB INSTANCE = new DB();
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistence");

    public Long createCompany(@Valid Company company) {
        return withinTransaction(em -> {
            company.getOwners().forEach(owner -> owner.setCompany(company));
            em.persist(company);
            company.getOwners().forEach(em::persist);
            return company.getId();
        });
    }

    public Long deleteCompany(Long companyId) {
        Company company = getCompany(companyId);
        return withinTransaction(em -> {
            company = em.merge(company);
            company.getOwners().forEach(em::remove);
            em.remove(company);
            return company.getId();
        });
    }

    public Long updateCompany(Company company) {
        return withinTransaction(em -> {
            company.getOwners().forEach(owner -> owner.setCompany(company));
            em.merge(company);
            company.getOwners().forEach(em::merge);
            return company.getId();
        });
    }

    public List<CompanyName> listCompanies() {
        return withinEM(em -> {
            List<CompanyName> list = em.createQuery("select NEW Company(c.id, c.name) from Company as c ORDER BY c.name", Company.class)
                    .getResultList().stream().map(c -> new CompanyName(c.getId(), c.getName()))
                    .collect(Collectors.toList());
            LOG.info("list companies: " + list);
            return list;
        });
    }

    public Company getCompany(Long id) {
        return withinEM(em -> em.find(Company.class, id));
    }

    public Long addOwner(long companyId, Owner owner) {
        Company company = getCompany(companyId);
        return withinTransaction(em -> {
            company.getOwners().add(owner);
            owner.setCompany(company);
            em.persist(owner);
            em.merge(company);
            return owner.getId();
        });
    }

    public Long deleteOwner(long companyId, long ownerId) {
        Company company = getCompany(companyId);
        Owner owner = getOwner(ownerId);
        return withinTransaction(em -> {
            owner = em.merge(owner);
            company.getOwners().remove(owner);
            em.merge(company);
            em.remove(owner);
            return owner.getId();
        });
    }<commit_msg>fixing entities<commit_after>package com.df.interview.companiesdk;

import com.df.interview.companiesdk.model.Company;
import com.df.interview.companiesdk.model.CompanyName;
import com.df.interview.companiesdk.model.Owner;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.RollbackException;
import javax.validation.ConstraintViolationException;
import javax.validation.Valid;
import java.util.List;
import java.util.function.Function;
import java.util.logging.Logger;
import java.util.stream.Collectors;

public class DB {
    private static Logger LOG = Logger.getLogger(DB.class.getName());
    private static final DB INSTANCE = new DB();
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistence");

    public Long createCompany(@Valid Company company) {
        return withinTransaction(em -> {
            company.getOwners().forEach(owner -> owner.setCompany(company));
            em.persist(company);
            company.getOwners().forEach(em::persist);
            return company.getId();
        });
    }

    public Long deleteCompany(Long companyId) {
        Company company = getCompany(companyId);
        return withinTransaction(em -> {
            Company mergedCompany = em.merge(company);
            mergedCompany.getOwners().forEach(em::remove);
            em.remove(mergedCompany);
            return mergedCompany.getId();
        });
    }

    public Long updateCompany(Company company) {
        return withinTransaction(em -> {
            company.getOwners().forEach(owner -> owner.setCompany(company));
            em.merge(company);
            company.getOwners().forEach(em::merge);
            return company.getId();
        });
    }

    public List<CompanyName> listCompanies() {
        return withinEM(em -> {
            List<CompanyName> list = em.createQuery("select NEW Company(c.id, c.name) from Company as c ORDER BY c.name", Company.class)
                    .getResultList().stream().map(c -> new CompanyName(c.getId(), c.getName()))
                    .collect(Collectors.toList());
            LOG.info("list companies: " + list);
            return list;
        });
    }

    public Company getCompany(Long id) {
        return withinEM(em -> em.find(Company.class, id));
    }

    public Long addOwner(long companyId, Owner owner) {
        Company company = getCompany(companyId);
        return withinTransaction(em -> {
            company.getOwners().add(owner);
            owner.setCompany(company);
            em.persist(owner);
            em.merge(company);
            return owner.getId();
        });
    }

    public Long deleteOwner(long companyId, long ownerId) {
        Company company = getCompany(companyId);
        Owner owner = getOwner(ownerId);
        return withinTransaction(em -> {
            Owner mergedOwner = em.merge(owner);
            company.getOwners().remove(owner);
            em.merge(company);
            em.remove(mergedOwner);
            return owner.getId();
        });
    }
{'fraction_non_alphanumeric': 0.11606854186873586, 'fraction_numerical': 0.0, 'mean_word_length': 2.5721709006928406, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 27, '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': '5520776', 'n_tokens_mistral': 1628, 'n_tokens_neox': 1613, 'n_words': 325}