/*
 * 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.apc.controller.admin;

import com.arrebol.apc.controller.util.HibernateUtil;
import com.arrebol.apc.model.catalog.MovimientosInventario;
import com.arrebol.apc.model.core.User;
import com.arrebol.apc.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 Oscar
 */
public class AjustarInventariosController implements Serializable {

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

   public boolean addMovimientosInventario(MovimientosInventario moldelo, User user, Boolean guardar) {
      logger.info("addMovimientosInventario");
      boolean success = false;
      Transaction transaction = null;

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

         if (guardar) {
            moldelo.setId(UUID.randomUUID().toString());
            moldelo.setCreatedBy(user.getId());
            moldelo.setCreatedOn(new Date());
            session.save(moldelo);
            transaction.commit();
            logger.info(moldelo.getClass() + " saved " + moldelo.getId());
         } else {
            moldelo.setLastUpdatedOn(new Date());
            moldelo.setLastUpdatedBy(user.getId());
            session.update(moldelo);
            transaction.commit();
            logger.info(moldelo.getClass() + " updated " + moldelo.getId());
         }

         success = true;
      } catch (HibernateException e) {
         logger.error(moldelo.getClass() + " can not be added ", e);
      } catch (Exception e) {
         logger.error("Method addMovimientosInventario() ", e);
      }
      return success;
   }

   public List<MovimientosInventario> fillMovimientosInventario() {
      logger.info("fillMovimientosInventario");
      List<MovimientosInventario> resultList = null;
      Transaction transaction = null;
      try {
         Session session = HibernateUtil.getSessionFactory().getCurrentSession();
         transaction = session.beginTransaction();
         CriteriaBuilder builder = session.getCriteriaBuilder();

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

         Predicate criterio = builder.equal(root.get("estatusActivo"), GenericEnumType.ENABLED);
         criteria.where(builder.and(criterio));
         criteria.orderBy(builder.asc(root.get("createdOn")));
         resultList = session.createQuery(criteria).getResultList();

         logger.info("MovimientosInventario encontrados: " + (null == resultList ? -1 : resultList.size()));
         transaction.commit();
      } catch (HibernateException e) {
         logger.error("No se econtraron MovimientosInventario: " + e);
      } catch (Exception e) {
         logger.error("Method fillMovimientosInventario ", e);
      }
      return resultList;
   }

}
