<commit_before>/*
 * jPOS Project [http://jpos.org]
 * Copyright (C) 2000-2020 jPOS Software SRL
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jpos.ee;

import org.hibernate.query.criteria.internal.OrderImpl;

import javax.persistence.NoResultException;
import javax.persistence.criteria.*;
import org.hibernate.query.Query;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DBManager<T> {

    protected DB db;
    private Class<T> clazz;

    public DBManager(DB db, Class<T> clazz) {
        this.db = db;
        this.clazz = clazz;
    }

    /** Convenience method */
    public T byId(Long id) {
        return db.session().get(clazz, id);
    }

    public int getItemCount()  {
        CriteriaBuilder criteriaBuilder = db.session().getCriteriaBuilder();
        CriteriaQuery<Long> query = criteriaBuilder.createQuery(Long.class);
        Root<T> root = query.from(clazz);
        Predicate[] predicates = buildFilters(root);
        if (predicates != null)
            query.where(predicates);
        query.select(criteriaBuilder.count(root));
        return db.session().createQuery(query).getSingleResult().intValue();
    }

    public List<T> getAll(int offset, int limit, Map<String,Boolean> orders) {
        CriteriaBuilder criteriaBuilder = db.session().getCriteriaBuilder();
        CriteriaQuery<T> query = criteriaBuilder.createQuery(clazz);
        Root<T> root = query.from(clazz);
        List<Order> orderList = new ArrayList<>();
        //ORDERS
        if (orders != null) {
            for (Map.Entry<String, Boolean> entry : orders.entrySet()) {
                OrderImpl order = new OrderImpl(root.get(entry.getKey()), entry.getValue());
                orderList.add(order);
            }
        }
        Predicate[] predicates = buildFilters(root);
        if (predicates != null)
            query.where(predicates);
        query.select(root);
        query.orderBy(orderList);
        Query<T> queryImp = db.session().createQuery(query);
        if (limit != -1) {
            queryImp.setMaxResults(limit);
        }
        List<T> list = queryImp
                .setFirstResult(offset)
                .getResultList();
        return list;
    }

    public List<T> getAll() {
        return this.getAll(0,-1,null);
    }

    public T getItemByParam(String param, Object value) {
        return getItemByParam(param,value,false);
    }

    public T getItemByParam(String param, Object value, boolean withFilter) {
        try {
            CriteriaQuery<T> query = createQueryByParam(param, value, withFilter);
            return db.session().createQuery(query).getSingleResult();
        } catch (NoResultException nre) {
            return null;
        }
    }

    public List<T> getItemsByParam(String param, Object value) {
        return getItemsByParam(param,value,false);
    }

    public List<T> getItemsByParam(String param, Object value, boolean withFilter) {
        try {
            CriteriaQuery<T> query = createQueryByParam(param, value, withFilter);
            return db.session().createQuery(query).list();
        } catch (NoResultException nre) {
            return null;
        }
    }

    private CriteriaQuery<T> createQueryByParam(String param, Object value, boolean withFilter) {
        CriteriaBuilder criteriaBuilder = db.session().getCriteriaBuilder();
        CriteriaQuery<T> query = criteriaBuilder.createQuery(clazz);
        Root<T> root = query.from(clazz);
        Predicate equals = criteriaBuilder.equal(root.get(param), value);
        query.where(equals);
        if (withFilter) {
            Predicate[] predicates = buildFilters(root);
            if (predicates != null) {
                //overrides previous predicates
                query.where(criteriaBuilder.and(criteriaBuilder.and(predicates), equals));
            }
        }<commit_msg>adding method getItemsByParam with offset and limit parameters<commit_after>/*
 * jPOS Project [http://jpos.org]
 * Copyright (C) 2000-2020 jPOS Software SRL
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.jpos.ee;

import org.hibernate.query.criteria.internal.OrderImpl;

import javax.persistence.NoResultException;
import javax.persistence.criteria.*;
import org.hibernate.query.Query;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DBManager<T> {

    protected DB db;
    private Class<T> clazz;

    public DBManager(DB db, Class<T> clazz) {
        this.db = db;
        this.clazz = clazz;
    }

    /** Convenience method */
    public T byId(Long id) {
        return db.session().get(clazz, id);
    }

    public int getItemCount()  {
        CriteriaBuilder criteriaBuilder = db.session().getCriteriaBuilder();
        CriteriaQuery<Long> query = criteriaBuilder.createQuery(Long.class);
        Root<T> root = query.from(clazz);
        Predicate[] predicates = buildFilters(root);
        if (predicates != null)
            query.where(predicates);
        query.select(criteriaBuilder.count(root));
        return db.session().createQuery(query).getSingleResult().intValue();
    }

    public List<T> getAll(int offset, int limit, Map<String,Boolean> orders) {
        CriteriaBuilder criteriaBuilder = db.session().getCriteriaBuilder();
        CriteriaQuery<T> query = criteriaBuilder.createQuery(clazz);
        Root<T> root = query.from(clazz);
        List<Order> orderList = new ArrayList<>();
        //ORDERS
        if (orders != null) {
            for (Map.Entry<String, Boolean> entry : orders.entrySet()) {
                OrderImpl order = new OrderImpl(root.get(entry.getKey()), entry.getValue());
                orderList.add(order);
            }
        }
        Predicate[] predicates = buildFilters(root);
        if (predicates != null)
            query.where(predicates);
        query.select(root);
        query.orderBy(orderList);
        Query<T> queryImp = db.session().createQuery(query);
        if (limit != -1) {
            queryImp.setMaxResults(limit);
        }
        List<T> list = queryImp
                .setFirstResult(offset)
                .getResultList();
        return list;
    }

    public List<T> getAll() {
        return this.getAll(0,-1,null);
    }

    public T getItemByParam(String param, Object value) {
        return getItemByParam(param,value,false);
    }

    public T getItemByParam(String param, Object value, boolean withFilter) {
        try {
            CriteriaQuery<T> query = createQueryByParam(param, value, withFilter);
            return db.session().createQuery(query).getSingleResult();
        } catch (NoResultException nre) {
            return null;
        }
    }

    public List<T> getItemsByParam(String param, Object value) {
        return getItemsByParam(param,value,false);
    }

    public List<T> getItemsByParam(String param, Object value, boolean withFilter) {
        try {
            CriteriaQuery<T> query = createQueryByParam(param, value, withFilter);
            return db.session().createQuery(query).list();
        } catch (NoResultException nre) {
            return null;
        }
    }

    public List<T> getItemsByParam(int offset, int limit, String param, Object value, boolean withFilter) {
        try {
            CriteriaQuery<T> query = createQueryByParam(param, value, withFilter);
            Query<T> queryImp = db.session().createQuery(query);
            if (limit != -1) {
                queryImp.setMaxResults(limit);
            }
            List<T> list = queryImp
                    .setFirstResult(offset)
                    .getResultList();
            return list;
        } catch (NoResultException nre) {
            return null;
        }
    }

    private CriteriaQuery<T> createQueryByParam(String param, Object value, boolean withFilter) {
        CriteriaBuilder criteriaBuilder = db.session().getCriteriaBuilder();
        CriteriaQuery<T> query = criteriaBuilder.createQuery(clazz);
        Root<T> root = query.from(clazz);
        Predicate equals = criteriaBuilder.equal(root.get(param), value);
        query.where(equals);
        if (withFilter) {
            Predicate[] predicates = buildFilters(root);
            if (predicates != null) {
                //overrides previous predicates
                query.where(criteriaBuilder.and(criteriaBuilder.and(predicates), equals));
            }
        }
{'fraction_non_alphanumeric': 0.10607312538731667, 'fraction_numerical': 0.00258211113406321, 'mean_word_length': 2.5757016248153617, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, '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': '5626333', 'n_tokens_mistral': 2657, 'n_tokens_neox': 2605, 'n_words': 712}