package com.fp.general.security.rules.query.code;

import com.fp.common.helper.Constant;
import com.fp.dto.query.QueryRequest;
import com.fp.dto.rules.QueryRule;
import com.fp.persistence.commondb.PersistenceHelper;
import com.fp.persistence.pgeneral.trans.TgeneTransaction;
import com.fp.persistence.pgeneral.trans.TgeneTransactionKey;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.SQLQuery;

/**
 * Clase que se encarga de devolver los datos de transacci&oacute
 *
 * @author scastillo
 */
public class TransactionSearch extends QueryRule {

    private final String lbl = "transaction";

    @Override
    public QueryRequest process(QueryRequest pQueryRequest) throws Exception {
        String label = (String) pQueryRequest.get("label");
        TgeneTransactionKey tgeneTransactionKey = new TgeneTransactionKey(pQueryRequest.getTransactionModule(),
                pQueryRequest.getTransactionCode(), pQueryRequest.getTransactionVersion());
        TgeneTransaction tgeneTransaction = TgeneTransaction.find(PersistenceHelper.getEntityManager(), tgeneTransactionKey);
        Map<String, Object> m = null;
        if (tgeneTransaction != null) {
            m = new HashMap<String, Object>();
            m.put("t", tgeneTransaction.getName());
            m.put("p", tgeneTransaction.getPage());
            m.put("a", tgeneTransaction.getAutoquery());
            m.put("m", tgeneTransaction.getPk().getTransactionmodule());
            m.put("tr", tgeneTransaction.getPk().getTransactioncode());
            m.put("v", tgeneTransaction.getPk().getTransactionversion());
            m.put("f", tgeneTransaction.getSignature());
            m.put("qa", tgeneTransaction.getQueryagain());
            m.put("sg", this.existGlosary(pQueryRequest.getTransactionModule(), pQueryRequest.getTransactionCode(),
                    pQueryRequest.getTransactionVersion(), pQueryRequest.getLanguage()));
        }
        pQueryRequest.getResponse().put(lbl, m);
        return pQueryRequest;
    }
    /**
     * Sentencia para saber si existe un glosaio disponible para la transaccion
     */
    private static final String HQL = "SELECT count(*) FROM TGENETRANSACTIONGLOSARY"
            + " where TRANSACTIONMODULE = :transactionmodule"
            + " and TRANSACTIONCODE = :transactioncode"
            + " and TRANSACTIONVERSION = :transactionversion"
            + " and LANGUAGECODE= :languagecode";

    /**
     * Metodo que devuelve Y o N si existe glosario en una transaccion
     *
     * @param module Modulo de la transaccion
     * @param transaction Codigo de transaccion
     * @param version Version de la transaccion
     * @param languagecode Lenguaje a utilizar
     * @return
     * @throws Exception
     */
    private String existGlosary(String module, Integer transaction, Integer version, String languagecode) throws Exception {
        SQLQuery qry = PersistenceHelper.getSession().createSQLQuery(HQL);
        qry.setString("transactionmodule", module);
        qry.setInteger("transactioncode", transaction);
        qry.setInteger("transactionversion", version);
        qry.setString("languagecode", languagecode);
        Object result = qry.uniqueResult();
        String returnValue = "N";
        if (result instanceof Integer) {
            Integer numero = (Integer) result;
            if (numero > 0) {
                returnValue = "Y";
            }
        } else if (result instanceof BigDecimal) {
            Integer numero = ((BigDecimal) result).intValue();
            if (numero > 0) {
                returnValue = "Y";
            }
        }
        return returnValue;
    }
}
