package com.fp.bpmlib.query.solicitud;

import java.util.List;

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

public class EstadoSolicitudQuery extends QueryRule{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@SuppressWarnings("unchecked")
	@Override
	public QueryRequest process(QueryRequest pQueryRequest) throws Exception {
		try {
			System.out.println("*********consulta*************************************");
			String codigosolicitud = (String) pQueryRequest.get("codigosolicitudbpm");
			System.out.println("CODIGO DE CONSULTA : "+codigosolicitud);
			if(codigosolicitud.contains("SOL")){
				codigosolicitud=codigosolicitud.replace("SOL", "");
			}
			if(codigosolicitud.contains("TRA")){
				codigosolicitud=codigosolicitud.replace("TRA", "");
			}
			
			StringBuilder sql = new StringBuilder("select DISTINCT ta.ACTIVITYNAME,ta.USERCODE,ta.GROUPCODE,tk.STATUS,ta.CREATED,ta.STARTED,\n" +
                            "ta.COMPLETED  from jbpm6.VARIABLEINSTANCELOG d, TBPMACTIVITIES ta \n" +
                            "LEFT JOIN jbpm6.TASK tk\n" +
                            "ON (tk.id = ta.TASKID)\n" +
                            "where  cast(d.processInstanceId as varchar(50))= ta.internalcode");
			
			if(codigosolicitud.contains("SOL") || codigosolicitud.contains("TRA")){
				sql.append(" and (d.value like '%SOL"+codigosolicitud+"%' or d.value like '%TRA"+codigosolicitud+"%') ");//SOL-0000491173
			}else{
				sql.append(" and d.value like '%"+codigosolicitud+"%' ");
			}
					
			sql.append(" order by ta.CREATED ");
			
			System.out.println("SQL_ESTADOS_SOLICITUDBPM: "+sql);
			List<Object[]> listaVariables=PersistenceHelper.getEntityManager().
				createNativeQuery(sql.toString()).getResultList();
			System.out.println("consulta*************************************"+listaVariables);
			pQueryRequest.getResponse().put("LISTASESTADOSOLICITUDBPM", listaVariables);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pQueryRequest;
	}	
	
}
