/*******************************************************************************
 * This file is part of Educatio.
 *
 *     Educatio is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     Educatio is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with Educatio.  If not, see <http://www.gnu.org/licenses/>.
 *     
 *     Authors:
 *           Filipe Marinho de Brito - filipe.marinho.brito@gmail.com
 *           Rodrigo de Souza Ataides - rodrigoataides@gmail.com
 *******************************************************************************/
package br.com.core.business.selecao; 

import java.util.HashMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import br.com.core.datatable.business.DataTableBusiness;
import br.com.core.datatable.modelo.ParametrosDataTable;
import br.com.core.datatable.utils.DataTableUtils;
import br.com.core.modelo.edital.CandidatoSelecao;



@Service
public class FiltrarCandidatoSelecaoBusiness {
	
	@Autowired
	DataTableBusiness dataTableBusiness;
	
	public String filtrarDataTable(String json){
		
		ParametrosDataTable parametrosDataTable = DataTableUtils.obtemParametros(json);
		parametrosDataTable.setClazzModelo(CandidatoSelecao.class);
		HashMap<String, String> sqls = DataTableUtils.constroiSqls(parametrosDataTable);
		
			StringBuffer sqlaaData = new StringBuffer(sqls.get("aaData"));
			StringBuffer iTotalRecords = new StringBuffer(sqls.get("iTotalRecords"));
			StringBuffer iTotalDisplayRecords = new StringBuffer(sqls.get("iTotalDisplayRecords"));
			StringBuffer sqlRelatorio = new StringBuffer(sqls.get("sqlRelatorio"));

			String sqlaaDataStr = sqlaaData.toString().replaceFirst("left join pessoa.pessoa on o.pessoa = pessoa.pessoa.id ", "");
			String iTotalRecordsStr = iTotalRecords.toString().replaceFirst("left join pessoa.pessoa on o.pessoa = pessoa.pessoa.id ", "");
			String iTotalDisplayRecordsStr = iTotalDisplayRecords.toString().replaceFirst("left join pessoa.pessoa on o.pessoa = pessoa.pessoa.id ", "");
			String sqlRelatorioStr = sqlRelatorio.toString().replaceFirst("left join pessoa.pessoa on o.pessoa = pessoa.pessoa.id ", "");

			sqls.put("aaData", sqlaaDataStr);
			sqls.put("iTotalRecords", iTotalRecordsStr);
			sqls.put("iTotalDisplayRecords", iTotalDisplayRecordsStr);
			sqls.put("sqlRelatorio", sqlRelatorioStr);

//			System.out.println("Sql alterado para :"+ sqlaaDataStr);
//			System.out.println("Sql alterado para :"+ iTotalRecordsStr);
//			System.out.println("Sql alterado para :"+ iTotalDisplayRecordsStr);
//			System.out.println("Sql alterado para :"+ sqlRelatorioStr);
			
		String dataTableJson = dataTableBusiness.jsonDataTableFromParameters(json,sqls,CandidatoSelecao.class);
		
		return dataTableJson;
		
	}

}


