class ChartOfAccountRepository {

	public Pagination<ChartOfAccount> search(ChartOfAccountSearch domain) {

		Set<ChartOfAccount> chartOfAccountSet = new HashSet<ChartOfAccount>();
		Pagination<ChartOfAccount> finalResult = new Pagination<>();
		Pagination<ChartOfAccount> result = new Pagination<>();
		
		if (!financialConfigurationService.fetchDataFrom().isEmpty()
	                && financialConfigurationService.fetchDataFrom().equalsIgnoreCase("es")) {
		    ChartOfAccountSearchContract chartOfAccountSearchContract = new ChartOfAccountSearchContract();
	            ModelMapper mapper = new ModelMapper();
	            mapper.map(domain, chartOfAccountSearchContract);
	            result = chartOfAccountESRepository.search(chartOfAccountSearchContract);
	        } else {
	            
	             result = chartOfAccountJdbcRepository.search(domain);
	        }

		if (domain != null && domain.getAccountCodePurpose() != null
				&& domain.getAccountCodePurpose().getId() != null) {

			domain.setAccountCodePurpose(null);

			for (ChartOfAccount coa : result.getPagedData()) {
				chartOfAccountSet.add(coa);
				domain.setGlcode(coa.getGlcode() + "%");
				Pagination<ChartOfAccount> result1 = chartOfAccountJdbcRepository.search(domain);
				for (ChartOfAccount temp : result1.getPagedData()) {
					chartOfAccountSet.add(temp);
				}
				finalResult = result1;
			}
			finalResult.setTotalResults(chartOfAccountSet.size());
			finalResult.setPagedData(new ArrayList<>(chartOfAccountSet));

			return finalResult;

		} else

			return result;

	}

}