/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
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.Location;
import com.arrebol.taxiservicios.model.catalog.UserLocation;
import com.arrebol.taxiservicios.model.catalog.UserOpenPay;
import com.arrebol.taxiservicios.model.core.User;
import com.arrebol.taxiservicios.model.enums.GenericEnumType;
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.Predicate;
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 pc one
 */
public class UserOpenPayController extends ConnectionManager implements Serializable {

    private static final long serialVersionUID = -6265444716269535946L;
    final Logger logger = LogManager.getLogger(getClass().getName());

    public boolean addUserOpenPay(UserOpenPay userOpenPay, User user, boolean save) {
        logger.info("UserOpenPay");
        boolean success = false;
        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            if (save) {
                userOpenPay.setId(UUID.randomUUID().toString());
                userOpenPay.setCreatedBy(user);
                userOpenPay.setCreatedOn(new Date());
                session.save(userOpenPay);
                transaction.commit();
                logger.info("UserOpenPay saved " + userOpenPay.getId());
            } else {
                userOpenPay.setLastUpdatedBy(user);
                session.update(userOpenPay);
                transaction.commit();
                logger.info("UserOpenPay updated " + userOpenPay.getId());
            }
            success = true;
        } catch (HibernateException e) {
            logger.error("UserOpenPay can not be added ", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method UserOpenPay() ", e);
            rollback(transaction);
        }
        return success;
    }

    public UserOpenPay fillUserOpenPayByUser(User user) {
        Transaction transaction = null;
        UserOpenPay result = null;
        try {
            logger.info("Searching UserOpenPay..");
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();
            CriteriaBuilder builder = session.getCriteriaBuilder();
            CriteriaQuery<UserOpenPay> criteria = builder.createQuery(UserOpenPay.class);
            Root<UserOpenPay> root = criteria.from(UserOpenPay.class);           
            
            Predicate criterioActivo = builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED);
            Predicate criterioUser = builder.equal(root.get("user").get("id"), user.getId());  
            criteria.where(builder.and(criterioActivo, criterioUser));
            result = session.createQuery(criteria).getSingleResult();
            
            logger.info("UserOpenPay found : " + result);
            transaction.commit();
        } catch (HibernateException e) {
            logger.error("Can not load UserOpenPay ", e);
            rollback(transaction);
        } catch (Exception e) {
            logger.error("Method fillUserOpenPayByUser by User", e);
            rollback(transaction);
        }
        return result;
    }

}
