List<GrantContractRegime> grantContractRegimes = rootDomainObject.getGrantContractRegimes();
List<GrantContractRegime> grantContractRegimes = new ArrayList<GrantContractRegime>(); grantContractRegimes.addAll(rootDomainObject.getGrantContractRegimes());
	  public Integer countAllByCriteria(Boolean justActiveContracts, Boolean justDesactiveContracts,	            Date dateBeginContract, Date dateEndContract, Integer grantTypeId,Date validToTheDate) 	  			throws FenixServiceException,FenixFilterException, ExcepcaoPersistencia {			Integer result =new Integer(0);						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++;	        }	       return result;	  }