List<GrantContractRegime> grantContractRegimes = rootDomainObject.getGrantContractRegimes(); ComparatorChain comparatorChain = new ComparatorChain(new BeanComparator("grantContract.grantOwner.number"), true);
List<GrantContractRegime> grantContractRegimes = new ArrayList<GrantContractRegime>(); grantContractRegimes.addAll(rootDomainObject.getGrantContractRegimes());  ComparatorChain comparatorChain = new ComparatorChain(new BeanComparator("grantContract.grantOwner.number"), true);
	public List<GrantContractRegime> readAllContractsByCriteria(String orderBy, Boolean justActiveContracts,            Boolean justDesactiveContracts, Date dateBeginContract, Date dateEndContract,            Integer spanNumber, Integer numberOfElementsInSpan, Integer grantTypeId,Date validToTheDate)            	throws FenixFilterException, FenixServiceException, ExcepcaoPersistencia ,Exception{						List<GrantContractRegime> result = new ArrayList<GrantContractRegime>();			Date ToTheDate = null;						List<GrantContractRegime> grantContractRegimes = rootDomainObject.getGrantContractRegimes();			ComparatorChain comparatorChain = new ComparatorChain(new BeanComparator("grantContract.grantOwner.number"), true);	        Collections.sort(grantContractRegimes, comparatorChain);	        Collections.reverse(grantContractRegimes);	        for (final GrantContractRegime grantContractRegime : ((List<GrantContractRegime>) grantContractRegimes)) {	            final GrantContract grantContract = grantContractRegime.getGrantContract();	            if (grantContract == null) {	                continue;	            }	            	            if ((validToTheDate == null || validToTheDate.equals(""))	                    && (dateBeginContract == null || dateBeginContract.equals(""))	                    && (dateEndContract == null || dateEndContract.equals(""))){                	if (justActiveContracts != null                	        && justActiveContracts.booleanValue()){                		if (grantContractRegime.getGrantContract().getEndContractMotive()!= null && !grantContractRegime.getGrantContract().getEndContractMotive().equals("")){    	            		continue;    	            	}                		if(!grantContractRegime.getContractRegimeActive()){                			continue;                         }                	}                	if (justDesactiveContracts != null                	        && justDesactiveContracts.booleanValue()){                		if(grantContractRegime.getContractRegimeActive()                				&& (grantContractRegime.getGrantContract().getEndContractMotive()== null                 						|| grantContractRegime.getGrantContract().getEndContractMotive().equals(""))) {	                    continue;                		}                	}	            }	            	            if (validToTheDate != null){	            	if (grantContractRegime.getDateBeginContract() == null || grantContractRegime.getDateEndContract() == null) {	                    continue;	                }	            	if (DateFormatUtil.isBefore("yyyy-MM-dd",grantContractRegime.getDateEndContract(),validToTheDate)) {		                    	                    continue;	                }	                if (DateFormatUtil.isAfter("yyyy-MM-dd",grantContractRegime.getDateBeginContract(),validToTheDate)) {	                    continue;	                }	            	if (grantContractRegime.getGrantContract().getEndContractMotive()!= null && !grantContractRegime.getGrantContract().getEndContractMotive().equals("")){	            		continue;	            	}	                            	}	            	            if ((dateBeginContract != null && !dateBeginContract.equals("")) 	            		&& (dateEndContract != null && !dateEndContract.equals(""))){	            	if (grantContractRegime.getDateBeginContract() == null || grantContractRegime.getDateEndContract() == null) {	                    continue;	                }	            		            	if (!grantContractRegime.belongsToPeriod(dateBeginContract,dateEndContract)) {		                    	                    continue;	                }	            		            	if (justActiveContracts != null	                        && justActiveContracts.booleanValue()){	            		if(!grantContractRegime.getState().equals(new Integer(1))){	            			continue;	            		}                      		if (grantContractRegime.getGrantContract().getEndContractMotive()!= null && !grantContractRegime.getGrantContract().getEndContractMotive().equals("")){    	            		continue;    	            	}	            	}	            		            	if (justDesactiveContracts != null	                        && justDesactiveContracts.booleanValue()){	            		if(grantContractRegime.getState().equals(new Integer(1))&& (grantContractRegime.getGrantContract().getEndContractMotive()== null         						|| grantContractRegime.getGrantContract().getEndContractMotive().equals(""))){	            			continue;	            		}	                        	            	}	            }	            if (grantTypeId != null) {	            	if(!grantContractRegime.getGrantContract().getGrantType().getIdInternal().equals(grantTypeId)){	            		continue;	            	}	            }	            result.add(grantContractRegime);	        }	        int begin = (spanNumber - 1) * numberOfElementsInSpan;	        int end = begin + numberOfElementsInSpan;	        return result.subList(begin, Math.min(end,result.size()));    }