/*
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.apc.controller.mobile.repository.user;

import com.arrebol.apc.controller.mobile.repository.GenericRepository;
import com.arrebol.apc.controller.mobile.util.HibernateUtil;
import com.arrebol.apc.model.ModelParameter;
import com.arrebol.apc.model.core.User;
import com.arrebol.apc.model.core.constance.UserCfg;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Tuple;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;

/**
 *
 * @author Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 */
public class UserRepository extends GenericRepository implements Serializable {

    /**
     *
     * @param xmlQuery
     * @param parameters
     * @return
     * @throws Exception
     */
    public String findUserStatusById(String xmlQuery, List<ModelParameter> parameters) throws Exception {
        try {
            for (Tuple tuple : xmlQueryTuple(xmlQuery, parameters)) {
                return tuple.get(UserCfg.FIELD_ID, String.class);
            }

            return null;
        } catch (Exception e) {
            logger.error("findUserStatusById", e);
            throw e;
        }
    }

    /**
     *
     * @param hbmQuery
     * @param params
     * @return
     * @throws Exception
     */
    public boolean containtsUserManagementProperty(String hbmQuery, List<ModelParameter> params) throws Exception {
        Transaction transaction = null;
        try {
            Session session = HibernateUtil.getSessionFactory().getCurrentSession();
            transaction = session.beginTransaction();

            Query query = session.createNamedQuery(hbmQuery);

            params.forEach((param) -> {
                query.setParameter(param.getParameter(), param.getValue());
            });

            Long total = (Long) query.getSingleResult();
            Long one = 1l;
            transaction.commit();

            return one.equals(total);
        } catch (Exception e) {
            logger.error("containtsUserManagementProperty", e);
            if (null != transaction) {
                transaction.rollback();
            }
            throw e;
        }
    }

    /**
     *
     * @param xmlQuery
     * @param parameters
     * @return
     * @throws Exception
     */
    public List<User> listOfUsersByOffice(String xmlQuery, List<ModelParameter> parameters) throws Exception {
        try {
            List<User> users = new ArrayList<>();

            xmlQueryTuple(xmlQuery, parameters).forEach(_item -> {
                users.add(
                        new User(
                                _item.get(UserCfg.FIELD_ID, String.class),
                                _item.get(UserCfg.FIELD_USER_NAME, String.class)
                        )
                );
            });

            return users;
        } catch (Exception e) {
            logger.error("listOfUsersByOffice", e);
            throw e;
        }
    }

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

}
