/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arrebol.taxiservicios.controller.catalog;

import com.arrebol.taxiservicios.controller.connection.ConnectionManager;
import com.arrebol.taxiservicios.controller.util.HibernateUtil;
import com.arrebol.taxiservicios.model.catalog.BankCard;
import com.arrebol.taxiservicios.model.catalog.BankCardKardex;
import com.arrebol.taxiservicios.model.catalog.Location;
import com.arrebol.taxiservicios.model.core.User;
import com.arrebol.taxiservicios.model.enums.BasicType;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.CriteriaUpdate;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author oscarvargas
 */
public class BankCardKardexController extends ConnectionManager implements Serializable {


    public List<BankCardKardex> fillDatatableByBankCard(String idBankCard) {

        Transaction transaction = null;
        List<BankCardKardex> result = null;

        try {
            logger.info("Searching all colors..");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();

            CriteriaQuery<BankCardKardex> criteria = builder.createQuery(BankCardKardex.class);
            Root<BankCardKardex> root = criteria.from(BankCardKardex.class);

            
            criteria.where(builder.equal(root.get("bankCard").get("id"), idBankCard));
            criteria.orderBy(builder.desc(root.get("createdOn")));
            result = session.createQuery(criteria).getResultList();

            logger.info("Total of BankCardKardex found : " + (null == result ? -1 : result.size()));
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("Can not load list of BankCardKardex ", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillDatatable of BankCardKardex", e);
            rollback(transaction);
        }

        return result;
    }
    public void save(BankCardKardex bankCardKardex, String userId) {
        Transaction transaction = null;

        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            
            

                bankCardKardex.setId(UUID.randomUUID().toString());
                bankCardKardex.setCreatedBy(new User(userId, ""));
                bankCardKardex.setCreatedOn(new Date());
                session.save(bankCardKardex);
                logger.info("The BankCardKardex was saved - " + bankCardKardex);
           

            transaction.commit();
        } catch (HibernateException e) {
            logger.error("The BankCardKardex can not be saved - " + bankCardKardex, e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method save(" + bankCardKardex + ")", e);
            rollback(transaction);
        }
    }

     
   

    /**
     *
     * @param locationId
     * @return
     */
   
    private static final long serialVersionUID = -559465932081273592L;

    final Logger logger = LogManager.getLogger(BankCardKardexController.class);

    public BankCardKardexController() {
    }

}
