/*
 * 
 */
package com.fp.bpmlib.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.DtoQuery;
import com.fp.dto.query.QueryBean;
import com.fp.dto.query.QueryRequest;
import com.fp.dto.rules.QueryRule;
import com.fp.persistence.commondb.GeneralQuery;
import com.fp.persistence.commondb.PersistenceHelper;

// TODO: Auto-generated Javadoc
/**
 * Clase que se encarga de presentar en el lov las transacciones que contiene la tabla TgeneTransactionLog.
 * 
 * @author gfiallos
 */

public class EndedFlows extends QueryRule {

    /** Constante serialVersionUID. */
    private static final long serialVersionUID = 1L;

    /**
     * Metodo que presenta la lista de transacciones en vuelo.
     * 
     * @param qr the qr
     * @return query request
     * @throws Exception la exception
     */
    @Override
    public QueryRequest process(QueryRequest qr) throws Exception {
    	if(qr.isJsf()){
    		
    	}else{
    		
    	}
        Response response = qr.getResponse();
        
        StringBuilder jpql = new StringBuilder(400);
        jpql.append("select t.pk.transactionmodule,t.pk.transactioncode,t.pk.transactionversion,t.name "
                + "from TgeneTransaction t where exists (  select  1 "
                + "from TgeneTransactionLog r  where exists(select 1 from TbpmProcessInstance o "
                + "where o.pk = r.pk and coalesce(o.finalized, 'N')='Y') and t.pk.transactionmodule=r.transactionmodule "
                + "and t.pk.transactioncode=r.transactioncode and t.pk.transactionversion=r.transactionversion ) ");

        //Ingresa y verifica el frontend
        Query qry = this.processQuery(qr, jpql);
        
        
        List<?> ltransaction = qry.getResultList();
        List<Map<String, Object>> detailgrid = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < ltransaction.size(); i++) {
            Map<String, Object> detail = new HashMap<String, Object>();
            Object[] datos = (Object[]) ltransaction.get(i);
            detail.put("pk_transactionmodule", datos[0]);
            detail.put("pk_transactioncode", datos[1]);
            detail.put("pk_transactionversion", datos[2]);
            detail.put("name", datos[3]);
           // detail.put("solicitudenumber", datos[4]);
            detailgrid.add(detail);
        }
        response.put("TGENETRANSACTION", detailgrid);
        return qr;
    }
    
    /**
     * Direcciona la consulta jsf y dojo
     * @param qr
     * @param jpql
     * @return
     * @throws Exception
     */
    private Query processQuery(QueryRequest qr, StringBuilder jpql) throws Exception {
    	Query qry = null;
    	if(qr.isJsf()){
    		DtoQuery dto = (DtoQuery)qr.getQueryTables().get("TGENETRANSACTION");
    		GeneralQuery.addParametersJSF(jpql, dto);
    		qry = PersistenceHelper.getEntityManager().createQuery(jpql.toString());
    		GeneralQuery.setParametersJSF(jpql, dto, qry);
    	}else{
    		QueryBean qb = (QueryBean) qr.get("TGENETRANSACTION");
            Map<String, Object> criteria = new HashMap<String, Object>();
            GeneralQuery.addParameters(qb, jpql, criteria);
            qry = PersistenceHelper.getEntityManager().createQuery(jpql.toString());
            /*
             * qry.setParameter("init", qr.getDate("creationdate")); qry.setParameter("end", qr.getDate("enddate"));
             */
            GeneralQuery.setParameters(qb, criteria, qry);
    	}
    	return qry;
    }
}
