/*
 * 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.predio.PredioController;
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.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.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 UserLocationController extends ConnectionManager implements Serializable {

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

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

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

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

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

            criteria.where(builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED));
            criteria.where(builder.equal(root.get("location").get("id"), location.getId()));
            criteria.orderBy(builder.asc(root.get("createdOn")));

            result = session.createQuery(criteria).getResultList();

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

        return result;
    } 
     
     public List<UserLocation> fillAllUserLocationByUser(User user) {

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

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

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

            criteria.where(builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED));
            criteria.where(builder.equal(root.get("user").get("id"), user.getId()));
            criteria.orderBy(builder.asc(root.get("createdOn")));

            result = session.createQuery(criteria).getResultList();

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

        return result;
    } 
     
}
