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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.fp.bpm.query.Query;
import com.fp.core.exception.CoreException;
import com.fp.dto.Response;
import com.fp.dto.query.QueryBean;
import com.fp.dto.query.QueryRequest;
import com.fp.dto.rules.QueryRule;
import com.fp.persistence.pgeneral.acco.TgeneAccount;

/**
 * Clase que se encarga de obtener una lista de cuentas, que se utiliza en el lov de cuentas en las paginas.
 * 
 * @author Jorge Vaca
 * @version 2.1
 */
public class LovAccount extends QueryRule {

    /**
     * Versión de la Clase
     */
    private static final long serialVersionUID = 1L;

    /** Tabla */
    private static final String TGENEACCOUNT = "TGENEACCOUNT";

    /*
     * (non-Javadoc)
     * 
     * @see com.fp.dto.rules.QueryRule#process(com.fp.dto.query.QueryRequest)
     */
    @Override
    public QueryRequest process(QueryRequest pQueryRequest) throws Exception {
        Response response = pQueryRequest.getResponse();
        Query query = new Query();
        query.process(pQueryRequest);
        this.fillResponse(response, pQueryRequest);
        return pQueryRequest;
    }

    /**
     * Metodo que construye la respuesta del lov de cuentas.
     * 
     * @param pResponse Objeto que contiene los objetos de respuesta.
     * @param pQueryRequest Peticion
     * @throws Exception
     */
    private void fillResponse(Response pResponse, QueryRequest pQueryRequest) throws Exception {
        List<Map<String, Object>> lresp = new ArrayList<Map<String, Object>>();
        @SuppressWarnings("unchecked")
        List<Object> lQuota = (List<Object>) pResponse.get(LovAccount.TGENEACCOUNT);
        for (Object object : lQuota) {
            TgeneAccount obj = (TgeneAccount) object;
            Map<String, Object> m = new HashMap<String, Object>();
            if (obj.getPk().getAccount() == null) {
                QueryBean qb = pQueryRequest.getBeans().get(LovAccount.TGENEACCOUNT);
                if ((qb.getCriteriaValue("pk.account") != null) && (qb.getCriteriaValue("pk.company") != null) && !qb.isNotin("pk.account")) {
                    throw new CoreException("CORE-0007", "CUENTA NO EXISTE: {0} COMPANIA {1}", qb.getCriteriaValue("pk.account"),
                            qb.getCriteriaValue("pk.company"));
                }
                break;
            }
            m.put("pk_account", obj.getPk().getAccount());
            m.put("accountname", obj.getAccountname());
            m.put("currencycode", obj.getCurrencycode());
            m.put("pk_company", obj.getPk().getCompany());
            m.put("personcode", obj.getPersoncode());
            m.put("modulecode", obj.getModulecode());
            m.put("productcode", obj.getProductcode());
            m.put("subproductcode", obj.getSubproductcode());
            m.put("statuscode", obj.getAccountstatuscode());
            lresp.add(m);
        }
        this.checkData(lresp);
        pResponse.remove(LovAccount.TGENEACCOUNT);
        pResponse.put(LovAccount.TGENEACCOUNT, lresp);
    }

    /**
     * Verifica y completa la estructura en el caso que la tabla no tenga datos
     * 
     * @param lresp
     */
    private void checkData(List<Map<String, Object>> lresp) {
        if (lresp.isEmpty()) {
            Map<String, Object> m = new HashMap<String, Object>();
            m.put("pk_account", null);
            m.put("accountname", null);
            m.put("currencycode", null);
            m.put("pk_company", null);
            m.put("personcode", null);
            m.put("modulecode", null);
            m.put("productcode", null);
            m.put("subproductcode", null);
            lresp.add(m);
        }
    }
}
