package com.fp.general.rules.query;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.persistence.Query;

import com.fp.dto.Response;
import com.fp.dto.query.QueryRequest;
import com.fp.dto.rules.QueryRule;
import com.fp.persistence.commondb.PersistenceHelper;

public class PrincipalInstallRates extends QueryRule {

    private String module = null;
    private String product = null;
    private String subproduct = null;
    private String currency = null;
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public QueryRequest process(QueryRequest pQueryRequest) throws Exception {
        Response response = pQueryRequest.getResponse();
        this.module = pQueryRequest.get("modulecode").toString();
        this.product = pQueryRequest.get("productcode").toString();
        this.subproduct = pQueryRequest.get("subproductcode").toString();
        this.currency = pQueryRequest.get("currencycode").toString();
        
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();        
        list = fillMap();        
        response.put("LOVTGENESUBPRODUCTINSTALLRATES", list);
        return pQueryRequest;
    }
    
    private List<Map<String, String>> fillMap() throws Exception {
        List<Map<String, String>> lresp = new ArrayList<Map<String,String>>();
        Query qry = PersistenceHelper.getEntityManager().createNativeQuery(SQL);
        //qry.setParameter("", "");
        List ldata = qry.getResultList();
        if(ldata.isEmpty()){
            return lresp;
        }
        for (Object object : ldata) {
            Object[] items = (Object[]) object;
            Map<String, String> m = new HashMap<String, String>();
            m.put("btype", items[0].toString());
            m.put("bgroup", items[1].toString());
            m.put("desc", items[2].toString());
            lresp.add(m);
        }
        return lresp;
    }
    

    
    
    private static final String SQL =
            "select unique ir.principalbalancetype, ir.principalbalancegroup, bt.description "+
            "from TGENESUBPRODUCTINSTALLRATES ir, TGENEBALANCETYPE  bt "+
            "where ir.principalbalancetype = bt.balancetype and "+
                  "ir.principalbalancegroup = bt.balancegroup and "+
                  "ir.modulecode=8 and ir.productcode=1 and " +
                  "ir.subproductcode=1 and ir.currencycode='USD' ";
    

}
