<commit_before>/*
 * iBankApp
 *
 * License : Apache License,Version 2.0, January 2004
 *
 * See the LICENSE file in English or LICENSE.zh_CN in chinese
 * in the root directory or <http://www.apache.org/licenses/>.
 */

package org.ibankapp.base.persistence.repository;

import org.ibankapp.base.persistence.Specification.ByIdsSpecification;
import org.ibankapp.base.persistence.domain.Page;
import org.ibankapp.base.persistence.domain.Pageable;
import org.ibankapp.base.persistence.domain.Sort;
import org.ibankapp.base.persistence.domain.Specification;
import org.ibankapp.base.persistence.validation.validator.UniqueValidator;
import org.ibankapp.base.validation.validator.BeanValidator;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import static org.ibankapp.base.persistence.util.QueryUtils.toOrders;

public class JpaRepositoryImpl implements JpaRepository {

    @PersistenceContext
    private EntityManager em;


    private void validateEntity(Object entity) {
        if (entity == null) {
            throw new NullPointerException();
        }

        BeanValidator.validate(entity);
        UniqueValidator.validate(entity, em);
    }

    @Override
    public <T> T persist(T entity) {

        validateEntity(entity);
        em.persist(entity);
        return entity;
    }

    @Override
    public <T> T merge(T entity) {
        validateEntity(entity);
        return em.merge(entity);
    }

    @Override
    public <T> List<T> persist(Iterable<T> entities) {

        List<T> result = new ArrayList<T>();

        if (entities == null) {
            return result;
        }

        for (T entity : entities) {
            result.add(persist(entity));
        }

        return result;
    }

    @Override
    public <T> List<T> merge(Iterable<T> entities) {

        List<T> result = new ArrayList<T>();

        if (entities == null) {
            return result;
        }

        for (T entity : entities) {
            result.add(merge(entity));
        }

        return result;
    }

    @Override
    public <T, ID extends Serializable> T findOne(Class<T> entityClass, ID id) {

        return em.find(entityClass, id);
    }

    @Override
    public <T, ID extends Serializable> boolean exist(Class<T> entityClass, ID id) {

        return findOne(entityClass, id) != null;
    }

    @Override
    public <T> List<T> findAll(Class<T> entityClass) {

        return findAll(entityClass, null, null, null, null);
    }

    @Override
    public <T> Page<T> findAll(Class<T> entityClass, Pageable pageable) {
        return findAll(entityClass, null, pageable);
    }


    @Override
    public <T> List<T> findAll(Class<T> entityClass, Sort sort) {
        return findAll(entityClass, null, sort, null, null);
    }


    @Override
    public <T> List<T> findAll(Class<T> entityClass, Specification<T> spec) {

        return findAll(entityClass, spec, null, null, null);
    }

    @Override
    public <T> Page<T> findAll(Class<T> entityClass, Specification<T> spec, Pageable pageable) {
        return findAll(entityClass, spec, null, pageable);
    }

    @Override
    public <T> List<T> findAll(Class<T> entityClass, Specification<T> spec, Sort sort) {

        return findAll(entityClass, spec, sort, null, null);
    }

    @Override
    public <T> Page<T> findAll(Class<T> entityClass, Specification<T> spec, Sort sort, Pageable pageable) {

        TypedQuery<T> query = getQuery(spec, entityClass, sort);
        long totalCount = count(entityClass, spec);

        if (pageable == null) {
            return new Page<T>(query.getResultList(), totalCount);
        }

        query.setFirstResult(pageable.getOffset());
        query.setMaxResults(pageable.getSize());

        if (totalCount > pageable.getOffset()) {
            return new Page<T>(query.getResultList(), totalCount, pageable.getSize(), pageable.getPage());
        } else {
            return new Page<T>(new ArrayList<T>(), totalCount, pageable.getSize(), pageable.getPage());
        }

//        return new Page<>(totalCount > pageable.getOffset() ? query.getResultList() : Collections.emptyList(),
//                totalCount, pageable.getSize(), pageable.getPage());
    }

    @Override
    public <T> List<T> findAll(Class<T> entityClass, Specification<T> spec, Sort sort, LockModeType lockMode,
                               Map<String, Object> queryHints) {


        TypedQuery<T> query = getQuery(spec, entityClass, sort);

        if (lockMode != null) {
            query = query.setLockMode(lockMode);
        }

        if (queryHints != null && queryHints.size() != 0) {

            for (Map.Entry<String, Object> hint : queryHints.entrySet()) {
                query.setHint(hint.getKey(), hint.getValue());
            }
        }

        return query.getResultList();
    }


    private <T> TypedQuery<T> getQuery(Specification<T> spec, Class<T> domainClass, Sort sort) {

        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<T> query = builder.createQuery(domainClass);

        Root<T> root = applySpecificationToCriteria(spec, domainClass, query);<commit_msg>去除QueryLint功能<commit_after>/*
 * iBankApp
 *
 * License : Apache License,Version 2.0, January 2004
 *
 * See the LICENSE file in English or LICENSE.zh_CN in chinese
 * in the root directory or <http://www.apache.org/licenses/>.
 */

package org.ibankapp.base.persistence.repository;

import org.ibankapp.base.persistence.Specification.ByIdsSpecification;
import org.ibankapp.base.persistence.domain.Page;
import org.ibankapp.base.persistence.domain.Pageable;
import org.ibankapp.base.persistence.domain.Sort;
import org.ibankapp.base.persistence.domain.Specification;
import org.ibankapp.base.persistence.validation.validator.UniqueValidator;
import org.ibankapp.base.validation.validator.BeanValidator;

import javax.persistence.EntityManager;
import javax.persistence.LockModeType;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.ibankapp.base.persistence.util.QueryUtils.toOrders;

public class JpaRepositoryImpl implements JpaRepository {

    @PersistenceContext
    private EntityManager em;


    private void validateEntity(Object entity) {
        if (entity == null) {
            throw new NullPointerException();
        }

        BeanValidator.validate(entity);
        UniqueValidator.validate(entity, em);
    }

    @Override
    public <T> T persist(T entity) {

        validateEntity(entity);
        em.persist(entity);
        return entity;
    }

    @Override
    public <T> T merge(T entity) {
        validateEntity(entity);
        return em.merge(entity);
    }

    @Override
    public <T> List<T> persist(Iterable<T> entities) {

        List<T> result = new ArrayList<T>();

        if (entities == null) {
            return result;
        }

        for (T entity : entities) {
            result.add(persist(entity));
        }

        return result;
    }

    @Override
    public <T> List<T> merge(Iterable<T> entities) {

        List<T> result = new ArrayList<T>();

        if (entities == null) {
            return result;
        }

        for (T entity : entities) {
            result.add(merge(entity));
        }

        return result;
    }

    @Override
    public <T, ID extends Serializable> T findOne(Class<T> entityClass, ID id) {

        return em.find(entityClass, id);
    }

    @Override
    public <T, ID extends Serializable> boolean exist(Class<T> entityClass, ID id) {

        return findOne(entityClass, id) != null;
    }

    @Override
    public <T> List<T> findAll(Class<T> entityClass) {

        return findAll(entityClass, null, null, (LockModeType) null);
    }

    @Override
    public <T> Page<T> findAll(Class<T> entityClass, Pageable pageable) {
        return findAll(entityClass, null, pageable);
    }


    @Override
    public <T> List<T> findAll(Class<T> entityClass, Sort sort) {
        return findAll(entityClass, null, sort, (LockModeType) null);
    }


    @Override
    public <T> List<T> findAll(Class<T> entityClass, Specification<T> spec) {

        return findAll(entityClass, spec, null, (LockModeType) null);
    }

    @Override
    public <T> Page<T> findAll(Class<T> entityClass, Specification<T> spec, Pageable pageable) {
        return findAll(entityClass, spec, null, pageable);
    }

    @Override
    public <T> List<T> findAll(Class<T> entityClass, Specification<T> spec, Sort sort) {

        return findAll(entityClass, spec, sort, (LockModeType) null);
    }

    @Override
    public <T> Page<T> findAll(Class<T> entityClass, Specification<T> spec, Sort sort, Pageable pageable) {

        TypedQuery<T> query = getQuery(spec, entityClass, sort);
        long totalCount = count(entityClass, spec);

        if (pageable == null) {
            return new Page<T>(query.getResultList(), totalCount);
        }

        query.setFirstResult(pageable.getOffset());
        query.setMaxResults(pageable.getSize());

        if (totalCount > pageable.getOffset()) {
            return new Page<T>(query.getResultList(), totalCount, pageable.getSize(), pageable.getPage());
        } else {
            return new Page<T>(new ArrayList<T>(), totalCount, pageable.getSize(), pageable.getPage());
        }

//        return new Page<>(totalCount > pageable.getOffset() ? query.getResultList() : Collections.emptyList(),
//                totalCount, pageable.getSize(), pageable.getPage());
    }

    @Override
    public <T> List<T> findAll(Class<T> entityClass, Specification<T> spec, Sort sort, LockModeType lockMode) {


        TypedQuery<T> query = getQuery(spec, entityClass, sort);

        if (lockMode != null) {
            query = query.setLockMode(lockMode);
        }

//        if (queryHints != null && queryHints.size() != 0) {
//
//            for (Map.Entry<String, Object> hint : queryHints.entrySet()) {
//                query.setHint(hint.getKey(), hint.getValue());
//            }
//        }

        return query.getResultList();
    }


    private <T> TypedQuery<T> getQuery(Specification<T> spec, Class<T> domainClass, Sort sort) {

        CriteriaBuilder builder = em.getCriteriaBuilder();
        CriteriaQuery<T> query = builder.createQuery(domainClass);

        Root<T> root = applySpecificationToCriteria(spec, domainClass, query);
{'fraction_non_alphanumeric': 0.11833093006488825, 'fraction_numerical': 0.0012617159336697908, 'mean_word_length': 2.943496801705757, 'pattern_counts': {'":': 0, '<': 124, '<?xml version=': 0, '>': 128, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7254844', 'n_tokens_mistral': 3325, 'n_tokens_neox': 3251, 'n_words': 756}