=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/TableAlteror.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/TableAlteror.java 2010-08-30 04:55:00 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/startup/TableAlteror.java 2010-08-30 07:50:48 +0000 @@ -268,7 +268,7 @@ } catch ( Exception ex ) { - log.info( ex ); + log.debug( ex ); return -1; } === modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/validation/DefaultValidationRuleService.java' --- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/validation/DefaultValidationRuleService.java 2010-08-30 06:24:45 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/validation/DefaultValidationRuleService.java 2010-08-30 07:50:48 +0000 @@ -29,12 +29,11 @@ import static org.hisp.dhis.system.util.MathUtils.expressionIsTrue; -import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.Date; +import java.util.HashMap; import java.util.HashSet; -import java.util.List; +import java.util.Map; import org.apache.commons.collections.CollectionUtils; import org.hisp.dhis.common.GenericIdentifiableObjectStore; @@ -43,7 +42,6 @@ import org.hisp.dhis.expression.ExpressionService; import org.hisp.dhis.period.Period; import org.hisp.dhis.period.PeriodService; -import org.hisp.dhis.period.comparator.PeriodComparator; import org.hisp.dhis.source.Source; import org.hisp.dhis.system.util.Filter; import org.hisp.dhis.system.util.FilterUtils; @@ -71,8 +69,7 @@ private GenericIdentifiableObjectStore validationRuleGroupStore; - public void setValidationRuleGroupStore( - GenericIdentifiableObjectStore validationRuleGroupStore ) + public void setValidationRuleGroupStore( GenericIdentifiableObjectStore validationRuleGroupStore ) { this.validationRuleGroupStore = validationRuleGroupStore; } @@ -99,19 +96,24 @@ { Collection validationViolations = new HashSet(); - Collection relevantRules = null; - - Collection relevantPeriods = periodService.getIntersectingPeriods( startDate, endDate ); - + Collection relevantPeriods = periodService.getPeriodsBetweenDates( startDate, endDate ); + + Map> relevantValidationRulesMap = getRelevantValidationRulesMap( sources ); + for ( Source source : sources ) { for ( DataSet dataSet : source.getDataSets() ) { - if ( (relevantRules = getRelevantValidationRules( dataSet )).size() > 0 ) - { - for ( Period period : relevantPeriods ) // TODO use only period with validation rule period type + Collection relevantRules = relevantValidationRulesMap.get( dataSet ); + + if ( relevantRules != null && relevantRules.size() > 0 ) + { + for ( Period period : relevantPeriods ) { - validationViolations.addAll( validate( period, source, relevantRules ) ); + if ( dataSet.getPeriodType().equals( period.getPeriodType() ) ) + { + validationViolations.addAll( validate( period, source, relevantRules ) ); + } } } } @@ -126,24 +128,24 @@ { Collection validationViolations = new HashSet(); - Collection relevantRules = null; + Map> relevantValidationRulesMap = getRelevantValidationRulesMap( sources ); + + Collection relevantPeriods = periodService.getPeriodsBetweenDates( startDate, endDate ); for ( Source source : sources ) { for ( DataSet dataSet : source.getDataSets() ) { - if ( (relevantRules = CollectionUtils.intersection( getRelevantValidationRules( dataSet ), group - .getMembers() )).size() > 0 ) + Collection relevantRules = CollectionUtils.intersection( relevantValidationRulesMap.get( dataSet ), group.getMembers() ); + + if ( relevantRules != null && relevantRules.size() > 0 ) { - // Get periods by periodType of dataSet and order-by desc - List relevantPeriods = new ArrayList( periodService - .getIntersectingPeriodsByPeriodType( dataSet.getPeriodType(), startDate, endDate ) ); - - Collections.sort( relevantPeriods, new PeriodComparator() ); - for ( Period period : relevantPeriods ) { - validationViolations.addAll( validate( period, source, relevantRules ) ); + if ( dataSet.getPeriodType().equals( period.getPeriodType() ) ) + { + validationViolations.addAll( validate( period, source, relevantRules ) ); + } } } } @@ -156,20 +158,22 @@ { Collection validationViolations = new HashSet(); - Collection relevantRules = null; + Map> relevantValidationRulesMap = getRelevantValidationRulesMap( source ); + + Collection relevantPeriods = periodService.getPeriodsBetweenDates( startDate, endDate ); for ( DataSet dataSet : source.getDataSets() ) { - if ( (relevantRules = getRelevantValidationRules( dataSet )).size() > 0 ) + Collection relevantRules = relevantValidationRulesMap.get( dataSet ); + + if ( relevantRules != null && relevantRules.size() > 0 ) { - List relevantPeriods = new ArrayList( periodService.getIntersectingPeriodsByPeriodType( - dataSet.getPeriodType(), startDate, endDate ) ); - - Collections.sort( relevantPeriods, new PeriodComparator() ); - for ( Period period : relevantPeriods ) { - validationViolations.addAll( validate( period, source, relevantRules ) ); + if ( dataSet.getPeriodType().equals( period.getPeriodType() ) ) + { + validationViolations.addAll( validate( period, source, relevantRules ) ); + } } } } @@ -224,6 +228,53 @@ } /** + * Creates a mapping between data set and its relevant validation rules for + * the given source. + * + * @param source the source. + * @return a map. + */ + private Map> getRelevantValidationRulesMap( Source source ) + { + Map> map = new HashMap>(); + + for ( DataSet dataSet : source.getDataSets() ) + { + if ( !map.keySet().contains( dataSet ) ) + { + map.put( dataSet, getRelevantValidationRules( dataSet ) ); + } + } + + return map; + } + + /** + * Creates a mapping between data set and its relevant validation rules for + * the given collection of sources. + * + * @param sources the collection of sources. + * @return a map. + */ + private Map> getRelevantValidationRulesMap( Collection sources ) + { + Map> map = new HashMap>(); + + for ( Source source : sources ) + { + for ( DataSet dataSet : source.getDataSets() ) + { + if ( !map.keySet().contains( dataSet ) ) + { + map.put( dataSet, getRelevantValidationRules( dataSet ) ); + } + } + } + + return map; + } + + /** * Returns all validation rules which have data elements assigned to it * which are members of the given data set. * @@ -233,24 +284,9 @@ */ private Collection getRelevantValidationRules( final DataSet dataSet ) { - return getRelevantValidationRules( dataSet, getAllValidationRules() ); - } - - /** - * Returns all validation rules which have data elements assigned to it - * which are members of the given data set. - * - * @param dataSet the data set. - * @param validationRules the validation rules. - * @return all validation rules which have data elements assigned to it - * which are members of the given data set. - */ - private Collection getRelevantValidationRules( final DataSet dataSet, - final Collection validationRules ) - { final Collection relevantValidationRules = new HashSet(); - for ( ValidationRule validationRule : validationRules ) + for ( ValidationRule validationRule : getAllValidationRules() ) { if ( validationRule.getPeriodType() == dataSet.getPeriodType() ) {