← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 7664: Improved performance of integrity checks

 

------------------------------------------------------------
revno: 7664
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Sat 2012-07-21 19:19:38 +0200
message:
  Improved performance of integrity checks
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java
  dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/dataintegrity/DefaultDataIntegrityService.java
  dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java	2012-07-20 11:41:57 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java	2012-07-21 17:19:38 +0000
@@ -177,6 +177,19 @@
      *         CATEGORYOPTIONCOMBO_DOES_NOT_EXIST if the category option combo does not exist.
      */
     String expressionIsValid( String formula );
+
+    /**
+     * Tests whether the expression is valid. Returns a positive value if the
+     * expression is valid, or a negative value if not.
+     * 
+     * @param formula the expression formula.
+     * @return VALID if the expression is valid.
+     *         DATAELEMENT_ID_NOT_NUMERIC if the data element is not a number.
+     *         CATEGORYOPTIONCOMBO_ID_NOT_NUMERIC if the category option combo id is not a number.
+     *         DATAELEMENT_DOES_NOT_EXIST if the data element does not exist.
+     *         CATEGORYOPTIONCOMBO_DOES_NOT_EXIST if the category option combo does not exist.
+     */
+    String expressionIsValid( String formula, Set<Integer> dataElements, Set<Integer> categoryOptionCombos, Set<Integer> constants );
     
     /**
      * Creates an expression string containing DataElement names and the names of

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/dataintegrity/DefaultDataIntegrityService.java'
--- dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/dataintegrity/DefaultDataIntegrityService.java	2012-07-14 13:44:38 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/java/org/hisp/dhis/dataintegrity/DefaultDataIntegrityService.java	2012-07-21 17:19:38 +0000
@@ -28,6 +28,7 @@
  */
 
 import static org.hisp.dhis.system.util.ListUtils.getDuplicates;
+import static org.hisp.dhis.system.util.ConversionUtils.*;
 
 import java.util.ArrayList;
 import java.util.Collection;
@@ -39,7 +40,11 @@
 import java.util.TreeMap;
 
 import org.hisp.dhis.common.comparator.IdentifiableObjectNameComparator;
+import org.hisp.dhis.constant.Constant;
+import org.hisp.dhis.constant.ConstantService;
 import org.hisp.dhis.dataelement.DataElement;
+import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
+import org.hisp.dhis.dataelement.DataElementCategoryService;
 import org.hisp.dhis.dataelement.DataElementGroup;
 import org.hisp.dhis.dataelement.DataElementGroupSet;
 import org.hisp.dhis.dataelement.DataElementService;
@@ -141,6 +146,20 @@
     {
         this.dataEntryFormService = dataEntryFormService;
     }
+    
+    private DataElementCategoryService categoryService;
+
+    public void setCategoryService( DataElementCategoryService categoryService )
+    {
+        this.categoryService = categoryService;
+    }
+    
+    private ConstantService constantService;
+
+    public void setConstantService( ConstantService constantService )
+    {
+        this.constantService = constantService;
+    }
 
     // -------------------------------------------------------------------------
     // DataIntegrityService implementation
@@ -339,9 +358,13 @@
     {
         SortedMap<Indicator, String> invalids = new TreeMap<Indicator, String>( IdentifiableObjectNameComparator.INSTANCE );
 
+        Set<Integer> dataElements = new HashSet<Integer>( getIdentifiers( DataElement.class, dataElementService.getAllDataElements() ) );
+        Set<Integer> categoryOptionCombos = new HashSet<Integer>( getIdentifiers( DataElementCategoryOptionCombo.class, categoryService.getAllDataElementCategoryOptionCombos() ) );
+        Set<Integer> constants = new HashSet<Integer>( getIdentifiers( Constant.class, constantService.getAllConstants() ) );
+        
         for ( Indicator indicator : indicatorService.getAllIndicators() )
         {
-            String result = expressionService.expressionIsValid( indicator.getNumerator() );
+            String result = expressionService.expressionIsValid( indicator.getNumerator(), dataElements, categoryOptionCombos, constants );
 
             if ( !result.equals( ExpressionService.VALID ) )
             {
@@ -356,9 +379,13 @@
     {
         SortedMap<Indicator, String> invalids = new TreeMap<Indicator, String>( IdentifiableObjectNameComparator.INSTANCE );
 
+        Set<Integer> dataElements = new HashSet<Integer>( getIdentifiers( DataElement.class, dataElementService.getAllDataElements() ) );
+        Set<Integer> categoryOptionCombos = new HashSet<Integer>( getIdentifiers( DataElementCategoryOptionCombo.class, categoryService.getAllDataElementCategoryOptionCombos() ) );
+        Set<Integer> constants = new HashSet<Integer>( getIdentifiers( Constant.class, constantService.getAllConstants() ) );
+        
         for ( Indicator indicator : indicatorService.getAllIndicators() )
         {
-            String result = expressionService.expressionIsValid( indicator.getDenominator() );
+            String result = expressionService.expressionIsValid( indicator.getDenominator(), dataElements, categoryOptionCombos, constants );
 
             if ( !result.equals( ExpressionService.VALID ) )
             {
@@ -512,9 +539,13 @@
         SortedMap<ValidationRule, String> invalids = new TreeMap<ValidationRule, String>(
             IdentifiableObjectNameComparator.INSTANCE );
 
+        Set<Integer> dataElements = new HashSet<Integer>( getIdentifiers( DataElement.class, dataElementService.getAllDataElements() ) );
+        Set<Integer> categoryOptionCombos = new HashSet<Integer>( getIdentifiers( DataElementCategoryOptionCombo.class, categoryService.getAllDataElementCategoryOptionCombos() ) );
+        Set<Integer> constants = new HashSet<Integer>( getIdentifiers( Constant.class, constantService.getAllConstants() ) );
+        
         for ( ValidationRule rule : validationRuleService.getAllValidationRules() )
         {
-            String result = expressionService.expressionIsValid( rule.getLeftSide().getExpression() );
+            String result = expressionService.expressionIsValid( rule.getLeftSide().getExpression(), dataElements, categoryOptionCombos, constants );
 
             if ( !result.equals( ExpressionService.VALID ) )
             {
@@ -530,9 +561,13 @@
         SortedMap<ValidationRule, String> invalids = new TreeMap<ValidationRule, String>(
             IdentifiableObjectNameComparator.INSTANCE );
 
+        Set<Integer> dataElements = new HashSet<Integer>( getIdentifiers( DataElement.class, dataElementService.getAllDataElements() ) );
+        Set<Integer> categoryOptionCombos = new HashSet<Integer>( getIdentifiers( DataElementCategoryOptionCombo.class, categoryService.getAllDataElementCategoryOptionCombos() ) );
+        Set<Integer> constants = new HashSet<Integer>( getIdentifiers( Constant.class, constantService.getAllConstants() ) );
+        
         for ( ValidationRule rule : validationRuleService.getAllValidationRules() )
         {
-            String result = expressionService.expressionIsValid( rule.getRightSide().getExpression() );
+            String result = expressionService.expressionIsValid( rule.getRightSide().getExpression(), dataElements, categoryOptionCombos, constants );
 
             if ( !result.equals( ExpressionService.VALID ) )
             {

=== modified file 'dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml	2012-07-21 15:19:55 +0000
+++ dhis-2/dhis-services/dhis-service-administration/src/main/resources/META-INF/dhis/beans.xml	2012-07-21 17:19:38 +0000
@@ -33,6 +33,8 @@
     <property name="validationRuleService" ref="org.hisp.dhis.validation.ValidationRuleService" />
     <property name="expressionService" ref="org.hisp.dhis.expression.ExpressionService" />
 	<property name="dataEntryFormService" ref="org.hisp.dhis.dataentryform.DataEntryFormService" />
+	<property name="categoryService" ref="org.hisp.dhis.dataelement.DataElementCategoryService" />
+	<property name="constantService" ref="org.hisp.dhis.constant.ConstantService" />
   </bean>
   
   <!-- Maintenance -->

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java	2012-07-20 11:41:57 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java	2012-07-21 17:19:38 +0000
@@ -315,6 +315,11 @@
 
     public String expressionIsValid( String formula )
     {
+        return expressionIsValid( formula, null, null, null );
+    }
+    
+    public String expressionIsValid( String formula, Set<Integer> dataElements, Set<Integer> categoryOptionCombos, Set<Integer> constants )
+    {
         if ( formula == null )
         {
             return EXPRESSION_IS_EMPTY;
@@ -347,7 +352,7 @@
                     return ID_NOT_NUMERIC;
                 }
                 
-                if ( constantService.getConstant( id ) == null )
+                if ( constants != null ? !constants.contains( id ) : constantService.getConstant( id ) == null )
                 {
                     return CONSTANT_DOES_NOT_EXIST;
                 }                    
@@ -363,12 +368,13 @@
                     return ID_NOT_NUMERIC;
                 }
 
-                if ( dataElementService.getDataElement( operand.getDataElementId() ) == null )
+                if ( dataElements != null ? !dataElements.contains( operand.getDataElementId() ) : dataElementService.getDataElement( operand.getDataElementId() ) == null )
                 {
                     return DATAELEMENT_DOES_NOT_EXIST;
                 }
 
-                if ( !operand.isTotal() && categoryService.getDataElementCategoryOptionCombo( operand.getOptionComboId() ) == null )
+                if ( !operand.isTotal() && ( categoryOptionCombos != null ? !categoryOptionCombos.contains( operand.getOptionComboId() ) :
+                    categoryService.getDataElementCategoryOptionCombo( operand.getOptionComboId() ) == null ) )
                 {
                     return CATEGORYOPTIONCOMBO_DOES_NOT_EXIST;
                 }