<commit_before>package cz.cvut.fel.a4m36jee.airlines.dao;


import cz.cvut.fel.a4m36jee.airlines.model.AbstractEntity;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;

/**
 * @author klimefi1
 */
abstract class GenericDAO<T extends AbstractEntity> implements DAO<T> {

    @Inject
    private EntityManager em;

    private Class<T> type;

    GenericDAO() {
        Type t = getClass().getGenericSuperclass();
        ParameterizedType pt = (ParameterizedType) t;
        type = (Class) pt.getActualTypeArguments()[0];
    }

    @Override
    public List<T> list() {
        return findByPropertyIsNull("deleted");
    }

    @Override
    public T save(final T entity) {
        this.em.persist(entity);
        this.em.flush();
        return entity;
    }

    @Override
    public void delete(final Long id) {
        delete(find(id));
    }

    /**
     * Soft delete entity.
     * @param entity entity
     */
    @Override
    public void delete(final T entity) {
        entity.setDeleted(new Date());
        update(entity);
    }

    @Override
    public T find(final Object id) {
        T entity = this.em.find(type, id);
        return entity != null && entity.getDeleted() == null ? entity : null;
    }

    @Override
    public T update(final T entity) {
        this.em.merge(entity);
        this.em.flush();
        return entity;
    }

    @Override
    public List<T> findBy(String property, Object value) {
        CriteriaQuery<T> select = createQuery(property, value);
        return em.createQuery(select).getResultList();
    }

    @Override
    public List<T> findByPropertyIsNull(String property) {
        CriteriaQuery<T> select = createQueryIsNull(property);
        return em.createQuery(select).getResultList();
    }

    private CriteriaQuery<T> createQuery(String property, Object value) {
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<T> query = criteriaBuilder.createQuery(type);
        Root<T> from = query.from(type);
        return query
                .select(from)
                .where(criteriaBuilder.equal(from.get(property), value))
                .where(criteriaBuilder.isNull(from.get("deleted")));
    }

    private CriteriaQuery<T> createQueryIsNull(String property) {
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<T> query = criteriaBuilder.createQuery(type);
        Root<T> from = query.from(type);
        return query
                .select(from)
                .where(criteriaBuilder.isNull(from.get(property)));
    }

    public EntityManager getEntityManager() {
        return em;
    }

}<commit_msg>oprava criteria api<commit_after>package cz.cvut.fel.a4m36jee.airlines.dao;


import cz.cvut.fel.a4m36jee.airlines.model.AbstractEntity;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;

/**
 * @author klimefi1
 */
abstract class GenericDAO<T extends AbstractEntity> implements DAO<T> {

    @Inject
    private EntityManager em;

    private Class<T> type;

    GenericDAO() {
        Type t = getClass().getGenericSuperclass();
        ParameterizedType pt = (ParameterizedType) t;
        type = (Class) pt.getActualTypeArguments()[0];
    }

    @Override
    public List<T> list() {
        return findByPropertyIsNull("deleted");
    }

    @Override
    public T save(final T entity) {
        this.em.persist(entity);
        this.em.flush();
        return entity;
    }

    @Override
    public void delete(final Long id) {
        delete(find(id));
    }

    /**
     * Soft delete entity.
     * @param entity entity
     */
    @Override
    public void delete(final T entity) {
        entity.setDeleted(new Date());
        update(entity);
    }

    @Override
    public T find(final Object id) {
        T entity = this.em.find(type, id);
        return entity != null && entity.getDeleted() == null ? entity : null;
    }

    @Override
    public T update(final T entity) {
        this.em.merge(entity);
        this.em.flush();
        return entity;
    }

    @Override
    public List<T> findBy(String property, Object value) {
        CriteriaQuery<T> select = createQuery(property, value);
        return em.createQuery(select).getResultList();
    }

    @Override
    public List<T> findByPropertyIsNull(String property) {
        CriteriaQuery<T> select = createQueryIsNull(property);
        return em.createQuery(select).getResultList();
    }

    private CriteriaQuery<T> createQuery(String property, Object value) {
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<T> query = criteriaBuilder.createQuery(type);
        Root<T> from = query.from(type);
        return query
                .select(from)
                .where(
                    criteriaBuilder.equal(from.get(property), value),
                    criteriaBuilder.isNull(from.get("deleted"))
                );
    }

    private CriteriaQuery<T> createQueryIsNull(String property) {
        CriteriaBuilder criteriaBuilder = em.getCriteriaBuilder();
        CriteriaQuery<T> query = criteriaBuilder.createQuery(type);
        Root<T> from = query.from(type);
        return query
                .select(from)
                .where(criteriaBuilder.isNull(from.get(property)));
    }

    public EntityManager getEntityManager() {
        return em;
    }

}
{'fraction_non_alphanumeric': 0.10996795412379828, 'fraction_numerical': 0.0026986001011975037, 'mean_word_length': 2.665018541409147, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 31, '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': '5999633', 'n_tokens_mistral': 1699, 'n_tokens_neox': 1680, 'n_words': 363}